Skip to content

Commit

Permalink
Add the end () method
Browse files Browse the repository at this point in the history
The end() method is a convenience over .doc().toString().
It converts the entire XML document to string.
  • Loading branch information
oozcitak committed Aug 31, 2012
1 parent f480bb3 commit 0343ace
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ As a result, version from v0.1.3 are **not** compatible with previous versions.

``` js
var builder = require('xmlbuilder');
var doc = builder.create('root')
var xml = builder.create('root')
.ele('xmlbuilder', {'for': 'node-js'})
.ele('repo', {'type': 'git'}, 'git://github.com/oozcitak/xmlbuilder-js.git')
.doc();
.end({ pretty: true});

console.log(doc.toString({ pretty: true }));
console.log(xml);
```

will result in:
Expand Down
5 changes: 5 additions & 0 deletions src/XMLBuilder.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ class XMLBuilder
return @rootObject


# Ends the document and converts string
end: (options) ->
return toString(options)


# Converts the XML document to string
#
# `options.pretty` pretty prints the result
Expand Down
4 changes: 4 additions & 0 deletions src/XMLFragment.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ class XMLFragment
return @root().documentObject


# Ends the document and converts string
end: (options) ->
return @document().toString(options)

# Gets the previous node
prev: () ->
if @isRoot
Expand Down
4 changes: 4 additions & 0 deletions test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,7 @@ xml14 = '<?xml version="1.1"?><test14><node>test</node></test14>'
test14 = xmlbuilder.create('test14', { 'version': '1.1' } ).ele('node').txt('test').doc().toString()
assert.strictEqual(xml14, test14)

# Test the end() method
xml15 = '<?xml version="1.1"?><test14><node>test</node></test14>'
test15 = xmlbuilder.create('test14', { 'version': '1.1' } ).ele('node').txt('test').end()
assert.strictEqual(xml15, test15)

0 comments on commit 0343ace

Please sign in to comment.