Skip to content

Commit

Permalink
* Second tagValueProcessor is not needed, double encodes (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Haddad authored Feb 1, 2022
1 parent 7ab5891 commit b0ea635
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
50 changes: 50 additions & 0 deletions spec/j2x_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,56 @@ textvalue&gt; <tag>
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 = `<a>
<element>
<subelement staticMessage="bar">foo &amp; bar</subelement>
</element>
<only_text_array>text &amp; value</only_text_array>
<regular_array f="abc">text &amp; value</regular_array>
<only_text_obj>another text &amp; val</only_text_obj>
</a>
`;
expect(result).toEqual(expected);
});


it("should format when parsing to XML", function() {
const jObj = {
Expand Down
3 changes: 1 addition & 2 deletions src/xmlbuilder/json2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '>' +
Expand Down

0 comments on commit b0ea635

Please sign in to comment.