From 50033cdfb2a24dd2ecd0dca732dc198517d2d77e Mon Sep 17 00:00:00 2001 From: patrickshipe Date: Fri, 29 Apr 2022 11:04:51 -0500 Subject: [PATCH 1/2] Fix stopNodes when same tag appears inside node --- spec/stopNodes_spec.js | 25 +++++++++++++++++++++++++ src/xmlparser/OrderedObjParser.js | 31 +++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/spec/stopNodes_spec.js b/spec/stopNodes_spec.js index d77474c3..48181ea4 100644 --- a/spec/stopNodes_spec.js +++ b/spec/stopNodes_spec.js @@ -295,4 +295,29 @@ 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
`; + const expected = { + issue: { + title: 'test 1', + fix1: '

p 1

div 1
more', + }, + }; + + 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..fb921802 100644 --- a/src/xmlparser/OrderedObjParser.js +++ b/src/xmlparser/OrderedObjParser.js @@ -486,17 +486,32 @@ 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, '>') + + const openTagName = tagData && tagData.tagName; + if (openTagName === tagName) { + openTagCount++; } } - i=closeIndex; } }//end for loop } From e60b8edc54c1c4f008f5394818d9779bce055706 Mon Sep 17 00:00:00 2001 From: patrickshipe Date: Fri, 29 Apr 2022 11:13:24 -0500 Subject: [PATCH 2/2] jump close index --- spec/stopNodes_spec.js | 3 ++- src/xmlparser/OrderedObjParser.js | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/spec/stopNodes_spec.js b/spec/stopNodes_spec.js index 48181ea4..25f0d6b5 100644 --- a/spec/stopNodes_spec.js +++ b/spec/stopNodes_spec.js @@ -297,11 +297,12 @@ describe("XMLParser StopNodes", function() { }); 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
`; + const xmlData = `test 1

p 1

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

p 1

div 1
more', + last: 'something' }, }; diff --git a/src/xmlparser/OrderedObjParser.js b/src/xmlparser/OrderedObjParser.js index fb921802..141198f3 100644 --- a/src/xmlparser/OrderedObjParser.js +++ b/src/xmlparser/OrderedObjParser.js @@ -507,9 +507,12 @@ function readStopNodeData(xmlData, tagName, i){ } else { const tagData = readTagExp(xmlData, i, '>') - const openTagName = tagData && tagData.tagName; - if (openTagName === tagName) { - openTagCount++; + if (tagData) { + const openTagName = tagData && tagData.tagName; + if (openTagName === tagName) { + openTagCount++; + } + i=tagData.closeIndex; } } }