parse5 icon indicating copy to clipboard operation
parse5 copied to clipboard

parse5 moves newlines to the last non-body tag

Open appersonj opened this issue 7 years ago • 6 comments

Run the following script in node. Note that the two \n's at the end are moved to "Hi there!\n\n

const parse5 = require('parse5');
const orig = '<!DOCTYPE html><html><head></head><body>Hi there!</body></html>\n\n';
const document = parse5.parse(orig);
const ser = parse5.serialize(document);
console.log(`============orig==========\n${orig}\n==============ser============\n${ser}\n`);

appersonj avatar May 01 '19 18:05 appersonj

Another example, this time the \ns are moved to the </div>:

const parse5 = require('parse5');
const orig = '<!DOCTYPE html><html><head></head><body><div>Hi there!</div></body></html>\n\n';
const document = parse5.parse(orig);
const ser = parse5.serialize(document);
console.log(`============orig==========\n${orig}\n==============ser============\n${ser}\n`);

appersonj avatar May 01 '19 18:05 appersonj

To make sure this is actually an issue, can you try this in a browser, check the DOM and make sure it doesn't behave the same way?

RReverser avatar May 01 '19 18:05 RReverser

Also, please use

```js
code block
```

for code snippets.

UPD: I've edited your comment myself for now to make snippets more readable and to understand what's going on.

RReverser avatar May 01 '19 18:05 RReverser

UGH - results in browsers are inconsistent - Chrome moves the '\n's up, to last non-body tag, similarly to parse5 Firefox deletes trailing '\n's

Still, ideally, as much as possible, I would guess that: parse5.serialize(parse5.parse(doc)) should produce doc

appersonj avatar May 01 '19 19:05 appersonj

parse5.serialize(parse5.parse(doc)) should produce doc

That's not the case, HTML parsing spec is not intended for roundtrip and does a lot of fixes and adjustments for real-world usage.

RReverser avatar May 02 '19 11:05 RReverser

I'm going to close this now, but feel free to open another issue if you find a violation of the spec, ideally with reproduction in browsers.

RReverser avatar May 02 '19 12:05 RReverser