Skip to content

Commit

Permalink
fix #501: parse for entities only once
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Oct 5, 2022
1 parent 443e8ed commit 8cfd915
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
24 changes: 24 additions & 0 deletions spec/entities_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,28 @@ describe("XMLParser External Entites", function() {
// console.log(output);
expect(output.replace(/\s+/g, "")).toEqual(expected.replace(/\s+/g, ""));
});

it("should replace '<' with '<'", function() {
const xmlData = `<SimpleScalarPropertiesInputOutput>
<stringValue>&amp;lt;</stringValue>
</SimpleScalarPropertiesInputOutput>`;

const expected = {
"SimpleScalarPropertiesInputOutput": {
"stringValue": "&lt;"
}
};

const options = {
attributeNamePrefix: "",
ignoreAttributes: false,
processEntities: true,
// preserveOrder: true
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
//console.log(JSON.stringify(result,null,4));

expect(result).toEqual(expected);
});
});
4 changes: 3 additions & 1 deletion src/xmlparser/OrderedObjParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ class OrderedObjParser{
this.tagsNodeStack = [];
this.docTypeEntities = {};
this.lastEntities = {
"amp" : { regex: /&(amp|#38|#x26);/g, val : "&"},
"apos" : { regex: /&(apos|#39|#x27);/g, val : "'"},
"gt" : { regex: /&(gt|#62|#x3E);/g, val : ">"},
"lt" : { regex: /&(lt|#60|#x3C);/g, val : "<"},
"quot" : { regex: /&(quot|#34|#x22);/g, val : "\""},
};
this.ampEntity = { regex: /&(amp|#38|#x26);/g, val : "&"};
this.htmlEntities = {
"space": { regex: /&(nbsp|#160);/g, val: " " },
// "lt" : { regex: /&(lt|#60);/g, val: "<" },
Expand Down Expand Up @@ -364,6 +364,7 @@ const parseXml = function(xmlData) {
}

const replaceEntitiesValue = function(val){

if(this.options.processEntities){
for(let entityName in this.docTypeEntities){
const entity = this.docTypeEntities[entityName];
Expand All @@ -379,6 +380,7 @@ const replaceEntitiesValue = function(val){
val = val.replace( entity.regex, entity.val);
}
}
val = val.replace( this.ampEntity.regex, this.ampEntity.val);
}
return val;
}
Expand Down
2 changes: 2 additions & 0 deletions src/xmlparser/XMLParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class XMLParser{
throw new Error("Entity value can't have '&'")
}else if(key.indexOf("&") !== -1 || key.indexOf(";") !== -1){
throw new Error("An entity must be set without '&' and ';'. Eg. use '#xD' for '&#xD;'")
}else if(value === "&"){
throw new Error("An entity with value '&' is not permitted");
}else{
this.externalEntities[key] = value;
}
Expand Down

0 comments on commit 8cfd915

Please sign in to comment.