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

Commit b37faab

Browse files
committed
Also run sanitize on the content when converting from xml to json.
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.
1 parent 7fa9541 commit b37faab

File tree

7 files changed

+9
-9
lines changed

7 files changed

+9
-9
lines changed

lib/json2xml.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ ToXml.prototype.openTag = function(key) {
7676
}
7777
ToXml.prototype.addAttr = function(key, val) {
7878
if (this.options.sanitize) {
79-
val = sanitizer.sanitize(val)
79+
val = sanitizer.sanitize(val);
8080
}
8181
this.xml += ' ' + key + '="' + val + '"';
8282
}
8383
ToXml.prototype.addTextContent = function(text) {
8484
this.completeTag();
85-
this.xml += text;
85+
this.xml += (this.options.sanitize ? sanitizer.sanitize(text) : text);
8686
}
8787
ToXml.prototype.closeTag = function(key) {
8888
this.completeTag();

lib/sanitize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ exports.sanitize = function sanitize(value, reverse) {
4141
});
4242

4343
return value;
44-
}
44+
};

test/fixtures/xmlsanitize.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"e":{"a":{"b":"Smith & Son"}}}
1+
{"e":{"a":{"b":"Smith & Son","$t":"Movers & <b>Shakers</b> Extraordinaire"}}}

test/fixtures/xmlsanitize.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<e><a b="Smith &amp; Son"></a></e>
1+
<e><a b="Smith &amp; Son">Movers &amp; &lt;b&gt;Shakers&lt;/b&gt; Extraordinaire</a></e>

test/fixtures/xmlsanitize2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"e":"Smith & Son"}
1+
{"e":"<b>Smith</b> & <b>Son</b>"}

test/fixtures/xmlsanitize2.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<e>Smith &amp; Son</e>
1+
<e>&lt;b&gt;Smith&lt;/b&gt; &amp; &lt;b&gt;Son&lt;/b&gt;</e>

test/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ describe('json2xml', function () {
179179

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

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

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

0 commit comments

Comments
 (0)