Chromium xmlString -> DOMParser() -> XMLSerializer() Round Trip Failure with Embedded CDATA

If you take an XML String which has CDATA elements embedded within CDATA elements with the correct structure. (Where the embedded CDATA close of ‘]]’ is represented as ‘]]]]>]]>”

And you perform a round trip xmlString -> DOMParser() -> XMLSerializer() -> xmlString
with something like

var parser = new DOMParser();
var resultDocument = parser.parseFromString(sourceXMLString, ‘text/xml’);

var serializer = new XMLSerializer();
var resultXMLString = serializer.serializeToString(resultDocument);

The resultant XML string in Edge/Chrome is incorrect
i.e.
“]]>”

So it has lost the correct encoding of the embedded CDATA close of ‘]]]]><![CDATA[‘. and replaced it with ‘]]’

Where as is Firefox and Safari the result is identical.

Further to this the resultDocument from parser.parseFromString(sourceXMLString, ‘text/xml’)
is in fact corrupt with XSL run against it failing

Is it possible to control the behaviour of parseFromString method on the chromium version of DOMParser to get the correct result?

Here is a very simple piece of html which clearly shows the issue, Try running it in Chrome/Edge vs Firefox/Safari to see the behaviours

<html>
<body>

<p id="demo"></p>

<script>

const simpleXML = "<root><element>Element Data</element></root>";
const cdataXML = "<root><htmlDef><![CDATA[<html><body><div></div></body></html>]]></htmlDef></root>";
const embeddedCDATAXML = "<root><htmlDefn><![CDATA[<div><![CDATA[  Just Rubbish Data $#$^#^$ ]]]]><![CDATA[></div><div></div>]]></htmlDefn></root>";

function testRoundTrip(sourceXMLString) {

   var parser = new DOMParser();
   var resultDocument = parser.parseFromString(sourceXMLString, 'text/xml');
   
   var serializer = new XMLSerializer();
   var resultXMLString = serializer.serializeToString(resultDocument);
   
   if (sourceXMLString === resultXMLString) {
       window.alert("Round Trip XML String to DOM and Back matches perfectly");
       window.alert("SOURCE : " + sourceXMLString);
   } else {
       window.alert("Round Trip XML String to DOM and Back DOES NOT MATCH");
       window.alert("SOURCE : " + sourceXMLString);
       window.alert("RESULT : " + resultXMLString);
   }
}
</script>

<h1>Test some XML Parsing Round Trips (In Different Browsers)</h1>
<h2>Using the Browser Built-in DOMParser() and XMLSerializer()</h2>
<button type="button" onclick="testRoundTrip(simpleXML)">Simple XML</button>
<button type="button" onclick="testRoundTrip(cdataXML)">XML with CDATA</button>
<button type="button" onclick="testRoundTrip(embeddedCDATAXML)">XML with Embedded CDATAs</button>
</body>
</html>

Leave a Comment