Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Soft deprecate begin() method

The begin() method will be deprecated in the future. The intented
usage to start a new document is via the XMLBuilder constructor
which is exposed as the create() method from the module.
  • Loading branch information...
commit 6da75531a19f4c58488abe5e42b25e07a8c67f8a 1 parent 5235ed1
@oozcitak authored
Showing with 46 additions and 18 deletions.
  1. +41 −18 src/XMLBuilder.coffee
  2. +5 −0 test/test.coffee
View
59 src/XMLBuilder.coffee
@@ -18,25 +18,14 @@ class XMLBuilder
@children = []
@rootObject = null
+ # shift arguments
+ if @is(name, 'Object')
+ [xmldec, doctype] = [name, xmldec]
+ name = null
+
if name?
+ name = '' + name or ''
xmldec ?= { 'version': '1.0' }
- @begin name, xmldec, doctype
-
-
- # Creates the XML prolog and the root element
- #
- # `name` name of the root element
- #
- # `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.ext` the external subset containing markup declarations
- begin: (name, xmldec, doctype) ->
- if not name?
- throw new Error "Root element needs a name"
- @children = []
- name = '' + name or ''
if xmldec? and not xmldec.version?
throw new Error "Version number is required"
@@ -60,7 +49,9 @@ class XMLBuilder
@children.push child
if doctype?
- att = { name: name }
+ att = {}
+ if name?
+ att.name = name
if doctype.ext?
doctype.ext = '' + doctype.ext or ''
@@ -69,6 +60,29 @@ class XMLBuilder
child = new XMLFragment @, '!DOCTYPE', att
@children.push child
+ if name?
+ @begin name
+
+
+ # Creates the root element
+ #
+ # `name` name of the root element
+ begin: (name, xmldec, doctype) ->
+ if not name?
+ throw new Error "Root element needs a name"
+
+ if @rootObject
+ # Erase old instance
+ @children = []
+ @rootObject = null
+
+ if xmldec?
+ # This will be deprecated in the future. XML prolog should be
+ #supplied to the constructor
+ doc = new XMLBuilder name, xmldec, doctype
+ return doc.root()
+
+ name = '' + name or ''
root = new XMLFragment @, name, {}
root.isRoot = true
root.documentObject = @
@@ -96,5 +110,14 @@ class XMLBuilder
return r
+ # Checks whether the given object is of the given type
+ #
+ # `obj` the object to check
+ # `type` the type to compare to. (String, Number, Object, Date, ...)
+ is: (obj, type) ->
+ clas = Object.prototype.toString.call(obj).slice(8, -1)
+ return obj? and clas is type
+
+
module.exports = XMLBuilder
View
5 test/test.coffee
@@ -209,3 +209,8 @@ xml13 = '<?xml version="1.0"?><test13><node>test</node></test13>'
test13 = xmlbuilder.create('test13').ele('node').txt('test').doc().toString()
assert.strictEqual(xml13, test13)
+# Test the create() method with arguments
+xml14 = '<?xml version="1.1"?><test14><node>test</node></test14>'
+test14 = xmlbuilder.create('test14', { 'version': '1.1' } ).ele('node').txt('test').doc().toString()
+assert.strictEqual(xml14, test14)
+
Please sign in to comment.
Something went wrong with that request. Please try again.