Skip to content

Commit 151b8f4

Browse files
committed
fix #654: parse attribute list correctly for self closing stop node
1 parent 1a384a5 commit 151b8f4

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

spec/stopNodes_spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,4 +414,46 @@ describe("XMLParser StopNodes", function () {
414414
// console.log(JSON.stringify(jObj, null, 4));
415415
expect(jObj).toEqual(expected);
416416
});
417+
it("should parse attributes correctly for self closing stop node", function() {
418+
419+
const xmlData = `<script/>`;
420+
const options = {
421+
allowBooleanAttributes: true,
422+
ignoreAttributes: false,
423+
stopNodes: ["*.pre", "*.script"],
424+
};
425+
const expected = {
426+
"script": ""
427+
}
428+
const parser = new XMLParser(options);
429+
// console.log(JSON.stringify(parser.parse(xml)));
430+
431+
let result = parser.parse(xmlData);
432+
433+
// console.log(JSON.stringify(result,null,4));
434+
expect(result).toEqual(expected);
435+
436+
});
437+
it("should parse attributes correctly for self closing stop node", function() {
438+
439+
const xmlData = `<script src="some.js" />`;
440+
const options = {
441+
allowBooleanAttributes: true,
442+
ignoreAttributes: false,
443+
stopNodes: ["*.pre", "*.script"],
444+
};
445+
const expected = {
446+
"script": {
447+
"@_src": "some.js"
448+
}
449+
}
450+
const parser = new XMLParser(options);
451+
// console.log(JSON.stringify(parser.parse(xml)));
452+
453+
let result = parser.parse(xmlData);
454+
455+
// console.log(JSON.stringify(result,null,4));
456+
expect(result).toEqual(expected);
457+
458+
});
417459
});

src/xmlparser/OrderedObjParser.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,18 @@ const parseXml = function(xmlData) {
311311
let tagContent = "";
312312
//self-closing tag
313313
if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){
314+
if(tagName[tagName.length - 1] === "/"){ //remove trailing '/'
315+
tagName = tagName.substr(0, tagName.length - 1);
316+
jPath = jPath.substr(0, jPath.length - 1);
317+
tagExp = tagName;
318+
}else{
319+
tagExp = tagExp.substr(0, tagExp.length - 1);
320+
}
314321
i = result.closeIndex;
315322
}
316323
//unpaired tag
317324
else if(this.options.unpairedTags.indexOf(tagName) !== -1){
325+
318326
i = result.closeIndex;
319327
}
320328
//normal tag

0 commit comments

Comments
 (0)