marko
Version:
UI Components + streaming, async, high performance, HTML templating for Node.js and the browser.
88 lines (75 loc) • 2.33 kB
JavaScript
;var parseHTML = require("./parse-html");
var VComment = require("./VComment");
var VComponent = require("./VComponent");
var VDocumentFragment = require("./VDocumentFragment");
var VElement = require("./VElement");
var VFragment = require("./VFragment");
var VNode = require("./VNode");
var VText = require("./VText");
var specialHtmlRegexp = /[&<]/;
function virtualizeChildNodes(node, vdomParent, ownerComponent) {
var curChild = node.firstChild;
while (curChild) {
vdomParent.bR_(virtualize(curChild, ownerComponent));
curChild = curChild.nextSibling;
}
}
function virtualize(node, ownerComponent) {
switch (node.nodeType) {
case 1:
return VElement.ck_(node, virtualizeChildNodes, ownerComponent);
case 3:
return new VText(node.nodeValue, ownerComponent);
case 8:
return new VComment(node.nodeValue, ownerComponent);
case 11:
var vdomDocFragment = new VDocumentFragment();
virtualizeChildNodes(node, vdomDocFragment, ownerComponent);
return vdomDocFragment;
}
}
function virtualizeHTML(html, ownerComponent) {
if (!specialHtmlRegexp.test(html)) {
return new VText(html, ownerComponent);
}
var vdomFragment = new VDocumentFragment();
var curChild = parseHTML(html);
var virtualized;
while (curChild) {
virtualized = virtualize(curChild, ownerComponent);
if (virtualized) vdomFragment.bR_(virtualized);
curChild = curChild.nextSibling;
}
return vdomFragment;
}
var Node_prototype = VNode.prototype;
/**
* Shorthand method for creating and appending a Text node with a given value
* @param {String} value The text value for the new Text node
*/
Node_prototype.t = function (value) {
var type = typeof value;
var vdomNode;
if (type !== "string") {
if (value == null) {
value = "";
} else if (type === "object") {
if (value.toHTML) {
vdomNode = virtualizeHTML(value.toHTML());
}
}
}
this.bR_(vdomNode || new VText(value.toString()));
return this.cj_();
};
Node_prototype.bW_ = function () {
return this.bR_(new VDocumentFragment());
};
exports.bA_ = VComment;
exports.bB_ = VDocumentFragment;
exports.bz_ = VElement;
exports.bC_ = VText;
exports.bD_ = VComponent;
exports.bE_ = VFragment;
exports.ck_ = virtualize;
exports.bF_ = virtualizeHTML;