5 ms·
> You don't need to close your tags. Not always true. I’ve run into numerous issues caused by the lack of closing tags, and just did earlier this week.
by mindctrl-org 6y ago
> You don't need to close your tags.
Not always true. I’ve run into numerous issues caused by the lack of closing tags, and just did earlier this week.
- duxup 6y agoYeah so many code formatters and such tell me to close the tag .. I'm gonna close it... must be a reason.
- lazyjones 6y agoThe reason is that the code formatters are broken and don't support the full html5 specification.
- johncmouser 6y agothey will be nested right. so <div> <p> <h1> <h2> without closing tabs create the DOM tree div->p->h1->h2 if you were actually developing production code and misplaced, let's say, <p>'s closing tag, then that would mess up the rest of your tree (from your perspective -- the computer doesnt care)
- naniwaduni 6y agoThis actually produces the DOM equivalent to <div> <p> </p><h1> </h1><h2> </h2></div>. Many of the rules for unclosed tags are more there so that browsers can agree on what to do with garbage first, and for you to rely on only incidentally! They defer to historical practice before common sense! In order to predict this reliably, you essentially need to have the list of content categories[1] memorized (or look them up). Not all of them are ... necessarily intuitive. [1]: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Cont...
- ufo 6y agoIs there a way to get warnings for HTML that looks valid with matching start and end tags but doesn't actually parse the way it is written? I get the impression that we end up needing to memorize those content categories even if we plan to only generate html with all the start and end tags. For example, <p>A<p>B</p>C</p> looks like two nested <p> but it is parsed as 3<p> next to each other: <p>A</p><p>B</p>C<p></p>.
- naniwaduni 6y agoAt the margins, yes, but in practice if you have seemingly balanced opening and closing tags but invalid nesting, the outer close tag generally makes the HTML invalid, for which there's plenty of tooling to check.
- uryga 6y agoi haven't tested it, but MDN says: "The end tag may be omitted if the <p> element is immediately followed by an <address>, <article>, <aside>, <blockquote>, <div>, <dl>, <fieldset>, <footer>, <form>, <h1>, <h2>, <h3>, <h4>, <h5>, <h6>, <header>, <hr>, <menu>, <nav>, <ol>, <pre>, <section>, <table>, <ul> or another <p> element" (i.e. the things that aren't valid children for a <p>, iirc) so i believe your example would end up parsing as <div> <p></p> <h1></h1> <h2></h2> </div> and not as <div> <p> <h1> <h2></h2> </h1> </p> </div> (i'm not sure how exactly the <h1>/<h2> would behave - an <h1> can't have an <h2> child, but they don't have self-closing rules. it's probably in the spec somewhere)