-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathrabbit.js
More file actions
152 lines (143 loc) · 4.69 KB
/
rabbit.js
File metadata and controls
152 lines (143 loc) · 4.69 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';
const instrument = (m => m.__esModule ? /* istanbul ignore next */ m.default : /* istanbul ignore next */ m)(require('uparser'));
const {cacheInfo} = require('./cache.js');
const {handlers} = require('./handlers.js');
const {isArray} = require('./array.js');
const {
createFragment, createWalker, getPath, getWire, importNode
} = require('./node.js');
const prefix = 'isµ';
const templates = new WeakMap;
const createEntry = (type, template) => {
const {content, updates} = mapUpdates(type, template);
return {type, template, content, updates, wire: null};
};
const mapTemplate = (type, template) => {
const text = instrument(template, prefix);
const content = createFragment(text, type);
const tw = createWalker(content);
const nodes = [];
const length = template.length - 1;
let i = 0;
let search = `${prefix}${i}`;
while (i < length) {
const node = tw.nextNode();
/* istanbul ignore next */
if (!node)
throw `bad template: ${text}`;
if (node.nodeType === 8) {
/* istanbul ignore else */
if (node.textContent === search) {
nodes.push({type: 'node', path: getPath(node)});
search = `${prefix}${++i}`;
}
}
else {
while (node.hasAttribute(search)) {
nodes.push({
type: 'attr',
path: getPath(node),
name: node.getAttribute(search),
// svg: type === 'svg'
});
node.removeAttribute(search);
search = `${prefix}${++i}`;
}
/* istanbul ignore next */
if (
/^(?:style|textarea)$/i.test(node.tagName) &&
node.textContent.trim() === `<!--${search}-->`
){
nodes.push({type: 'text', path: getPath(node)});
search = `${prefix}${++i}`;
}
}
}
return {content, nodes};
};
const mapUpdates = (type, template) => {
const {content, nodes} = templates.get(template) || setTemplate(type, template);
const fragment = importNode.call(document, content, true);
const updates = nodes.map(handlers, fragment);
return {content: fragment, updates};
};
const retrieve = (info, hole) => {
const {sub, stack} = info;
const counter = {
a: 0, aLength: sub.length,
i: 0, iLength: stack.length
};
const wire = unroll(info, hole, counter);
const {a, i, aLength, iLength} = counter;
if (a < aLength)
sub.splice(a);
// TODO: this is actually pointless, as I believe
// such case never exists, being the stack related
// to the template, hence static.
// `i` and `iLength` are only useful for the first pass,
// but from that time on, will never change.
// Verify this and get rid of this extra check.
/* istanbul ignore next */
if (i < iLength)
stack.splice(i);
return wire;
};
exports.retrieve = retrieve;
const setTemplate = (type, template) => {
const result = mapTemplate(type, template);
templates.set(template, result);
return result;
};
const unroll = (info, hole, counter) => {
const {stack} = info;
const {i, iLength} = counter;
const {type, template, values} = hole;
const unknown = i === iLength;
if (unknown)
counter.iLength = stack.push(createEntry(type, template));
counter.i++;
unrollArray(info, values, counter);
let entry = stack[i];
if (!unknown && (entry.template !== template || entry.type !== type))
stack[i] = (entry = createEntry(type, template));
const {content, updates, wire} = entry;
for (let i = 0, {length} = updates; i < length; i++)
updates[i](values[i]);
return wire || (entry.wire = getWire(content));
};
const unrollArray = (info, values, counter) => {
for (let i = 0, {length} = values; i < length; i++) {
const hole = values[i];
if (typeof hole === 'object' && hole) {
/* istanbul ignore else */
if (hole instanceof Hole)
values[i] = unroll(info, hole, counter);
else if (isArray(hole)) {
for (let i = 0, {length} = hole; i < length; i++) {
const inner = hole[i];
if (typeof inner === 'object' && inner && inner instanceof Hole) {
const {sub} = info;
const {a, aLength} = counter;
if (a === aLength)
counter.aLength = sub.push(cacheInfo());
counter.a++;
hole[i] = retrieve(sub[a], inner);
}
}
}
}
}
};
/**
* Holds all necessary details needed to render the content further on.
* @constructor
* @param {string} type The hole type, either `html` or `svg`.
* @param {string[]} template The template literals used to the define the content.
* @param {Array} values Zero, one, or more interpolated values to render.
*/
function Hole(type, template, values) {
this.type = type;
this.template = template;
this.values = values;
}
exports.Hole = Hole;