Skip to content

Commit 6fa6d02

Browse files
committed
support attribute collections
1 parent 49a4869 commit 6fa6d02

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

dist/tonic.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class Tonic extends window.HTMLElement {
5050
return s.match(/[A-Z][a-z]*/g).join('-')
5151
}
5252

53+
static _normalizeAttrs (o, x = {}) {
54+
[...o].forEach(o => (x[o.name] = o.value))
55+
return x
56+
}
57+
5358
html ([s, ...strings], ...values) {
5459
const refs = o => {
5560
switch (({}).toString.call(o)) {
@@ -58,6 +63,7 @@ class Tonic extends window.HTMLElement {
5863
case '[object Array]':
5964
case '[object Object]':
6065
case '[object Function]': return this._prop(o)
66+
case '[object NamedNodeMap]': return this._prop(Tonic._normalizeAttrs(o))
6167
case '[object Number]': return `${o}__float`
6268
case '[object Boolean]': return `${o}__boolean`
6369
}

test/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,18 +504,23 @@ test('spread props', t => {
504504
FooBar: '"ok"'
505505
}
506506

507+
const el = document.querySelector('#el').attributes
508+
507509
return this.html`
508510
<spread-component ...${o}>
509511
</spread-component>
510512
511513
<div ...${o}>
512514
</div>
515+
516+
<span ...${el}></span>
513517
`
514518
}
515519
}
516520

517521
document.body.innerHTML = `
518522
<app-container></app-container>
523+
<div id="el" d="1" e="3.3" f="xxx"></div>
519524
`
520525

521526
Tonic.add(AppContainer)
@@ -525,9 +530,10 @@ test('spread props', t => {
525530
t.equal(component.getAttribute('a'), 'testing')
526531
t.equal(component.getAttribute('b'), '2.2')
527532
t.equal(component.getAttribute('foo-bar'), '"ok"')
528-
const div = document.querySelector('spread-component div')
533+
const div = document.querySelector('div:first-of-type')
534+
const span = document.querySelector('span:first-of-type')
529535
t.equal(div.attributes.length, 3, 'div also got expanded attributes')
530-
console.log(document.body.innerHTML)
536+
t.equal(span.attributes.length, 4, 'span got all attributes from div#el')
531537
t.end()
532538
})
533539

0 commit comments

Comments
 (0)