Skip to content

Commit

Permalink
ignore XML declration and pi tag if set
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Feb 3, 2022
1 parent b0ea635 commit 76a3b36
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
40 changes: 40 additions & 0 deletions spec/pi_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<?xml version="1.0"?>
<?elementnames <fred>, <bert>, <harry> ?>
<h1></h1>
`;

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 = `<?xml version="1.0"?>
<?elementnames <fred>, <bert>, <harry> ?>
<h1></h1>
`;

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);
});
});
2 changes: 2 additions & 0 deletions src/fxp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/xmlparser/OptionsBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const defaultOptions = {
unpairedTags: [],
processEntities: true,
htmlEntities: false,
ignoreDeclaration: false,
ignorePiTags: false
};

const buildOptions = function(options) {
Expand Down
20 changes: 14 additions & 6 deletions src/xmlparser/OrderedObjParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) === '!--') {
Expand Down

0 comments on commit 76a3b36

Please sign in to comment.