by computomatic · 3y ago · 0 comments
That said, it failed to persuade me. I’d still favour the self-closing style in any style guide. At least now it would be a more informed (and better scoped) preference.
Considering two style guides:
- <tag> is an opening tag
- </tag> is a closing tag
- <tag /> is a self-closing tag
Note: it remains the responsibility of the author to know which tags are self-closing.
Seems more useful than: - <tag> is an opening tag or a self-closing tag
- </tag> is a closing tag
Admittedly, it’s subjective. That’s why any consistent guide is better than none.And then, when you've done that, you can just remove the "/>" bit, since it's a redundant rule.
The writer of the HTML will be told off if they try to use /> on a tag that won't actually self-close, and they will also be told off if they neglect /> on a tag that self-closes.
Once written, the reader knows that a tag that ends in /> closes itself, and if it ends in > then there's definitely a closing tag somewhere.
By the time the parser has seen the sequence `<br` it already knows which element this is, because it can only be the BR element, and has already finalized the DOM node for it because that's the rule for the BR tag. The moment it sees the opening angled bracket and tag, that node is created in an already closed state.
So those extra two `/>` do, in the most literal sense possible, nothing at all. They're allowed to be there for historical reason but they literally do nothing: they're treated as bytes to be ignored and the DOM parser skips over them while it looks for the next active token in order to continue building the DOM.
<p>A<br B</p>
<p>C</p>
To render as this: A
B
C
But instead it renders as this: A
C
The parser treats `B<` and `p` as attributes of `br`, and determines that the `<br` tag ends with the `>` at the end of line 1.If I end the `<br` with either `/>` or `>`, it parses correctly. Which means that `/>` and `>` are both considered valid endings for the `<br` tag, but you have to have one or the other and it will keep looking until it finds one.
And they should be able to get almost all of the way there by remembering one very simple rule: self-closing tags to insert something at that point in the document, open/close tags to describe the content they surround.