Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds arguments to XMLBuilder constructor

Allows the name of the root element and XML prolog to be defined
in one line with the XMLBuilder constructor.
  • Loading branch information...
commit 5235ed16e1682333a6875133220a035ed46bd931 1 parent 31dd510
Ozgur Ozcitak authored
Showing with 30 additions and 4 deletions.
  1. +15 −2 src/XMLBuilder.coffee
  2. +10 −2 src/index.coffee
  3. +5 −0 test/test.coffee
17 src/XMLBuilder.coffee
View
@@ -5,12 +5,25 @@ class XMLBuilder
# Initializes a new instance of `XMLBuilder`
- constructor: () ->
+ # and creates the XML prolog
+ #
+ # `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
+ constructor: (name, xmldec, doctype) ->
@children = []
@rootObject = null
+ if name?
+ xmldec ?= { 'version': '1.0' }
+ @begin name, xmldec, doctype
+
- # Creates the XML prolog
+ # Creates the XML prolog and the root element
#
# `name` name of the root element
#
12 src/index.coffee
View
@@ -1,5 +1,13 @@
XMLBuilder = require './XMLBuilder'
-module.exports.create = ->
- new XMLBuilder()
+module.exports.create = (name, xmldec, doctype) ->
+ if name?
+ # Create a new document and return the root node for
+ # chain-building the document tree
+ new XMLBuilder(name, xmldec, doctype).root()
+ else
+ # This path allows for documents without an xml prolog
+ # It is being kept for compatibility with earlier versions
+ # but should be deprecated in the future
+ new XMLBuilder()
5 test/test.coffee
View
@@ -204,3 +204,8 @@ test12 = builder.begin('test12')
.doc().toString()
assert.strictEqual(xml12, test12)
+# Test the create() method with arguments
+xml13 = '<?xml version="1.0"?><test13><node>test</node></test13>'
+test13 = xmlbuilder.create('test13').ele('node').txt('test').doc().toString()
+assert.strictEqual(xml13, test13)
+
Please sign in to comment.
Something went wrong with that request. Please try again.