Skip to content

Commit

Permalink
Do not omit empty indent string when pretty printing. Closes #59
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Aug 12, 2014
1 parent a189a9d commit 02b9e4c
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/XMLBuilder.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ module.exports = class XMLBuilder
# `options.newline` newline sequence for pretty print
toString: (options) ->
pretty = options?.pretty or false
indent = options?.indent or ' '
newline = options?.newline or '\n'
indent = options?.indent ? ' '
newline = options?.newline ? '\n'

r = ''
r += @xmldec.toString options if @xmldec?
Expand Down
4 changes: 2 additions & 2 deletions src/XMLCData.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ module.exports = class XMLCData extends XMLNode
# `options.newline` newline sequence for pretty print
toString: (options, level) ->
pretty = options?.pretty or false
indent = options?.indent or ' '
newline = options?.newline or '\n'
indent = options?.indent ? ' '
newline = options?.newline ? '\n'
level or= 0

space = new Array(level + 1).join(indent)
Expand Down
4 changes: 2 additions & 2 deletions src/XMLComment.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ module.exports = class XMLComment extends XMLNode
# `options.newline` newline sequence for pretty print
toString: (options, level) ->
pretty = options?.pretty or false
indent = options?.indent or ' '
newline = options?.newline or '\n'
indent = options?.indent ? ' '
newline = options?.newline ? '\n'
level or= 0

space = new Array(level + 1).join(indent)
Expand Down
4 changes: 2 additions & 2 deletions src/XMLDTDAttList.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ module.exports = class XMLDTDAttList
# `options.newline` newline sequence for pretty print
toString: (options, level) ->
pretty = options?.pretty or false
indent = options?.indent or ' '
newline = options?.newline or '\n'
indent = options?.indent ? ' '
newline = options?.newline ? '\n'
level or= 0

space = new Array(level + 1).join(indent)
Expand Down
4 changes: 2 additions & 2 deletions src/XMLDTDElement.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ module.exports = class XMLDTDElement
# `options.newline` newline sequence for pretty print
toString: (options, level) ->
pretty = options?.pretty or false
indent = options?.indent or ' '
newline = options?.newline or '\n'
indent = options?.indent ? ' '
newline = options?.newline ? '\n'
level or= 0

space = new Array(level + 1).join(indent)
Expand Down
4 changes: 2 additions & 2 deletions src/XMLDTDEntity.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ module.exports = class XMLDTDEntity
# `options.newline` newline sequence for pretty print
toString: (options, level) ->
pretty = options?.pretty or false
indent = options?.indent or ' '
newline = options?.newline or '\n'
indent = options?.indent ? ' '
newline = options?.newline ? '\n'
level or= 0

space = new Array(level + 1).join(indent)
Expand Down
4 changes: 2 additions & 2 deletions src/XMLDTDNotation.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ module.exports = class XMLDTDNotation
# `options.newline` newline sequence for pretty print
toString: (options, level) ->
pretty = options?.pretty or false
indent = options?.indent or ' '
newline = options?.newline or '\n'
indent = options?.indent ? ' '
newline = options?.newline ? '\n'
level or= 0

space = new Array(level + 1).join(indent)
Expand Down
4 changes: 2 additions & 2 deletions src/XMLDeclaration.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ module.exports = class XMLDeclaration extends XMLNode
# `options.newline` newline sequence for pretty print
toString: (options, level) ->
pretty = options?.pretty or false
indent = options?.indent or ' '
newline = options?.newline or '\n'
indent = options?.indent ? ' '
newline = options?.newline ? '\n'
level or= 0

space = new Array(level + 1).join(indent)
Expand Down
4 changes: 2 additions & 2 deletions src/XMLDocType.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ module.exports = class XMLDocType
# `options.newline` newline sequence for pretty print
toString: (options, level) ->
pretty = options?.pretty or false
indent = options?.indent or ' '
newline = options?.newline or '\n'
indent = options?.indent ? ' '
newline = options?.newline ? '\n'
level or= 0

space = new Array(level + 1).join(indent)
Expand Down
4 changes: 2 additions & 2 deletions src/XMLElement.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ module.exports = class XMLElement extends XMLNode
# `options.newline` newline sequence for pretty print
toString: (options, level) ->
pretty = options?.pretty or false
indent = options?.indent or ' '
newline = options?.newline or '\n'
indent = options?.indent ? ' '
newline = options?.newline ? '\n'
level or= 0

space = new Array(level + 1).join(indent)
Expand Down
4 changes: 2 additions & 2 deletions src/XMLProcessingInstruction.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ module.exports = class XMLProcessingInstruction
# `options.newline` newline sequence for pretty print
toString: (options, level) ->
pretty = options?.pretty or false
indent = options?.indent or ' '
newline = options?.newline or '\n'
indent = options?.indent ? ' '
newline = options?.newline ? '\n'
level or= 0

space = new Array(level + 1).join(indent)
Expand Down
4 changes: 2 additions & 2 deletions src/XMLRaw.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ module.exports = class XMLRaw extends XMLNode
# `options.newline` newline sequence for pretty print
toString: (options, level) ->
pretty = options?.pretty or false
indent = options?.indent or ' '
newline = options?.newline or '\n'
indent = options?.indent ? ' '
newline = options?.newline ? '\n'
level or= 0

space = new Array(level + 1).join(indent)
Expand Down
4 changes: 2 additions & 2 deletions src/XMLText.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ module.exports = class XMLText extends XMLNode
# `options.newline` newline sequence for pretty print
toString: (options, level) ->
pretty = options?.pretty or false
indent = options?.indent or ' '
newline = options?.newline or '\n'
indent = options?.indent ? ' '
newline = options?.newline ? '\n'
level or= 0

space = new Array(level + 1).join(indent)
Expand Down
41 changes: 41 additions & 0 deletions test/createxml.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,47 @@ vows
"""
assert.strictEqual topic.end({ pretty: true, indent: ' ' }), xml

'Pretty printing with empty indent':
topic: () ->
xmlbuilder.create('root')
.ele('xmlbuilder', {'for': 'node-js' })
.com('CoffeeScript is awesome.')
.nod('repo', {'type': 'git'}, 'git://github.com/oozcitak/xmlbuilder-js.git')
.up()
.up()
.ele('test', {'escaped': 'chars <>\'"&\t\n\r'}, 'complete 100%<>\'"&\t\n\r')
.up()
.ele('cdata')
.cdata('<test att="val">this is a test</test>\nSecond line')
.up()
.ele('raw')
.raw('&<>&')
.up()
.ele('atttest', { 'att': 'val' }, 'text')
.up()
.ele('atttest', 'text')
.att('att', () -> 'val')

'resulting XML': (topic) ->
xml = """
<?xml version="1.0"?>
<root>
<xmlbuilder for="node-js">
<!-- CoffeeScript is awesome. -->
<repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo>
</xmlbuilder>
<test escaped="chars &lt;>\'&quot;&amp;&#x9;&#xA;&#xD;">complete 100%&lt;&gt;\'"&amp;\t\n&#xD;</test>
<cdata>
<![CDATA[<test att="val">this is a test</test>
Second line]]>
</cdata>
<raw>&<>&</raw>
<atttest att="val">text</atttest>
<atttest att="val">text</atttest>
</root>
"""
assert.strictEqual topic.end({ pretty: true, indent: '' }), xml

'Short form with attributes':
topic: () ->
xmlbuilder.create('root')
Expand Down

0 comments on commit 02b9e4c

Please sign in to comment.