NEWS: Welcome to my new homepage! <3

Merge branch 'main' of https://github.com/typable/segment - figure - Unnamed repository; edit this file 'description' to name the repository.

figure

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit c42e8026ff612258848707a8fb37ab6a0a6f194c
parent b674b08e305c7fb1c9163d7c2aa0c16bcd3fc781
Author: typable <contact@typable.dev>
Date:   Thu,  3 Nov 2022 10:02:40 +0100

Merge branch 'main' of https://github.com/typable/segment

Diffstat:
Mlib.js | 38++++++++++++++++++++++++++------------
Mmods.js | 8++++----
2 files changed, 30 insertions(+), 16 deletions(-)

diff --git a/lib.js b/lib.js @@ -7,6 +7,17 @@ const figure = ({createElement}) => { } const html = (strings, ...props) => { + const elements = parse(strings, ...props); + if(elements.length === 0) { + throw 'No DOM element was returned!'; + } + if(elements.length > 1) { + console.warn('Only one DOM element can be returned!'); + } + return elements[0]; +} + +const parse = (strings, ...props) => { const [html, refs] = compose(strings, props); let dom; try { @@ -16,15 +27,8 @@ const html = (strings, ...props) => { console.error(error); throw 'Invalid DOM structure!'; } - const node = dom.body.childNodes[0] ?? dom.head.childNodes[0]; - const elements = render(node, refs); - if(elements.length === 0) { - throw 'No DOM element was returned!'; - } - if(elements.length > 1) { - console.warn('Only one DOM element can be returned!'); - } - return elements[0]; + const nodes = [...dom.head.childNodes, ...dom.body.childNodes]; + return nodes.map((node) => render(node, refs)); } /** @@ -37,12 +41,16 @@ const html = (strings, ...props) => { * @return {any[]} The joined HTML string and the properties mapped to there references. */ const compose = (strings, props) => { + if(strings === undefined) { + // handles dyn function without body + return ['', {}]; + } const refs = {}; let string = ''; for(let i = 0; i < strings.length; i++) { string += strings[i]; if(props[i] !== undefined) { - const uid = `$tlx-${count++}`; + const uid = `$seg-${count++}`; refs[uid] = props[i]; string += uid ?? ''; } @@ -58,7 +66,7 @@ const compose = (strings, props) => { * @return {any[]} The string populate with the passed properties */ const feed = (string, refs) => { - const expr = /\$tlx-\d+/g; + const expr = /\$seg-\d+/g; let values = []; let match = null; let last = 0; @@ -77,6 +85,12 @@ const feed = (string, refs) => { return values; } +const dyn = (element, props) => { + return (strings, ...childProps) => { + return create(element, props, ...parse(strings, ...childProps)); + }; +} + const render = (node, refs) => { if(node.nodeType === Node.TEXT_NODE) { return feed(node.textContent, refs); @@ -138,4 +152,4 @@ import {defCss, defStyle} from './mods.js'; const css = defCss({html, compose, feed}); const style = defStyle({html, compose, feed}); -export { figure, html, css, style }; +export { figure, html, dyn, css, style }; diff --git a/mods.js b/mods.js @@ -1,6 +1,6 @@ export const defCss = ({html, compose, feed}) => { - return (parts, ...props) => { - const [css, refs] = compose(parts, props); + return (strings, ...props) => { + const [css, refs] = compose(strings, props); return html` <style type="text/css"> ${feed(css, refs).join('')} @@ -10,8 +10,8 @@ export const defCss = ({html, compose, feed}) => { } export const defStyle = ({compose, feed}) => { - return (parts, ...props) => { - const [css, refs] = compose(parts, props); + return (strings, ...props) => { + const [css, refs] = compose(strings, props); const styles = {}; for(const item of css.split(';')) { if(item.trim().length === 0) {