This repository was archived by the owner on Jan 7, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 6 files changed +33
-15
lines changed Expand file tree Collapse file tree 6 files changed +33
-15
lines changed Original file line number Diff line number Diff line change @@ -76,7 +76,7 @@ ToXml.prototype.openTag = function(key) {
76
76
}
77
77
ToXml . prototype . addAttr = function ( key , val ) {
78
78
if ( this . options . sanitize ) {
79
- val = sanitizer . sanitize ( val ) ;
79
+ val = sanitizer . sanitize ( val , false , true ) ;
80
80
}
81
81
this . xml += ' ' + key + '="' + val + '"' ;
82
82
}
Original file line number Diff line number Diff line change 12
12
* " "
13
13
* ' '
14
14
*/
15
- var chars = {
15
+ // used for body text
16
+ var charsEscape = {
17
+ '&' : '&' ,
18
+ '<' : '<' ,
19
+ '>' : '>'
20
+ } ;
21
+
22
+ var charsUnescape = {
23
+ '&' : '&' ,
24
+ '#' : '#' ,
25
+ '<' : '<' ,
26
+ '>' : '>' ,
27
+ '(' : '(' ,
28
+ ')' : ')' ,
29
+ '"' : '"' ,
30
+ ''' : "'" ,
31
+ "" : "\u001F"
32
+ } ;
33
+
34
+ // used in attribute values
35
+ var charsAttrEscape = {
16
36
'&' : '&' ,
17
- '#' : '#' ,
18
37
'<' : '<' ,
19
38
'>' : '>' ,
20
- '(' : '(' ,
21
- ')' : ')' ,
22
39
'"' : '"' ,
23
40
"'" : '''
24
41
} ;
@@ -27,17 +44,17 @@ function escapeRegExp(string) {
27
44
return string . replace ( / ( [ . * + ? ^ = ! : $ { } ( ) | \[ \] \/ \\ ] ) / g, "\\$1" ) ;
28
45
}
29
46
30
- exports . sanitize = function sanitize ( value , reverse ) {
47
+ // sanitize body text
48
+ exports . sanitize = function sanitize ( value , reverse , attribute ) {
31
49
if ( typeof value !== 'string' ) {
32
50
return value ;
33
51
}
34
52
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
- }
53
+ var chars = reverse ? charsUnescape : ( attribute ? charsAttrEscape : charsEscape ) ;
54
+ var keys = Object . keys ( chars ) ;
55
+
56
+ keys . forEach ( function ( key ) {
57
+ value = value . replace ( new RegExp ( escapeRegExp ( key ) , 'g' ) , chars [ key ] ) ;
41
58
} ) ;
42
59
43
60
return value ;
Original file line number Diff line number Diff line change @@ -60,6 +60,7 @@ function endElement(name) {
60
60
currentObject [ textNodeName ( ) ] = currentObject [ textNodeName ( ) ] . trim ( )
61
61
}
62
62
63
+ // node-expat already reverse sanitizes it whether we like it or not
63
64
//if (options.sanitize) {
64
65
// currentObject[textNodeName()] = sanitizer.sanitize(currentObject[textNodeName()], true);
65
66
//}
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " xml2json" ,
3
- "version" : " 0.11.1 " ,
3
+ "version" : " 0.11.2 " ,
4
4
"description" : " Converts xml to json and vice-versa, using node-expat." ,
5
5
"repository" : " git://github.com/buglabs/node-xml2json.git" ,
6
6
"license" : " MIT" ,
Original file line number Diff line number Diff line change 1
- {"e" :{"a" :{"b" :" Smith & Son" ,"$t" :" Movers & <b>Shakers</b> Extraordinaire" }}}
1
+ {"e" :{"a" :{"b" :" < \" Smith\" & ' Son'> " ,"$t" :" Movers & <b>Shakers</b> Extraordinaire #() \" ' " }}}
Original file line number Diff line number Diff line change 1
- <e ><a b =" Smith & Son" >Movers & < b> Shakers< /b> Extraordinaire</a ></e >
1
+ <e ><a b =" <" Smith" & ' Son'> " >Movers & < b> Shakers< /b> Extraordinaire #()"' </a ></e >
You can’t perform that action at this time.
0 commit comments