Skip to content

Commit

Permalink
Allow multiple values in att() and ins()
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Dec 29, 2013
1 parent 509f6d1 commit c1a5fc5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 25 deletions.
34 changes: 25 additions & 9 deletions src/XMLElement.coffee
Expand Up @@ -61,20 +61,29 @@ module.exports = class XMLElement extends XMLNode
# `name` attribute name
# `value` attribute value
attribute: (name, value) ->
value = value.apply() if _.isFunction value
if not @options.skipNullAttributes or value?
@attributes[name] = new XMLAttribute @, name, value
if _.isObject name # expand if object
for own attName, attValue of name
@attribute attName, attValue
else
value = value.apply() if _.isFunction value
if not @options.skipNullAttributes or value?
@attributes[name] = new XMLAttribute @, name, value

return @


# Removes an attribute
#
# `name` attribute name
removeAttribute: (name) ->
if not name?
throw new Error "Missing attribute name"
if _.isArray name # expand if array
for attName in name
delete @attributes[attName]
else
if not name?
throw new Error "Missing attribute name"

delete @attributes[name]
delete @attributes[name]

return @

Expand All @@ -84,9 +93,16 @@ module.exports = class XMLElement extends XMLNode
# `target` instruction target
# `value` instruction value
instruction: (target, value) ->
value = value.apply() if _.isFunction value
instruction = new XMLProcessingInstruction @, target, value
@instructions.push instruction
if _.isArray target # expand if array
for insTarget in target
@instruction insTarget
else if _.isObject target # expand if object
for own insTarget, insValue of target
@instruction insTarget, insValue
else
value = value.apply() if _.isFunction value
instruction = new XMLProcessingInstruction @, target, value
@instructions.push instruction
return @


Expand Down
26 changes: 22 additions & 4 deletions test/attributes.coffee
Expand Up @@ -16,17 +16,35 @@ vows
xml = '<test4><node first="1" second="2" third="3">element</node></test4>'
assert.strictEqual doc.end(), xml

'Add attribute (multiple with object argument)':
topic: () ->
xmlbuilder.create('test4', { headless: true })
.ele('node').att({"first":"1", "second":"2"})

'resulting XML': (doc) ->
xml = '<test4><node first="1" second="2"/></test4>'
assert.strictEqual doc.end(), xml

'Remove attribute':
topic: () ->
root = xmlbuilder.create('test4', { headless: true })
ele = root.ele('node', 'element', {"first":"1", "second":"2", "third":"3"})
ele.removeAttribute("second")
root
xmlbuilder.create('test4', { headless: true })
.ele('node', 'element', {"first":"1", "second":"2", "third":"3"})
.removeAttribute("second")

'resulting XML': (doc) ->
xml = '<test4><node first="1" third="3">element</node></test4>'
assert.strictEqual doc.end(), xml

'Remove multiple attributes':
topic: () ->
xmlbuilder.create('test4', { headless: true })
.ele('node', 'element', {"first":"1", "second":"2", "third":"3"})
.removeAttribute(["second", "third"])

'resulting XML': (doc) ->
xml = '<test4><node first="1">element</node></test4>'
assert.strictEqual doc.end(), xml

'Throw if null attribute (ele)':
topic: () ->
xmlbuilder.create('test4', { headless: true })
Expand Down
38 changes: 26 additions & 12 deletions test/instructions.coffee
Expand Up @@ -8,26 +8,40 @@ vows
.addBatch
'Simple':
topic: () ->
xmlbuilder.create('test17', { 'version': '1.1' } )
xmlbuilder.create('test17', { headless: true })
.ins('pi', 'mypi')
.ele('node')
.txt('test')

'resulting XML': (topic) ->
xml = '<?xml version="1.1"?><?pi mypi?><test17><node>test</node></test17>'
assert.strictEqual topic.end(), xml
'resulting XML': (doc) ->
xml = '<?pi mypi?><test17/>'
assert.strictEqual doc.end(), xml

'From object':
topic: () ->
xmlbuilder.create('test17', { headless: true })
.ins({'pi': 'mypi', 'pi2': 'mypi2', 'pi3': null})

'resulting XML': (doc) ->
xml = '<?pi mypi?><?pi2 mypi2?><?pi3?><test17/>'
assert.strictEqual doc.end(), xml

'From array':
topic: () ->
xmlbuilder.create('test17', { headless: true })
.ins(['pi', 'pi2'])

'resulting XML': (doc) ->
xml = '<?pi?><?pi2?><test17/>'
assert.strictEqual doc.end(), xml

'Complex':
topic: () ->
xmlbuilder.create('test18', { 'version': '1.1' } )
xmlbuilder.create('test18', { headless: true })
.ins('renderCache.subset', '"Verdana" 0 0 ISO-8859-1 4 268 67 "#(),-./')
.ele('node')
.ins('pitarget', () -> 'pivalue')
.txt('test')

'resulting XML': (topic) ->
xml = '<?xml version="1.1"?><?renderCache.subset "Verdana" 0 0 ISO-8859-1 4 268 67 "#(),-./?><test18><?pitarget pivalue?><node>test</node></test18>'
assert.strictEqual topic.end(), xml
'resulting XML': (doc) ->
xml = '<?renderCache.subset "Verdana" 0 0 ISO-8859-1 4 268 67 "#(),-./?><?pitarget pivalue?><test18/>'
assert.strictEqual doc.end(), xml


.export(module)
Expand Down

0 comments on commit c1a5fc5

Please sign in to comment.