Skip to content

Commit

Permalink
Added the begin() export function.
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Mar 24, 2016
1 parent 0013c04 commit 26da624
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/XMLDocType.coffee
Expand Up @@ -111,5 +111,5 @@ module.exports = class XMLDocType extends XMLNode
ent: (name, value) -> @entity name, value
pent: (name, value) -> @pEntity name, value
not: (name, value) -> @notation name, value
up: () -> @root()
up: () -> @root() or @documentObject

29 changes: 1 addition & 28 deletions src/XMLDocument.coffee
Expand Up @@ -9,19 +9,7 @@ module.exports = class XMLDocument extends XMLNode


# Initializes a new instance of `XMLDocument`
# and creates the XML prolog
#
# `name` name of the root element
#
# `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
#
# `options.pubID` public identifier of the external subset
# `options.sysID` system identifier of the external subset
#
# `options.headless` whether XML declaration and doctype will be included:
# true or false
# `options.allowSurrogateChars` whether surrogates will be allowed: true or
# false
# `options.skipNullAttributes` whether attributes with null values will be
Expand All @@ -38,12 +26,9 @@ module.exports = class XMLDocument extends XMLNode
# `options.writer` the default XML writer to use for converting nodes to
# string. If the default writer is not set, the built-in XMLStringWriter
# will be used instead.
constructor: (name, options) ->
constructor: (options) ->
super null

if not name?
throw new Error "Root element needs a name"

options ?= {}
if not options.writer then options.writer = new XMLStringWriter()

Expand All @@ -52,18 +37,6 @@ module.exports = class XMLDocument extends XMLNode

@isDocument = true

root = @element name
root.isRoot = true
root.documentObject = @
@rootObject = root

# prolog
if not options.headless
@declaration options

if options.pubID? or options.sysID?
@doctype options


# Ends the document and passes it to the given XML writer
#
Expand Down
6 changes: 6 additions & 0 deletions src/XMLElement.coffee
Expand Up @@ -24,6 +24,12 @@ module.exports = class XMLElement extends XMLNode

@attribute attributes if attributes?

# set properties if this is the root node
if parent.isDocument
@isRoot = true
@documentObject = parent
parent.rootObject = @


# Creates and returns a deep clone of `this`
#
Expand Down
2 changes: 1 addition & 1 deletion src/XMLNode.coffee
Expand Up @@ -335,7 +335,7 @@ module.exports = class XMLNode
else
doc.children.unshift xmldec

return doc.root()
return doc.root() or doc


# Creates the document type declaration
Expand Down
39 changes: 38 additions & 1 deletion src/index.coffee
Expand Up @@ -33,8 +33,45 @@ XMLStreamWriter = require './XMLStreamWriter'
# string. If the default writer is not set, the built-in XMLStringWriter
# will be used instead.
module.exports.create = (name, xmldec, doctype, options) ->
if not name?
throw new Error "Root element needs a name"

options = Object.assign { }, xmldec, doctype, options
new XMLDocument(name, options).root()

# create the document node
doc = new XMLDocument(options)
# add the root node
root = doc.element name

# prolog
if not options.headless
doc.declaration options

if options.pubID? or options.sysID?
doc.doctype options

return root

# Creates a new document and returns the document node for
#
# `options.allowSurrogateChars` whether surrogates will be allowed: true or
# false
# `options.skipNullAttributes` whether attributes with null values will be
# ignored: true or false
# `options.ignoreDecorators` whether decorator strings will be ignored when
# converting JS objects: true or false
# `options.separateArrayItems` whether array items are created as separate
# nodes when passed as an object value: true or false
# `options.noDoubleEncoding` whether existing html entities are encoded:
# true or false
# `options.stringify` a set of functions to use for converting values to
# strings
#
# `options.writer` the default XML writer to use for converting nodes to
# string. If the default writer is not set, the built-in XMLStringWriter
# will be used instead.
module.exports.begin = (options) ->
new XMLDocument(options)

module.exports.stringWriter = (options) ->
new XMLStringWriter(options)
Expand Down
12 changes: 12 additions & 0 deletions test/basic/begin.coffee
@@ -0,0 +1,12 @@
suite 'Creating XML:', ->
test 'begin()', ->
eq(
doc({ headless: true }).ele('root', { att: 'val' }).ele('test').end()
'<root att="val"><test/></root>'
)

test 'begin() with prolog', ->
eq(
doc().dec().dtd().up().ele('root').end()
'<?xml version="1.0"?><!DOCTYPE root><root/>'
)
1 change: 1 addition & 0 deletions test/common.coffee
@@ -1,5 +1,6 @@
global.builder = require('../src/index.coffee')
global.xml = builder.create
global.doc = builder.begin
global.writer = builder.stringWriter
global.eq = require('assert').strictEqual
global.err = require('assert').throws
Expand Down

0 comments on commit 26da624

Please sign in to comment.