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

Commit 3461641

Browse files
committed
Merges #58
1 parent 2517a44 commit 3461641

8 files changed

Lines changed: 93 additions & 41 deletions

File tree

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test:
2+
node test/test.js
3+
4+
deps:
5+
npm install .
6+
7+
.PHONY: test deps

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ parser.toJson(xml, options);
3434
parser.toXml(json);
3535
```
3636

37-
### Options object
37+
### Options object for `toJson`
3838

3939
Default values:
4040
```javascript
@@ -68,7 +68,16 @@ var chars = {
6868
};
6969
```
7070

71+
### Options object for `toXml`
7172

73+
Default values:
74+
```javascript
75+
var options = {
76+
sanitize: false
77+
};
78+
```
79+
80+
`sanitize: false` is the default option to behave like previous versions
7281

7382

7483
(*) xml2json tranforms CDATA content to JSON, but it doesn't generate a reversible structure.

lib/json2xml.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
module.exports = function toXml(json, xml) {
1+
var sanitizer = require('./sanitize.js')
2+
3+
module.exports = function toXml(json, options) {
24
if (json instanceof Buffer) {
35
json = json.toString();
46
}
@@ -13,7 +15,7 @@ module.exports = function toXml(json, xml) {
1315
} else {
1416
obj = json;
1517
}
16-
var toXml = new ToXml();
18+
var toXml = new ToXml(options);
1719
toXml.parse(obj);
1820
return toXml.xml;
1921
}
@@ -38,7 +40,7 @@ ToXml.prototype.parse = function(obj) {
3840
self.addAttr(key, subVal);
3941
}
4042
}
41-
})
43+
});
4244
}
4345
}
4446

@@ -73,6 +75,9 @@ ToXml.prototype.openTag = function(key) {
7375
this.tagIncomplete = true;
7476
}
7577
ToXml.prototype.addAttr = function(key, val) {
78+
if (this.options.sanitize) {
79+
val = sanitizer.sanitize(val)
80+
}
7681
this.xml += ' ' + key + '="' + val + '"';
7782
}
7883
ToXml.prototype.addTextContent = function(text) {
@@ -89,7 +94,18 @@ ToXml.prototype.completeTag = function() {
8994
this.tagIncomplete = false;
9095
}
9196
}
92-
function ToXml() {
97+
function ToXml(options) {
98+
var defaultOpts = {
99+
sanitize: false
100+
};
101+
102+
if (options) {
103+
for (var opt in options) {
104+
defaultOpts[opt] = options[opt];
105+
}
106+
}
107+
108+
this.options = defaultOpts;
93109
this.xml = '';
94110
this.tagIncomplete = false;
95111
}

lib/sanitize.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Simple sanitization. It is not intended to sanitize
3+
* malicious element values.
4+
*
5+
* character | escaped
6+
* < &lt;
7+
* > &gt;
8+
* ( &#40;
9+
* ) &#41;
10+
* # &#35;
11+
* & &amp;
12+
* " &quot;
13+
* ' &apos;
14+
*/
15+
var chars = {
16+
'<': '&lt;',
17+
'>': '&gt;',
18+
'(': '&#40;',
19+
')': '&#41;',
20+
'#': '&#35;',
21+
'&': '&amp;',
22+
'"': '&quot;',
23+
"'": '&apos;'
24+
};
25+
26+
exports.sanitize = function sanitize(value) {
27+
if (typeof value !== 'string') {
28+
return value;
29+
}
30+
31+
Object.keys(chars).forEach(function(key) {
32+
value = value.replace(key, chars[key]);
33+
});
34+
35+
return value;
36+
}

lib/xml2json.js

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var expat = require('node-expat');
2+
var sanitizer = require('./sanitize.js')
23

34
// This object will hold the final result.
45
var obj = {};
@@ -58,7 +59,7 @@ function text(data) {
5859
}
5960

6061
if (options.sanitize) {
61-
data = sanitize(data);
62+
data = sanitizer.sanitize(data);
6263
}
6364

6465
currentObject['$t'] = coerce((currentObject['$t'] || '') + data);
@@ -108,41 +109,6 @@ function coerce(value) {
108109
}
109110

110111

111-
/**
112-
* Simple sanitization. It is not intended to sanitize
113-
* malicious element values.
114-
*
115-
* character | escaped
116-
* < &lt;
117-
* > &gt;
118-
* ( &#40;
119-
* ) &#41;
120-
* # &#35;
121-
* & &amp;
122-
* " &quot;
123-
* ' &apos;
124-
*/
125-
var chars = { '<': '&lt;',
126-
'>': '&gt;',
127-
'(': '&#40;',
128-
')': '&#41;',
129-
'#': '&#35;',
130-
'&': '&amp;',
131-
'"': '&quot;',
132-
"'": '&apos;' };
133-
134-
function sanitize(value) {
135-
if (typeof value !== 'string') {
136-
return value;
137-
}
138-
139-
Object.keys(chars).forEach(function(key) {
140-
value = value.replace(key, chars[key]);
141-
});
142-
143-
return value;
144-
}
145-
146112
/**
147113
* Parses xml to json using node-expat.
148114
* @param {String|Buffer} xml The xml to be parsed to json.

test/fixtures/xmlsanitize.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"e":{"a":{"b":"Smith & Son"}}}

test/fixtures/xmlsanitize.xml

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

test/test-xmlsanitize.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var parser = require('../lib');
4+
var assert = require('assert');
5+
6+
var expected = fs.readFileSync(__dirname + '/fixtures/xmlsanitize.xml', {encoding: 'utf8'});
7+
//console.log("expected: " + expected)
8+
var json = parser.toJson(expected, {object: true, space: true});
9+
//console.log('xml => json: \n%j', json);
10+
11+
var xmlres = parser.toXml(json, { sanitize: true });
12+
//console.log(xmlres)
13+
//assert.deepEqual(json.doc.Column.length, 5, 'should have 5 Columns');
14+
assert.strictEqual(expected, xmlres, 'xml strings not equal!')
15+
16+
console.log('xml2json toXml sanitize passed!');

0 commit comments

Comments
 (0)