Skip to content

Commit

Permalink
fix: support suppressBooleanAttrs by builder
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Jan 8, 2022
1 parent f309930 commit 65e9828
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/xmlbuilder/json2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function Builder(options) {
this.buildObjectNode = buildObjectNode;

this.replaceEntitiesValue = replaceEntitiesValue;
this.buildAttrPairStr = buildAttrPairStr;
}

Builder.prototype.build = function(jObj) {
Expand All @@ -90,16 +91,16 @@ Builder.prototype.j2x = function(jObj, level) {
if (typeof jObj[key] === 'undefined') {
// supress undefined node
} else if (jObj[key] === null) {
val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
if(key[0] === "?") val += this.indentate(level) + '<' + key + '?' + this.tagEndChar;
else val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
// val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
} else if (jObj[key] instanceof Date) {
val += this.buildTextNode(jObj[key], key, '', level);
} else if (typeof jObj[key] !== 'object') {
//premitive type
const attr = this.isAttribute(key);
if (attr) {
let val = this.options.attributeValueProcessor(attr, '' + jObj[key]);
val = this.replaceEntitiesValue(val);
attrStr += ' ' + attr + '="' + val + '"';
attrStr += this.buildAttrPairStr(attr, '' + jObj[key]);
}else {
//tag value
if (key === this.options.textNodeName) {
Expand All @@ -117,7 +118,9 @@ Builder.prototype.j2x = function(jObj, level) {
if (typeof item === 'undefined') {
// supress undefined node
} else if (item === null) {
val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
if(key[0] === "?") val += this.indentate(level) + '<' + key + '?' + this.tagEndChar;
else val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
// val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
} else if (typeof item === 'object') {
val += this.processTextOrObjNode(item, key, level)
} else {
Expand All @@ -130,9 +133,7 @@ Builder.prototype.j2x = function(jObj, level) {
const Ks = Object.keys(jObj[key]);
const L = Ks.length;
for (let j = 0; j < L; j++) {
let val = this.options.attributeValueProcessor(Ks[j], '' + jObj[key][Ks[j]]);
val = this.replaceEntitiesValue(val);
attrStr += ' ' + Ks[j] + '="' + val + '"';
attrStr += this.buildAttrPairStr(Ks[j], '' + jObj[key][Ks[j]]);
}
} else {
val += this.processTextOrObjNode(jObj[key], key, level)
Expand All @@ -142,6 +143,14 @@ Builder.prototype.j2x = function(jObj, level) {
return {attrStr: attrStr, val: val};
};

function buildAttrPairStr(attrName, val){
val = this.options.attributeValueProcessor(attrName, '' + val);
val = this.replaceEntitiesValue(val);
if (this.options.suppressBooleanAttributes && val === "true") {
return ' ' + attrName;
} else return ' ' + attrName + '="' + val + '"';
}

function processTextOrObjNode (object, key, level) {
const result = this.j2x(object, level + 1);
if (object[this.options.textNodeName] !== undefined && Object.keys(object).length === 1) {
Expand Down Expand Up @@ -187,7 +196,9 @@ function buildEmptyObjNode(val, key, attrStr, level) {
if (val !== '') {
return this.buildObjectNode(val, key, attrStr, level);
} else {
return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
if(key[0] === "?") return this.indentate(level) + '<' + key + attrStr+ '?' + this.tagEndChar;
else return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
// return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
//+ this.newLine
}
}
Expand Down Expand Up @@ -225,7 +236,9 @@ function buildEmptyTextNode(val, key, attrStr, level) {
}else if (val !== '') {
return this.buildTextValNode(val, key, attrStr, level);
} else {
return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
if(key[0] === "?") return this.indentate(level) + '<' + key + attrStr+ '?' + this.tagEndChar;
else return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
// return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
}
}

Expand Down

0 comments on commit 65e9828

Please sign in to comment.