Axiom Of The Empty Set

A list without any items in it is still a list, I must insist. Just like a box without anything in it is still a box, or a Smurf village without any Smurfs (Smurves?) is still a frickin Smurf villiage..

But not according to the W3C, apparently. File this one under Rant Of The Day: none of the standard XHTML doctypes acknowledge the existance of the empty list.

<!ELEMENT ul (li)+>

That is, neither the <ul></ul> or the <ul/> representations are valid XHTML. This seems pretty broken to me at first glance, although I’m certianly willing to hear a rebuttal if anyone has one. Even if you don’t think it’s busted, it certainly adds complication to a very common web dev scenario…

So we have a list of resources in our view and there’s a good possibility that our list is empty. We want to be able to display that list, and have a button on the page that lets me add new items to the list with a little Ajax love so that we don’t have to reload the page. If we can represent an empty list in the page, it’s simple: we can just render :update in Rails…

render :update do |page|
page.insert_html(:bottom, :my_list, "<li>#{item}</li>")

The code above assumes that there’s a DOM ID ‘my\_list’ that has 0 or more elements. But since XHTML won’t let us represent a 0-element list, the obvious thing to do is to bake some extra smarts into the update code.

Now it’s the job of the update to determine if a list exists, and if not to create it, but only for the special one-off case of the zero element list? Yuck, bleh. No thanks. This is probably what most people do, but it just strikes me as putting too much intelligence into something that’s supposed to be pretty dumb.

The alternate solution listed here also qualifies as a hack, no bones about it. But, at least in my opinion, it’s a somewhat more elegant hack than what was described above. File under simple Workaround Of The Day:

<ul id="list_things">
<li class="invisible" style="display: none"/>

Note that the W3C does support empty list item elements :-). Thanks to azta for the suggestion.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.