The vanilla alternative for writing JSX-based React applications
git clone git://soophie.de/figure
7
}9
const html = (strings, ...props) => {10
const elements = parse(strings, ...props);11
if(elements.length === 0) {12
throw 'No DOM element was returned!';13
}14
if(elements.length > 1) {15
console.warn('Only one DOM element can be returned!');16
}17
return elements[0];18
}20
const parse = (strings, ...props) => {21
const [html, refs] = compose(strings, props);22
let dom;23
try {
27
console.error(error);28
throw 'Invalid DOM structure!';29
}19
const node = dom.body.childNodes[0] ?? dom.head.childNodes[0];20
const elements = render(node, refs);21
if(elements.length === 0) {22
throw 'No DOM element was returned!';23
}24
if(elements.length > 1) {25
console.warn('Only one DOM element can be returned!');26
}27
return elements[0];30
const nodes = [...dom.head.childNodes, ...dom.body.childNodes];31
return nodes.map((node) => render(node, refs));32
}34
/**
41
* @return {any[]} The joined HTML string and the properties mapped to there references.42
*/43
const compose = (strings, props) => {44
if(strings === undefined) {45
// handles dyn function without body46
return ['', {}];47
}48
const refs = {};49
let string = '';50
for(let i = 0; i < strings.length; i++) {51
string += strings[i];52
if(props[i] !== undefined) {45
const uid = `$tlx-${count++}`;53
const uid = `$seg-${count++}`;54
refs[uid] = props[i];55
string += uid ?? '';56
}
66
* @return {any[]} The string populate with the passed properties67
*/68
const feed = (string, refs) => {61
const expr = /\$tlx-\d+/g;69
const expr = /\$seg-\d+/g;70
let values = [];71
let match = null;72
let last = 0;
85
return values;86
}88
const dyn = (element, props) => {89
return (strings, ...childProps) => {90
return create(element, props, ...parse(strings, ...childProps));91
};92
}94
const render = (node, refs) => {95
if(node.nodeType === Node.TEXT_NODE) {96
return feed(node.textContent, refs);
152
const css = defCss({html, compose, feed});153
const style = defStyle({html, compose, feed});141
export { figure, html, css, style };155
export { figure, html, dyn, css, style };
1
export const defCss = ({html, compose, feed}) => {2
return (parts, ...props) => {3
const [css, refs] = compose(parts, props);2
return (strings, ...props) => {3
const [css, refs] = compose(strings, props);4
return html`5
<style type="text/css">6
${feed(css, refs).join('')}
10
}12
export const defStyle = ({compose, feed}) => {13
return (parts, ...props) => {14
const [css, refs] = compose(parts, props);13
return (strings, ...props) => {14
const [css, refs] = compose(strings, props);15
const styles = {};16
for(const item of css.split(';')) {17
if(item.trim().length === 0) {