From 263141b6d5b4d459bf8a10edb5acbd8e724531c3 Mon Sep 17 00:00:00 2001 From: Amit Gupta Date: Fri, 4 Feb 2022 06:13:38 +0530 Subject: [PATCH] builder: supports decoding & to & --- spec/entities_spec.js | 76 ++++++++++++++++++++++++++++++++- spec/j2x_spec.js | 5 +-- src/xmlbuilder/json2xml.js | 17 ++++---- src/xmlbuilder/orderedJs2Xml.js | 4 +- 4 files changed, 88 insertions(+), 14 deletions(-) diff --git a/spec/entities_spec.js b/spec/entities_spec.js index 0a04d11c..5d7cf624 100644 --- a/spec/entities_spec.js +++ b/spec/entities_spec.js @@ -395,5 +395,79 @@ describe("XMLParser External Entites", function() { expect(result.note).toEqual(`> \"Alert" + } + } + ]; + + const expected = ` + + + (3 & 4) < 5 + Writer: Donald Duck. + + `; + + 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 = ` + + + Writer: Donald Duck. + (3 & 4) < 5 + + `; + + 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, "")); + }); }); \ No newline at end of file diff --git a/spec/j2x_spec.js b/spec/j2x_spec.js index bb84f164..c3e504eb 100644 --- a/spec/j2x_spec.js +++ b/spec/j2x_spec.js @@ -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 = `valtextvalue>34`; + const expected = `valtextvalue>34`; expect(result).toEqual(expected); }); diff --git a/src/xmlbuilder/json2xml.js b/src/xmlbuilder/json2xml.js index 7fc23a07..db627e72 100644 --- a/src/xmlbuilder/json2xml.js +++ b/src/xmlbuilder/json2xml.js @@ -22,12 +22,13 @@ const defaultOptions = { preserveOrder: false, commentPropName: false, unpairedTags: [], - entities: { - ">" : { regex: new RegExp(">", "g"), val: ">" }, - "<" : { regex: new RegExp("<", "g"), val: "<" }, - "sQuot" : { regex: new RegExp("\'", "g"), val: "'" }, - "dQuot" : { regex: new RegExp("\"", "g"), val: """ } - }, + entities: [ + { regex: new RegExp("&", "g"), val: "&" },//it must be on top + { regex: new RegExp(">", "g"), val: ">" }, + { regex: new RegExp("<", "g"), val: "<" }, + { regex: new RegExp("\'", "g"), val: "'" }, + { regex: new RegExp("\"", "g"), val: """ } + ], processEntities: true, stopNodes: [] }; @@ -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 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); } }