Skip to content

Commit

Permalink
fix #654: parse attribute list correctly for self closing stop node
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed May 18, 2024
1 parent 1a384a5 commit 151b8f4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
42 changes: 42 additions & 0 deletions spec/stopNodes_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,46 @@ describe("XMLParser StopNodes", function () {
// console.log(JSON.stringify(jObj, null, 4));
expect(jObj).toEqual(expected);
});
it("should parse attributes correctly for self closing stop node", function() {

const xmlData = `<script/>`;
const options = {
allowBooleanAttributes: true,
ignoreAttributes: false,
stopNodes: ["*.pre", "*.script"],
};
const expected = {
"script": ""
}
const parser = new XMLParser(options);
// console.log(JSON.stringify(parser.parse(xml)));

let result = parser.parse(xmlData);

// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);

});
it("should parse attributes correctly for self closing stop node", function() {

const xmlData = `<script src="some.js" />`;
const options = {
allowBooleanAttributes: true,
ignoreAttributes: false,
stopNodes: ["*.pre", "*.script"],
};
const expected = {
"script": {
"@_src": "some.js"
}
}
const parser = new XMLParser(options);
// console.log(JSON.stringify(parser.parse(xml)));

let result = parser.parse(xmlData);

// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);

});
});
8 changes: 8 additions & 0 deletions src/xmlparser/OrderedObjParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,18 @@ const parseXml = function(xmlData) {
let tagContent = "";
//self-closing tag
if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){
if(tagName[tagName.length - 1] === "/"){ //remove trailing '/'
tagName = tagName.substr(0, tagName.length - 1);
jPath = jPath.substr(0, jPath.length - 1);
tagExp = tagName;
}else{
tagExp = tagExp.substr(0, tagExp.length - 1);
}
i = result.closeIndex;
}
//unpaired tag
else if(this.options.unpairedTags.indexOf(tagName) !== -1){

i = result.closeIndex;
}
//normal tag
Expand Down

0 comments on commit 151b8f4

Please sign in to comment.