Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add forceCdata option to builder to force the use of CDATA tags for all content. #396

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ Possible options are:
* `cdata` (default: `false`): wrap text nodes in `<![CDATA[ ... ]]>` instead of
escaping when necessary. Does not add `<![CDATA[ ... ]]>` if it is not required.
Added in 0.4.5.
* `forceCdata` (default: `false`): always wrap text nodes in `<![CDATA[ ... ]]>`,
even if not required.

`renderOpts`, `xmldec`,`doctype` and `headless` pass through to
[xmlbuilder-js](https://github.com/oozcitak/xmlbuilder-js).
Expand Down
8 changes: 4 additions & 4 deletions src/builder.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class exports.Builder
render = (element, obj) =>
if typeof obj isnt 'object'
# single element, just append it as text
if @options.cdata && requiresCDATA obj
if @options.forceCdata || (@options.cdata && requiresCDATA obj)
element.raw wrapCDATA obj
else
element.txt obj
Expand All @@ -64,7 +64,7 @@ class exports.Builder

# Case #2 Char data (CDATA, etc.)
else if key is charkey
if @options.cdata && requiresCDATA child
if @options.forceCdata || (@options.cdata && requiresCDATA child)
element = element.raw wrapCDATA child
else
element = element.txt child
Expand All @@ -73,7 +73,7 @@ class exports.Builder
else if Array.isArray child
for own index, entry of child
if typeof entry is 'string'
if @options.cdata && requiresCDATA entry
if @options.forceCdata || (@options.cdata && requiresCDATA entry)
element = element.ele(key).raw(wrapCDATA entry).up()
else
element = element.ele(key, entry).up()
Expand All @@ -86,7 +86,7 @@ class exports.Builder

# Case #5 String and remaining types
else
if typeof child is 'string' && @options.cdata && requiresCDATA child
if typeof child is 'string' && (@options.forceCdata || (@options.cdata && requiresCDATA child))
element = element.ele(key).raw(wrapCDATA child).up()
else
if not child?
Expand Down
1 change: 1 addition & 0 deletions src/defaults.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ exports.defaults = {
chunkSize: 10000
emptyTag: ''
cdata: false
forceCdata: false
}
15 changes: 15 additions & 0 deletions test/builder.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,18 @@ module.exports =
actual = builder.buildObject obj
diffeq expected, actual
test.finish()

'test always uses cdata for string values with forceCdata enabled': (test) ->
expected = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<MsgId><![CDATA[hello world]]></MsgId>
</xml>

"""
opts = forceCdata: true
builder = new xml2js.Builder opts
obj = {"xml":{"MsgId":"hello world"}}
actual = builder.buildObject obj
diffeq expected, actual
test.finish()