Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pureact",
"version": "1.3.0",
"version": "1.3.1",
"author": "Christian Landgren",
"license": "MIT",
"repository": {
Expand All @@ -10,7 +10,7 @@
"main": "lib/index.js",
"module": "src/index.js",
"dependencies": {
"snabbdom": "^0.6.9"
"snabbdom": "^0.7.4"
},
"scripts": {
"build": "npx microbundle src/*",
Expand Down
90 changes: 89 additions & 1 deletion src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,99 @@ const h = require('snabbdom/h').default
const omit = (o, fields) => Object.keys(o).reduce((a, b) => !fields.includes(b) ? Object.assign(a, {[b]: o[b]}) : a, {})
const shadowRoot = (child) => h('span', {}, child.map(deflate)) // Should be replaced with reference to parent instead?
const deflate = (child) => child ? (Array.isArray(child) ? shadowRoot(child) : child.tagName ? vtree(child) : child) : ''
const ScriptAttributes = new Set([
'onafterprint',
'onbeforeprint',
'onbeforeunload',
'onerror',
'onhashchange',
'onload',
'onoffline',
'ononline',
'onpagehide',
'onpageshow',
'onpopstate',
'onresize',
'onstorage',
'onunload',
'onblur',
'onchange',
'oncontextmenu',
'onfocus',
'oninput',
'oninvalid',
'onreset',
'onsearch',
'onselect',
'onsubmit',
'onkeydown',
'onkeypress',
'onkeyup',
'onclick',
'ondblclick',
'onmousedown',
'onmousemove',
'onmouseout',
'onmouseover',
'onmouseup',
'onmousewheel',
'onwheel',
'ondrag',
'ondragend',
'ondragenter',
'ondragleave',
'ondragover',
'ondragstart',
'ondrop',
'onscroll',
'oncopy',
'oncut',
'onpaste',
'ondetail',
'onabort',
'oncanplay',
'oncanplaythrough',
'oncuechange',
'ondurationchange',
'onemptied',
'onended',
'onerror',
'onloadeddata',
'onloadedmetadata',
'onloadstart',
'onpause',
'onplay',
'onplaying',
'onprogress',
'onratechange',
'onseeked',
'onseeking',
'onstalled',
'onsuspend',
'ontimeupdate',
'onvolumechange',
'onwaiting',
]);

function collectOns(obj) {
let on = {};
for(let key in obj) {
let name = key.toLocaleLowerCase();
if(ScriptAttributes.has(name)) {
on[name.slice(2)] = obj[key];
delete obj[key];
}
}
return on;
}

function vtree (tree) {
const props = omit(tree, ['element', 'children', 'style', 'tagName'])
const on = collectOns(props);
const children = tree.children && tree.children.map(deflate)
return h(tree.tagName, {props, style: tree.style, attrs: props.attrs || (props.properties || {}).attributes}, children)
return h(tree.tagName, {props, style: tree.style, attrs: props.attrs || (props.properties || {}).attributes, on}, children)
}

function render (tree, node, oldTree) {
const newTree = vtree(tree)
if (oldTree) { patch(oldTree, newTree) } else { patch(node, newTree) }
Expand Down