Skip to content

Commit ae99fc6

Browse files
authored
Fix stopNodes to work with removeNSPrefix (#607) (#608)
Signed-off-by: Craig Andrews <[email protected]>
1 parent 3c9e9fe commit ae99fc6

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

Diff for: spec/stopNodes_spec.js

+51
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,57 @@ const { XMLParser, XMLValidator } = require("../src/fxp");
44
const he = require("he");
55

66
describe("XMLParser StopNodes", function () {
7+
it("should support single stopNode with namespace and removeNSPrefix set", function () {
8+
const xmlData = `<issue><title>test 1</title><namespace:fix1><p>p 1</p><div class="show">div 1</div></namespace:fix1></issue>`;
9+
const expected = {
10+
"issue": {
11+
"title": "test 1",
12+
"fix1": "<p>p 1</p><div class=\"show\">div 1</div>"
13+
}
14+
};
15+
16+
const options = {
17+
attributeNamePrefix: "",
18+
ignoreAttributes: false,
19+
parseAttributeValue: true,
20+
removeNSPrefix: true,
21+
stopNodes: ["issue.fix1"]
22+
};
23+
const parser = new XMLParser(options);
24+
let result = parser.parse(xmlData);
25+
26+
// console.log(JSON.stringify(result,null,4));
27+
expect(result).toEqual(expected);
28+
29+
result = XMLValidator.validate(xmlData);
30+
expect(result).toBe(true);
31+
});
32+
33+
it("should support single stopNode with namespace", function () {
34+
const xmlData = `<issue><title>test 1</title><namespace:fix1><p>p 1</p><div class="show">div 1</div></namespace:fix1></issue>`;
35+
const expected = {
36+
"issue": {
37+
"title": "test 1",
38+
"namespace:fix1": "<p>p 1</p><div class=\"show\">div 1</div>"
39+
}
40+
};
41+
42+
const options = {
43+
attributeNamePrefix: "",
44+
ignoreAttributes: false,
45+
parseAttributeValue: true,
46+
stopNodes: ["issue.namespace:fix1"]
47+
};
48+
const parser = new XMLParser(options);
49+
let result = parser.parse(xmlData);
50+
51+
// console.log(JSON.stringify(result,null,4));
52+
expect(result).toEqual(expected);
53+
54+
result = XMLValidator.validate(xmlData);
55+
expect(result).toBe(true);
56+
});
57+
758
it("1a. should support single stopNode", function () {
859
const xmlData = `<issue><title>test 1</title><fix1><p>p 1</p><div class="show">div 1</div></fix1></issue>`;
960
const expected = {

Diff for: src/xmlparser/OrderedObjParser.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ const parseXml = function(xmlData) {
280280
}else {//Opening tag
281281
let result = readTagExp(xmlData,i, this.options.removeNSPrefix);
282282
let tagName= result.tagName;
283+
const rawTagName = result.rawTagName;
283284
let tagExp = result.tagExp;
284285
let attrExpPresent = result.attrExpPresent;
285286
let closeIndex = result.closeIndex;
@@ -305,7 +306,7 @@ const parseXml = function(xmlData) {
305306
if(tagName !== xmlObj.tagname){
306307
jPath += jPath ? "." + tagName : tagName;
307308
}
308-
if (this.isItStopNode(this.options.stopNodes, jPath, tagName)) { //TODO: namespace
309+
if (this.isItStopNode(this.options.stopNodes, jPath, tagName)) {
309310
let tagContent = "";
310311
//self-closing tag
311312
if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){
@@ -318,8 +319,8 @@ const parseXml = function(xmlData) {
318319
//normal tag
319320
else{
320321
//read until closing tag is found
321-
const result = this.readStopNodeData(xmlData, tagName, closeIndex + 1);
322-
if(!result) throw new Error(`Unexpected end of ${tagName}`);
322+
const result = this.readStopNodeData(xmlData, rawTagName, closeIndex + 1);
323+
if(!result) throw new Error(`Unexpected end of ${rawTagName}`);
323324
i = result.i;
324325
tagContent = result.tagContent;
325326
}
@@ -504,6 +505,7 @@ function readTagExp(xmlData,i, removeNSPrefix, closingChar = ">"){
504505
tagExp = tagExp.substr(separatorIndex + 1);
505506
}
506507

508+
const rawTagName = tagName;
507509
if(removeNSPrefix){
508510
const colonIndex = tagName.indexOf(":");
509511
if(colonIndex !== -1){
@@ -517,6 +519,7 @@ function readTagExp(xmlData,i, removeNSPrefix, closingChar = ">"){
517519
tagExp: tagExp,
518520
closeIndex: closeIndex,
519521
attrExpPresent: attrExpPresent,
522+
rawTagName: rawTagName,
520523
}
521524
}
522525
/**

0 commit comments

Comments
 (0)