question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Html Serializer retains excess whitespace between tags

See original GitHub issue

For example,

Html.deserialize(`<div>
    <strong>Test</strong> 
</div>`)

Will render Test with four leading spaces. I’m not sure if you want to leave this up to the developer to implement their own rules, but I believe we should provide a foundation that follows spec.

I would recommend changing the default text node rule at html.js#L41 to something like:

         if (el.type == 'text') {
             let text = el.data

             // Strip extra spaces
             if (!text.trim()) text = el.next && el.prev ? ' ' : ''

             return {
                 kind: 'text',
                 text: text,
             }
         }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
roadscapecommented, Oct 25, 2016

Whitespace normalization is not very straightforward; your change may break formatting in <pre> elements for example.

In serializers/html.js#L94, you can try changing

const $ = cheerio.load(html).root()

to

const $ = cheerio.load(html, {normalizeWhitespace: true}).root()

cheerio will pass this option directly to the underlying htmlparser2, unfortunately it does not do a proper job.

I had much better results running incoming html through htmlclean before passing it to cheerio. But it seems to rely on regex and if this is the case I wouldn’t trust it unless you knew incoming HTML was always well-formed.

0reactions
zhujinxuancommented, Jan 9, 2018

Got it. Thank you very much. On Mon, Jan 8, 2018 at 19:48 Ian Storm Taylor notifications@github.com wrote:

@zhujinxuan https://github.com/zhujinxuan I think this is something that should be handled outside of Slate if it’s something you need. For the most part, the HTML serializer is used with programmatically generated HTML where whitespace is significant.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ianstormtaylor/slate/issues/407#issuecomment-356144140, or mute the thread https://github.com/notifications/unsubscribe-auth/AAtir7XCecz7EoEnwlAERE_8WYpAav6Uks5tIrdUgaJpZM4KgQYv .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tidy strips whitespace after HTML tags AND adds newlines ...
css that uses the display:inline property for list items), the stripped whitespace in tags OR the added newlines between tags becomes important) ...
Read more >
Preserve whitespace between tags HTML - Stack Overflow
HTML won't invent whitespace when it's not in the source. You can always say: <a>one</a> <a>two</a>. if you want whitespace.
Read more >
5 Ways to Insert Spaces in HTML - wikiHow
1. Open your HTML code in a text editor. You can use any text editor, such as Notepad for Windows, or TextEdit for...
Read more >
YAML Ain't Markup Language (YAML™) revision 1.2.2
Local tags start with “ ! ”, are not URIs and are not expected to be globally unique. YAML provides a “ TAG...
Read more >
Jinja Documentation (3.0.x) » Template Designer ...
A Jinja template doesn't need to have a specific extension: .html , .xml , or any other ... You must not add whitespace...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found