Skip to content

Commit

Permalink
Version fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Lcfvs committed Dec 10, 2020
1 parent d1380e7 commit 2dd3905
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
51 changes: 30 additions & 21 deletions lib/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ function resolve (content, template) {
}, template)
}

function content (fragment) {
return fragment.childNodes[0].content
}

function render (template) {
const { [symbols.node]: node } = template
const clone = from(template, node.cloneNode(true))
Expand All @@ -130,12 +126,6 @@ function wrap (source) {
return `<template>${source}</template>`
}

function parse (document, source) {
return document
.createRange()
.createContextualFragment(source)
}

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

Expand All @@ -149,44 +139,62 @@ function tree (fragment) {
return fragment
}

function rewrite (source, match) {//console.log(source)
function rewrite (source, match) {
return source.replace(match, wrap(match))
}

function dom ({ document, NodeFilter }, source) {
function parse (parser, doctype, contents) {
const document = parser.parseFromString(`${doctype}${contents}`, 'text/html')

if (doctype.length) {
document.write(contents)

return document.documentElement
} else {
return document.createRange()
.createContextualFragment(contents)
}
}

function dom ({ DOMParser, NodeFilter }, source) {
let [, doctype, contents] = source.match(parts)
const fragment = parse(document, contents)
const walker = document.createTreeWalker(fragment, NodeFilter.SHOW_TEXT)
const parser = new DOMParser()
const fragment = parse(parser, doctype, contents)
const { ownerDocument } = fragment
const walker = ownerDocument.createTreeWalker(fragment, NodeFilter.SHOW_TEXT)

while (true) {
const node = walker.nextNode()

if (!node) {
if (!node || node.parentNode.nodeName === 'TITLE') {
break
}

const { nodeValue } = node
const matches = nodeValue.match(identifier) || []
const matches = node.nodeValue.match(identifier) || []

contents = matches.reduce(rewrite, contents)
}

if (!doctype.length) {
contents = wrap(contents)
}

return {
doctype,
template: tree(parse(document, wrap(contents)))
node: tree(parse(parser, doctype, contents))
}
}

export function template (window, source = '', { ...data } = {}) {
const { doctype, template } = dom(window, source)
const { doctype, node } = dom(window, source)

return from({
...data,
...proto,
[symbols.doctype]: doctype,
[symbols.source]: source,
[symbols.window]: window
}, template)
}, node)
}

export function serialize (template) {
Expand All @@ -195,7 +203,8 @@ export function serialize (template) {
[symbols.window]: { XMLSerializer }
} = template

const source = new XMLSerializer().serializeToString(render(template))
const source = new XMLSerializer()
.serializeToString(render(template))

return `${doctype}${source}`
}
2 changes: 1 addition & 1 deletion 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": "1.1.3",
"version": "1.1.4",
"description": "A composable DOM based template engine",
"type": "module",
"main": "lib/engine.js",
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const html = serialize(p)
```js
import { template } from '@lcf.vs/dom-engine/lib/backend.js'

const template = template(source, { ...fields } = {}, type = 'text/html')
const template = template(source, { ...fields } = {})
```

```js
Expand All @@ -127,7 +127,7 @@ const rendered = serialize(template)
```js
import { template } from '@lcf.vs/dom-engine/lib/frontend.js'

const template = template(source, { ...fields } = {}, type = 'text/html')
const template = template(source, { ...fields } = {})
```

```js
Expand Down

0 comments on commit 2dd3905

Please sign in to comment.