3 ms·
2. And there’s no ambiguity there, just invalid HTML because paragraphs aren’t nestable.
by hnlmorg 10mo ago
2. And there’s no ambiguity there, just invalid HTML because paragraphs aren’t nestable.
- kzrdude 10mo 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 10mo 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 10mo agoHence why I said “2” ;)
- deleted 10mo ago[deleted]
- vbezhenar 10mo 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 10mo 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 10mo 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 10mo 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 10mo 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 10mo ago> Browser will parse that as three HTMLParagraphElements Why? > You may think that's invalid HTML, but browser will parse it and won't indicate any kind of error. It isn’t an opinion, it literally is invalid HTML. What you’re responding to is an assumption that I was suggesting browsers couldn’t render that. Which isn’t what I claimed at all. I know full well that browsers will gracefully handle incorrect HTML, but that doesn’t mean that the source is magically compliant with the HTML specification.
- vbezhenar 10mo ago> Why? I don't know why. Try it out. That's the way browsers are coded. > It isn’t an opinion, it literally is invalid HTML. It matters not. You're writing HTML for browser to consume, not for validator to accept. And most of webpages are invalid HTML. This very HN page contains 412 errors and warnings according to W3C validator, so the whole point of HTML validness is moot.
- hnlmorg 10mo ago> I don't know why. Try it out. That's the way browsers are coded. I’m not saying you’re wrong, but I’d need more than that to be convinced. Sorry. > It matters not. You're writing HTML for browser to consume, not for validator to accept. It matters because you’re arguing a strawman argument. We weren’t discussing what a browser can render. We were discussing the source code. So your comment wasn’t a rebuttal of mine. It was a related tangent or addition.
- vbezhenar 10mo ago> I’m not saying you’re wrong, but I’d need more than that to be convinced. Sorry. So basically my point is: 1. You can avoid closing some tags, letting browser to close tags for you. It won't do any harm. 2. You can choose to explicitly close all tags. It won't do anything for valid HTML, but it'll introduce subtle and hard to find DOM bugs by adding empty elements. So you're trying to improve HTML source readability by risking to introduce subtle bugs. If you want to do that, I'd recommend to implement HTML validation for build or test pipeline at least. Another alternative is to use HTML comments to close tags, as this closing tag is supposed to be documentation-only and won't be used by browser in a proper code.
- urbandw311er 10mo agoExcellent catch