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

Commit 3c8552c

Browse files
authored
Merge pull request #129 from ehoogerbeets/master
Also run sanitize on the content when converting from xml to json.
2 parents f2e88e5 + 1189825 commit 3c8552c

File tree

10 files changed

+59
-13
lines changed

10 files changed

+59
-13
lines changed

lib/json2xml.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,14 @@ 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+
var newText = (this.options.sanitize ? sanitizer.sanitize(text) : text);
86+
this.xml += newText;
8687
}
8788
ToXml.prototype.closeTag = function(key) {
8889
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+
};

lib/xml2json.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ function endElement(name) {
6060
currentObject[textNodeName()] = currentObject[textNodeName()].trim()
6161
}
6262

63-
if (options.sanitize) {
64-
currentObject[textNodeName()] = sanitizer.sanitize(currentObject[textNodeName()], true);
65-
}
63+
//if (options.sanitize) {
64+
// currentObject[textNodeName()] = sanitizer.sanitize(currentObject[textNodeName()], true);
65+
//}
6666

6767
currentObject[textNodeName()] = coerce(currentObject[textNodeName()],name);
6868
}

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":{"$t":"<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/fixtures/xmlsanitize3.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"e":{"$t":"&lt;b&gt;Smith&lt;/b&gt; &amp;"}}

test/fixtures/xmlsanitize3.xml

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

test/test.js

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,57 @@ describe('xml2json', function () {
9999
it('does xmlsanitize of text', function (done) {
100100

101101
var xml = internals.readFixture('xmlsanitize2.xml');
102-
var result = parser.toJson(xml, {sanitize: true});
102+
var result = parser.toJson(xml, {sanitize: true, reversible: true});
103103
var json = internals.readFixture('xmlsanitize2.json');
104104

105105
expect(result).to.equal(json);
106106

107107
done();
108108
});
109109

110+
it('does json unsanitize', function (done) {
111+
112+
var json = internals.readFixture('xmlsanitize.json');
113+
var result = parser.toXml(json, {sanitize: true});
114+
var xml = internals.readFixture('xmlsanitize.xml');
115+
116+
expect(result).to.equal(xml);
117+
118+
done();
119+
});
120+
121+
it('does json unsanitize of text', function (done) {
122+
123+
var json = internals.readFixture('xmlsanitize2.json');
124+
var result = parser.toXml(json, {sanitize: true});
125+
var xml = internals.readFixture('xmlsanitize2.xml');
126+
127+
expect(result).to.equal(xml);
128+
129+
done();
130+
});
131+
132+
it('does doesnt double sanitize', function (done) {
133+
134+
var json = internals.readFixture('xmlsanitize3.json');
135+
var result = parser.toXml(json, {sanitize: true});
136+
var xml = internals.readFixture('xmlsanitize3.xml');
137+
138+
expect(result).to.equal(xml);
139+
140+
done();
141+
});
142+
143+
it('does doesnt double unsanitize', function (done) {
144+
145+
var xml = internals.readFixture('xmlsanitize3.xml');
146+
var result = parser.toJson(xml, {sanitize: true, reversible: true});
147+
var json = internals.readFixture('xmlsanitize3.json');
148+
149+
expect(result).to.equal(json);
150+
done();
151+
});
152+
110153
it('converts with forceArrays', function(done) {
111154
var xml = internals.readFixture('forceArray.xml');
112155
var result = parser.toJson(xml, {arrayNotation: ['drivers', 'vehicles']});
@@ -247,8 +290,8 @@ describe('json2xml', function () {
247290

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

250-
var xml = fs.readFileSync('./test/fixtures/array-notation.xml');
251-
var expectedJson = JSON.parse( fs.readFileSync('./test/fixtures/array-notation.json') );
293+
var xml = internals.readFixture('array-notation.xml');
294+
var expectedJson = JSON.parse( internals.readFixture('array-notation.json') );
252295

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

0 commit comments

Comments
 (0)