Skip to content

Commit

Permalink
builder: supports decoding & to &
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Feb 4, 2022
1 parent da13b31 commit 263141b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 14 deletions.
76 changes: 75 additions & 1 deletion spec/entities_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,5 +395,79 @@ describe("XMLParser External Entites", function() {

expect(result.note).toEqual(`><last`);
});

it("should build by decoding '&' prserve mode", function() {
const jsObj = [
{
"note": [
{
"body": [
{ "#text": "(3 & 4) < 5" },
{ "attr": [ { "#text": "Writer: Donald Duck." } ] }
]
}
],
":@": {
"@heading": "Reminder > \"Alert"
}
}
];

const expected = `
<note heading="Reminder &gt; &quot;Alert">
<body>
(3 &amp; 4) &lt; 5
<attr>Writer: Donald Duck.</attr>
</body>
</note>`;

const options = {
attributeNamePrefix: "@",
ignoreAttributes: false,
preserveOrder: true,
// processEntities: false
};

const parser = new XMLParser(options);
let result = parser.parse(expected);
// console.log(JSON.stringify(result,null,4));

const builder = new XMLBuilder(options);
result = builder.build(jsObj);
// console.log(result);
expect(result.replace(/\s+/g, "")).toEqual(expected.replace(/\s+/g, ""));
});
it("should build by decoding '&'", function() {
const jsObj = {
"note": {
"body": {
"attr": "Writer: Donald Duck.",
"#text": "(3 & 4) < 5"
},
"@heading": "Reminder > \"Alert"
}
};

const expected = `
<note heading="Reminder &gt; &quot;Alert">
<body>
<attr>Writer: Donald Duck.</attr>
(3 &amp; 4) &lt; 5
</body>
</note>`;

const options = {
attributeNamePrefix: "@",
ignoreAttributes: false,
};

const parser = new XMLParser(options);
let result = parser.parse(expected);
console.log(JSON.stringify(result,null,4));
// expect(expected).toEqual(jsObj);

const builder = new XMLBuilder(options);
const output = builder.build(jsObj);
// console.log(output);
expect(output.replace(/\s+/g, "")).toEqual(expected.replace(/\s+/g, ""));
});
});
5 changes: 2 additions & 3 deletions spec/j2x_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,11 @@ describe("XMLBuilder", function() {
attributesGroupName: "@",
encodeHTMLchar: true,
suppressEmptyNode: true,
tagValueProcessor: (tagName,a)=> { a= ''+ a; return he.encode(a, { useNamedReferences: true}) },
attributeValueProcessor: (attrName, a) => he.encode(a, {isAttributeValue: true, useNamedReferences: true})
processEntities:false
});
const result = builder.build(jObj);
// console.log(result);
const expected = `<a b="val&gt;1" c="val&lt;2"><notattr>val</notattr>textvalue&gt;<tag><k>34</k><g/><nested b="val&gt;1" c="val&lt;2"/></tag></a>`;
const expected = `<a b="val>1" c="val<2"><notattr>val</notattr>textvalue><tag><k>34</k><g/><nested b="val>1" c="val<2"/></tag></a>`;
expect(result).toEqual(expected);
});

Expand Down
17 changes: 9 additions & 8 deletions src/xmlbuilder/json2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ const defaultOptions = {
preserveOrder: false,
commentPropName: false,
unpairedTags: [],
entities: {
">" : { regex: new RegExp(">", "g"), val: "&gt;" },
"<" : { regex: new RegExp("<", "g"), val: "&lt;" },
"sQuot" : { regex: new RegExp("\'", "g"), val: "&apos;" },
"dQuot" : { regex: new RegExp("\"", "g"), val: "&quot;" }
},
entities: [
{ regex: new RegExp("&", "g"), val: "&amp;" },//it must be on top
{ regex: new RegExp(">", "g"), val: "&gt;" },
{ regex: new RegExp("<", "g"), val: "&lt;" },
{ regex: new RegExp("\'", "g"), val: "&apos;" },
{ regex: new RegExp("\"", "g"), val: "&quot;" }
],
processEntities: true,
stopNodes: []
};
Expand Down Expand Up @@ -211,8 +212,8 @@ function buildTextValNode(val, key, attrStr, level) {

function replaceEntitiesValue(textValue){
if(textValue && textValue.length > 0 && this.options.processEntities){
for (const entityName in this.options.entities) {
const entity = this.options.entities[entityName];
for (let i=0; i<this.options.entities.length; i++) {
const entity = this.options.entities[i];
textValue = textValue.replace(entity.regex, entity.val);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/xmlbuilder/orderedJs2Xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ function isStopNode(jPath, options){

function replaceEntitiesValue(textValue, options){
if(textValue && textValue.length > 0 && options.processEntities){
for (const entityName in options.entities) {
const entity = options.entities[entityName];
for (let i=0; i< options.entities.length; i++) {
const entity = options.entities[i];
textValue = textValue.replace(entity.regex, entity.val);
}
}
Expand Down

0 comments on commit 263141b

Please sign in to comment.