Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call require() once only #62

Merged
merged 2 commits into from
Nov 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/XMLDocType.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
create = require 'lodash-node/modern/objects/create'
isObject = require 'lodash-node/modern/objects/isObject'

XMLCData = require './XMLCData'
XMLComment = require './XMLComment'
XMLDTDAttList = require './XMLDTDAttList'
XMLDTDEntity = require './XMLDTDEntity'
XMLDTDElement = require './XMLDTDElement'
XMLDTDNotation = require './XMLDTDNotation'
XMLProcessingInstruction = require './XMLProcessingInstruction'

# Represents doctype declaration
module.exports = class XMLDocType

Expand Down Expand Up @@ -38,7 +46,6 @@ module.exports = class XMLDocType
# `name` element name
# `value` element content (defaults to #PCDATA)
element: (name, value) ->
XMLDTDElement = require './XMLDTDElement'
child = new XMLDTDElement @, name, value
@children.push child
return @
Expand All @@ -54,7 +61,6 @@ module.exports = class XMLDocType
# `defaultValue` default value of the attribute
# (only used for #FIXED or #DEFAULT)
attList: (elementName, attributeName, attributeType, defaultValueType, defaultValue) ->
XMLDTDAttList = require './XMLDTDAttList'
child = new XMLDTDAttList @, elementName, attributeName, attributeType, defaultValueType, defaultValue
@children.push child
return @
Expand All @@ -68,7 +74,6 @@ module.exports = class XMLDocType
# `value.sysID` system identifier
# `value.nData` notation declaration
entity: (name, value) ->
XMLDTDEntity = require './XMLDTDEntity'
child = new XMLDTDEntity @, false, name, value
@children.push child
return @
Expand All @@ -81,7 +86,6 @@ module.exports = class XMLDocType
# `value.pubID` public identifier
# `value.sysID` system identifier
pEntity: (name, value) ->
XMLDTDEntity = require './XMLDTDEntity'
child = new XMLDTDEntity @, true, name, value
@children.push child
return @
Expand All @@ -94,7 +98,6 @@ module.exports = class XMLDocType
# `value.pubID` public identifier
# `value.sysID` system identifier
notation: (name, value) ->
XMLDTDNotation = require './XMLDTDNotation'
child = new XMLDTDNotation @, name, value
@children.push child
return @
Expand All @@ -104,7 +107,6 @@ module.exports = class XMLDocType
#
# `value` element text without CDATA delimiters
cdata: (value) ->
XMLCData = require './XMLCData'
child = new XMLCData @, value
@children.push child
return @
Expand All @@ -114,7 +116,6 @@ module.exports = class XMLDocType
#
# `value` comment text
comment: (value) ->
XMLComment = require './XMLComment'
child = new XMLComment @, value
@children.push child
return @
Expand All @@ -125,7 +126,6 @@ module.exports = class XMLDocType
# `target` instruction target
# `value` instruction value
instruction: (target, value) ->
XMLProcessingInstruction = require './XMLProcessingInstruction'
child = new XMLProcessingInstruction @, target, value
@children.push child
return @
Expand Down
26 changes: 19 additions & 7 deletions src/XMLNode.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ isArray = require 'lodash-node/modern/objects/isArray'
isFunction = require 'lodash-node/modern/objects/isFunction'
isEmpty = require 'lodash-node/modern/objects/isEmpty'

XMLElement = null
XMLCData = null
XMLComment = null
XMLDeclaration = null
XMLDocType = null
XMLRaw = null
XMLText = null

# Represents a generic XMl element
module.exports = class XMLNode

Expand All @@ -14,6 +22,17 @@ module.exports = class XMLNode
@options = @parent.options
@stringify = @parent.stringify

# first execution, load dependencies that are otherwise
# circular (so we can't load them at the top)
if XMLElement is null
XMLElement = require './XMLElement'
XMLCData = require './XMLCData'
XMLComment = require './XMLComment'
XMLDeclaration = require './XMLDeclaration'
XMLDocType = require './XMLDocType'
XMLRaw = require './XMLRaw'
XMLText = require './XMLText'


# Creates and returns a deep clone of `this`
clone: () ->
Expand Down Expand Up @@ -167,7 +186,6 @@ module.exports = class XMLNode
if not isObject attributes
[text, attributes] = [attributes, text]

XMLElement = require './XMLElement'
child = new XMLElement @, name, attributes
child.text(text) if text?
@children.push child
Expand All @@ -178,7 +196,6 @@ module.exports = class XMLNode
#
# `value` element text
text: (value) ->
XMLText = require './XMLText'
child = new XMLText @, value
@children.push child
return @
Expand All @@ -188,7 +205,6 @@ module.exports = class XMLNode
#
# `value` element text without CDATA delimiters
cdata: (value) ->
XMLCData = require './XMLCData'
child = new XMLCData @, value
@children.push child
return @
Expand All @@ -198,7 +214,6 @@ module.exports = class XMLNode
#
# `value` comment text
comment: (value) ->
XMLComment = require './XMLComment'
child = new XMLComment @, value
@children.push child
return @
Expand All @@ -208,7 +223,6 @@ module.exports = class XMLNode
#
# `value` text
raw: (value) ->
XMLRaw = require './XMLRaw'
child = new XMLRaw @, value
@children.push child
return @
Expand All @@ -221,7 +235,6 @@ module.exports = class XMLNode
# `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()
Expand All @@ -233,7 +246,6 @@ module.exports = class XMLNode
# `sysID` the system identifier of the external subset
doctype: (pubID, sysID) ->
doc = @document()
XMLDocType = require './XMLDocType'
doctype = new XMLDocType doc, pubID, sysID
doc.doctype = doctype
return doctype
Expand Down
2 changes: 1 addition & 1 deletion src/XMLStringifier.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = class XMLStringifier
xmlEncoding: (val) ->
val = '' + val or ''
if not val.match /[A-Za-z](?:[A-Za-z0-9._-]|-)*/
throw new Error "Invalid encoding: " + options.val
throw new Error "Invalid encoding: " + val
val
xmlStandalone: (val) ->
if val then "yes" else "no"
Expand Down