Skip to content

Commit

Permalink
Escape whitespace in attribute values by default
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Feb 5, 2016
1 parent 84da166 commit 272b53f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/XMLStringifier.coffee
Expand Up @@ -118,4 +118,8 @@ module.exports = class XMLStringifier
str.replace(ampregex, '&')
.replace(/</g, '&lt;')
.replace(/"/g, '&quot;')
.replace(/\t/g, '&#x9;')
.replace(/\r\n/g, '\n')
.replace(/\r/g, '\n')
.replace(/\n/g, '&#xA;')

20 changes: 0 additions & 20 deletions test/basic/createxml.coffee
Expand Up @@ -10,10 +10,6 @@ suite 'Creating XML:', ->
.txt('git://github.com/oozcitak/xmlbuilder-js.git')
.up()
.up()
.ele('test')
.att('escaped', 'chars <>\'"&\t\n\r')
.txt('complete 100%<>\'"&\t\n\r')
.up()
.ele('cdata')
.cdata('<test att="val">this is a test</test>\nSecond line')
.up()
Expand All @@ -31,7 +27,6 @@ suite 'Creating XML:', ->
'<!-- CoffeeScript is awesome. -->' +
'<repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo>' +
'</xmlbuilder>' +
'<test escaped="chars &lt;>\'&quot;&amp;\t\n\r">complete 100%&lt;&gt;\'"&amp;\t\n&#xD;</test>' +
'<cdata><![CDATA[<test att="val">this is a test</test>\nSecond line]]></cdata>' +
'<raw>&<>&</raw>' +
'<atttest att="val">text</atttest>' +
Expand All @@ -47,8 +42,6 @@ suite 'Creating XML:', ->
.nod('repo', {'type': 'git'}, 'git://github.com/oozcitak/xmlbuilder-js.git')
.up()
.up()
.ele('test', {'escaped': 'chars <>\'"&\t\n\r'}, 'complete 100%<>\'"&\t\n\r')
.up()
.ele('cdata')
.cdata('<test att="val">this is a test</test>\nSecond line')
.up()
Expand All @@ -67,7 +60,6 @@ suite 'Creating XML:', ->
'<!-- CoffeeScript is awesome. -->' +
'<repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo>' +
'</xmlbuilder>' +
'<test escaped="chars &lt;>\'&quot;&amp;\t\n\r">complete 100%&lt;&gt;\'"&amp;\t\n&#xD;</test>' +
'<cdata><![CDATA[<test att="val">this is a test</test>\nSecond line]]></cdata>' +
'<raw>&<>&</raw>' +
'<atttest att="val">text</atttest>' +
Expand All @@ -83,8 +75,6 @@ suite 'Creating XML:', ->
.nod('repo', {'type': 'git'}, 'git://github.com/oozcitak/xmlbuilder-js.git')
.up()
.up()
.ele('test', {'escaped': 'chars <>\'"&\t\n\r'}, 'complete 100%<>\'"&\t\n\r')
.up()
.ele('cdata')
.cdata('<test att="val">this is a test</test>\nSecond line')
.up()
Expand All @@ -104,7 +94,6 @@ suite 'Creating XML:', ->
<!-- CoffeeScript is awesome. -->
<repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo>
</xmlbuilder>
<test escaped="chars &lt;>\'&quot;&amp;\t\n\r">complete 100%&lt;&gt;\'"&amp;\t\n&#xD;</test>
<cdata>
<![CDATA[<test att="val">this is a test</test>
Second line]]>
Expand All @@ -124,8 +113,6 @@ suite 'Creating XML:', ->
.nod('repo', {'type': 'git'}, 'git://github.com/oozcitak/xmlbuilder-js.git')
.up()
.up()
.ele('test', {'escaped': 'chars <>\'"&\t\n\r'}, 'complete 100%<>\'"&\t\n\r')
.up()
.ele('cdata')
.cdata('<test att="val">this is a test</test>\nSecond line')
.up()
Expand All @@ -146,7 +133,6 @@ suite 'Creating XML:', ->
<!-- CoffeeScript is awesome. -->
<repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo>
</xmlbuilder>
<test escaped="chars &lt;>\'&quot;&amp;\t\n\r">complete 100%&lt;&gt;\'"&amp;\t\n&#xD;</test>
<cdata>
<![CDATA[<test att="val">this is a test</test>
Second line]]>
Expand All @@ -171,8 +157,6 @@ suite 'Creating XML:', ->
.nod('repo', {'type': 'git'}, 'git://github.com/oozcitak/xmlbuilder-js.git')
.up()
.up()
.ele('test', {'escaped': 'chars <>\'"&\t\n\r'}, 'complete 100%<>\'"&\t\n\r')
.up()
.ele('cdata')
.cdata('<test att="val">this is a test</test>\nSecond line')
.up()
Expand All @@ -192,7 +176,6 @@ suite 'Creating XML:', ->
<!-- CoffeeScript is awesome. -->
<repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo>
</xmlbuilder>
<test escaped="chars &lt;>\'&quot;&amp;\t\n\r">complete 100%&lt;&gt;\'"&amp;\t\n&#xD;</test>
<cdata>
<![CDATA[<test att="val">this is a test</test>
Second line]]>
Expand All @@ -212,8 +195,6 @@ suite 'Creating XML:', ->
.n('repo', {'type': 'git'}, 'git://github.com/oozcitak/xmlbuilder-js.git')
.u()
.u()
.e('test', {'escaped': 'chars <>\'"&\t\n\r'}, 'complete 100%<>\'"&\t\n\r')
.u()
.e('cdata')
.d('<test att="val">this is a test</test>\nSecond line')
.u()
Expand All @@ -234,7 +215,6 @@ suite 'Creating XML:', ->
'<!-- CoffeeScript is awesome. -->' +
'<repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo>' +
'</xmlbuilder>' +
'<test escaped="chars &lt;>\'&quot;&amp;\t\n\r">complete 100%&lt;&gt;\'"&amp;\t\n&#xD;</test>' +
'<cdata><![CDATA[<test att="val">this is a test</test>\nSecond line]]></cdata>' +
'<raw>&<>&</raw>' +
'<atttest att="val">text</atttest>' +
Expand Down
28 changes: 28 additions & 0 deletions test/basic/escaping.coffee
@@ -0,0 +1,28 @@
suite 'Text Processing', ->
test 'Escaping element value', ->
eq(
xml('root', { headless: true })
.ele('e', 'escaped <>\'"&\t\n').up()
.ele('e', 'escaped <>\'"&\t\r\n').up()
.ele('e', 'escaped <>\'"&\t\n\r').up()
.ele('e')
.att('a1', 'escaped <>\'"&\t\n')
.att('a2', 'escaped <>\'"&\t\r')
.att('a3', 'escaped <>\'"&\t\n\r')
.att('a4', 'escaped <>\'"&\t\r\n')
.up()
.end()

'<root>' +
'<e>escaped &lt;&gt;\'"&amp;\t\n</e>' +
'<e>escaped &lt;&gt;\'"&amp;\t&#xD;\n</e>' +
'<e>escaped &lt;&gt;\'"&amp;\t\n&#xD;</e>' +
'<e' +
' a1="escaped &lt;>\'&quot;&amp;&#x9;&#xA;"' +
' a2="escaped &lt;>\'&quot;&amp;&#x9;&#xA;"' +
' a3="escaped &lt;>\'&quot;&amp;&#x9;&#xA;&#xA;"' +
' a4="escaped &lt;>\'&quot;&amp;&#x9;&#xA;"' +
'/>' +
'</root>'
)

0 comments on commit 272b53f

Please sign in to comment.