Skip to content

Commit f63ca6e

Browse files
committed
Added the declaration() method
1 parent 0342ed7 commit f63ca6e

6 files changed

+89
-22
lines changed

src/XMLBuilder.coffee

+8-8
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ module.exports = class XMLBuilder
3131
options ?= {}
3232
@stringify = new XMLStringifier options
3333

34+
temp = new XMLElement @, 'doc' # temporary node so that we can call element()
35+
root = temp.element name
36+
root.isRoot = true
37+
root.documentObject = @
38+
@rootObject = root
39+
3440
# prolog
3541
if not options.headless
36-
@xmldec = new XMLDeclaration @, options
42+
root.declaration options
3743

3844
if options.pubID? or options.sysID?
39-
@doctype = new XMLDocType @, options.pubID, options.sysID
40-
41-
root = new XMLElement @, 'doc'
42-
root = root.element name
43-
root.isRoot = true
44-
root.documentObject = @
45-
@rootObject = root
45+
root.doctype options.pubID, options.sysID
4646

4747

4848
# Gets the root node

src/XMLDeclaration.coffee

+15-11
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,26 @@ module.exports = class XMLDeclaration extends XMLNode
1010
#
1111
# `parent` the document object
1212
#
13-
# `options.version` A version number string, e.g. 1.0
14-
# `options.encoding` Encoding declaration, e.g. UTF-8
15-
# `options.standalone` standalone document declaration: true or false
16-
constructor: (parent, options) ->
13+
# `version` A version number string, e.g. 1.0
14+
# `encoding` Encoding declaration, e.g. UTF-8
15+
# `standalone` standalone document declaration: true or false
16+
constructor: (parent, version, encoding, standalone) ->
1717
super parent
1818

19-
options = _.extend { 'version': '1.0' }, options
19+
# arguments may also be passed as an object
20+
if _.isObject version
21+
{ version, encoding, standalone } = version
2022

21-
if options.version?
22-
@version = @stringify.xmlVersion options.version
23+
version = '1.0' if not version
2324

24-
if options.encoding?
25-
@encoding = @stringify.xmlEncoding options.encoding
25+
if version?
26+
@version = @stringify.xmlVersion version
2627

27-
if options.standalone?
28-
@standalone = @stringify.xmlStandalone options.standalone
28+
if encoding?
29+
@encoding = @stringify.xmlEncoding encoding
30+
31+
if standalone?
32+
@standalone = @stringify.xmlStandalone standalone
2933

3034

3135
# Converts to string

src/XMLDocType.coffee

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ module.exports = class XMLDocType
1616

1717
@children = []
1818

19+
# arguments may also be passed as an object
20+
if _.isObject pubID
21+
{ pubID, sysID } = pubID
22+
1923
if not sysID?
2024
[sysID, pubID] = [pubID, sysID]
2125

src/XMLNode.coffee

+19-2
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,32 @@ module.exports = class XMLNode
193193
@children.push child
194194
return @
195195

196+
197+
# Creates the xml declaration
198+
#
199+
# `version` A version number string, e.g. 1.0
200+
# `encoding` Encoding declaration, e.g. UTF-8
201+
# `standalone` standalone document declaration: true or false
202+
declaration: (version, encoding, standalone) ->
203+
doc = @document()
204+
XMLDeclaration = require './XMLDeclaration'
205+
xmldec = new XMLDeclaration doc, version, encoding, standalone
206+
doc.xmldec = xmldec
207+
return doc.root()
208+
209+
196210
# Creates the document type declaration
197211
#
198212
# `pubID` the public identifier of the external subset
199213
# `sysID` the system identifier of the external subset
200214
doctype: (pubID, sysID) ->
215+
doc = @document()
201216
XMLDocType = require './XMLDocType'
202-
doctype = new XMLDocType @, pubID, sysID
203-
@document().doctype = doctype
217+
doctype = new XMLDocType doc, pubID, sysID
218+
doc.doctype = doctype
204219
return doctype
205220

221+
206222
# Gets the parent node
207223
up: () ->
208224
if @isRoot
@@ -277,6 +293,7 @@ module.exports = class XMLNode
277293
dat: (value) -> @cdata value
278294
com: (value) -> @comment value
279295
doc: () -> @document()
296+
dec: (version, encoding, standalone) -> @declaration version, encoding, standalone
280297
dtd: (pubID, sysID) -> @doctype pubID, sysID
281298
e: (name, attributes, text) -> @element name, attributes, text
282299
n: (name, attributes, text) -> @node name, attributes, text

test/createxml.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ vows
158158
'</root>'
159159
assert.strictEqual topic.end(), xml
160160

161-
'create() without with arguments':
161+
'create() without arguments':
162162
topic: () ->
163163
xmlbuilder.create('test14').ele('node').txt('test')
164164

test/xmldeclaration.coffee

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
vows = require 'vows'
2+
assert = require 'assert'
3+
4+
xmlbuilder = require '../src/index.coffee'
5+
6+
vows
7+
.describe('XML Declaration')
8+
.addBatch
9+
'From create() without arguments':
10+
topic: () ->
11+
xmlbuilder.create('test')
12+
13+
'resulting XML': (topic) ->
14+
xml = '<?xml version="1.0"?><test/>'
15+
assert.strictEqual topic.end(), xml
16+
17+
'From create() with arguments':
18+
topic: () ->
19+
xmlbuilder.create('test', { version: '1.1', encoding: 'UTF-8', standalone: true })
20+
21+
'resulting XML': (topic) ->
22+
xml = '<?xml version="1.1" encoding="UTF-8" standalone="yes"?><test/>'
23+
assert.strictEqual topic.end(), xml
24+
25+
'From dec() without arguments':
26+
topic: () ->
27+
xmlbuilder.create('test', { headless: true }).dec().ele('node')
28+
29+
'resulting XML': (topic) ->
30+
xml = '<?xml version="1.0"?><test><node/></test>'
31+
assert.strictEqual topic.end(), xml
32+
33+
'From dec() with arguments':
34+
topic: () ->
35+
xmlbuilder.create('test').dec({ version: '1.1', encoding: 'UTF-8', standalone: true }).ele('node')
36+
37+
'resulting XML': (topic) ->
38+
xml = '<?xml version="1.1" encoding="UTF-8" standalone="yes"?><test><node/></test>'
39+
assert.strictEqual topic.end(), xml
40+
41+
.export(module)
42+

0 commit comments

Comments
 (0)