Skip to content

Commit

Permalink
Don't escape entities in CDATA sections (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
wackbyte authored Jan 19, 2024
1 parent 291fe73 commit 92e07cb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
16 changes: 16 additions & 0 deletions spec/cdata_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,20 @@ patronymic</person></root>`;
// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);
});

it("should not process entities in CDATA", function() {
const xmlData =`<xml><![CDATA[&lt;text&gt;]]></xml>`;

const expected = { xml: '&lt;text&gt;' };

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);
});
});
2 changes: 1 addition & 1 deletion spec/entities_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("XMLParser Entities", function() {
"a:b": 2,
"a-b:b-a": 2,
"a:c": "test&\nтест<\ntest",
"a:el": "<a><<a/><b>2</b>]]]]>&a",
"a:el": "<a>&lt;<a/>&lt;b&gt;2</b>]]]]>&amp;a",
"c:string": {
"#text": "&#x441;&#x442;&#x440;&#x430;&#x445;&#x43e;&#x432;&#x430;&#x43d;&#x438;&#x44f; » &#x43e;&#x442; &#x441;&#x443;&#x43c;&#x43c;&#x44b; &#x435;&#x433;&#x43e; &#x430;&#x43a;&#x442;&#x438;&#x432;&#x43e;&#x432;",
"@_lang": "ru"
Expand Down
7 changes: 3 additions & 4 deletions src/xmlparser/OrderedObjParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 92e07cb

Please sign in to comment.