Skip to content

Commit 36023b4

Browse files
committed
fix (#785) unpairedTag node should not have tag content
1 parent b366026 commit 36023b4

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

spec/unpairedTags_spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,4 +531,27 @@ describe("unpaired tas position", function () {
531531

532532
});
533533

534+
it("XMLParser should not move tag data into the unpairedTag node #785", function () {
535+
536+
const xmlData = `<p>hello<br>world</p>`;
537+
const options = {
538+
ignoreAttributes: false,
539+
unpairedTags: ["hr", "br", "link", "meta"],
540+
};
541+
const expected = {
542+
"p": {
543+
"br": "",
544+
"#text": "helloworld"
545+
}
546+
}
547+
const parser = new XMLParser(options);
548+
// console.log(JSON.stringify(parser.parse(xml)));
549+
550+
let result = parser.parse(xmlData);
551+
552+
// console.log(JSON.stringify(result, null, 4));
553+
expect(result).toEqual(expected);
554+
555+
});
556+
534557
});

src/xmlparser/OrderedObjParser.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,17 @@ const parseXml = function (xmlData) {
414414
this.addChild(currentNode, childNode, jPath, startIndex);
415415
jPath = jPath.substr(0, jPath.lastIndexOf("."));
416416
}
417+
else if(this.options.unpairedTags.indexOf(tagName) !== -1){//unpaired tag
418+
const childNode = new xmlNode(tagName);
419+
if(tagName !== tagExp && attrExpPresent){
420+
childNode[":@"] = this.buildAttributesMap(tagExp, jPath);
421+
}
422+
this.addChild(currentNode, childNode, jPath, startIndex);
423+
jPath = jPath.substr(0, jPath.lastIndexOf("."));
424+
i = result.closeIndex;
425+
// Continue to next iteration without changing currentNode
426+
continue;
427+
}
417428
//opening tag
418429
else {
419430
const childNode = new xmlNode(tagName);
@@ -535,19 +546,19 @@ const replaceEntitiesValue = function (val, tagName, jPath) {
535546
}
536547

537548

538-
function saveTextToParentTag(textData, currentNode, jPath, isLeafNode) {
549+
function saveTextToParentTag(textData, parentNode, jPath, isLeafNode) {
539550
if (textData) { //store previously collected data as textNode
540-
if (isLeafNode === undefined) isLeafNode = currentNode.child.length === 0
551+
if (isLeafNode === undefined) isLeafNode = parentNode.child.length === 0
541552

542553
textData = this.parseTextData(textData,
543-
currentNode.tagname,
554+
parentNode.tagname,
544555
jPath,
545556
false,
546-
currentNode[":@"] ? Object.keys(currentNode[":@"]).length !== 0 : false,
557+
parentNode[":@"] ? Object.keys(parentNode[":@"]).length !== 0 : false,
547558
isLeafNode);
548559

549560
if (textData !== undefined && textData !== "")
550-
currentNode.add(this.options.textNodeName, textData);
561+
parentNode.add(this.options.textNodeName, textData);
551562
textData = "";
552563
}
553564
return textData;

0 commit comments

Comments
 (0)