diff --git a/spec/stopNodes_spec.js b/spec/stopNodes_spec.js index d77474c3..25f0d6b5 100644 --- a/spec/stopNodes_spec.js +++ b/spec/stopNodes_spec.js @@ -295,4 +295,30 @@ describe("XMLParser StopNodes", function() { expect(expected).toEqual(result); }); + + it("should not incorrectly close the stop node if the same tag name appears inside", function() { + const xmlData = `test 1

p 1

div 1
more
something
`; + const expected = { + issue: { + title: 'test 1', + fix1: '

p 1

div 1
more', + last: 'something' + }, + }; + + const options = { + attributeNamePrefix: "", + ignoreAttributes: false, + parseAttributeValue: true, + stopNodes: ["issue.fix1"] + }; + const parser = new XMLParser(options); + let result = parser.parse(xmlData); + + // console.log(JSON.stringify(result,null,4)); + expect(result).toEqual(expected); + + result = XMLValidator.validate(xmlData); + expect(result).toBe(true); + }); }); diff --git a/src/xmlparser/OrderedObjParser.js b/src/xmlparser/OrderedObjParser.js index 004845d3..141198f3 100644 --- a/src/xmlparser/OrderedObjParser.js +++ b/src/xmlparser/OrderedObjParser.js @@ -486,17 +486,35 @@ function readTagExp(xmlData,i, removeNSPrefix, closingChar = ">"){ */ function readStopNodeData(xmlData, tagName, i){ const startIndex = i; + // Starting at 1 since we already have an open tag + let openTagCount = 1; + for (; i < xmlData.length; i++) { - if( xmlData[i] === "<" && xmlData[i+1] === "/"){ - const closeIndex = findClosingIndex(xmlData, ">", i, `${tagName} is not closed`); - let closeTagName = xmlData.substring(i+2,closeIndex).trim(); - if(closeTagName === tagName){ - return { - tagContent: xmlData.substring(startIndex, i), - i : closeIndex + if( xmlData[i] === "<"){ + if (xmlData[i+1] === "/") { + const closeIndex = findClosingIndex(xmlData, ">", i, `${tagName} is not closed`); + let closeTagName = xmlData.substring(i+2,closeIndex).trim(); + if(closeTagName === tagName){ + openTagCount--; + if (openTagCount === 0) { + return { + tagContent: xmlData.substring(startIndex, i), + i : closeIndex + } + } + } + i=closeIndex; + } else { + const tagData = readTagExp(xmlData, i, '>') + + if (tagData) { + const openTagName = tagData && tagData.tagName; + if (openTagName === tagName) { + openTagCount++; + } + i=tagData.closeIndex; } } - i=closeIndex; } }//end for loop }