News for hackers. Stuff that matters.
Re:The compiler flag that time forgot by lelandbatey on Monday May 04, 2015 @08:56PM (#9490301)
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

Re:The compiler flag that time forgot by chias on Monday May 04, 2015 @09:04PM (#9490322)
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.

Re:The compiler flag that time forgot by lelandbatey on Monday May 04, 2015 @09:11PM (#9490346)
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.
Re:The compiler flag that time forgot by nkozyra on Monday May 04, 2015 @10:01PM (#9490476)
Sarcasm? PHP has a built-in function for any oddball thing you can think of.
Re:The compiler flag that time forgot by duskwuff on Tuesday May 05, 2015 @03:12AM (#9491237)
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

Re:The compiler flag that time forgot by ars on Monday May 04, 2015 @11:17PM (#9490683)
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.)
Re:The compiler flag that time forgot by kedean on Monday May 04, 2015 @09:14PM (#9490360)
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.
Re:The compiler flag that time forgot by userbinator on Monday May 04, 2015 @10:40PM (#9490603)
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.

Re:The compiler flag that time forgot by ars on Monday May 04, 2015 @11:18PM (#9490685)
Minor correction: There will not be a second <br> in your second example. The </br> will be completely ignored.
Re:The compiler flag that time forgot by sirrocco on Monday May 04, 2015 @11:59PM (#9490782)
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 ?
Re:The compiler flag that time forgot by ars on Tuesday May 05, 2015 @01:00AM (#9490922)
> 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.

Re:The compiler flag that time forgot by egeozcan on Tuesday May 05, 2015 @04:30AM (#9491429)
> 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.

Re:The compiler flag that time forgot by xj9 on Tuesday May 05, 2015 @02:18AM (#9491111)
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.
Re:The compiler flag that time forgot by jontro on Tuesday May 05, 2015 @03:08AM (#9491231)
Try doing <div/> or <script/> and you will see why it's a bad idea
Re:The compiler flag that time forgot by arcatek on Tuesday May 05, 2015 @06:37AM (#9491783)
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...

Re:The compiler flag that time forgot by xj9 on Wednesday May 06, 2015 @10:26AM (#9498829)
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.

Re:The compiler flag that time forgot by realityking on Tuesday May 05, 2015 @04:46AM (#9491476)
Not a problem if you send it as application/xhtml+xml ;)
Re:The compiler flag that time forgot by ygra on Tuesday May 05, 2015 @05:47AM (#9491657)
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.