UNPKG

marko

Version:

UI Components + streaming, async, high performance, HTML templating for Node.js and the browser.

82 lines (69 loc) 2.13 kB
"use strict";var parseHTML = require("./parse-html"); 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.bN_(virtualize(curChild, ownerComponent)); curChild = curChild.nextSibling; } } function virtualize(node, ownerComponent) { switch (node.nodeType) { case 1: return VElement.cf_(node, virtualizeChildNodes, ownerComponent); case 3: return new VText(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); while (curChild) { vdomFragment.bN_(virtualize(curChild, ownerComponent)); 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.bN_(vdomNode || new VText(value.toString())); return this.ce_(); }; Node_prototype.bS_ = function () { return this.bN_(new VDocumentFragment()); }; exports.bx_ = VDocumentFragment; exports.bw_ = VElement; exports.by_ = VText; exports.bz_ = VComponent; exports.bA_ = VFragment; exports.cf_ = virtualize; exports.bB_ = virtualizeHTML;