Skip to content

Commit b2b8b36

Browse files
committed
Backport noDoubleEncoding fix from xmlbuilder2.
See: oozcitak/xmlbuilder2#16
1 parent d87b597 commit b2b8b36

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/XMLStringifier.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ module.exports = class XMLStringifier
162162
# `str` the string to escape
163163
textEscape: (str) ->
164164
if @options.noValidation then return str
165-
ampregex = if @options.noDoubleEncoding then /(?!&\S+;)&/g else /&/g
165+
ampregex = if @options.noDoubleEncoding then /(?!&(lt|gt|amp|apos|quot);)&/g else /&/g
166166
str.replace(ampregex, '&')
167167
.replace(/</g, '&lt;')
168168
.replace(/>/g, '&gt;')
@@ -175,7 +175,7 @@ module.exports = class XMLStringifier
175175
# `str` the string to escape
176176
attEscape: (str) ->
177177
if @options.noValidation then return str
178-
ampregex = if @options.noDoubleEncoding then /(?!&\S+;)&/g else /&/g
178+
ampregex = if @options.noDoubleEncoding then /(?!&(lt|gt|amp|apos|quot);)&/g else /&/g
179179
str.replace(ampregex, '&amp;')
180180
.replace(/</g, '&lt;')
181181
.replace(/"/g, '&quot;')

test/issues/97.coffee

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
obj =
22
root:
3-
'@att': 'attribute value with &num; and &#35;'
4-
'#text': 'HTML entities for umlaut are &uuml; and &#252;.'
3+
'@att': 'attribute value with &amp; and &#38;'
4+
'#text': 'XML entities for ampersand are &amp; and &#38;.'
55

66
suite 'Tests specific to issues:', ->
77
test 'Issue #97 (No double encoding)', ->
88
eq(
99
xml(obj, { noDoubleEncoding: true }).end()
1010
'<?xml version="1.0"?>' +
11-
'<root att="attribute value with &num; and &#35;">' +
12-
'HTML entities for umlaut are &uuml; and &#252;.' +
11+
'<root att="attribute value with &amp; and &amp;#38;">' +
12+
'XML entities for ampersand are &amp; and &amp;#38;.' +
1313
'</root>'
1414
)
1515

1616
test 'Issue #97 (Double encoding - default behavior)', ->
1717
eq(
1818
xml(obj).end()
1919
'<?xml version="1.0"?>' +
20-
'<root att="attribute value with &amp;num; and &amp;#35;">' +
21-
'HTML entities for umlaut are &amp;uuml; and &amp;#252;.' +
20+
'<root att="attribute value with &amp;amp; and &amp;#38;">' +
21+
'XML entities for ampersand are &amp;amp; and &amp;#38;.' +
2222
'</root>'
2323
)
2424

0 commit comments

Comments
 (0)