3 ms·
Closing optional HTML tags just adds more ambiguity. How many HTMLParagraphElements here, what do you think? <p> text1 <p> text2
by vbezhenar 9mo ago
Closing optional HTML tags just adds more ambiguity. How many HTMLParagraphElements here, what do you think?
<p>
text1
<p>
text2
</p>
</p>
- have_faith 9mo agoWhy would you nest paragraph tags?
- Hendrikto 9mo agoThey are not nested, according to HTML5 parsing rules. You get 3 (yes, three) sibling paragraphs, including an empty one. There being nesting is just implied by the closing tags and indentation. But it is not actually there. I think this is the point of the example: Adding the closing tags just confuses the reader, by implying nesting that is not actually there, and even introduces a third empty paragraph. It might be better left out entirely.
- hnlmorg 9mo ago2. And there’s no ambiguity there, just invalid HTML because paragraphs aren’t nestable.
- kzrdude 9mo agoIt may look nested but the first p is actually closed when the second p starts, according to https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/p https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/...
- jchw 9mo agoWouldn't this still result in just two paragraph elements? Yes, the first gets auto-closed, but I don't see how a third paragraph could emerge out of this. Surely that closing tag should just get discarded as invalid. edit: Indeed, it creates three: the </p> seems to create an empty paragraph tag. Not the first time I've been surprised by tag soup rules.
- hnlmorg 9mo agoHence why I said “2” ;)
- deleted 9mo ago[deleted]
- vbezhenar 9mo agoBrowser will parse that as three HTMLParagraphElements. You may think that's invalid HTML, but browser will parse it and won't indicate any kind of error.
- psychoslave 9mo agoAt the end of the day, browsers have to handle most of the invalid atrocities thrown at it. It doesn't make the code valid according to the specifications.
- vbezhenar 9mo agoMy point is that by closing optional tags you can introduce subtle bugs into your layout that might take some time to find and browser won't be of any help. You write closing tag, browser will implicitly add starting tag. It's better to memorise which tags are optional and do not close them at all.
- hnlmorg 9mo agoYou can also introduce subtle bugs by not closing them. Or forgetting which tags can be closed and thus leaving the wrong ones dangling. So I think your argument here is tough to take at face value. It feels a lot more like you’re arguing personal preference as fact.
- psychoslave 9mo agoPrecisely, it's an added burden to remember and what might be skipped. The less many exception, the better. Though if a linter is formatting the whole codebase on its own in an homogeneous way, and someone else will deal with the added parsing complexity, that might feel okayish also to me. Generally speaking, the less clutter the better. A bit like with a js codebase which is semicolon free where possible. For pleasant experience of read and write, html in a simple text editor is very low quality. Pug for example is bringing far less clutter, though mandatory space indentation could be avoided with some alternative syntactic choices.
- hnlmorg 9mo ago
- urbandw311er 9mo agoExcellent catch
- b2ccb2 9mo agoThat is invalid syntax. Only phrasing content is allowed the p element (https://developer.mozilla.org/en-US/docs/Web/HTML/Guides/Content_categories#phrasing_content https://developer.mozilla.org/en-US/docs/Web/HTML/Guides/Con...)
- jeroenhd 9mo agoThe second <p> is not inside of the first. The first <p> closes when the second <p> is encountered. The syntax is invalid, but that's because the final </p> has no opening <p> that it can close.
- psychoslave 9mo agoThis is invalid html, p tag can be nested in a p tag.
- Hendrikto 9mo agoEven though it arguably should be, according to HTML5 parsing rules, this is not invalid. It is just interpreted differently from what most people would probably expect. I think this is the point of the example, afaiui: The closing tags don’t clarify anything, quite the contrary, actually. They serve only to confuse the reader.