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 support for 'always' cdata builder option #449

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.swp
.idea
node_modules
npm-debug.log
.nyc_output
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,10 @@ Possible options are:
* `headless` (default: `false`): omit the XML header. Added in 0.4.3.
* `allowSurrogateChars` (default: `false`): allows using characters from the Unicode
surrogate blocks.
* `cdata` (default: `false`): wrap text nodes in `<![CDATA[ ... ]]>` instead of
escaping when necessary. Does not add `<![CDATA[ ... ]]>` if it is not required.
* `cdata` (default: `false`):
* When `true` wrap text nodes in `<![CDATA[ ... ]]>` instead of escaping when necessary. Does not add `<![CDATA[ ... ]]>` if it is not required.
Added in 0.4.5.
* When `'always'` all inner text wrapped in `<![CDATA[ ... ]]>` regardless of content.

`renderOpts`, `xmldec`,`doctype` and `headless` pass through to
[xmlbuilder-js](https://github.com/oozcitak/xmlbuilder-js).
Expand Down
8 changes: 4 additions & 4 deletions lib/builder.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.cdata is 'always' or (@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.cdata is 'always' or (@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.cdata is 'always' or (@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.cdata is 'always' or (@options.cdata && requiresCDATA child)
element = element.ele(key).raw(wrapCDATA child).up()
else
if not child?
Expand Down
2 changes: 1 addition & 1 deletion src/parser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class exports.Parser extends events.EventEmitter
nodeName = obj["#name"]
delete obj["#name"] if not @options.explicitChildren or not @options.preserveChildrenOrder

if obj.cdata == true
if obj.cdata is 'always' or obj.cdata == true
cdata = obj.cdata
delete obj.cdata

Expand Down
16 changes: 16 additions & 0 deletions test/builder.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,22 @@ module.exports =
diffeq expected, actual
test.finish()

"test uses cdata for all chars when cdata is 'always'": (test) ->
expected = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<MsgId><![CDATA[& <<]]></MsgId>
<Message><![CDATA[Hello]]></Message>
</xml>

"""
opts = cdata: 'always'
builder = new xml2js.Builder opts
obj = {"xml":{"MsgId":["& <<"],"Message":["Hello"]}}
actual = builder.buildObject obj
diffeq expected, actual
test.finish()

'test uses cdata for string values of objects': (test) ->
expected = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
Expand Down