Skip to content

Commit

Permalink
allow to suppressUnpairedNode
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Feb 4, 2022
1 parent 9ff2040 commit da13b31
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 12 deletions.
119 changes: 118 additions & 1 deletion spec/unpairedTags_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("unpaired and empty tags", function() {
const expectedXmlData = `<rootNode>
<tag>value</tag>
<empty></empty>
<unpaired></unpaired>
<unpaired>
</rootNode>`;

const options = {
Expand Down Expand Up @@ -142,4 +142,121 @@ describe("unpaired and empty tags", function() {
// console.log(output);
expect(output.replace(/\s+/g, "")).toEqual(expectedXml.replace(/\s+/g, ""));
});

it("should supress paired tag but not unpaired tag when suppressUnpairedNode:false", function() {
const xmlData = `<rootNode>
<tag>value</tag>
<empty />
<unpaired>
</rootNode>`;
const expectedXmlData = `<rootNode>
<tag>value</tag>
<empty/>
<unpaired/>
</rootNode>`;

const options = {
// format: true,
// preserveOrder: true,
suppressEmptyNode: true,
suppressUnpairedNode: false,
unpairedTags: ["unpaired"]
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
// console.log(JSON.stringify(result, null,4));

const builder = new XMLBuilder(options);
const output = builder.build(result);
// console.log(output);
expect(output.replace(/\s+/g, "")).toEqual(expectedXmlData.replace(/\s+/g, ""));
});

it("should not suppress paired tag but unpaired tag when suppressUnpairedNode:true", function() {
const xmlData = `<rootNode>
<tag>value</tag>
<empty />
<unpaired>
</rootNode>`;
const expectedXmlData = `<rootNode>
<tag>value</tag>
<empty></empty>
<unpaired>
</rootNode>`;

const options = {
// format: true,
// preserveOrder: true,
// suppressEmptyNode: true,
suppressUnpairedNode: true,
unpairedTags: ["unpaired"]
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
// console.log(JSON.stringify(result, null,4));

const builder = new XMLBuilder(options);
const output = builder.build(result);
// console.log(output);
expect(output.replace(/\s+/g, "")).toEqual(expectedXmlData.replace(/\s+/g, ""));
});

it("should supress paired tag but not unpaired tag when suppressUnpairedNode:false (ordered)", function() {
const xmlData = `<rootNode>
<tag>value</tag>
<empty />
<unpaired>
</rootNode>`;
const expectedXmlData = `<rootNode>
<tag>value</tag>
<empty/>
<unpaired/>
</rootNode>`;

const options = {
// format: true,
preserveOrder: true,
suppressEmptyNode: true,
suppressUnpairedNode: false,
unpairedTags: ["unpaired"]
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
// console.log(JSON.stringify(result, null,4));

const builder = new XMLBuilder(options);
const output = builder.build(result);
// console.log(output);
expect(output.replace(/\s+/g, "")).toEqual(expectedXmlData.replace(/\s+/g, ""));
});

it("should not suppress paired tag but unpaired tag when suppressUnpairedNode:true (ordered)", function() {
const xmlData = `<rootNode>
<tag>value</tag>
<empty />
<unpaired>
</rootNode>`;
const expectedXmlData = `<rootNode>
<tag>value</tag>
<empty></empty>
<unpaired>
</rootNode>`;

const options = {
// format: true,
preserveOrder: true,
// suppressEmptyNode: true,
suppressUnpairedNode: true,
unpairedTags: ["unpaired"]
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
// console.log(JSON.stringify(result, null,4));

const builder = new XMLBuilder(options);
const output = builder.build(result);
// console.log(output);
expect(output.replace(/\s+/g, "")).toEqual(expectedXmlData.replace(/\s+/g, ""));
});

});
1 change: 1 addition & 0 deletions src/fxp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type XmlBuilderOptions = {
indentBy: string;
arrayNodeName: string;
suppressEmptyNode: boolean;
suppressUnpairedNode: boolean;
suppressBooleanAttributes: boolean;
preserveOrder: boolean;
unpairedTags: string[];
Expand Down
33 changes: 23 additions & 10 deletions src/xmlbuilder/json2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const defaultOptions = {
format: false,
indentBy: ' ',
suppressEmptyNode: false,
suppressUnpairedNode: true,
suppressBooleanAttributes: true,
tagValueProcessor: function(key, a) {
return a;
Expand Down Expand Up @@ -193,11 +194,19 @@ function buildEmptyObjNode(val, key, attrStr, level) {

function buildTextValNode(val, key, attrStr, level) {
const textValue = this.replaceEntitiesValue(val);

return (
this.indentate(level) + '<' + key + attrStr + '>' +
textValue +
'</' + key + this.tagEndChar );

if( textValue === '' && this.options.unpairedTags.indexOf(key) !== -1){ //unpaired
if(this.options.suppressUnpairedNode){
return this.indentate(level) + '<' + key + this.tagEndChar;
}else{
return this.indentate(level) + '<' + key + "/" + this.tagEndChar;
}
}else{
return (
this.indentate(level) + '<' + key + attrStr + '>' +
textValue +
'</' + key + this.tagEndChar );
}
}

function replaceEntitiesValue(textValue){
Expand All @@ -211,13 +220,17 @@ function replaceEntitiesValue(textValue){
}

function buildEmptyTextNode(val, key, attrStr, level) {
if( val === '' && this.options.unpairedTags.indexOf(key) !== -1){
return this.indentate(level) + '<' + key + attrStr + this.tagEndChar;
}else if (val !== '') {
if( val === '' && this.options.unpairedTags.indexOf(key) !== -1){ //unpaired
if(this.options.suppressUnpairedNode){
return this.indentate(level) + '<' + key + this.tagEndChar;
}else{
return this.indentate(level) + '<' + key + "/" + this.tagEndChar;
}
}else if (val !== '') { //empty
return this.buildTextValNode(val, key, attrStr, level);
} else {
if(key[0] === "?") return this.indentate(level) + '<' + key + attrStr+ '?' + this.tagEndChar;
else return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
if(key[0] === "?") return this.indentate(level) + '<' + key + attrStr+ '?' + this.tagEndChar; //PI tag
else return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar; //normal
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/xmlbuilder/orderedJs2Xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ function arrToStr(arr, options, jPath, level){
let tagStart = indentation + `<${tagName}${attStr}`;
let tagValue = arrToStr(tagObj[tagName], options, newJPath, level + 1);
if(options.unpairedTags.indexOf(tagName) !== -1){
xmlStr += tagStart + ">";
if(options.suppressUnpairedNode) xmlStr += tagStart + ">";
else xmlStr += tagStart + "/>";
}else if( (!tagValue || tagValue.length === 0) && options.suppressEmptyNode){
xmlStr += tagStart + "/>";
}else{
Expand Down

0 comments on commit da13b31

Please sign in to comment.