Skip to content

Commit

Permalink
fix: when a tag with name "attributes"
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Dec 4, 2021
1 parent 19bbd64 commit c68fed8
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/v4/2.XMLparseOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ const XMLdata = `
]
}
],
"attributes": {
":@": {
"standard" : "3"
}
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion spec/entities_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ describe("XMLParser Entities", function() {
]
}
],
"attributes": {
":@": {
"@heading": "Reminder > \"Alert"
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/xmlbuilder/orderedJs2Xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function arrToStr(arr, options, jPath, level){
xmlStr += indentation + `<!--${tagObj[tagName][0][options.textNodeName]}-->`;
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){
Expand All @@ -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;
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/xmlparser/OrderedObjParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/xmlparser/node2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/xmlparser/xmlNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down

0 comments on commit c68fed8

Please sign in to comment.