Skip to content

Commit

Permalink
Fix stopNodes to work with removeNSPrefix (NaturalIntelligence#607)
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Andrews <candrews@integralblue.com>
  • Loading branch information
candrews committed Sep 5, 2023
1 parent 3c9e9fe commit 4cb109c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
51 changes: 51 additions & 0 deletions spec/stopNodes_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<issue><title>test 1</title><namespace:fix1><p>p 1</p><div class="show">div 1</div></namespace:fix1></issue>`;
const expected = {
"issue": {
"title": "test 1",
"fix1": "<p>p 1</p><div class=\"show\">div 1</div>"
}
};

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 = `<issue><title>test 1</title><namespace:fix1><p>p 1</p><div class="show">div 1</div></namespace:fix1></issue>`;
const expected = {
"issue": {
"title": "test 1",
"namespace:fix1": "<p>p 1</p><div class=\"show\">div 1</div>"
}
};

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 = `<issue><title>test 1</title><fix1><p>p 1</p><div class="show">div 1</div></fix1></issue>`;
const expected = {
Expand Down
9 changes: 6 additions & 3 deletions src/xmlparser/OrderedObjParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ const parseXml = function(xmlData) {
}else {//Opening tag
let result = readTagExp(xmlData,i, this.options.removeNSPrefix);
let tagName= result.tagName;
const tagNameWithNamespace = result.tagNameWithNamespace;
let tagExp = result.tagExp;
let attrExpPresent = result.attrExpPresent;
let closeIndex = result.closeIndex;
Expand All @@ -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){
Expand All @@ -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, tagNameWithNamespace, closeIndex + 1);
if(!result) throw new Error(`Unexpected end of ${tagNameWithNamespace}`);
i = result.i;
tagContent = result.tagContent;
}
Expand Down Expand Up @@ -504,6 +505,7 @@ function readTagExp(xmlData,i, removeNSPrefix, closingChar = ">"){
tagExp = tagExp.substr(separatorIndex + 1);
}

const tagNameWithNamespace = tagName;
if(removeNSPrefix){
const colonIndex = tagName.indexOf(":");
if(colonIndex !== -1){
Expand All @@ -517,6 +519,7 @@ function readTagExp(xmlData,i, removeNSPrefix, closingChar = ">"){
tagExp: tagExp,
closeIndex: closeIndex,
attrExpPresent: attrExpPresent,
tagNameWithNamespace: tagNameWithNamespace,
}
}
/**
Expand Down

0 comments on commit 4cb109c

Please sign in to comment.