Malformed HTML parsed differently from browsers
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 2.6k
- Forks
- 288
- Avg merge
- 2d 22h
- Merged PRs (30d)
- 8
Description
I have an HTML file with markup that can be reduced to the following:
<html>
<body>
<div id="div0">
<a hr
</div>
<div id="div1">
<div id="div2"></div>
<div id="div3">
<a href="/">bar</a>
</div>
</div>
</body>
</html>
Notice the truncated <a tag on line 4 (caused by an HTML fragment accidentally truncated in the DB).
If I create a file with this content, load it in Firefox and print the resulting DOM with document.getElementsByTagName("html")[0].outerHTML , Firefox returns:
<html><head></head><body>
<div id="div0">
<a hr="" <="" div="">
</a><div id="div1"><a hr="" <="" div="">
<div id="div2"></div>
</a><div id="div3"><a hr="" <="" div="">
</a><a href="/">bar</a>
</div>
</div>
</body></html>
- The truncated link results in 3 nodes in the DOM
- The well form tag with text
baris still present in the output
However, if I parse the input with html5ever and print back the result, I get:
<html><head></head><body>
<div id="div0">
<a hr="" <="" div="">
</a><div id="div1"><a hr="" <="" div="">
<div id="div2"></div>
</div>
</div></body></html>
- The truncated link only appears twice
- The well-formed link with
barcompletely disappeared!
EDIT: See next message, there are still some differences but the ones here seem to be caused by the TreeSink impl I used, not the parser.
This difference in interpretation between Firefox/Chrome and html5ever is causing me issues when processing these documents to recover them. I'm well aware that the input is broken, but I would expect html5ever to produce the same structure as real browsers.
EDIT: Even smaller repro, removing the newline fixes the mismatch.
<html><body><div><a hr</div><div><div></div>
<div><a href="/">bar</a></div></div></body></html>
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the smaller HTML reproduction in the issue and compare html5ever's parsed output with the browser DOM, accounting for the noted TreeSink differences. The work is done when the malformed input produces the same relevant node structure and preserves the well-formed link as browsers do.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- web-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100