Skip to content

Commit

Permalink
fix #435: should skip unpaired and self-closing nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Mar 3, 2022
1 parent 7788846 commit 83fef6d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
44 changes: 44 additions & 0 deletions spec/stopNodes_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,48 @@ describe("XMLParser StopNodes", function() {

expect(expected).toEqual(result);
});

it("should skip self-closing stop nodes", function() {
const XMLdata = `<root><foo name="bar"></foo><bar name="bar"/></root>`;

const expected = [{
root: [
{ foo: [ { '#text': '' } ] },
{ bar: [ { '#text': '' } ] }
]
}];

const options = {
// ignoreAttributes: true,
stopNodes: ["*.foo", "*.bar"],
preserveOrder: true,
};
const parser = new XMLParser(options);
let result = parser.parse(XMLdata);
// console.log(JSON.stringify(result, null,4));

expect(expected).toEqual(result);
});
it("should skip unpaired stop nodes", function() {
const XMLdata = `<root><foo name="bar"></foo><bar name="bar"></root>`;

const expected = [{
root: [
{ foo: [ { '#text': '' } ] },
{ bar: [ { '#text': '' } ] }
]
}];

const options = {
// ignoreAttributes: true,
stopNodes: ["*.foo", "*.bar"],
unpairedTags: ["bar"],
preserveOrder: true,
};
const parser = new XMLParser(options);
let result = parser.parse(XMLdata);
// console.log(JSON.stringify(result, null,4));

expect(expected).toEqual(result);
});
});
9 changes: 6 additions & 3 deletions src/xmlparser/OrderedObjParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,13 @@ const parseXml = function(xmlData) {
if (this.isItStopNode(this.options.stopNodes, jPath, tagName)) { //TODO: namespace
let tagContent = "";
//self-closing tag
if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){}
if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){
i = result.closeIndex;
}
//boolean tag
else if(this.options.unpairedTags.indexOf(tagName) !== -1){}
else if(this.options.unpairedTags.indexOf(tagName) !== -1){
i = result.closeIndex;
}
//normal tag
else{
//read until closing tag is found
Expand All @@ -312,7 +316,6 @@ const parseXml = function(xmlData) {
}else{
//selfClosing tag
if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){

if(tagName[tagName.length - 1] === "/"){ //remove trailing '/'
tagName = tagName.substr(0, tagName.length - 1);
tagExp = tagName;
Expand Down

0 comments on commit 83fef6d

Please sign in to comment.