-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathrender.js
More file actions
38 lines (33 loc) · 1.26 KB
/
Copy pathrender.js
File metadata and controls
38 lines (33 loc) · 1.26 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
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;