From e566e3ce83fb4ef0d5930336b2d85a59c74f622d Mon Sep 17 00:00:00 2001 From: Jan Holthuis Date: Sat, 27 May 2017 13:14:37 +0200 Subject: [PATCH] Fix "]]>" serialization in text nodes This fixes issue #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. --- dom.js | 2 +- test/dom/serializer.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dom.js b/dom.js index b13b371..6864f8c 100644 --- a/dom.js +++ b/dom.js @@ -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( ''); case COMMENT_NODE: diff --git a/test/dom/serializer.js b/test/dom/serializer.js index d9df5b9..407d44e 100644 --- a/test/dom/serializer.js +++ b/test/dom/serializer.js @@ -5,7 +5,7 @@ wows.describe('XML Serializer').addBatch({ 'text node containing "]]>"': function() { var doc = new DOMParser().parseFromString('', '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 ]]> there',doc.documentElement.firstChild.toString()); }, '', 'text/html');