Skip to content

Commit

Permalink
text() and cdata() now return parent element.
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Apr 27, 2011
1 parent b641432 commit 02b8a14
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 16 deletions.
18 changes: 4 additions & 14 deletions src/XMLFragment.coffee
Expand Up @@ -29,8 +29,6 @@ class XMLFragment
for own key, val of attributes
attributes[key] = @escape val

if @value
throw new Error "Text nodes cannot have child nodes"
if not name.match "^" + @val.Name + "$"
throw new Error "Invalid element name: " + name

Expand All @@ -43,8 +41,6 @@ class XMLFragment
#
# `value` element text
text: (value) ->
if @value
throw new Error "Text nodes cannot have child nodes"
if not value?
throw new Error "Missing element text"

Expand All @@ -56,34 +52,30 @@ class XMLFragment

child = new XMLFragment @, '', {}, value
@children.push child
return child
return @


# Creates a CDATA node
#
# `value` element text without CDATA delimiters
cdata: (value) ->
if @value
throw new Error "Text nodes cannot have child nodes"
if not value?
throw new Error "Missing element text"
throw new Error "Missing CDATA text"

value = '' + value or ''

if not value.match @val.CDATA
throw new Error "Invalid element text: " + value
throw new Error "Invalid CDATA text: " + value

child = new XMLFragment @, '', {}, '<![CDATA[' + value + ']]>'
@children.push child
return child
return @


# Creates a comment node
#
# `value` comment text
comment: (value) ->
if @value
throw new Error "Text nodes cannot have child nodes"
if not value?
throw new Error "Missing comment text"

Expand All @@ -110,8 +102,6 @@ class XMLFragment
# `name` attribute name
# `value` attribute value
attribute: (name, value) ->
if @value
throw new Error "Text nodes cannot have attributes"
if not name?
throw new Error "Missing attribute name"
if not value?
Expand Down
2 changes: 0 additions & 2 deletions test/test.js
Expand Up @@ -15,13 +15,11 @@ builder.begin('root')
.ele('repo')
.att('type', 'git')
.txt('git://github.com/oozcitak/xmlbuilder-js.git')
.up()
.up()
.up()
.ele('test')
.att('escaped', 'chars <>\'"&')
.txt('complete 100%')
.up()
.up()
.ele('cdata')
.cdata('<test att="val">this is a test</test>');
Expand Down

0 comments on commit 02b8a14

Please sign in to comment.