Skip to content

Commit c1a5fc5

Browse files
committed
Allow multiple values in att() and ins()
1 parent 509f6d1 commit c1a5fc5

File tree

3 files changed

+73
-25
lines changed

3 files changed

+73
-25
lines changed

src/XMLElement.coffee

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,29 @@ module.exports = class XMLElement extends XMLNode
6161
# `name` attribute name
6262
# `value` attribute value
6363
attribute: (name, value) ->
64-
value = value.apply() if _.isFunction value
65-
if not @options.skipNullAttributes or value?
66-
@attributes[name] = new XMLAttribute @, name, value
64+
if _.isObject name # expand if object
65+
for own attName, attValue of name
66+
@attribute attName, attValue
67+
else
68+
value = value.apply() if _.isFunction value
69+
if not @options.skipNullAttributes or value?
70+
@attributes[name] = new XMLAttribute @, name, value
71+
6772
return @
6873

6974

7075
# Removes an attribute
7176
#
7277
# `name` attribute name
7378
removeAttribute: (name) ->
74-
if not name?
75-
throw new Error "Missing attribute name"
79+
if _.isArray name # expand if array
80+
for attName in name
81+
delete @attributes[attName]
82+
else
83+
if not name?
84+
throw new Error "Missing attribute name"
7685

77-
delete @attributes[name]
86+
delete @attributes[name]
7887

7988
return @
8089

@@ -84,9 +93,16 @@ module.exports = class XMLElement extends XMLNode
8493
# `target` instruction target
8594
# `value` instruction value
8695
instruction: (target, value) ->
87-
value = value.apply() if _.isFunction value
88-
instruction = new XMLProcessingInstruction @, target, value
89-
@instructions.push instruction
96+
if _.isArray target # expand if array
97+
for insTarget in target
98+
@instruction insTarget
99+
else if _.isObject target # expand if object
100+
for own insTarget, insValue of target
101+
@instruction insTarget, insValue
102+
else
103+
value = value.apply() if _.isFunction value
104+
instruction = new XMLProcessingInstruction @, target, value
105+
@instructions.push instruction
90106
return @
91107

92108

test/attributes.coffee

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,35 @@ vows
1616
xml = '<test4><node first="1" second="2" third="3">element</node></test4>'
1717
assert.strictEqual doc.end(), xml
1818

19+
'Add attribute (multiple with object argument)':
20+
topic: () ->
21+
xmlbuilder.create('test4', { headless: true })
22+
.ele('node').att({"first":"1", "second":"2"})
23+
24+
'resulting XML': (doc) ->
25+
xml = '<test4><node first="1" second="2"/></test4>'
26+
assert.strictEqual doc.end(), xml
27+
1928
'Remove attribute':
2029
topic: () ->
21-
root = xmlbuilder.create('test4', { headless: true })
22-
ele = root.ele('node', 'element', {"first":"1", "second":"2", "third":"3"})
23-
ele.removeAttribute("second")
24-
root
30+
xmlbuilder.create('test4', { headless: true })
31+
.ele('node', 'element', {"first":"1", "second":"2", "third":"3"})
32+
.removeAttribute("second")
2533

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

38+
'Remove multiple attributes':
39+
topic: () ->
40+
xmlbuilder.create('test4', { headless: true })
41+
.ele('node', 'element', {"first":"1", "second":"2", "third":"3"})
42+
.removeAttribute(["second", "third"])
43+
44+
'resulting XML': (doc) ->
45+
xml = '<test4><node first="1">element</node></test4>'
46+
assert.strictEqual doc.end(), xml
47+
3048
'Throw if null attribute (ele)':
3149
topic: () ->
3250
xmlbuilder.create('test4', { headless: true })

test/instructions.coffee

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,40 @@ vows
88
.addBatch
99
'Simple':
1010
topic: () ->
11-
xmlbuilder.create('test17', { 'version': '1.1' } )
11+
xmlbuilder.create('test17', { headless: true })
1212
.ins('pi', 'mypi')
13-
.ele('node')
14-
.txt('test')
1513

16-
'resulting XML': (topic) ->
17-
xml = '<?xml version="1.1"?><?pi mypi?><test17><node>test</node></test17>'
18-
assert.strictEqual topic.end(), xml
14+
'resulting XML': (doc) ->
15+
xml = '<?pi mypi?><test17/>'
16+
assert.strictEqual doc.end(), xml
17+
18+
'From object':
19+
topic: () ->
20+
xmlbuilder.create('test17', { headless: true })
21+
.ins({'pi': 'mypi', 'pi2': 'mypi2', 'pi3': null})
22+
23+
'resulting XML': (doc) ->
24+
xml = '<?pi mypi?><?pi2 mypi2?><?pi3?><test17/>'
25+
assert.strictEqual doc.end(), xml
26+
27+
'From array':
28+
topic: () ->
29+
xmlbuilder.create('test17', { headless: true })
30+
.ins(['pi', 'pi2'])
31+
32+
'resulting XML': (doc) ->
33+
xml = '<?pi?><?pi2?><test17/>'
34+
assert.strictEqual doc.end(), xml
1935

2036
'Complex':
2137
topic: () ->
22-
xmlbuilder.create('test18', { 'version': '1.1' } )
38+
xmlbuilder.create('test18', { headless: true })
2339
.ins('renderCache.subset', '"Verdana" 0 0 ISO-8859-1 4 268 67 "#(),-./')
24-
.ele('node')
2540
.ins('pitarget', () -> 'pivalue')
26-
.txt('test')
2741

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

3246

3347
.export(module)

0 commit comments

Comments
 (0)