From abdae898060dff13f1cce8369752065263a84fee Mon Sep 17 00:00:00 2001 From: Santiago Aguiar Date: Fri, 21 Jun 2019 14:23:10 -0300 Subject: [PATCH] Fix serialization of ]]> text content --- dom.js | 2 +- test/dom/serializer.js | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/dom.js b/dom.js index f79a561..497978d 100644 --- a/dom.js +++ b/dom.js @@ -951,7 +951,7 @@ function serializeToString(node,buf){ 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 73d86ad..81490dc 100644 --- a/test/dom/serializer.js +++ b/test/dom/serializer.js @@ -2,10 +2,20 @@ var wows = require('vows'); var DOMParser = require('xmldom').DOMParser; 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()); + }, + 'text node containing "<]]>"': function() { + var doc = new DOMParser().parseFromString('', 'text/xml'); + doc.documentElement.appendChild(doc.createTextNode(' there')); + console.assert(doc.documentElement.firstChild.toString() == '<hello ]]> there',doc.documentElement.firstChild.toString()); + }, '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');