From ae99fc6f25ea518c4223b8e34bbfc9c951210bf5 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Sat, 9 Sep 2023 20:45:20 -0400 Subject: [PATCH] Fix stopNodes to work with removeNSPrefix (#607) (#608) Signed-off-by: Craig Andrews --- spec/stopNodes_spec.js | 51 +++++++++++++++++++++++++++++++ src/xmlparser/OrderedObjParser.js | 9 ++++-- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/spec/stopNodes_spec.js b/spec/stopNodes_spec.js index afefa5b5..ec2da913 100644 --- a/spec/stopNodes_spec.js +++ b/spec/stopNodes_spec.js @@ -4,6 +4,57 @@ const { XMLParser, XMLValidator } = require("../src/fxp"); const he = require("he"); describe("XMLParser StopNodes", function () { + it("should support single stopNode with namespace and removeNSPrefix set", function () { + const xmlData = `test 1

p 1

div 1
`; + const expected = { + "issue": { + "title": "test 1", + "fix1": "

p 1

div 1
" + } + }; + + const options = { + attributeNamePrefix: "", + ignoreAttributes: false, + parseAttributeValue: true, + removeNSPrefix: 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); + }); + + it("should support single stopNode with namespace", function () { + const xmlData = `test 1

p 1

div 1
`; + const expected = { + "issue": { + "title": "test 1", + "namespace:fix1": "

p 1

div 1
" + } + }; + + const options = { + attributeNamePrefix: "", + ignoreAttributes: false, + parseAttributeValue: true, + stopNodes: ["issue.namespace: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); + }); + it("1a. should support single stopNode", function () { const xmlData = `test 1

p 1

div 1
`; const expected = { diff --git a/src/xmlparser/OrderedObjParser.js b/src/xmlparser/OrderedObjParser.js index db1268a9..0047837f 100644 --- a/src/xmlparser/OrderedObjParser.js +++ b/src/xmlparser/OrderedObjParser.js @@ -280,6 +280,7 @@ const parseXml = function(xmlData) { }else {//Opening tag let result = readTagExp(xmlData,i, this.options.removeNSPrefix); let tagName= result.tagName; + const rawTagName = result.rawTagName; let tagExp = result.tagExp; let attrExpPresent = result.attrExpPresent; let closeIndex = result.closeIndex; @@ -305,7 +306,7 @@ const parseXml = function(xmlData) { if(tagName !== xmlObj.tagname){ jPath += jPath ? "." + tagName : tagName; } - if (this.isItStopNode(this.options.stopNodes, jPath, tagName)) { //TODO: namespace + if (this.isItStopNode(this.options.stopNodes, jPath, tagName)) { let tagContent = ""; //self-closing tag if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){ @@ -318,8 +319,8 @@ const parseXml = function(xmlData) { //normal tag else{ //read until closing tag is found - const result = this.readStopNodeData(xmlData, tagName, closeIndex + 1); - if(!result) throw new Error(`Unexpected end of ${tagName}`); + const result = this.readStopNodeData(xmlData, rawTagName, closeIndex + 1); + if(!result) throw new Error(`Unexpected end of ${rawTagName}`); i = result.i; tagContent = result.tagContent; } @@ -504,6 +505,7 @@ function readTagExp(xmlData,i, removeNSPrefix, closingChar = ">"){ tagExp = tagExp.substr(separatorIndex + 1); } + const rawTagName = tagName; if(removeNSPrefix){ const colonIndex = tagName.indexOf(":"); if(colonIndex !== -1){ @@ -517,6 +519,7 @@ function readTagExp(xmlData,i, removeNSPrefix, closingChar = ">"){ tagExp: tagExp, closeIndex: closeIndex, attrExpPresent: attrExpPresent, + rawTagName: rawTagName, } } /**