diff --git a/CHANGELOG.md b/CHANGELOG.md index a5af4255..b353df04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ Note: If you find missing information about particular minor version, that version must have been changed without any functional change in this library. +** 4.0.0-beta.5 / 2021-12-04** +* fix: when a tag with name "attributes" + ** 4.0.0-beta.4 / 2021-12-02** * Support HTML document parsing * skip stop nodes parsing when building the XML from JS object diff --git a/docs/v4/2.XMLparseOptions.md b/docs/v4/2.XMLparseOptions.md index 5616b7e6..adb0e923 100644 --- a/docs/v4/2.XMLparseOptions.md +++ b/docs/v4/2.XMLparseOptions.md @@ -560,7 +560,7 @@ const XMLdata = ` ] } ], - "attributes": { + ":@": { "standard" : "3" } } diff --git a/package-lock.json b/package-lock.json index 8d642328..9f5e02f0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "fast-xml-parser", - "version": "4.0.0-beta.4", + "version": "4.0.0-beta.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "fast-xml-parser", - "version": "4.0.0-beta.4", + "version": "4.0.0-beta.5", "license": "MIT", "dependencies": { "strnum": "^1.0.5" diff --git a/package.json b/package.json index dfe1e5fe..dd4aec19 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fast-xml-parser", - "version": "4.0.0-beta.4", + "version": "4.0.0-beta.5", "description": "Validate XML, Parse XML, Build XML without C/C++ based libraries", "main": "./src/fxp.js", "scripts": { diff --git a/spec/entities_spec.js b/spec/entities_spec.js index 1675fbe7..509e3fe4 100644 --- a/spec/entities_spec.js +++ b/spec/entities_spec.js @@ -246,7 +246,7 @@ describe("XMLParser Entities", function() { ] } ], - "attributes": { + ":@": { "@heading": "Reminder > \"Alert" } } diff --git a/src/xmlbuilder/orderedJs2Xml.js b/src/xmlbuilder/orderedJs2Xml.js index 4cba7e39..44de29ad 100644 --- a/src/xmlbuilder/orderedJs2Xml.js +++ b/src/xmlbuilder/orderedJs2Xml.js @@ -40,7 +40,7 @@ function arrToStr(arr, options, jPath, level){ xmlStr += indentation + ``; continue; } - const attStr = attr_to_str(tagObj.attributes, options); + const attStr = attr_to_str(tagObj[":@"], options); let tagStart = indentation + `<${tagName}${attStr}`; let tagValue = arrToStr(tagObj[tagName], options, newJPath, level + 1); if( (!tagValue || tagValue.length === 0) && options.suppressEmptyNode){ @@ -62,7 +62,7 @@ function propName(obj){ const keys = Object.keys(obj); for (let i = 0; i < keys.length; i++) { const key = keys[i]; - if(key !== "attributes") return key; + if(key !== ":@") return key; } } diff --git a/src/xmlparser/OrderedObjParser.js b/src/xmlparser/OrderedObjParser.js index aaa30225..718d7c5f 100644 --- a/src/xmlparser/OrderedObjParser.js +++ b/src/xmlparser/OrderedObjParser.js @@ -196,7 +196,7 @@ const parseXml = function(xmlData) { , currentNode.tagname , jPath ,false - , currentNode.attributes ? Object.keys(currentNode.attributes).length !== 0 : false + , currentNode[":@"] ? Object.keys(currentNode[":@"]).length !== 0 : false , Object.keys(currentNode.child).length === 0); if(textData !== undefined && textData !== "") currentNode.add(this.options.textNodeName, textData); textData = ""; @@ -220,7 +220,7 @@ const parseXml = function(xmlData) { , currentNode.tagname , jPath ,false - , currentNode.attributes ? Object.keys(currentNode.attributes).length !== 0 : false + , currentNode[":@"] ? Object.keys(currentNode[":@"]).length !== 0 : false , Object.keys(currentNode.child).length === 0); if(textData !== undefined && textData !== "") currentNode.add(this.options.textNodeName, textData); @@ -242,7 +242,7 @@ const parseXml = function(xmlData) { , currentNode.tagname , jPath ,false - , currentNode.attributes ? Object.keys(currentNode.attributes).length !== 0 : false + , currentNode[":@"] ? Object.keys(currentNode[":@"]).length !== 0 : false , Object.keys(currentNode.child).length === 0); if(textData !== undefined && textData !== "") currentNode.add(this.options.textNodeName, textData); @@ -277,7 +277,7 @@ const parseXml = function(xmlData) { , currentNode.tagname , jPath , false - , currentNode.attributes ? Object.keys(currentNode.attributes).length !== 0 : false + , currentNode[":@"] ? Object.keys(currentNode[":@"]).length !== 0 : false , false); if(textData !== undefined && textData !== "") currentNode.add(this.options.textNodeName, textData); textData = ""; @@ -311,7 +311,7 @@ const parseXml = function(xmlData) { const childNode = new xmlNode(tagName); if(tagName !== tagExp && attrExpPresent){ - childNode.attributes = this.buildAttributesMap(tagExp, jPath); + childNode[":@"] = this.buildAttributesMap(tagExp, jPath); } jPath = jPath.substr(0, jPath.lastIndexOf(".")); childNode.add(this.options.textNodeName, tagContent); @@ -330,7 +330,7 @@ const parseXml = function(xmlData) { const childNode = new xmlNode(tagName); if(tagName !== tagExp && attrExpPresent){ - childNode.attributes = this.buildAttributesMap(tagExp, jPath); + childNode[":@"] = this.buildAttributesMap(tagExp, jPath); } jPath = jPath.substr(0, jPath.lastIndexOf(".")); currentNode.addChild(childNode); @@ -341,7 +341,7 @@ const parseXml = function(xmlData) { this.tagsNodeStack.push(currentNode); if(tagName !== tagExp && attrExpPresent){ - childNode.attributes = this.buildAttributesMap(tagExp, jPath); + childNode[":@"] = this.buildAttributesMap(tagExp, jPath); } currentNode.addChild(childNode); currentNode = childNode; diff --git a/src/xmlparser/node2json.js b/src/xmlparser/node2json.js index 3a952832..0ce08512 100644 --- a/src/xmlparser/node2json.js +++ b/src/xmlparser/node2json.js @@ -37,8 +37,8 @@ function compress(arr, options, jPath){ let val = compress(tagObj[property], options, newJpath); const isLeaf = isLeafTag(val, options); - if(tagObj.attributes){ - assignAttributes( val, tagObj.attributes, newJpath, options); + if(tagObj[":@"]){ + assignAttributes( val, tagObj[":@"], newJpath, options); }else if(Object.keys(val).length === 1 && val[options.textNodeName] !== undefined && !options.alwaysCreateTextNode){ val = val[options.textNodeName]; }else if(Object.keys(val).length === 0){ @@ -74,7 +74,7 @@ function propName(obj){ const keys = Object.keys(obj); for (let i = 0; i < keys.length; i++) { const key = keys[i]; - if(key !== "attributes") return key; + if(key !== ":@") return key; } } diff --git a/src/xmlparser/xmlNode.js b/src/xmlparser/xmlNode.js index 3e758cd5..ff60d709 100644 --- a/src/xmlparser/xmlNode.js +++ b/src/xmlparser/xmlNode.js @@ -4,15 +4,15 @@ class XmlNode{ constructor(tagname) { this.tagname = tagname; this.child = []; //nested tags, text, cdata, comments in order - this.attributes = {}; //attributes map + this[":@"] = {}; //attributes map } add(key,val){ // this.child.push( {name : key, val: val, isCdata: isCdata }); this.child.push( {[key]: val }); } addChild(node) { - if(node.attributes && Object.keys(node.attributes).length > 0){ - this.child.push( { [node.tagname]: node.child, attributes: node.attributes }); + if(node[":@"] && Object.keys(node[":@"]).length > 0){ + this.child.push( { [node.tagname]: node.child, [":@"]: node[":@"] }); }else{ this.child.push( { [node.tagname]: node.child }); }