From 66384d116e5f2316fa0100170869e0f570756118 Mon Sep 17 00:00:00 2001 From: wackbyte Date: Sun, 14 Jan 2024 02:22:54 -0500 Subject: [PATCH] Don't escape entities in CDATA sections --- spec/cdata_spec.js | 16 ++++++++++++++++ spec/entities_spec.js | 2 +- src/xmlparser/OrderedObjParser.js | 7 +++---- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/spec/cdata_spec.js b/spec/cdata_spec.js index 3c0e872f..3742edfe 100644 --- a/spec/cdata_spec.js +++ b/spec/cdata_spec.js @@ -446,4 +446,20 @@ patronymic`; // console.log(JSON.stringify(result,null,4)); expect(result).toEqual(expected); }); + + it("should not process entities in CDATA", function() { + const xmlData =``; + + const expected = { xml: '<text>' }; + + const options = { + ignoreAttributes: false, + }; + + const parser = new XMLParser(options); + let result = parser.parse(xmlData); + + // console.log(JSON.stringify(result,null,4)); + expect(result).toEqual(expected); + }); }); diff --git a/spec/entities_spec.js b/spec/entities_spec.js index 41ef08af..c87ced65 100644 --- a/spec/entities_spec.js +++ b/spec/entities_spec.js @@ -27,7 +27,7 @@ describe("XMLParser Entities", function() { "a:b": 2, "a-b:b-a": 2, "a:c": "test&\nтест<\ntest", - "a:el": "<2]]]]>&a", + "a:el": "<<b>2]]]]>&a", "c:string": { "#text": "страхования » от суммы его активов", "@_lang": "ru" diff --git a/src/xmlparser/OrderedObjParser.js b/src/xmlparser/OrderedObjParser.js index fafc6804..75d97184 100644 --- a/src/xmlparser/OrderedObjParser.js +++ b/src/xmlparser/OrderedObjParser.js @@ -265,14 +265,13 @@ const parseXml = function(xmlData) { textData = this.saveTextToParentTag(textData, currentNode, jPath); + let val = this.parseTextData(tagExp, currentNode.tagname, jPath, true, false, true, true); + if(val == undefined) val = ""; + //cdata should be set even if it is 0 length string if(this.options.cdataPropName){ - // let val = this.parseTextData(tagExp, this.options.cdataPropName, jPath + "." + this.options.cdataPropName, true, false, true); - // if(!val) val = ""; currentNode.add(this.options.cdataPropName, [ { [this.options.textNodeName] : tagExp } ]); }else{ - let val = this.parseTextData(tagExp, currentNode.tagname, jPath, true, false, true); - if(val == undefined) val = ""; currentNode.add(this.options.textNodeName, val); }