12.2. Handling Pre-Formatted Text

You may occasionally have HTML-preformatted text that you need to integrate into a Bebop component. This might come from a globalization translator, user input HTML, or as part of feed or stream from an outside source. This section discusses methods for handling this pre-formatted text intelligently.

WAF provides an explicit technique for this situation by using the Bebop Label component and by disabling output escaping in the XSLT processor for the context of the label. The code:

Label l = new Label("This is a <b>new</b> label.", false);
l.generateXML(pageState, element);

will generate the XML fragment:

<bebop:label escape="yes">This is a &lt;b>new&lt;/b&gt; 
   label.</bebop:label>

The result is that the HTML-preformatted text is a single, text-node child to the bebop:label element. On output, the escape="yes" means that the angle brackets will not be output-escaped. They will appear in the HTML stream as angle brackets and be interpreted as such by the user's web browser, causing the word new to appear in bold.

NoteNote
 

The escape="yes" attribute to bebop:label is confusing because it is shorthand for what will become the XSLT attribute disable-output-escaping="yes".

By default, the XSLT processor escapes output according to the output mode specified in the stylesheet with the xsl:output element. This default makes angle brackets appear as angle brackets in the user's browser, which means they are transmitted as &lt;'s in the HTML stream.

If you want angle brackets to show up in your output (e.g less-than signs for mathematics), you want to enable output escaping, which is the default setting.

Label l = new Label("3.141 < pi < 3.142");

CautionCaution
 

Inserting HTML formatting into the Bebop DOM, where it is difficult to style globally, is not generally recommended.

An alternative solutions is to parse the string argument to the Label constructor as a DOM fragment, instead of simply disabling output escaping, so that the formatting elements are a full-fledged part of the DOM and eligible for global styling. A drawback to this is the expense of parsing XML.

An additional drawback is that most people do not write well-formed XHTML, while browsers are much more tolerant than XML parsers. In other words, it is possible to write the following:

Label l = new Label("<ul> <li> list item 1 <li> list item 2 
   </ul>", false);

This will be displayed correctly as an unordered list in the browser, even though the label contents are not well-formed [X]HTML.