About those labels
March 19th, 2008After some recent newsgroup posts, I thought it might be a good idea to clarify how text is used in XUL.
XUL provides two elements for displaying text, <label> and <description>. The <label> element should be used when labelling a control or user interface element. It supports a control attribute which may be used to associate the label with another element, and an accesskey attribute to support a key shortcut for focusing the associated element.
The <description> element should be used for all other text in XUL. This element does not support the control or accesskey features.
Apart from that, both elements are the same, and are implemented the same.
There is, however, another characteristic that applies to both the <label> and <description> elements, and determines how the text is rendered.
One form involves placing the text inside the element as a child:
<label>Search:</label> <description>Enter your login information</description>
The two lines of code above cause the elements to become CSS block elements. This text is displayed in the same way as that for displaying the text in web pages for instance, and supports things like breaking paragraphs into lines, drawing underlines, selection and spellcheck highlighting, and adjusting text as necessary to support right to left text, ligatures and various additional characteristics of certain languages. Thus, you can put whatever elements are allowed in a CSS block within these elements, for instance an html <br> element.
The second form is by using the value attribute:
<label value="Back"/> <description value="Font Selection"/>
The code that implements this is different, is much simpler and doesn’t support line breaking – everything is rendered on one line – nor does it support selection and spellcheck highlighting or blinking text, although it does support other line decorations, and supports a crop attribute for displaying an ellipsis if there is not enough width available. The width is determined from the amount of text and the default height is determined from the height of the font. This type behaves more like a form control that happens to display read only text. In CSS terms, it would probably be considered a replaced element.
Usually, the most common purpose of using one over the other is to get wrapped text versus text that appears on one line. Naturally you would choose the ‘text as child’ form if you wanted more complex text and choose the ‘value attribute’ form for simple single line text. You would not choose which tag (<label> versus <description>) to use based on this criteria, although historically, many people have (incorrectly) done so. It doesn’t help of course that the XUL tutorial on XULPlanet says to do exactly that though. Boo.
To change the value in the first form with the text as a child, use the textContent property. To change the value in the attribute form, use the value property.
label.textContent = "New Text:"; label.value = "New Text";
Another common technique is to use a plain-styled read only <textbox> element:
<textbox class="plain" readonly="true"/>
This creates text which looks like a single line label or description, but allows selection and provides a context menu. This latter feature is really the only reason to use this form, so it should only be used when it is likely that the user will want to select the text. Specifically, the control and accesskey features won’t work, so you should not use this for labels.
You may see references in some documents to a <text> element. This element is an older element that should not be used. For historical reference, it is equivalent to a <description> element but only supports the value attribute form. Also, it doesn’t support any extra APIs so you should not use this element. I mention this because someone linked to a page which used the <text> element incorrectly. In fact, the author of that page had meant to use a different element, <textnode>, which is intended to be used in templates. Although it looks similar, the <textnode> element isn’t related to the description or label elements; instead it simply generates a DOM Text node in a template, which is useful when you want to use the text as child form:
<description><textnode value="?name"/></description>
This code generates output like the following when used in a template:
<description>Marian Dodson</description>
