Skip to content

Commit

Permalink
Added the root method
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Nov 28, 2011
1 parent 0523c13 commit dad5bc6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/XMLBuilder.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class XMLBuilder extends XMLFragment
@children.push child

root = new XMLFragment @, name, {}
root.isRoot = true
@children.push root

return root
Expand Down
31 changes: 25 additions & 6 deletions src/XMLFragment.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class XMLFragment
# `attributes` an object containing name/value pairs of attributes
# `text` element text
constructor: (parent, name, attributes, text) ->
@isRoot = false
@parent = parent
@name = name
@attributes = attributes
Expand Down Expand Up @@ -57,6 +58,9 @@ class XMLFragment
# `attributes` an object containing name/value pairs of attributes
# `text` element text
insertBefore: (name, attributes, text) ->
if @isRoot
throw new Error "Cannot insert elements at root level"

if not name?
throw new Error "Missing element name"

Expand Down Expand Up @@ -93,6 +97,9 @@ class XMLFragment
# `attributes` an object containing name/value pairs of attributes
# `text` element text
insertAfter: (name, attributes, text) ->
if @isRoot
throw new Error "Cannot insert elements at root level"

if not name?
throw new Error "Missing element name"

Expand Down Expand Up @@ -126,7 +133,7 @@ class XMLFragment
# Deletes a child element node
#
remove: () ->
if not @parent?
if @isRoot
throw new Error "Cannot remove the root element"

i = @parent.children.indexOf @
Expand Down Expand Up @@ -204,14 +211,26 @@ class XMLFragment

# Gets the parent node
up: () ->
if not @parent?
if @isRoot
throw new Error "This node has no parent"
return @parent


# Gets the root node
root: () ->
if @isRoot
return @

child = @parent
child = child.parent while not child.isRoot

return child


# Gets the previous node
prev: () ->
if not @parent?
throw new Error "This node has no parent"
if @isRoot
throw new Error "Root node has no siblings"

i = @parent.children.indexOf @
if i < 1
Expand All @@ -221,8 +240,8 @@ class XMLFragment

# Gets the next node
next: () ->
if not @parent?
throw new Error "This node has no parent"
if @isRoot
throw new Error "Root node has no siblings"

i = @parent.children.indexOf @
if i == -1 || i == @parent.children.length - 1
Expand Down
6 changes: 4 additions & 2 deletions test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ ele.removeAttribute("second")
test4 = builder.toString()
assert.strictEqual(xml4, test4)

# Prev/next
xml5 = '<test5><node prev="yes">1</node><node>element</node><node next="yes">2</node></test5>'
# Prev/next/root
xml5 = '<test5 root="yes"><node prev="yes">1</node><node>element</node><node next="yes">2</node></test5>'
builder.begin('test5')
.e('node','1')
.up()
Expand All @@ -132,6 +132,8 @@ builder.begin('test5')
.next()
.next()
.att('next','yes')
.root()
.att('root', 'yes')
test5 = builder.toString()
assert.strictEqual(xml5, test5)

Expand Down

0 comments on commit dad5bc6

Please sign in to comment.