← Back to context

Comment by hnlmorg

19 hours ago

2. And there’s no ambiguity there, just invalid HTML because paragraphs aren’t nestable.

It 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/...

  • Wouldn'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.

Browser 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.

  • > 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.

    • >Why?

      Because the second open p-tag closes the first p-tag and then the last closing p has no matching starting p-tag and creates one thus resulting in 3 p-elements.

      > It isn’t an opinion, it literally is invalid HTML.

      the only "invalid" part is the last closing p.

    • > 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.

      4 replies →

  • At 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.

    • My 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.

      2 replies →