Skip to content

Commit f5cd5a5

Browse files
committed
set xml version to decoder even if attributes are ignored
1 parent f44b923 commit f5cd5a5

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

spec/entities_spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,4 +718,38 @@ describe("@nodable/entities", function () {
718718

719719
expect(result).toEqual(expected);
720720
})
721+
722+
it("should remove '#x1B' ncr as per XML version 1.0 with default config", function () {
723+
724+
const xmlData = `<?xml version="1.0" ?><root>&#x1B;&#27;</root>`;
725+
const expected = {
726+
"root": '&#x1B;&#27;'
727+
}
728+
const options = {
729+
//ignoreAttributes: false, //should work without this
730+
processEntities: true,
731+
// htmlEntities: true
732+
};
733+
const parser = new XMLParser(options);
734+
let result = parser.parse(xmlData);
735+
// console.log(JSON.stringify(result, null, 4));
736+
737+
expect(result.root.length).toEqual(0);
738+
})
739+
it("should leave '#x1B' ncr as per XML version 1.1 with default config", function () {
740+
741+
const xmlData = `<?xml version="1.1" ?><root>&#x1B;&#27;</root>`;
742+
const expected = {
743+
"root": '&#x1B;&#27;'
744+
}
745+
const options = {
746+
//ignoreAttributes: false, //should work without this
747+
processEntities: true
748+
};
749+
const parser = new XMLParser(options);
750+
let result = parser.parse(xmlData);
751+
// console.log(JSON.stringify(result, null, 4));
752+
753+
expect(result.root.length).toEqual(11);
754+
})
721755
})

src/xmlparser/OrderedObjParser.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ function resolveNameSpace(tagname) {
193193
//const attrsRegx = new RegExp("([\\w\\-\\.\\:]+)\\s*=\\s*(['\"])((.|\n)*?)\\2","gm");
194194
const attrsRegx = new RegExp('([^\\s=]+)\\s*(=\\s*([\'"])([\\s\\S]*?)\\3)?', 'gm');
195195

196-
function buildAttributesMap(attrStr, jPath, tagName) {
196+
function buildAttributesMap(attrStr, jPath, tagName, force = false) {
197197
const options = this.options;
198-
if (options.ignoreAttributes !== true && typeof attrStr === 'string') {
198+
if (force === true || (options.ignoreAttributes !== true && typeof attrStr === 'string')) {
199199
// attrStr = attrStr.replace(/\r?\n/g, ' ');
200200
//attrStr = attrStr || attrStr.trim();
201201

@@ -339,9 +339,10 @@ const parseXml = function (xmlData) {
339339
if (!tagData) throw new Error("Pi Tag is not closed.");
340340

341341
textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher);
342-
const attsMap = this.buildAttributesMap(tagData.tagExp, this.matcher, tagData.tagName);
343-
if (attsMap && attsMap["version"]) {
344-
this.entityDecoder.setXmlVersion(attsMap["version"]);
342+
const attsMap = this.buildAttributesMap(tagData.tagExp, this.matcher, tagData.tagName, true);
343+
if (attsMap) {
344+
const ver = attsMap[this.options.attributeNamePrefix + "version"];
345+
this.entityDecoder.setXmlVersion(Number(ver) || 1.0);
345346
}
346347
if ((options.ignoreDeclaration && tagData.tagName === "?xml") || options.ignorePiTags) {
347348
//do nothing
@@ -350,7 +351,7 @@ const parseXml = function (xmlData) {
350351
const childNode = new xmlNode(tagData.tagName);
351352
childNode.add(options.textNodeName, "");
352353

353-
if (tagData.tagName !== tagData.tagExp && tagData.attrExpPresent) {
354+
if (tagData.tagName !== tagData.tagExp && tagData.attrExpPresent && options.ignoreAttributes !== true) {
354355
childNode[":@"] = attsMap
355356
}
356357
this.addChild(currentNode, childNode, this.readonlyMatcher, i);
@@ -472,6 +473,7 @@ const parseXml = function (xmlData) {
472473

473474
if (prefixedAttrs) {
474475
// Extract raw attributes (without prefix) for our use
476+
//TODO: seems a performance overhead
475477
rawAttrs = extractRawAttributes(prefixedAttrs, options);
476478
}
477479
}

0 commit comments

Comments
 (0)