Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ignoreWhitespace] are newlines equal to spaces? #150

Open
Feder1co5oave opened this issue Jan 15, 2018 · 2 comments
Open

[ignoreWhitespace] are newlines equal to spaces? #150

Feder1co5oave opened this issue Jan 15, 2018 · 2 comments

Comments

@Feder1co5oave
Copy link

I'd like to use html-differ to write a test system for marked, where generated html output is to be compared to pre-written html code, to determine if the two are equivalent, so I would like to ignore whitespace differences, when they are not meaningful (as in <p> tags), but I noticed that newlines are treated differently than spaces:

<p>A link. Not anymore.</p>

is not considered equal to

<p>A link.
Not anymore.</p>

when, IMO, they should be.

I've tracked this to be probably determined by this line of code (line 39 on the right).

I'm aware this issue is related to CSS, and not HTML itself. According to CSS specification, when the white-space property is not pre, pre-wrap, or pre-line, segment breaks (i.e. newlines) are usually converted to a space U+0020.

@eGavr
Copy link
Member

eGavr commented Jan 30, 2018

Hi!

Spaces are considered to be not equal to newline inside of html-tags...

<p>
Hello, Bob!
</p>

and

<p>Hello, Bob!</p>

are equal, but

<p>Hello, Bob!</p>

and

<p>Hello, 
Bob!</p>

are not!


If you need another behaviour, so you pull request some option, I think.

@Feder1co5oave
Copy link
Author

Ah, now I see what went wrong there.
Since you strip all newlines in ignoreWhitespace(), the following two paragraphs are equivalent:

<p>wrapped
paragraph</p>

<p>wrappedparagraph</p>
HtmlDiffer = require('html-differ').HtmlDiffer;
htmlDiffer = new HtmlDiffer();
first = '<p>wrapped\nparagraph</p>';
second = '<p>wrappedparagraph</p>';
console.log(htmlDiffer.isEqual(first, second));
> true

I think you should replace them with a space to adhere to the segment break transformation rules for non-pre white-space CSS styles. Multiple adjacent spaces are then collapsed to a single one in the next step, so the following two paragraphs will be equivalent, instead:

<p>wrapped
paragraph</p>

<p>wrapped paragraph</p>

which looks correct to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants