Permalink
Browse files

XMLBuilder no longer extends XMLFragment. Why did it in the first place?

  • Loading branch information...
1 parent 95741c6 commit 3ede09d0082ecbed046b267dce5437efb72b840a @oozcitak committed Nov 29, 2011
Showing with 17 additions and 19 deletions.
  1. +10 −3 src/XMLBuilder.coffee
  2. +7 −16 src/XMLFragment.coffee
View
@@ -1,13 +1,13 @@
XMLFragment = require './XMLFragment'
# Represents an XML builder
-class XMLBuilder extends XMLFragment
+class XMLBuilder
# Initializes a new instance of `XMLBuilder`
constructor: () ->
- super null, '', {}, ''
- @isDoc = true
+ @children = []
+ @rootObject = null
# Creates the XML prolog
@@ -58,11 +58,18 @@ class XMLBuilder extends XMLFragment
root = new XMLFragment @, name, {}
root.isRoot = true
+ root.documentObject = @
@children.push root
+ @rootObject = root
return root
+ # Gets the root node
+ root: () ->
+ return @rootObject
+
+
# Converts the XML document to string
#
# `options.pretty` pretty prints the result
View
@@ -9,8 +9,8 @@ class XMLFragment
# `attributes` an object containing name/value pairs of attributes
# `text` element text
constructor: (parent, name, attributes, text) ->
- @isDoc = false
@isRoot = false
+ @documentObject = null
@parent = parent
@name = name
@attributes = attributes
@@ -59,7 +59,7 @@ class XMLFragment
# `attributes` an object containing name/value pairs of attributes
# `text` element text
insertBefore: (name, attributes, text) ->
- if @isRoot || @isDoc
+ if @isRoot
throw new Error "Cannot insert elements at root level"
if not name?
@@ -98,7 +98,7 @@ class XMLFragment
# `attributes` an object containing name/value pairs of attributes
# `text` element text
insertAfter: (name, attributes, text) ->
- if @isRoot || @isDoc
+ if @isRoot
throw new Error "Cannot insert elements at root level"
if not name?
@@ -134,7 +134,7 @@ class XMLFragment
# Deletes a child element node
#
remove: () ->
- if @isRoot || @isDoc
+ if @isRoot
throw new Error "Cannot remove the root element"
i = @parent.children.indexOf @
@@ -222,9 +222,6 @@ class XMLFragment
if @isRoot
return @
- if @isDoc
- return @children[@children.length - 1]
-
child = @parent
child = child.parent while not child.isRoot
@@ -233,18 +230,12 @@ class XMLFragment
# Gets the node representing the XML document
document: () ->
- if @isDoc
- return @
-
- child = @parent
- child = child.parent while not child.isDoc
-
- return child
+ return @root().documentObject
# Gets the previous node
prev: () ->
- if @isRoot || @isDoc
+ if @isRoot
throw new Error "Root node has no siblings"
i = @parent.children.indexOf @
@@ -255,7 +246,7 @@ class XMLFragment
# Gets the next node
next: () ->
- if @isRoot || @isDoc
+ if @isRoot
throw new Error "Root node has no siblings"
i = @parent.children.indexOf @

0 comments on commit 3ede09d

Please sign in to comment.