-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathrender.js
More file actions
40 lines (35 loc) · 1.47 KB
/
render.js
File metadata and controls
40 lines (35 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict';
const WeakMap = (m => m.__esModule ? /* istanbul ignore next */ m.default : /* istanbul ignore next */ m)(require('@ungap/weakmap'));
const {OWNER_SVG_ELEMENT} = require('../shared/constants.js');
const {Tagger} = require('../objects/Updates.js');
const {reArguments} = require('../shared/utils.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 = reArguments.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() {
const args = reArguments.apply(null, arguments);
const type = OWNER_SVG_ELEMENT in this ? 'svg' : 'html';
const tagger = new Tagger(type);
bewitched.set(this, {tagger, template: args[0]});
this.textContent = '';
this.appendChild(tagger.apply(null, args));
}
Object.defineProperty(exports, '__esModule', {value: true}).default = render;