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

Commit 45b3a73

Browse files
authored
Merge pull request #2 from ehoogerbeets/asymmetricSanitize
Support asymmetric sanitization
2 parents 10e9be4 + 39f3d23 commit 45b3a73

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

lib/json2xml.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ 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, false, true);
8080
}
8181
this.xml += ' ' + key + '="' + val + '"';
8282
}

lib/sanitize.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,32 @@
1212
* " "
1313
* ' '
1414
*/
15-
var chars = {
15+
// used for body text
16+
var charsEscape = {
1617
'&': '&',
1718
'#': '#',
1819
'<': '&lt;',
1920
'>': '&gt;',
20-
'(': '&#40;',
21-
')': '&#41;',
21+
"\u001F": "&#31;"
22+
};
23+
24+
var charsUnescape = {
25+
'&amp;': '&',
26+
'&#35;': '#',
27+
'&lt;': '<',
28+
'&gt;': '>',
29+
'&#40;': '(',
30+
'&#41;': ')',
31+
'&quot;': '"',
32+
'&apos;': "'",
33+
"&#31;": "\u001F"
34+
};
35+
36+
// used in attribute values
37+
var charsAttrEscape = {
38+
'&': '&amp;',
39+
'<': '&lt;',
40+
'>': '&gt;',
2241
'"': '&quot;',
2342
"'": '&apos;'
2443
};
@@ -27,17 +46,17 @@ function escapeRegExp(string) {
2746
return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
2847
}
2948

30-
exports.sanitize = function sanitize(value, reverse) {
49+
// sanitize body text
50+
exports.sanitize = function sanitize(value, reverse, attribute) {
3151
if (typeof value !== 'string') {
3252
return value;
3353
}
3454

35-
Object.keys(chars).forEach(function(key) {
36-
if (reverse) {
37-
value = value.replace(new RegExp(escapeRegExp(chars[key]), 'g'), key);
38-
} else {
39-
value = value.replace(new RegExp(escapeRegExp(key), 'g'), chars[key]);
40-
}
55+
var chars = reverse ? charsUnescape : (attribute ? charsAttrEscape : charsEscape);
56+
var keys = Object.keys(chars);
57+
58+
keys.forEach(function(key) {
59+
value = value.replace(new RegExp(escapeRegExp(key), 'g'), chars[key]);
4160
});
4261

4362
return value;

lib/xml2json.js

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

63+
// node-expat already reverse sanitizes it whether we like it or not
6364
//if (options.sanitize) {
6465
// currentObject[textNodeName()] = sanitizer.sanitize(currentObject[textNodeName()], true);
6566
//}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xml2json",
3-
"version": "0.11.1",
3+
"version": "0.11.2",
44
"description": "Converts xml to json and vice-versa, using node-expat.",
55
"repository": "git://github.com/buglabs/node-xml2json.git",
66
"license": "MIT",

0 commit comments

Comments
 (0)