Skip to content

Commit

Permalink
src: Handle SPREAD at html() time.
Browse files Browse the repository at this point in the history
This allows for using the return value of `this.html`<...>``
instead of only doing SPREAD handling at render time just before
setting `innerHTML`.
  • Loading branch information
Raynos committed Apr 15, 2020
1 parent 501e203 commit 9462ea6
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,17 @@ class Tonic extends window.HTMLElement {
out.push(strings[i], refs(values[i]))
}
out.push(strings[strings.length - 1])
return Tonic.raw(out.join(''), strings)
let htmlStr = out.join('')
htmlStr = htmlStr.replace(Tonic.SPREAD, (_, p) => {
const o = Tonic._data[p.split('__')[1]][p]
return Object.entries(o).map(([key, value]) => {
const k = key.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
if (value === true) return k
else if (value) return `${k}="${Tonic.escape(String(value))}"`
else return ''
}).filter(Boolean).join(' ')
})
return Tonic.raw(htmlStr, strings)
}

setState (o) {
Expand Down Expand Up @@ -243,16 +253,6 @@ class Tonic extends window.HTMLElement {
}

if (typeof content === 'string') {
content = content.replace(Tonic.SPREAD, (_, p) => {
const o = Tonic._data[p.split('__')[1]][p]
return Object.entries(o).map(([key, value]) => {
const k = key.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
if (value === true) return k
else if (value) return `${k}="${Tonic.escape(String(value))}"`
else return ''
}).filter(Boolean).join(' ')
})

if (this.stylesheet) {
content = `<style>${this.stylesheet()}</style>${content}`
}
Expand Down

0 comments on commit 9462ea6

Please sign in to comment.