UNPKG

starplate

Version:

View engine built on incremental-dom

286 lines (235 loc) 7.99 kB
'use strict'; /** * Module dependencies. */ Object.defineProperty(exports, '__esModule', { value: true }); var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var _parse5 = require('parse5'); var _parse52 = _interopRequireDefault(_parse5); var _incrementalDom = require('incremental-dom'); /** * Generates a random unique hex ID string. * * @private * @function * @name uid * @return {String} */ var uid = function uid(_) { return Math.abs(Math.random() * Date.now() | 1).toString('16'); }; /** * Ensures a function. * * @private * @function * @name ensureFunction * @param {Mixed} fn * @return {Function} */ var ensureFunction = function ensureFunction(fn) { return 'function' == typeof fn ? fn : function () { return void 0; }; }; /** * Parser class. * * @public * @class Parser * @extends parse5.Parser */ // Parser shared instance var instance_ = null; var Parser = (function (_parse5$Parser) { _inherits(Parser, _parse5$Parser); _createClass(Parser, null, [{ key: 'sharedInstance', /** * Shared parser instance * * @public * @static * @method * @name sharedInstance * @return {Parser} */ value: function sharedInstance() { instance_ = instance_ || new Parser(); return instance_; } /** * Parser constructor. * * @public * @constructor */ }]); function Parser() { _classCallCheck(this, Parser); _get(Object.getPrototypeOf(Parser.prototype), 'constructor', this).call(this, _parse52['default'].TreeAdapters.htmlparser2); /** * Known patches for this parser state. * * @public * @type {Map} * @name patches */ this.patches = new Map(); } /** * Creates a patch function used for updating * a given DOM Element from the provided source * HTML or DOM Element. * * @public * @method * @name createPatch * @param {String|Element} source * @return {Function} (domElement, [done]) => {Undefined} */ _createClass(Parser, [{ key: 'createPatch', value: function createPatch(source) { var html = source; // get cached patch if diff doesn't exist if (!this.hasPatch(source)) { return this.getPatch(source); } // consume source HTML if an element is given if (source instanceof HTMLElement) { html = source.innerHTML; } html = String(html).replace(/\n/g, ' ').replace(/\r/g, ' '); var root = this.parseFragment(html); var nodes = root.children; var stack = []; /** * Creates and pushes an instruction * to the render stack. * * @private * @function * @name createInstruction * @param {Function} fn */ var createInstruction = function createInstruction(fn) { return stack.push(fn); }; /** * Call each routine in stack. * * @private * @function * @name render */ var render = function render(_) { return stack.forEach(function (routine) { return routine(); }); }; /** * Patch routine for a given DOM Element. * * @public * @function * @param {Element} domElement * @param {Function} [done] */ var partial = function partial(domElement, done) { done = ensureFunction(done); (0, _incrementalDom.patch)(domElement, function (_) { stack.forEach(function (routine) { return routine(); }); done(); }); }; /** * Traverse node recursively appending * instructions to stack. * * @private * @function * @name traverse * @param {Object} node */ function traverse(node) { var kv = []; var id = node.attribs ? node.attribs.id : uid(); var attrs = node.attribs; var parent = node.parent; var hasChildren = Boolean(node.children ? node.children.length : 0); if (attrs && Object.keys(attrs).length) for (var key in attrs) { if (attrs[key]) kv.push(key, attrs[key]); }if ('tag' == node.type) { // begin node createInstruction(function (_) { return _incrementalDom.elementOpen.apply(undefined, [node.name, id, null].concat(kv)); }); // define child nodes if (hasChildren) node.children.forEach(traverse); // close node createInstruction(function (_) { return (0, _incrementalDom.elementClose)(node.name); }); } else if ('text' == node.type && node.data) { // handle text nodes createInstruction(function (_) { return (0, _incrementalDom.text)(node.data); }); } else if ('script' == node.type) { // skip script } else { // @TODO(werle) - what else ? throw new TypeError('Unhandled node type ' + node.type + '.'); } }; // Walk tree and generate // incremental DOM routines nodes.forEach(traverse); // set patch this.patches.set(source, partial); // provide partial patch function return partial; } /** * Predicate to determine if source given * is an already defined patch. * * @public * @method * @name hasPatch * @param {Mixed} source * @return {Boolean} */ }, { key: 'hasPatch', value: function hasPatch(source) { return false == this.patches.has(source); } /** * Returns patch by source. * * @public * @method * @name getPatch * @param {Mixed} source * @return {Function} */ }, { key: 'getPatch', value: function getPatch(source) { return this.patches.get(source) || null; } }]); return Parser; })(_parse52['default'].Parser); exports['default'] = Parser; module.exports = exports['default'];