19

Someone posted an html/css question the other day and I answered it by directly referencing a piece of code found on w3.org.

I know I found exactly the answer the OP was looking for, and since my answer was taken from a reliable source I didn't test it or check it for errors.

The code I pasted was for how to generate dot leaders, and this is the markup:

<ul class=leaders>
  <li><span>Salmon Ravioli</span>
    <span>7.95</span>
  <li><span>Fried Calamari</span>
    <span>8.95</span>
  <li><span>Almond Prawn Cocktail</span>
    <span>7.95</span>
  <li><span>Bruschetta</span>
    <span>5.25</span>
  <li><span>Margherita Pizza</span>
    <span>10.95</span>
</ul>

At a quick glance, the markup looks fine, but if you look closer you'll notice that there is no closing list element </li> tag anywhere. Another user pointed this out and edited my answer.

I found this to be peculiar, given the fact that I had never seen such formatting (for li) but on the other hand the code was taken from no else than the World Wide Web Consortium. So I googled it and found that the closing </li> tag is actually optional.

An li element's end tag may be omitted if the li element is immediately followed by another li element or if there is no more content in the parent element.

So my question here is, which would be the right/best format of this code for an answer here on Stackoverflow? Should the conventional ways of markup-formatting be upheld or should quoted information from reliable sources always remain unedited?

14
  • 8
    Simply state that the code was "derived" from the source rather than copied from the source. Same effect, but makes it clear that it may have been modified by you.
    – Kevin B
    Commented Oct 20, 2015 at 20:03
  • That's a very good idea indeed.
    – Chris
    Commented Oct 20, 2015 at 20:07
  • 2
    Adding that note as well would be a valuable addition to your answer, for all future visitors, who may wonder why your answer diverges from "the standard".
    – Jongware
    Commented Oct 20, 2015 at 20:20
  • 29
    I would leave it as found on W3C, and add a note explaining </li> is optional. This way there is more to learn from the answer.
    – alain
    Commented Oct 21, 2015 at 11:05
  • 1
    I honestly didn't know that there are optional tags in html5. This seems strange to me, but kind of makes sense considering <script> and <template> elements may be children of <ul> or <ol>. You can roll it back if you want.
    – Artjom B.
    Commented Oct 21, 2015 at 11:41
  • 2
    Please consider changing the link text in your question from "W3.com" to "w3.org", as that is the canonical URL. W3.com is not associated with the World Wide Web Consortium. Also, minor thing, but the name of the website (and company) is Stack Overflow, two words. Commented Oct 21, 2015 at 13:56
  • @MikeMcCaughan whoops! Fixed the typo.
    – Chris
    Commented Oct 21, 2015 at 14:07
  • 3
    Btw, if you see somebody who made a wrong or questionable edit, you can talk to them directly by using @Editor'sDisplayName in the comments under the post. There won't be name autocompletion, but they will receive a notification. This doesn't work for rejected edit suggestions. The editor actually has to be in the edit history.
    – Artjom B.
    Commented Oct 22, 2015 at 13:02
  • Another thing I noticed is the missing quotes around the class-name in the ul. hmm...
    – Chris
    Commented Oct 22, 2015 at 18:24
  • 2
    @Chris Also optional, if the attribute value is a single word. Not recommended though.
    – Mr Lister
    Commented Oct 22, 2015 at 19:46
  • If it's actually a quote then I'd always leave it as-is (though presumably you'd want to quote a better source if it was wrong). Perhaps they were an XHTML "pro".
    – Klors
    Commented Oct 23, 2015 at 20:02
  • FWIW, the entire kerfuffle likely would have been avoided had you, before posting an answer, done a quick search on Stack Overflow to determine that the question was in fact a duplicate of many existing questions, including one particular example that included an answer that referenced the same exact example you did. Commented Oct 23, 2015 at 21:29
  • @PeterDuniho kerfuffle?? I see it as a learning experience. Why "upgrade it" to something dramatic when it never was? Nobody seems bothered by neither this meta post nor my answer, apart from yourself. Also, considering the fact that there are at least 3 other possible answers to the question, it only seems reasonable to mark it as "too broad" rather than a duplicate, and especially so when OP didn't include any code in his/her post.
    – Chris
    Commented Oct 23, 2015 at 22:39
  • I wonder why the post was deleted, rather than just closed.
    – Chris
    Commented Oct 25, 2015 at 2:17

2 Answers 2

32

This edit should be rolled back.

Yes, if the edit had been made to make the HTML more readable, then the edit would have been valid. Whether it actually is more readable is arguable, but I suspect pretty much everyone will at least agree that it's not less readable.

But that's not why the edit was made. The edit summary states "pro-tip: produce valid HTML". Editing an answer that contains perfectly valid HTML while claiming the edit is to make it valid is just wrong.

The edit summary matters. People do look at the revision history of answers. Leaving it as it is now spreads misinformation.

7
  • 29
    Also, writing "pro tip" as edit remark could be considered rude.
    – Lundin
    Commented Oct 22, 2015 at 11:57
  • 3
    Maybe a comment should be added to address the issue to the "pro-" editor as well. (Unless an automatic notification is generated anyway.)
    – moooeeeep
    Commented Oct 22, 2015 at 12:09
  • 1
    I disagree that the edit should be rolled back - I think a comment on the answer explaining that the original was valid HTML, but the edit is preferred best practice is more informative and leaves the answer in a better state for future users.
    – Jason
    Commented Oct 22, 2015 at 12:50
  • 2
    @Jason It's not a preferred best practice, at least not a generally accepted one. You may personally prefer it, that's fine, but your preference is not universal. (The original version is not a generally preferred best practice either. There's simply no general agreement.)
    – user743382
    Commented Oct 22, 2015 at 12:59
  • That my preference is not universal doesn't mean that it shouldn't be :)
    – Jason
    Commented Oct 22, 2015 at 13:35
  • @hvd I went looking for anyone who recommended omitting closing tags, and found no less than Google doing so - however, even in that circumstance, it makes far more sense to write the HTML with closing tags and then process it with a (perhaps hypothetical, at the moment) HTML compiler to produce the output with closing tags omitted... I can't think of any situation where I would want to maintain their recommended example.
    – Jason
    Commented Oct 22, 2015 at 13:50
  • 4
    @Lundin - it is rude if you are right, moronic if you are wrong. Pro-tip, in this case moronic :)
    – Hogan
    Commented Oct 22, 2015 at 18:12
5

My gut feeling is do what makes the answer better. If someone quoted some code from somewhere but was nigh unreadable for formatting reasons, I would expect them to format it better. In this case, I'd expect the closing tags would cause less confusion and problems in browsers. I would add the closing tags in and maybe make a note that you added them.

If you choose to leave it alone, I would add a note with the link mentioning that the closing tags are optional.

As long as you aren't changing things in any major way or in a way that makes the code wrong or contradicts what you are quoting, I wouldn't worry about it.

9
  • 5
    As far as I know, browsers are almost always very good at handling missing closing tags, even when they aren't officially optional. So there aren't likely to be any real problems, except perhaps with poorly-written regex parsers, but we know the <center> cannot hold on those anyway. Commented Oct 20, 2015 at 20:05
  • 2
    Yes, it's certainly valid to omit them, but by doing so you're likely to draw attention of other users who will dispute the correctness, which is of course irrelevant in this case. Better to just have them than to have to have that discussion in comments in my opinion.
    – Kevin B
    Commented Oct 20, 2015 at 20:08
  • 1
    @NathanTuggy Agreed that most browsers do just fine. But I know I've seen more than a few pages turn out funny because of a missing tag (and finding that tag can be a pain). Also, if you wanted to parse that HTML with a library, who knows how it would handle it. At any rate, fixing it doesn't hurt in this specific case.
    – Becuzz
    Commented Oct 20, 2015 at 20:09
  • @Becuzz: If you're parsing with l33td00d's s00perSpeedy HTML library (v3.14), sure, there's probably going to be glitches. Anything vaguely professional, though, like the libraries present in various languages, I would be quite surprised to see choke on that. Commented Oct 20, 2015 at 20:11
  • @NathanTuggy: having an XHTML parser somewhere in the chain can be enough to turn <iframe></iframe> into <iframe /> into (in the browser) <iframe>-and-whoops-no-end-tag, so it's not as "that's what you get for using homebrew tools" as you might expect. Commented Oct 22, 2015 at 12:05
  • 1
    @KevinB, Better to just have them than to have to have that discussion in comments IMO - I disagree. The point of the discussion is to find a common ground (and differences) and figure out the truth. Without discussion, those who think </li> is required will go on having false information. With discussion, more information is shared and arguments from both sides can be posed as to best practices vs legal html.
    – Reed
    Commented Oct 22, 2015 at 18:13
  • Right, but it isn't relevant to the question being asked in any way shape or form, and isn't an issue as serious as sql injection. No discussion of it needs to happen in the comments to that question.
    – Kevin B
    Commented Oct 22, 2015 at 18:14
  • Exactly. Pointing out how it is, is an educational experience for those who don't know. Personally, I'm fond of pointing out that for instance tbody has an optional start tag as well, which means that even if you write <table><tr> you will end up with a tbody in your table. People often don't realise that.
    – Mr Lister
    Commented Oct 22, 2015 at 19:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .