Permalink
Browse files

Allow multiple values in att() and ins()

  • Loading branch information...
1 parent 509f6d1 commit c1a5fc523a43d84470555f4400a4dd8e50eefc47 @oozcitak committed Dec 29, 2013
Showing with 73 additions and 25 deletions.
  1. +25 −9 src/XMLElement.coffee
  2. +22 −4 test/attributes.coffee
  3. +26 −12 test/instructions.coffee
View
@@ -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 @
@@ -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 @
View
@@ -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 })
View
@@ -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)

0 comments on commit c1a5fc5

Please sign in to comment.