all repos — h3 @ 1f3b876aeca7602c445f55742d97c6136172505d

A tiny, extremely minimalist JavaScript microframework.

Renamed VNode#element into VNode#type.
h3rald h3rald@h3rald.com
Wed, 15 Apr 2020 11:43:09 +0200
commit

1f3b876aeca7602c445f55742d97c6136172505d

parent

66b9947629e70e86e1d17b0359c99382068ab6af

1 files changed, 9 insertions(+), 11 deletions(-)

jump to
M h3.jsh3.js

@@ -57,9 +57,8 @@

// Virtual Node Implementation with HyperScript-like syntax class VNode { from(data) { - this.type = data.type; this.value = data.value; - this.element = data.element; + this.type = data.type; this.id = data.id; this.key = data.key; this.style = data.style;

@@ -82,7 +81,7 @@ delete this.attributes.style;

} constructor(...args) { - this.element = null; + this.type = null; this.attributes = {}; this.data = {}; this.id = null;

@@ -106,7 +105,6 @@ this.from(vnode);

return; } } else { - this.type = "element"; const elSelector = String(args[0]); if (args[1] && !args[2]) { // assuming no attributes

@@ -125,15 +123,15 @@ const selectorRegex = /^([a-z0-9:_=-]+)(#[a-z0-9:_=-]+)?(\..+)?$/i;

if (!elSelector.match(selectorRegex)) { throw new Error(`[VNode] Invalid selector: ${elSelector}`); } - const [, element, id, classes] = elSelector.match(selectorRegex); - this.element = element; + const [, type, id, classes] = elSelector.match(selectorRegex); + this.type = type; if (id) { this.id = id.slice(1); } this.classList = (classes && classes.split(".").slice(1)) || []; this.children = this.children.map((c) => { if (typeof c === "string") { - return new VNode({ type: "text", value: c }); + return new VNode({ type: "#text", value: c }); } else if (typeof c === "function") { return new VNode(c); } else {

@@ -145,10 +143,10 @@ }

// Renders the actual DOM Node corresponding to the current Virtual Node render() { - if (this.type === "text") { + if (this.type === "#text") { return document.createTextNode(this.value); } - const node = document.createElement(this.element); + const node = document.createElement(this.type); if (this.id) { node.id = this.id; }

@@ -200,7 +198,7 @@ const newvnode = vnode;

const oldvnode = this; if ( oldvnode.constructor !== newvnode.constructor || - oldvnode.element !== newvnode.element + oldvnode.type !== newvnode.type ) { // Different node types, replace the whole node (requires valid parent node) node.parentNode.replaceChild(newvnode.render(), node);

@@ -288,7 +286,7 @@ if (equal(newmap, oldmap) && (notFoundInNew >= 0 || notFoundInOld >= 0)) {

// Something changed for (let i = 0; i < newmap.length; i++) { if (newmap[i] === -1) { - if (oldvnode.children[i].type === "text") { + if (oldvnode.children[i].type === "#text") { oldvnode.children[i] = newvnode.children[i]; node.childNodes[i].nodeValue = newvnode.children[i].value; } else {