Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

exposing renderOpts and fixing a few tests

  • Loading branch information...
commit 3f9f626e5b23a56418af9048c332ac9eebf0d0f8 1 parent f61b652
Jack Senechal jacksenechal authored
11 README.md
View
@@ -223,12 +223,19 @@ Options for the `Builder` class
* `rootName` (default `root`): root element name to be used in case
`explicitiRoot` is `false` or to override the root element name.
- * `pretty` (default `true`): prettify generated xml.
- * `xmldec` (default `{'version': '1.0', 'encoding': 'UTF-8', 'standalone': true}`:
+ * `renderOpts` (default `{ 'pretty': true, 'indent': ' ', 'newline': '\n' }`):
+ Rendering options for xmlbuilder-js.
+ * pretty: prettify generated XML
+ * indent: whitespace for indentation (only when pretty)
+ * newline: newline char (only when pretty)
+ * `xmldec` (default `{ 'version': '1.0', 'encoding': 'UTF-8', 'standalone': true }`:
XML declaration attributes.
* `xmldec.version` A version number string, e.g. 1.0
* `xmldec.encoding` Encoding declaration, e.g. UTF-8
* `xmldec.standalone` standalone document declaration: true or false
+ * `doctype` (default `null`): optional DTD. Eg. `{'ext': 'hello.dtd'}`
+
+renderOpts, xmldec and doctype pass through to [xmlbuilder-js](https://github.com/oozcitak/xmlbuilder-js)
Updating to new version
=======================
12 lib/xml2js.js
View
@@ -56,13 +56,17 @@
async: false,
strict: true,
rootName: 'root',
- pretty: true,
xmldec: {
'version': '1.0',
'encoding': 'UTF-8',
'standalone': true
},
- doctype: null
+ doctype: null,
+ renderOpts: {
+ 'pretty': true,
+ 'indent': ' ',
+ 'newline': '\n'
+ }
}
};
@@ -143,9 +147,7 @@
return element;
};
rootElement = builder.create(rootName, this.options.xmldec, this.options.doctype);
- return render(rootElement, rootObj).end({
- pretty: this.options.pretty
- });
+ return render(rootElement, rootObj).end(this.options.renderOpts);
};
return Builder;
4 src/xml2js.coffee
View
@@ -58,9 +58,9 @@ exports.defaults =
strict: true
# xml building options
rootName: 'root'
- pretty: true
xmldec: {'version': '1.0', 'encoding': 'UTF-8', 'standalone': true}
doctype: null
+ renderOpts: { 'pretty': true, 'indent': ' ', 'newline': '\n' }
class exports.ValidationError extends Error
constructor: (message) ->
@@ -125,7 +125,7 @@ class exports.Builder
rootElement = builder.create(rootName, @options.xmldec, @options.doctype)
- render(rootElement, rootObj).end(pretty: @options.pretty)
+ render(rootElement, rootObj).end(@options.renderOpts)
class exports.Parser extends events.EventEmitter
constructor: (opts) ->
47 test/builder.test.coffee
View
@@ -9,6 +9,8 @@ diff = require 'diff'
# shortcut, because it is quite verbose
equ = assert.equal
+
+# equality test with diff output
diffeq = (expected, actual) ->
diffless = "Index: test\n===================================================================\n--- test\texpected\n+++ test\tactual\n"
patch = diff.createPatch('test', expected, actual, 'expected', 'actual')
@@ -18,20 +20,20 @@ module.exports =
'test building basic XML structure': (test) ->
expected = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><xml><Label></Label><MsgId>5850440872586764820</MsgId></xml>'
obj = {"xml":{"Label":[""],"MsgId":["5850440872586764820"]}}
- builder = new xml2js.Builder({pretty: false})
+ builder = new xml2js.Builder(renderOpts: { pretty: false })
actual = builder.buildObject obj
- diffeq actual, expected
+ diffeq expected, actual
test.finish()
'test setting XML declaration': (test) ->
- expected = '<?xml version="1.0" encoding="WTF-8" standalone="no"?>'
+ expected = '<?xml version="1.2" encoding="WTF-8" standalone="no"?><root/>'
opts = {
- pretty: false
- xmldec: {'version': '1.0', 'encoding': 'WTF-8', 'standalone': false}
+ renderOpts: { pretty: false }
+ xmldec: {'version': '1.2', 'encoding': 'WTF-8', 'standalone': false}
}
builder = new xml2js.Builder(opts)
actual = builder.buildObject {}
- diffeq actual, expected
+ diffeq expected, actual
test.finish()
'test pretty by default': (test) ->
@@ -45,43 +47,58 @@ module.exports =
builder = new xml2js.Builder()
obj = {"xml":{"MsgId":["5850440872586764820"]}}
actual = builder.buildObject obj
- diffeq actual, expected
+ diffeq expected, actual
+ test.finish()
+
+ 'test setting indentation': (test) ->
+ expected = """
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+ <xml>
+ <MsgId>5850440872586764820</MsgId>
+ </xml>
+
+ """
+ opts = { renderOpts: { pretty: true, indent: ' ' } }
+ builder = new xml2js.Builder(opts)
+ obj = {"xml":{"MsgId":["5850440872586764820"]}}
+ actual = builder.buildObject obj
+ diffeq expected, actual
test.finish()
'test explicit rootName is always used: 1. when there is only one element': (test) ->
expected = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><FOO><MsgId>5850440872586764820</MsgId></FOO>'
- opts = { pretty: false, rootName: 'FOO' }
+ opts = { renderOpts: { pretty: false }, rootName: 'FOO' }
builder = new xml2js.Builder(opts)
obj = {"MsgId":["5850440872586764820"]}
actual = builder.buildObject obj
- diffeq actual, expected
+ diffeq expected, actual
test.finish()
'test explicit rootName is always used: 2. when there are multiple elements': (test) ->
expected = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><FOO><MsgId>5850440872586764820</MsgId></FOO>'
- opts = { pretty: false, rootName: 'FOO' }
+ opts = { renderOpts: { pretty: false }, rootName: 'FOO' }
builder = new xml2js.Builder(opts)
obj = {"MsgId":["5850440872586764820"]}
actual = builder.buildObject obj
- diffeq actual, expected
+ diffeq expected, actual
test.finish()
'test default rootName is used when there is more than one element in the hash': (test) ->
expected = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><root><MsgId>5850440872586764820</MsgId><foo>bar</foo></root>'
- opts = { pretty: false }
+ opts = { renderOpts: { pretty: false } }
builder = new xml2js.Builder(opts)
obj = {"MsgId":["5850440872586764820"],"foo":"bar"}
actual = builder.buildObject obj
- diffeq actual, expected
+ diffeq expected, actual
test.finish()
'test when there is only one first-level element in the hash, that is used as root': (test) ->
expected = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><first><MsgId>5850440872586764820</MsgId><foo>bar</foo></first>'
- opts = { pretty: false }
+ opts = { renderOpts: { pretty: false } }
builder = new xml2js.Builder(opts)
obj = {"first":{"MsgId":["5850440872586764820"],"foo":"bar"}}
actual = builder.buildObject obj
- diffeq actual, expected
+ diffeq expected, actual
test.finish()
'test parser -> builder roundtrip': (test) ->
Please sign in to comment.
Something went wrong with that request. Please try again.