Skip to content

Commit 51cd0dc

Browse files
CamilleDrapierCamille Drapier
andauthored
Fix namspaced tag parsing (#321)
* Add spec * Do not attempt to parse non existing attributes when namespace is ignored Co-authored-by: Camille Drapier <[email protected]>
1 parent 4023f6d commit 51cd0dc

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

spec/data_spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,25 @@ describe("XMLParser", function() {
178178
expect(result).toEqual(expected);
179179
});
180180

181+
it("should parse XML when namespaced ignored", function() {
182+
const xmlData = `<a:b>c</a:b><a:d/><a:e atr="sasa" boolean>`;
183+
const expected = {
184+
"b" : "c",
185+
"d" : "",
186+
"e" : {
187+
"@_atr": "sasa",
188+
"@_boolean": true,
189+
}
190+
};
191+
192+
const result = parser.parse(xmlData, {
193+
ignoreAttributes: false,
194+
allowBooleanAttributes: true,
195+
ignoreNameSpace: true,
196+
});
197+
expect(result).toEqual(expected);
198+
});
199+
181200
it("should parse XML with undefined as text", function() {
182201
const xmlData = `<tag><![CDATA[undefined]]><nested>undefined</nested></tag>`;
183202
const expected = {

src/xmlstr2xmlnode.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ const getTraversalObj = function(xmlData, options) {
253253
const closeIndex = result.index;
254254
const separatorIndex = tagExp.indexOf(" ");
255255
let tagName = tagExp;
256+
let shouldBuildAttributesMap = true;
256257
if(separatorIndex !== -1){
257258
tagName = tagExp.substr(0, separatorIndex).replace(/\s\s*$/, '');
258259
tagExp = tagExp.substr(separatorIndex + 1);
@@ -262,6 +263,7 @@ const getTraversalObj = function(xmlData, options) {
262263
const colonIndex = tagName.indexOf(":");
263264
if(colonIndex !== -1){
264265
tagName = tagName.substr(colonIndex+1);
266+
shouldBuildAttributesMap = tagName !== result.data.substr(colonIndex + 1);
265267
}
266268
}
267269

@@ -292,7 +294,7 @@ const getTraversalObj = function(xmlData, options) {
292294
if (options.stopNodes.length && options.stopNodes.includes(childNode.tagname)) {
293295
childNode.startIndex=closeIndex;
294296
}
295-
if(tagName !== tagExp){
297+
if(tagName !== tagExp && shouldBuildAttributesMap){
296298
childNode.attrsMap = buildAttributesMap(tagExp, options);
297299
}
298300
currentNode.addChild(childNode);

0 commit comments

Comments
 (0)