Skip to content

Commit

Permalink
src: Tonic.raw() expose template strings
Browse files Browse the repository at this point in the history
Store the template strings used in `this.html` on the
returned instance of `TonicRaw` so that other features
can leverage it ( Like `TonicCached` ).
  • Loading branch information
Raynos committed Apr 15, 2020
1 parent c53945c commit 36c2be8
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class TonicRaw {
constructor (rawText) {
constructor (rawText, templateStrings) {
this.isTonicRaw = true
this.rawText = rawText
this.templateStrings = templateStrings
}

valueOf () { return this.rawText }
Expand Down Expand Up @@ -118,11 +119,11 @@ class Tonic extends window.HTMLElement {
return s.replace(Tonic.ESC, c => Tonic.MAP[c])
}

static raw (s) {
return new TonicRaw(s)
static raw (s, templateStrings) {
return new TonicRaw(s, templateStrings)
}

html ([s, ...strings], ...values) {
html (strings, ...values) {
const refs = o => {
if (o && o.__children__) return this._placehold(o)
if (o && o.isTonicRaw) return o.rawText
Expand Down Expand Up @@ -150,9 +151,14 @@ class Tonic extends window.HTMLElement {
return o
}

const reduce = (a, b) => a.concat(b, strings.shift())
const str = values.map(refs).reduce(reduce, [s]).join('')
return Tonic.raw(str)
let out = ''
for (let i = 0; i < strings.length - 1; i++) {
out += strings[i]
out += refs(values[i])
}
out += strings[strings.length - 1]

return Tonic.raw(out, strings)
}

setState (o) {
Expand Down

0 comments on commit 36c2be8

Please sign in to comment.