diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-10-04 11:50:26 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-10-04 11:50:26 +0200 |
commit | 133a8c672c26609e9d56d4e184b779ca26971a8c (patch) | |
tree | f3aa4779a75279c95553cd68dd2a3bbe5e5b9dde /thirdparty/preact/src/h.js | |
parent | 0697c987c5314232056a86fa128b518c866d2f12 (diff) | |
parent | 30b577138dda685f65a8529be1866afa6e321845 (diff) |
Merge commit '30b577138dda685f65a8529be1866afa6e321845' as 'thirdparty/preact'
Diffstat (limited to 'thirdparty/preact/src/h.js')
-rw-r--r-- | thirdparty/preact/src/h.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/thirdparty/preact/src/h.js b/thirdparty/preact/src/h.js new file mode 100644 index 000000000..e57ce4bde --- /dev/null +++ b/thirdparty/preact/src/h.js @@ -0,0 +1,51 @@ +import { VNode } from './vnode'; +import options from './options'; + + +let stack = []; + + + +/** JSX/hyperscript reviver +* Benchmarks: https://esbench.com/bench/57ee8f8e330ab09900a1a1a0 + * @see http://jasonformat.com/wtf-is-jsx + * @public + * @example + * /** @jsx h *\/ + * import { render, h } from 'preact'; + * render(<span>foo</span>, document.body); + */ +export function h(nodeName, attributes) { + let children, lastSimple, child, simple, i; + for (i=arguments.length; i-- > 2; ) { + stack.push(arguments[i]); + } + if (attributes && attributes.children) { + if (!stack.length) stack.push(attributes.children); + delete attributes.children; + } + while (stack.length) { + if ((child = stack.pop()) instanceof Array) { + for (i=child.length; i--; ) stack.push(child[i]); + } + else if (child!=null && child!==false) { + if (typeof child=='number' || child===true) child = String(child); + simple = typeof child=='string'; + if (simple && lastSimple) { + children[children.length-1] += child; + } + else { + if (children) children.push(child); + else children = [child]; + lastSimple = simple; + } + } + } + + let p = new VNode(nodeName, attributes || undefined, children); + + // if a "vnode hook" is defined, pass every created VNode to it + if (options.vnode) options.vnode(p); + + return p; +} |