Skip to content

Commit

Permalink
+ object properties support
Browse files Browse the repository at this point in the history
  • Loading branch information
Lcfvs committed Mar 22, 2021
1 parent 6d44fe1 commit 2f5f269
Show file tree
Hide file tree
Showing 4 changed files with 3,198 additions and 251 deletions.
26 changes: 17 additions & 9 deletions lib/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const proto = {
}

function attribute (template, node) {
const rules = identify(node.nodeValue)
const { nodeValue } = node
const rules = identify(strip(nodeValue), nodeValue)
const value = resolve(template, rules)

if (value === null) {
Expand All @@ -27,7 +28,8 @@ function attribute (template, node) {
}

function content (template, node) {
const rules = identify(node.textContent)
const { textContent } = node
const rules = identify(strip(textContent), textContent)
const value = resolve(template, rules)

if (value === null) {
Expand Down Expand Up @@ -98,8 +100,7 @@ function fill (template) {
}
}

function identify (identifier) {
const rule = identifier.slice(1, -1)
function identify (rule, identifier) {
const [first] = rule
const optional = first === '?'
const name = optional ? rule.slice(1) : rule
Expand Down Expand Up @@ -178,13 +179,16 @@ function replace (template) {
}

function resolve (template, { identifier, optional, name }) {
const { [name]: value = null } = template
return name.split('.')
.reduce((data, name) => {
const { [name]: value = null } = data ?? {}

if (value === null && !optional) {
throw new Error(`Missing ${identifier} in \`${template[symbols.source]}\``)
}
if (value === null && !optional) {
throw new Error(`Missing ${identifier} in \`${template[symbols.source]}\``)
}

return value
return value
}, template)
}

function render (template) {
Expand All @@ -199,6 +203,10 @@ function rewrite (source, match) {
return source.replace(match, wrap(match))
}

function strip (identifier) {
return identifier.slice(1, -1)
}

function tree (fragment) {
const [...templates] = fragment.querySelectorAll('template')

Expand Down
Loading

0 comments on commit 2f5f269

Please sign in to comment.