3 ms·
<div /> Hello …should reformat to: <div>Hello</div> I would argue that this should probably reformat to <div></div> Hello Although this
by michaelmior 3y ago
<div />
Hello
…should reformat to:
<div>Hello</div>
I would argue that this should probably reformat to
<div></div>
Hello
Although this is different from how browsers would interpret it, I think it's much more likely that whoever wrote <div/> intended to have an empty div.
- jaffathecake 3y agoPrettier aims to be a reformatter only. As in, the result should be the same.
- jraph 3y agoYou have a point and it is attractive, but I think a tool that reformats HTML should not change its actual meaning, the result should render the same way as the input unless explicitly asked. The original page could have been tested and a difference would be surprising. It could be an option though.
- taeric 3y agoWait, the expectation is that <div/> some text will be interpreted as <div>some text</div> Why? I'm assuming this is "well codified" somewhere, but why?! I would understand if you can't have empty divs, but you can.
- layer8 3y agoIt’s because the trailing “/“ has zero semantics now, by definition. You can add or remove it from any tag, and it doesn’t change the meaning of your HTML. <div/> some text is thus equivalent to <div> some text which in the absence of any further tags or content is interpreted as <div> some text </div> (Note however the exception for foreign content like SVG, as noted in the article.)
- oneeyedpigeon 3y agoWhat's your actual rule here? How would you handle: <div/> some text and some more text or <div/> some text, an <img src="" />, and some more text or <div/> some <em>emphasized</em> text ??
- taeric 3y agoIt is HTML5. https://html.spec.whatwg.org/multipage/syntax.html#optional-tags https://html.spec.whatwg.org/multipage/syntax.html#optional-..., if I'm not mistaken. Rules are of the form: A td element's end tag may be omitted if the td element is immediately followed by a td or th element, or if there is no more content in the parent element. Edit: I don't know the "div" rules. Don't see that here. Edit2: I'm actually curious on if what I put here is wrong.
- bentley 3y agoYou’re quoting a description of implicit end tags, but the conversation is about self‐closing tags, which are a different thing.
- taeric 3y agoSorta? The thread was about how odd it is that <div> has implicit end tags? So, makes sense that the topic seems different, but it is the explanation for why a div grows to contain everything under it. For example, this is needed to know why the div in the following text closes. <table> <tr> <td>Some cell <td>Another cell <tr> <td>And <div>Another <tr> <td>With more stuff. Right? I get that the original confusion is thinking you can make a self closing div. But the fact that it expands to cover things that come after it requires you to know about the implicit closing. Hard to understand one without understanding the other. Edit: I suppose it would be better for me to say that none of the examples on the immediate parent post are different, because none of them close the div? It isn't that the one div expands to cover everything after it, is that nothing caused it to be closed. Indeed, if those are just snippets from a document, you can't be sure that stuff that comes after it is also not included in the div.
- deleted 3y ago[deleted]
- pdw 3y agoIf I understand it correctly, The `<foo />` syntax is an XML invention. It was never added to HTML. Traditional HTML parsers see the slash as a malformed attribute and ignore it, and that behavior was codified in HTML5.
- taeric 3y agoThe whole "we want to look like XML, but not /be/ XML" has to be one of the most annoying aspects of it. I shouldn't be too surprised on this particular edge, but holy crap I also wouldn't be shocked to know that I did it wrong in some pages.
- michaelmior 3y ago> should not change its actual meaning I would tend to agree, which is why I made this suggestion. I think it's much more likely that what was meant is to insert an empty tag, even if that's not how browsers would interpret it.