Skip to content

Commit

Permalink
Backport noDoubleEncoding fix from xmlbuilder2.
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Apr 8, 2020
1 parent d87b597 commit b2b8b36
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/XMLStringifier.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ module.exports = class XMLStringifier
# `str` the string to escape
textEscape: (str) ->
if @options.noValidation then return str
ampregex = if @options.noDoubleEncoding then /(?!&\S+;)&/g else /&/g
ampregex = if @options.noDoubleEncoding then /(?!&(lt|gt|amp|apos|quot);)&/g else /&/g
str.replace(ampregex, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
Expand All @@ -175,7 +175,7 @@ module.exports = class XMLStringifier
# `str` the string to escape
attEscape: (str) ->
if @options.noValidation then return str
ampregex = if @options.noDoubleEncoding then /(?!&\S+;)&/g else /&/g
ampregex = if @options.noDoubleEncoding then /(?!&(lt|gt|amp|apos|quot);)&/g else /&/g
str.replace(ampregex, '&amp;')
.replace(/</g, '&lt;')
.replace(/"/g, '&quot;')
Expand Down
12 changes: 6 additions & 6 deletions test/issues/97.coffee
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
obj =
root:
'@att': 'attribute value with &num; and &#35;'
'#text': 'HTML entities for umlaut are &uuml; and &#252;.'
'@att': 'attribute value with &amp; and &#38;'
'#text': 'XML entities for ampersand are &amp; and &#38;.'

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

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

0 comments on commit b2b8b36

Please sign in to comment.