Skip to content
Browse files

Added the declaration() method

  • Loading branch information...
1 parent 0342ed7 commit f63ca6e02284e30e05133b9a4201332fb3039797 @oozcitak committed Dec 24, 2013
Showing with 89 additions and 22 deletions.
  1. +8 −8 src/XMLBuilder.coffee
  2. +15 −11 src/XMLDeclaration.coffee
  3. +4 −0 src/XMLDocType.coffee
  4. +19 −2 src/XMLNode.coffee
  5. +1 −1 test/createxml.coffee
  6. +42 −0 test/xmldeclaration.coffee
View
16 src/XMLBuilder.coffee
@@ -31,18 +31,18 @@ module.exports = class XMLBuilder
options ?= {}
@stringify = new XMLStringifier options
+ temp = new XMLElement @, 'doc' # temporary node so that we can call element()
+ root = temp.element name
+ root.isRoot = true
+ root.documentObject = @
+ @rootObject = root
+
# prolog
if not options.headless
- @xmldec = new XMLDeclaration @, options
+ root.declaration options
if options.pubID? or options.sysID?
- @doctype = new XMLDocType @, options.pubID, options.sysID
-
- root = new XMLElement @, 'doc'
- root = root.element name
- root.isRoot = true
- root.documentObject = @
- @rootObject = root
+ root.doctype options.pubID, options.sysID
# Gets the root node
View
26 src/XMLDeclaration.coffee
@@ -10,22 +10,26 @@ module.exports = class XMLDeclaration extends XMLNode
#
# `parent` the document object
#
- # `options.version` A version number string, e.g. 1.0
- # `options.encoding` Encoding declaration, e.g. UTF-8
- # `options.standalone` standalone document declaration: true or false
- constructor: (parent, options) ->
+ # `version` A version number string, e.g. 1.0
+ # `encoding` Encoding declaration, e.g. UTF-8
+ # `standalone` standalone document declaration: true or false
+ constructor: (parent, version, encoding, standalone) ->
super parent
- options = _.extend { 'version': '1.0' }, options
+ # arguments may also be passed as an object
+ if _.isObject version
+ { version, encoding, standalone } = version
- if options.version?
- @version = @stringify.xmlVersion options.version
+ version = '1.0' if not version
- if options.encoding?
- @encoding = @stringify.xmlEncoding options.encoding
+ if version?
+ @version = @stringify.xmlVersion version
- if options.standalone?
- @standalone = @stringify.xmlStandalone options.standalone
+ if encoding?
+ @encoding = @stringify.xmlEncoding encoding
+
+ if standalone?
+ @standalone = @stringify.xmlStandalone standalone
# Converts to string
View
4 src/XMLDocType.coffee
@@ -16,6 +16,10 @@ module.exports = class XMLDocType
@children = []
+ # arguments may also be passed as an object
+ if _.isObject pubID
+ { pubID, sysID } = pubID
+
if not sysID?
[sysID, pubID] = [pubID, sysID]
View
21 src/XMLNode.coffee
@@ -193,16 +193,32 @@ module.exports = class XMLNode
@children.push child
return @
+
+ # Creates the xml declaration
+ #
+ # `version` A version number string, e.g. 1.0
+ # `encoding` Encoding declaration, e.g. UTF-8
+ # `standalone` standalone document declaration: true or false
+ declaration: (version, encoding, standalone) ->
+ doc = @document()
+ XMLDeclaration = require './XMLDeclaration'
+ xmldec = new XMLDeclaration doc, version, encoding, standalone
+ doc.xmldec = xmldec
+ return doc.root()
+
+
# Creates the document type declaration
#
# `pubID` the public identifier of the external subset
# `sysID` the system identifier of the external subset
doctype: (pubID, sysID) ->
+ doc = @document()
XMLDocType = require './XMLDocType'
- doctype = new XMLDocType @, pubID, sysID
- @document().doctype = doctype
+ doctype = new XMLDocType doc, pubID, sysID
+ doc.doctype = doctype
return doctype
+
# Gets the parent node
up: () ->
if @isRoot
@@ -277,6 +293,7 @@ module.exports = class XMLNode
dat: (value) -> @cdata value
com: (value) -> @comment value
doc: () -> @document()
+ dec: (version, encoding, standalone) -> @declaration version, encoding, standalone
dtd: (pubID, sysID) -> @doctype pubID, sysID
e: (name, attributes, text) -> @element name, attributes, text
n: (name, attributes, text) -> @node name, attributes, text
View
2 test/createxml.coffee
@@ -158,7 +158,7 @@ vows
'</root>'
assert.strictEqual topic.end(), xml
- 'create() without with arguments':
+ 'create() without arguments':
topic: () ->
xmlbuilder.create('test14').ele('node').txt('test')
View
42 test/xmldeclaration.coffee
@@ -0,0 +1,42 @@
+vows = require 'vows'
+assert = require 'assert'
+
+xmlbuilder = require '../src/index.coffee'
+
+vows
+ .describe('XML Declaration')
+ .addBatch
+ 'From create() without arguments':
+ topic: () ->
+ xmlbuilder.create('test')
+
+ 'resulting XML': (topic) ->
+ xml = '<?xml version="1.0"?><test/>'
+ assert.strictEqual topic.end(), xml
+
+ 'From create() with arguments':
+ topic: () ->
+ xmlbuilder.create('test', { version: '1.1', encoding: 'UTF-8', standalone: true })
+
+ 'resulting XML': (topic) ->
+ xml = '<?xml version="1.1" encoding="UTF-8" standalone="yes"?><test/>'
+ assert.strictEqual topic.end(), xml
+
+ 'From dec() without arguments':
+ topic: () ->
+ xmlbuilder.create('test', { headless: true }).dec().ele('node')
+
+ 'resulting XML': (topic) ->
+ xml = '<?xml version="1.0"?><test><node/></test>'
+ assert.strictEqual topic.end(), xml
+
+ 'From dec() with arguments':
+ topic: () ->
+ xmlbuilder.create('test').dec({ version: '1.1', encoding: 'UTF-8', standalone: true }).ele('node')
+
+ 'resulting XML': (topic) ->
+ xml = '<?xml version="1.1" encoding="UTF-8" standalone="yes"?><test><node/></test>'
+ assert.strictEqual topic.end(), xml
+
+ .export(module)
+

0 comments on commit f63ca6e

Please sign in to comment.
Something went wrong with that request. Please try again.