Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Is <br /> really prone to that? I've only seen that in people actually trying to do XHTML.


It is a really common thing for people to erroneously close void elements[0] that aren't supposed to have closing tags, or to give them contents when they can't have any. For example:

    <p> some paragraph </p>
    <br></br>
    <p> a second paragraph </p>
or:

    <p> some first paragraph </p>
    <br><p> a second paragraph </p></br>
    <p> a third thing </p>
Neither of those are valid, and the last one is interpreted as:

    <p> some first paragraph </p>
    <br>
    <p> a second paragraph </p>
    <br>
    <p> a third thing </p>
New users find behavior like this confusing all the time.

[0] - http://www.w3.org/TR/html5/syntax.html#void-elements


This is in reference to "<br />" not "</br>"

The "/>" is syntax for a self-closing tag in xhtml (e.g. you were supposed to use <img src="" /> etc).

You see "<br />" a lot in php forums because these will often filter user input through the function nl2br whose purpose is to add HTML linebreaks wherever a person has a newline in their post. PHP's nl2br function by default inserts "<br />" unless you tell it specifically not to use xhtml-compatible linebreaks.


Wow that's odd. Also, PHP has a built in function for filtering filtering plain-text user input to create HTML? That's... not something I would have expected.


Sarcasm? PHP has a built-in function for any oddball thing you can think of.


Personal fav: gzgetss(). Reads a line from a gzip file handle and strips HTML tags from it. Why? I don't know!

http://php.net/manual/en/function.gzgetss.php


It's super useful. If you have a textarea for users to type paragraphs into you can use nl2br to output it more or less the same, but in HTML. (Be sure and escape the text before nl2br not after.)


He wasn't talking about closing void elements, he meant self-closing void tags, like <img/>, <br/>, and <hr/>. These were introduced by XHTML to fit with the idea that every tag needs to be closed. HTML5 does away with the requirement, but the HTML5 grammar does explicitly support self-closing tags (http://www.w3.org/TR/html5/syntax.html#self-closing-start-ta...). At this point, self-closing tags are more of a visual preference, as they have a negligable effect on parsing.


At this point, self-closing tags are more of a visual preference, as they have a negligable effect on parsing.

In fact they can be said to have no effect on parsing HTML itself, as the only time when self-closing makes any difference is when the tag's name isn't one that HTML defines - what the spec calls "foreign elements" (e.g. inline SVG or MathML). Those will close when their tag is self-closing, but the closing behaviour of HTML tags is hardcoded in the parser.


Minor correction: There will not be a second <br> in your second example. The </br> will be completely ignored.


Actually .. it wouldn't : http://jsfiddle.net/kv2tzb5n/ (at least in chrome). Would you say this is a good moment to pass judgment on your programming abilities ?


> Would you say this is a good moment to pass judgment on your programming abilities ?

Sure. Interestingly </hr> is ignored, but <br></br> is parsed as a block of some kind. I did not know this, so yes, you can judge me for it.

I know you are poking me for what I said about self closing tags, but I stand by it. I've seen a lot of html, and it's one very easy way to find the bad programmers - they all self close <br>. I'm sure there are exceptions, but I have not come across any.

It's a sign of a tutorial programmer (not exactly), maybe, more or a copy/paste programmer, who finds "blocks" of code that work, and just apply it everywhere.

They tend to lack the deep fundamentals of how html works.


> I've seen a lot of html, and it's one very easy way to find the bad programmers - they all self close <br>.

There are a lot of people, including me, who does it out of habit, aesthetic worries, or to make the markup easier to parse for (some) humans. It's a bit like using Egyptian Brackets in JavaScript.


I use self-closing tags for aesthetic reasons, even though I know they aren't HTML. I like the consistency. I suppose I should get with the times, though, if I am to be taken seriously.


Try doing <div/> or <script/> and you will see why it's a bad idea


Try doing a parser and you will see why self-closing void tags are generally a good idea.

Anyway, I don't understand this discussion at all. I was expecting something a bit better from the HN community. Since when did we start making strong judgments based on purely esthetical things?

An applicant to a position we offered once submitted a code without <head/> nor <body/> tags, and his justification was totally right: the standard doesn't actually require it[1]. So does that make you a bad programmer for using them? Not using them? I believe nobody really care, and I would have to be a really bad person to judge my colleagues based on that.

[1] http://www.w3.org/TR/2011/WD-html5-20110525/syntax.html#opti...


Not a problem if you send it as application/xhtml+xml ;)


Those aren't void tags.

I've been working with web technologies for years and I happened to like a lot of things about XHTML. I know how the shit works, I just feel its more consistent to close all of your tags. You obviously wouldn't self-close a <div> or a <section> because those tags are supposed to have content!

As an aside, I would argue that <script> should be nullable when it has a src attribute.


It's not parsed as a block of some kind. The standalone <br> is parsed as it should be, without surprises. The closing tag, however, is parsed as another <br> tag. This is usual HTML behaviour, though, and since HTML 5 thoroughly specified.


The ever-tolerant HTML browser will automatically fix it up. The irony is in that this XHTML syntax has bled back into some code styles in HTML, however inaccurate in that language, but since the introduction of JSX and React, the self-closing tag is required again (all tags must close, or self-close, to be able to parse the file at all)


..Which is ironic since xhtml was supposed to be strict enough that browsers could stop being so tolerant.


and in true contemporary web development style, we threw it all away and wrote a javascript pre-processor for it instead.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: