Skip to content

Commit

Permalink
Fix "]]>" serialization in text nodes
Browse files Browse the repository at this point in the history
This fixes issue jindw#164, which is a regression caused by commit 47fa9b8.
Before, text nodes were serialized correctly (thanks to commit 22fff92).

Section 2.4 of the XML 1.0 (5th Ed) recommendation states:

    The right angle bracket (>) may be represented using the string
    ">", and MUST, for compatibility, be escaped using either ">"
    or a character reference when it appears in the string "]]>" in
    content, when that string is not marking the end of a CDATA section.

See https://www.w3.org/TR/2008/REC-xml-20081126/#syntax for details.

Thus, this commit escapes the right angle bracket in text nodes if it
appears as part of "]]>". If not, the right angle bracket is not
escaped.

The unittest that was broken since commit 47fa9b8 has been fixed, too.
  • Loading branch information
Holzhaus committed May 27, 2017
1 parent 366159a commit e566e3c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){
case ATTRIBUTE_NODE:
return buf.push(' ',node.name,'="',node.value.replace(/[<&"]/g,_xmlEncoder),'"');
case TEXT_NODE:
return buf.push(node.data.replace(/[<&]/g,_xmlEncoder));
return buf.push(node.data.replace(/[<&]/g,_xmlEncoder).replace(/\]\]>/g,']]'+_xmlEncoder('>')));
case CDATA_SECTION_NODE:
return buf.push( '<![CDATA[',node.data,']]>');
case COMMENT_NODE:
Expand Down
2 changes: 1 addition & 1 deletion test/dom/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ wows.describe('XML Serializer').addBatch({
'text node containing "]]>"': function() {
var doc = new DOMParser().parseFromString('<test/>', 'text/xml');
doc.documentElement.appendChild(doc.createTextNode('hello ]]> there'));
console.assert(doc.documentElement.firstChild.toString() == 'hello ]]> there',doc.documentElement.firstChild.toString());
console.assert(doc.documentElement.firstChild.toString() == 'hello ]]&gt; there',doc.documentElement.firstChild.toString());
},
'<script> element with no children': function() {
var doc = new DOMParser({xmlns:{xmlns:'http://www.w3.org/1999/xhtml'}}).parseFromString('<html2><script></script></html2>', 'text/html');
Expand Down

0 comments on commit e566e3c

Please sign in to comment.