Skip to content

Commit

Permalink
Pretty print attributes. Closes #167
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Apr 30, 2019
1 parent df3a52b commit c92ba3e
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 12 deletions.
25 changes: 19 additions & 6 deletions src/XMLStreamWriter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ module.exports = class XMLStreamWriter extends XMLWriterBase
for child in doc.children
@writeChildNode child, options, 0

attribute: (att, options, level) ->
@stream.write super(att, options, level)

cdata: (node, options, level) ->
@stream.write super(node, options, level)

Expand Down Expand Up @@ -84,11 +81,27 @@ module.exports = class XMLStreamWriter extends XMLWriterBase
# open tag
@openNode(node, options, level)
options.state = WriterState.OpenTag
@stream.write @indent(node, options, level) + '<' + node.name
r = @indent(node, options, level) + '<' + node.name

# attributes
for own name, att of node.attribs
@attribute att, options, level
if options.pretty and options.width > 0
len = r.length
for own name, att of node.attribs
ratt = @attribute att, options, level
attLen = ratt.length
if len + attLen > options.width
rline = @indent(node, options, level + 1) + ratt
r += @endline(node, options, level) + rline
len = rline.length
else
rline = ' ' + ratt
r += rline
len += rline.length
else
for own name, att of node.attribs
r += @attribute att, options, level

@stream.write r

childNodeCount = node.children.length
firstChildNode = if childNodeCount is 0 then null else node.children[0]
Expand Down
29 changes: 23 additions & 6 deletions src/XMLWriterBase.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = class XMLWriterBase
# `options.indent` indentation string
# `options.newline` newline sequence
# `options.offset` a fixed number of indentations to add to every line
# `options.width` maximum column width
# `options.allowEmpty` do not self close empty element tags
# 'options.dontPrettyTextNodes' if any text is present in node, don't indent or LF
# `options.spaceBeforeSlash` add a space before the closing slash of empty elements
Expand All @@ -55,6 +56,7 @@ module.exports = class XMLWriterBase
filteredOptions.indent = options.indent ? ' '
filteredOptions.newline = options.newline ? '\n'
filteredOptions.offset = options.offset ? 0
filteredOptions.width = options.width ? 0
filteredOptions.dontPrettyTextNodes = options.dontPrettyTextNodes ? options.dontprettytextnodes ? 0

filteredOptions.spaceBeforeSlash = options.spaceBeforeSlash ? options.spacebeforeslash ? ''
Expand Down Expand Up @@ -95,7 +97,10 @@ module.exports = class XMLWriterBase

attribute: (att, options, level) ->
@openAttribute(att, options, level)
r = ' ' + att.name + '="' + att.value + '"'
if options.pretty and options.width > 0
r = att.name + '="' + att.value + '"'
else
r = ' ' + att.name + '="' + att.value + '"'
@closeAttribute(att, options, level)
return r

Expand Down Expand Up @@ -178,16 +183,28 @@ module.exports = class XMLWriterBase
level or= 0
prettySuppressed = false

r = ''

# open tag
@openNode(node, options, level)
options.state = WriterState.OpenTag
r += @indent(node, options, level) + '<' + node.name
r = @indent(node, options, level) + '<' + node.name

# attributes
for own name, att of node.attribs
r += @attribute att, options, level
if options.pretty and options.width > 0
len = r.length
for own name, att of node.attribs
ratt = @attribute att, options, level
attLen = ratt.length
if len + attLen > options.width
rline = @indent(node, options, level + 1) + ratt
r += @endline(node, options, level) + rline
len = rline.length
else
rline = ' ' + ratt
r += rline
len += rline.length
else
for own name, att of node.attribs
r += @attribute att, options, level

childNodeCount = node.children.length
firstChildNode = if childNodeCount is 0 then null else node.children[0]
Expand Down
61 changes: 61 additions & 0 deletions test/basic/prettyattributes.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
suite 'Creating XML with string writer:', ->
test 'Pretty print attributes - 1', ->
eq(
xml('test', { headless: true })
.ele('node', {"first":"1", "second":"2"})
.end({ pretty: true, width: 20 })
"""
<test>
<node first="1"
second="2"/>
</test>
"""
)

test 'Pretty print attributes - 2', ->
eq(
xml('test', { headless: true })
.ele('node', {"first":"1", "second":"2", "third":"33333333333333333333", "fourth": 4})
.end({ pretty: true, width: 10 })
"""
<test>
<node
first="1"
second="2"
third="33333333333333333333"
fourth="4"/>
</test>
"""
)

test 'Pretty print attributes - 3', ->
eq(
xml('test', { headless: true })
.ele('node', {"first":"1", "second":"2", "third":"33333333333333333333", "fourth": 4})
.end({ pretty: true, width: 1 })
"""
<test>
<node
first="1"
second="2"
third="33333333333333333333"
fourth="4"/>
</test>
"""
)

test 'Pretty print attributes - 4', ->
eq(
xml('test', { headless: true })
.ele('node', {"first":"1", "second":"2"}).ele('child')
.end({ pretty: true, width: 10 })
"""
<test>
<node
first="1"
second="2">
<child/>
</node>
</test>
"""
)
73 changes: 73 additions & 0 deletions test/basic/prettyattributesstream.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
suite 'Creating XML with stream writer:', ->
hook = null
setup 'hook stdout.write', ->
hook = captureStream(process.stdout)
return
teardown 'unhook stdout.write', ->
hook.unhook()
return

test 'Pretty print attributes - 1', ->
xml('test', { headless: true })
.ele('node', {"first":"1", "second":"2"})
.end(builder.streamWriter(process.stdout, { pretty: true, width: 20 }))
eq(
hook.captured()
"""
<test>
<node first="1"
second="2"/>
</test>
"""
)

test 'Pretty print attributes - 2', ->
xml('test', { headless: true })
.ele('node', {"first":"1", "second":"2", "third":"33333333333333333333", "fourth": 4})
.end(builder.streamWriter(process.stdout, { pretty: true, width: 10 }))
eq(
hook.captured()
"""
<test>
<node
first="1"
second="2"
third="33333333333333333333"
fourth="4"/>
</test>
"""
)

test 'Pretty print attributes - 3', ->
xml('test', { headless: true })
.ele('node', {"first":"1", "second":"2", "third":"33333333333333333333", "fourth": 4})
.end(builder.streamWriter(process.stdout, { pretty: true, width: 1 }))
eq(
hook.captured()
"""
<test>
<node
first="1"
second="2"
third="33333333333333333333"
fourth="4"/>
</test>
"""
)

test 'Pretty print attributes - 4', ->
xml('test', { headless: true })
.ele('node', {"first":"1", "second":"2"}).ele('child')
.end(builder.streamWriter(process.stdout, { pretty: true, width: 10 }))
eq(
hook.captured()
"""
<test>
<node
first="1"
second="2">
<child/>
</node>
</test>
"""
)

0 comments on commit c92ba3e

Please sign in to comment.