Skip to content

Commit

Permalink
fix builder: pi closing tags when sequence is not preserved
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Jan 8, 2022
1 parent 65e9828 commit 0ea75cb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 35 deletions.
45 changes: 44 additions & 1 deletion spec/pi_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,50 @@ describe("XMLParser", function() {

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

it("should process PI tag with tag attributes when order is not preserved", function(){
const xmlData = `<?xml version="1.0"?>
<?elementnames <fred>, <bert>, <harry> ?>
<h1></h1>
`;

const builderOptions = {
allowBooleanAttributes: true,
attributeNamePrefix: '',
attributesGroupName: 'attr',
textNodeName: 'text',
ignoreAttributes: false,
format: true,
// suppressEmptyNode: true,
suppressBooleanAttributes: true
};

const parseOptions = {
attributeNamePrefix: '',
attributesGroupName: 'attr',
textNodeName: 'text',
ignoreAttributes: false,
removeNSPrefix: false,
allowBooleanAttributes: true,
parseTagValue: true,
parseAttributeValue: false,
trimValues: true,
parseTrueNumberOnly: false,
arrayMode: false,
alwaysCreateTextNode: true,
numberParseOptions: {
hex: true,
leadingZeros: false
}
}

const result = new XMLParser(parseOptions).parse(xmlData)
const builder = new XMLBuilder(builderOptions);
const output = builder.build(result);
expect(output.replace(/\s+/g, "")).toEqual(xmlData.replace(/\s+/g, ""));
// console.log(output);
});
});
48 changes: 14 additions & 34 deletions src/xmlbuilder/json2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,34 +161,24 @@ function processTextOrObjNode (object, key, level) {
}

function buildObjectNode(val, key, attrStr, level) {
let tagEndExp = '</' + key + this.tagEndChar;
let piClosingChar = "";

if(key[0] === "?") {
piClosingChar = "?";
tagEndExp = "";
}

if (attrStr && val.indexOf('<') === -1) {
return (
this.indentate(level) +
'<' +
key +
attrStr +
'>' +
this.indentate(level) + '<' + key + attrStr + piClosingChar + '>' +
val +
//+ this.newLine
// + this.indentate(level)
'</' +
key +
this.tagEndChar
);
tagEndExp );
} else {
return (
this.indentate(level) +
'<' +
key +
attrStr +
this.tagEndChar +
this.indentate(level) + '<' + key + attrStr + piClosingChar + this.tagEndChar +
val +
//+ this.newLine
this.indentate(level) +
'</' +
key +
this.tagEndChar
);
this.indentate(level) + tagEndExp );
}
}

Expand All @@ -198,8 +188,6 @@ function buildEmptyObjNode(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;
// return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
//+ this.newLine
}
}

Expand All @@ -208,16 +196,9 @@ function buildTextValNode(val, key, attrStr, level) {
textValue = this.replaceEntitiesValue(textValue);

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

function replaceEntitiesValue(textValue){
Expand All @@ -238,7 +219,6 @@ function buildEmptyTextNode(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;
// return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
}
}

Expand Down

0 comments on commit 0ea75cb

Please sign in to comment.