Warn on invalid HTML hierarchy
See original GitHub issueExample
Elements inside a JavaScript list which are rendered as children of a <p>
have their HTML attributes dropped and put onto other elements of the same tag found below.
This bug only affects <p>
, does not happen to <div>
, <span>
, <b>
, nor <section>
.
Result
Expected
preact-cli actually pre-renders it correctly. We can get the correct result by disabling JavaScript in the browser.
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (7 by maintainers)
Top Results From Across the Web
8 Invalid HTML Elements You Should Stop Using Immediately »
1 Valid but Poorly Implemented. 1.1 DOCTYPE; 1.2 Identifying the Character Set · 2 HTML Elements to Stop Using Immediately · 3 Tasks...
Read more >Visual Studio warns me about some invalid html attributes
I used the attributes idAffaire and idSuite for retrieving some infos later. I know the official identification attribute is "id" but in my...
Read more >aria-invalid - Accessibility - MDN Web Docs
The aria-invalid attribute is used to indicate that the value entered into an input field is not in a format or a value...
Read more >6 Reasons Why Google Says Valid HTML Matters
A recent tweet from Google's Gary Illyes called attention to problem of invalid HTML. Google is fine with invalid HTML.
Read more >Validating your HTML - W3C Wiki
Validation is your early-warning system about introducing bugs into your ... When a browser encounters invalid HTML, it has to take an educated...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Alright, I got it 🎉 Initially I assumed that there must be a problem in
preact-render-to-string
but that’s not the case. It renders the correct result.Instead the weird behaviour is actually written down in the HTML spec itself. It states:
So this means that this:
will be turned into:
At this point the DOM hierarchy has changed substantially and the
<p>
-tag doesn’t wrap the colored children anymore.What’s worse is that the DOM hierarchy you’re trying to render is invalid and browsers immediately enter the “quirks”-mode, where the browser tries to guess what the developer tried to achieve with the html document. This differs from browser to browser and is somewhat undefined behaviour. It’s safe to say that the outcome is rarely what’s expected.
The reason the HTML is invalid is because a
p
-tag does only allow phrasing content as children, which is defined here: https://html.spec.whatwg.org/multipage/dom.html#phrasing-content-2 Putting adiv
tag as a child ofp
is therefore invalid. Same as nestingp
tag. That’s invalid, too.Following the guidelines of the spec we can use
span
elements as children:Proof:
I’m wondering if there is a way for us to warn the user in
preact/debug
.Definitely can’t go the spec validation route here, so our options are basically limited to bailing out of non-corrective hydration when encountering tree mismatch. I have a WIP version of this locally I’ll try to extract from my progressive hydration stuff.