UNPKG

hyperhtml

Version:

A Fast & Light Virtual DOM Alternative

39 lines (33 loc) 1.29 kB
import WeakMap from '@ungap/weakmap'; import tta from '@ungap/template-tag-arguments'; import {OWNER_SVG_ELEMENT} from '../shared/constants.js'; import {Tagger} from '../objects/Updates.js'; // a weak collection of contexts that // are already known to hyperHTML const bewitched = new WeakMap; // better known as hyper.bind(node), the render is // the main tag function in charge of fully upgrading // or simply updating, contexts used as hyperHTML targets. // The `this` context is either a regular DOM node or a fragment. function render() { const wicked = bewitched.get(this); const args = tta.apply(null, arguments); if (wicked && wicked.template === args[0]) { wicked.tagger.apply(null, args); } else { upgrade.apply(this, args); } return this; } // an upgrade is in charge of collecting template info, // parse it once, if unknown, to map all interpolations // as single DOM callbacks, relate such template // to the current context, and render it after cleaning the context up function upgrade(template) { const type = OWNER_SVG_ELEMENT in this ? 'svg' : 'html'; const tagger = new Tagger(type); bewitched.set(this, {tagger, template: template}); this.textContent = ''; this.appendChild(tagger.apply(null, arguments)); } export default render;