Skip to content

Commit

Permalink
Parsing improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Lcfvs committed Aug 19, 2021
1 parent c0bd128 commit 928a5dd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
35 changes: 25 additions & 10 deletions lib/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ const symbols = {
}

const parts = /(^\s*<!doctype [^>]+>|)([\s\S]*)$/i
const identifier = /{((\??)([a-z][a-z\d_]*(?:\.[a-z][a-z\d_]*)*))}/gi
const attribute = /(<!--dom-engine::(\??)([a-z][a-z\d_]*(?:\.[a-z][a-z\d_]*)*)::dom-engine-->)/gi
const text = /^(dom-engine::(\??)([a-z][a-z\d_]*(?:\.[a-z][a-z\d_]*)*)::dom-engine)$/gi
const identifier = /{((\??)([a-z]\w*(?:\.[a-z]\w*)*))}/g
const attribute = /(<!--dom-engine::(\??)([a-z]\w*(?:\.[a-z]\w*)*)::dom-engine-->)/g
const text = /^(dom-engine::(\??)([a-z]\w*(?:\.[a-z]\w*)*)::dom-engine)$/g
const escaped = /^(&lt;!--dom-engine::(\??)([a-z]\w*(?:\.[a-z]\w*)*)::dom-engine--&gt;)/g

const html = source => context.policy?.createHTML(source) ?? source

Expand All @@ -33,7 +34,14 @@ const tokenize = node => {
const map = new Map()

for (const [key, current] of search(node).entries()) {
if (current.nodeType === 1) {

if (current.nodeName === 'TITLE') {
const tokens = identify(escaped, current.innerHTML)

if (tokens.length) {
map.set(key, { tokens })
}
} else if (current.nodeType === 1) {
const attributes = new Map()

for (const { name, value } of current.attributes) {
Expand All @@ -50,8 +58,7 @@ const tokenize = node => {
})
}
} else {
const { nodeValue } = current
const tokens = identify(text, nodeValue)
const tokens = identify(text, current.nodeValue)

if (tokens.length) {
map.set(key, { tokens })
Expand Down Expand Up @@ -171,10 +178,10 @@ const value = (tokens, node, name, template) => {

const content = (tokens, node, template) => {
const nodes = []
let { nodeValue } = node
let [target, content] = pick(node)

for (const { identifier, key, optional } of tokens) {
const [prefix] = nodeValue.split(identifier)
const [prefix] = content.split(identifier)
const value = resolve(key, optional, template)
const replacements = Array.isArray(value)
? value
Expand All @@ -183,10 +190,18 @@ const content = (tokens, node, template) => {
: [value]

nodes.push(prefix, ...replacements.filter(isFilled).map(child))
nodeValue = nodeValue.replace(`${prefix}${identifier}`, '')
content = content.replace(`${prefix}${identifier}`, '')
}

target.replaceWith(...nodes, content)
}

const pick = node => {
if (node.nodeName === 'TITLE') {
return [node.firstChild, node.innerHTML]
}

node.replaceWith(...nodes, nodeValue)
return [node, node.nodeValue]
}

function child (value) {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lcf.vs/dom-engine",
"version": "5.2.3",
"version": "5.2.4",
"description": "A composable DOM based template engine",
"type": "module",
"exports": {
Expand Down

0 comments on commit 928a5dd

Please sign in to comment.