Opera 9: The LSSerializer XML Declaration Bug

This page demonstrates a bug in Opera 9's implementation of the LSSerializer object. Look at the source code of the page to see the javascript that triggers the bug.

LSSerializer is part of the W3C's Document Object Model (DOM) Level 3 Load and Save specification. It provides a means to convert (serialise) an XML document to a text string. At time of writing, I believe that Opera 9 is the only browser to implement this aspect of the DOM specification.

When you click the buttons below some javascript is executed. First I create an XML document. This document looks like this:

<?xml version="1.0"?>
<wine xmlns="http://www.w3.org/2001/XMLSchema-instance">
	<merlot>A rich dark red wine.</merlot>
</wine>

Then I use the LSSerializer object to convert the XML document to a text string. This string is put into the box below.

The LSSerialiser object allows you to configure how it converts the XML document to a string. One parameter is called "xml-declaration". When this is set to 'true', the default setting, the serialised string should include the xml declaration (i.e. <?xml version="1.0"?>). When set to 'false', the string should not include the xml declaration. Opera 9, unfortunately, misbehaves.

In Opera 9, when "xml-declaration" is set to true, an xml declaration is added to the XML document. This happens irrespective of whether an xml declaration already exists. If you click the first button below, you can see this in action. The serialised XML document ends up with two xml declarations.

When "xml-declaration" is set to false, on the other hand, Opera 9 does not add an xml declaration. But nor does it remove the xml declaration already there. So our serialised string contains one xml declaration, when it should in fact contain no such xml declaration. You can see this by clicking the second button below.

View the source of this page to see the javascript code that demonstrates the problem.

Note: I have reported this bug to Opera using their bug reporting system.