diff --git a/spec/j2x_spec.js b/spec/j2x_spec.js index 1c79a95e..bb84f164 100644 --- a/spec/j2x_spec.js +++ b/spec/j2x_spec.js @@ -299,6 +299,56 @@ textvalue> expect(result).toEqual(expected); }); + it("should not double encode tag values", function() { + const jObj = { + a: { + element: { + subelement: { + "#text": "foo & bar", + "@": {"staticMessage": "bar"} + } + }, + only_text_array: [ + { + '#text': 'text & value' + } + ], + regular_array: [ + { + '#text': 'text & value', + '@': { + 'f': 'abc' + } + } + ], + only_text_obj: { + '#text': 'another text & val' + } + } + }; + const builder = new XMLBuilder({ + attributesGroupName: "@", + processEntities: false, + format: true, + tagValueProcessor: (tagName, a) => { a = '' + a; return he.encode(a, { useNamedReferences: true }) }, + attributeValueProcessor: (attrName, a) => he.encode(a, { isAttributeValue: true, useNamedReferences: true }), + attributeNamePrefix: '', + ignoreAttributes: false, + suppressEmptyNode: true + }); + const result = builder.build(jObj); + const expected = ` + + foo & bar + + text & value + text & value + another text & val + +`; + expect(result).toEqual(expected); + }); + it("should format when parsing to XML", function() { const jObj = { diff --git a/src/xmlbuilder/json2xml.js b/src/xmlbuilder/json2xml.js index 2bcb0352..9f862beb 100644 --- a/src/xmlbuilder/json2xml.js +++ b/src/xmlbuilder/json2xml.js @@ -192,8 +192,7 @@ function buildEmptyObjNode(val, key, attrStr, level) { } function buildTextValNode(val, key, attrStr, level) { - let textValue = this.options.tagValueProcessor(key, val); - textValue = this.replaceEntitiesValue(textValue); + const textValue = this.replaceEntitiesValue(val); return ( this.indentate(level) + '<' + key + attrStr + '>' +