From 76a3b366f7b36ade535a280efcc37467c6267094 Mon Sep 17 00:00:00 2001 From: Amit Gupta Date: Thu, 3 Feb 2022 19:56:39 +0530 Subject: [PATCH] ignore XML declration and pi tag if set --- spec/pi_spec.js | 40 +++++++++++++++++++++++++++++++ src/fxp.d.ts | 2 ++ src/xmlparser/OptionsBuilder.js | 2 ++ src/xmlparser/OrderedObjParser.js | 20 +++++++++++----- 4 files changed, 58 insertions(+), 6 deletions(-) diff --git a/spec/pi_spec.js b/spec/pi_spec.js index ea43d444..a1825175 100644 --- a/spec/pi_spec.js +++ b/spec/pi_spec.js @@ -140,4 +140,44 @@ describe("XMLParser", function() { expect(output.replace(/\s+/g, "")).toEqual(xmlData.replace(/\s+/g, "")); // console.log(output); }); + + it("should strip xml declarion tag", function(){ + const xmlData = ` + , , ?> +

+ `; + + const options = { + allowBooleanAttributes: true, + ignoreDeclaration: true, + }; + + const expected = { + "?elementnames": "", + "h1": "" + } + const result = new XMLParser(options).parse(xmlData); + // console.log(JSON.stringify(result, null,4)); + expect(expected).toEqual(result); + }); + + it("should strip xml declarion tag", function(){ + const xmlData = ` + , , ?> +

+ `; + + const options = { + allowBooleanAttributes: true, + ignoreDeclaration: true, + ignorePiTags: true, + }; + + const expected = { + "h1": "" + } + const result = new XMLParser(options).parse(xmlData); + // console.log(JSON.stringify(result, null,4)); + expect(expected).toEqual(result); + }); }); \ No newline at end of file diff --git a/src/fxp.d.ts b/src/fxp.d.ts index c786fed8..b3d6ad77 100644 --- a/src/fxp.d.ts +++ b/src/fxp.d.ts @@ -20,6 +20,8 @@ type X2jOptions = { isArray: (tagName: string, jPath: string, isLeafNode: boolean, isAttribute: boolean) => boolean; processEntities: boolean; htmlEntities: boolean; + ignoreDeclaration: boolean; + ignorePiTags: boolean; }; type strnumOptions = { hex: boolean; diff --git a/src/xmlparser/OptionsBuilder.js b/src/xmlparser/OptionsBuilder.js index 9e3e30f0..f46d0b21 100644 --- a/src/xmlparser/OptionsBuilder.js +++ b/src/xmlparser/OptionsBuilder.js @@ -29,6 +29,8 @@ const defaultOptions = { unpairedTags: [], processEntities: true, htmlEntities: false, + ignoreDeclaration: false, + ignorePiTags: false }; const buildOptions = function(options) { diff --git a/src/xmlparser/OrderedObjParser.js b/src/xmlparser/OrderedObjParser.js index 305b2991..a8082e4c 100644 --- a/src/xmlparser/OrderedObjParser.js +++ b/src/xmlparser/OrderedObjParser.js @@ -203,17 +203,25 @@ const parseXml = function(xmlData) { textData = ""; i = closeIndex; } else if( xmlData[i+1] === '?') { + let tagData = readTagExp(xmlData,i, false, "?>"); if(!tagData) throw new Error("Pi Tag is not closed."); + textData = this.saveTextToParentTag(textData, currentNode, jPath); + if( (this.options.ignoreDeclaration && tagData.tagName === "?xml") || this.options.ignorePiTags){ + + }else{ + + const childNode = new xmlNode(tagData.tagName); + childNode.add(this.options.textNodeName, ""); + + if(tagData.tagName !== tagData.tagExp && tagData.attrExpPresent){ + childNode[":@"] = this.buildAttributesMap(tagData.tagExp, jPath); + } + currentNode.addChild(childNode); - const childNode = new xmlNode(tagData.tagName); - childNode.add(this.options.textNodeName, ""); - - if(tagData.tagName !== tagData.tagExp && tagData.attrExpPresent){ - childNode[":@"] = this.buildAttributesMap(tagData.tagExp, jPath); } - currentNode.addChild(childNode); + i = tagData.closeIndex + 1; } else if(xmlData.substr(i + 1, 3) === '!--') {