Skip to content
This repository has been archived by the owner on Jan 7, 2021. It is now read-only.

Commit

Permalink
Also run sanitize on the content when converting from xml to json.
Browse files Browse the repository at this point in the history
Previously, the content was unsanitized when the data was converted
from json to xml, but not when converted from xml to json. This is
corrected now so that the conversion is reversible.
  • Loading branch information
ehoogerbeets committed Oct 22, 2016
1 parent 7fa9541 commit b37faab
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/json2xml.js
Expand Up @@ -76,13 +76,13 @@ ToXml.prototype.openTag = function(key) {
}
ToXml.prototype.addAttr = function(key, val) {
if (this.options.sanitize) {
val = sanitizer.sanitize(val)
val = sanitizer.sanitize(val);
}
this.xml += ' ' + key + '="' + val + '"';
}
ToXml.prototype.addTextContent = function(text) {
this.completeTag();
this.xml += text;
this.xml += (this.options.sanitize ? sanitizer.sanitize(text) : text);
}
ToXml.prototype.closeTag = function(key) {
this.completeTag();
Expand Down
2 changes: 1 addition & 1 deletion lib/sanitize.js
Expand Up @@ -41,4 +41,4 @@ exports.sanitize = function sanitize(value, reverse) {
});

return value;
}
};
2 changes: 1 addition & 1 deletion test/fixtures/xmlsanitize.json
@@ -1 +1 @@
{"e":{"a":{"b":"Smith & Son"}}}
{"e":{"a":{"b":"Smith & Son","$t":"Movers & <b>Shakers</b> Extraordinaire"}}}
2 changes: 1 addition & 1 deletion test/fixtures/xmlsanitize.xml
@@ -1 +1 @@
<e><a b="Smith &amp; Son"></a></e>
<e><a b="Smith &amp; Son">Movers &amp; &lt;b&gt;Shakers&lt;/b&gt; Extraordinaire</a></e>
2 changes: 1 addition & 1 deletion test/fixtures/xmlsanitize2.json
@@ -1 +1 @@
{"e":"Smith & Son"}
{"e":"<b>Smith</b> & <b>Son</b>"}
2 changes: 1 addition & 1 deletion test/fixtures/xmlsanitize2.xml
@@ -1 +1 @@
<e>Smith &amp; Son</e>
<e>&lt;b&gt;Smith&lt;/b&gt; &amp; &lt;b&gt;Son&lt;/b&gt;</e>
4 changes: 2 additions & 2 deletions test/test.js
Expand Up @@ -179,8 +179,8 @@ describe('json2xml', function () {

it('works with array notation', function (done) {

var xml = fs.readFileSync('./test/fixtures/array-notation.xml');
var expectedJson = JSON.parse( fs.readFileSync('./test/fixtures/array-notation.json') );
var xml = internals.readFixture('array-notation.xml');
var expectedJson = JSON.parse( internals.readFixture('array-notation.json') );

var json = parser.toJson(xml, {object: true, arrayNotation: true});

Expand Down

0 comments on commit b37faab

Please sign in to comment.