parse5 moves newlines to the last non-body tag
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`);
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`);
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?
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.
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
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.
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.