Skip to content

Commit 32480ba

Browse files
committed
remove or edit attributes
1 parent c093244 commit 32480ba

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,12 @@ function addAttrs (str, existing, update) {
232232
var k = Object.keys(newAttrs)
233233
for (var i = 0; i < k.length; i++) {
234234
if (k[i][0] === '_') continue
235+
235236
var value = newAttrs[k[i]]
237+
if (typeof value === 'function') value = value(existing[k[i]] || '')
238+
239+
if (value == null) continue
240+
236241
attrs.push(' ' + k[i] + '="')
237242
if (typeof value === 'object' && !isStream(value)) {
238243
if (value.prepend) attrs.push(value.prepend)

test/attrs.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var test = require('tape')
22
var concat = require('simple-concat')
3+
var through = require('through2')
34
var hyperstream = require('../')
45

56
test('add an attribute', function (t) {
@@ -26,6 +27,18 @@ test('replace an attribute', function (t) {
2627
hs.end('<div class="it did not work" id="a"></div>')
2728
})
2829

30+
test('remove attribute', function (t) {
31+
var hs = hyperstream({
32+
'#a': { class: null }
33+
})
34+
concat(hs, function (err, result) {
35+
t.ifError(err)
36+
t.equal(result + '', '<div id="a"></div>')
37+
t.end()
38+
})
39+
hs.end('<div class="it did not work" id="a"></div>')
40+
})
41+
2942
test('prepend to attribute', function (t) {
3043
var hs = hyperstream({
3144
'#a': { class: { prepend: 'it ' } }
@@ -49,3 +62,41 @@ test('append to attribute', function (t) {
4962
})
5063
hs.end('<div class="it" id="a"></div>')
5164
})
65+
66+
test('edit attribute', function (t) {
67+
var hs = hyperstream({
68+
'#a': {
69+
class: function (original) {
70+
return original.toUpperCase()
71+
}
72+
}
73+
})
74+
concat(hs, function (err, result) {
75+
t.ifError(err)
76+
t.equal(result + '', '<div class="CLASSNAME" id=a></div>')
77+
t.end()
78+
})
79+
hs.end('<div class="classname" id=a></div>')
80+
})
81+
82+
test('edit attribute with streams', function (t) {
83+
var hs = hyperstream({
84+
'#a': {
85+
class: function (initial) {
86+
var stream = through(function (chunk, enc, cb) {
87+
cb()
88+
}, function (cb) {
89+
cb(null, 'beep boop')
90+
})
91+
stream.end(initial)
92+
return stream
93+
}
94+
}
95+
})
96+
concat(hs, function (err, result) {
97+
t.ifError(err)
98+
t.equal(result + '', '<div class="beep boop" id=a></div>')
99+
t.end()
100+
})
101+
hs.end('<div class="classname" id=a></div>')
102+
})

0 commit comments

Comments
 (0)