-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom-templating.js
463 lines (326 loc) · 9.79 KB
/
dom-templating.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/* -------------------------------------------------------------------------- */
'use strict';
import * as common from './common.js';
const {
dbg,
log,
assert,
GalkError,} = common;
/* -------------------------------------------------------------------------- */
dbg && assert(document instanceof HTMLDocument);
/* inspired by https://github.com/Freak613/stage0 */
const refPrefix = `@`;
const refSep = `-`;
const kPaths = Symbol(`refPaths`);
const kRefs = Symbol(`refNodes`);
const emptyAttrs = document.createElement(`p`).attributes;
/* careful about using this in reentrant functions: */
const treeWalker = document.createTreeWalker(document, NodeFilter.SHOW_ALL);
export function createTemplate(s, opts) {
let templ = createRawTemplate(s);
return prepareTemplate(templ, opts);
};
export function createRawTemplate(s) {
let templ = document.createElement(`template`);
templ.innerHTML = s;
return templ;
};
export function isPreparedTemplate(templ) {
return (templ instanceof HTMLTemplateElement) && (kPaths in templ);
};
export function prepareTemplate(templ, opts) {
/* `templ` should be a raw template;
the node hierarchy within the template should not be modified once
the template has been prepared */
if (isPreparedTemplate(templ)) {
throw new GalkError(`template already prepared`);};
dbg && assert(templ instanceof HTMLTemplateElement);
templ[kPaths] = traceTemplate(templ, opts);
return templ;
};
export function instantiateTemplate(
/* <template>: */ {content, [kPaths] : paths},
{preserveTemplate = true} = {})
{
dbg && assert(content instanceof DocumentFragment);
dbg && assert.arr(paths);
let frag;
if (preserveTemplate) {
/* leave template untouched so it can be instantiated again */
frag = content.cloneNode(true);
} else {
/* remove the template's contents, leaving it empty */
frag = document.createDocumentFragment();
frag.appendChild(content);
};
let startNode = frag.firstChild;
if (startNode !== null) {
collectAndMemoiseRefs(paths, startNode);};
return frag;
};
export function collectTemplateRefs({content, [kPaths] : paths}) {
/* note that refs are not memoised when collected from a template */
dbg && assert(content instanceof DocumentFragment);
dbg && (paths === undefined || assert.arr(paths));
if (paths === undefined) {
/* unprepared template; trace refs without modifying any nodes: */
paths = traceTemplate({content},
{clearRefs : false, stripEmptyText : false});
};
let startNode = content.firstChild;
if (startNode !== null) {
return collectRefs(paths, startNode).refNodes;};
return null;
};
export function cloneTemplate(templ) {
/* note: the [kPaths] array will be cloned, but not the path objects
themselves, which should be considered immutable */
dbg && assert(templ instanceof HTMLTemplateElement);
let clone = templ.cloneNode(true);
if (kPaths in templ) {
dbg && assert.arr(templ[kPaths]);
clone[kPaths] = templ[kPaths].slice();
};
return clone;
};
function traceTemplate(
/* <template>: */ {content},
/* opts: */ {
stripEmptyText = false,
subTemplates = {},
clearRefs = true,} = {})
{
/* examples:
element references:
<span @category>…</span>
note: attribute names are case-insensitive;
kebab-case will be converted to camelCase
attribute references:
<span title='@tooltip'>
text-node references:
<span>@words</span>
returns: [{name, offset, attrName} …]
ref tokens in `content` will be cleared by default
subtemplates may be either prepared <template> elements
or raw subtemplate objects of the form {template, ...opts} */
dbg && assert(content instanceof DocumentFragment);
dbg && assert.bool(stripEmptyText);
dbg && assert.obj(subTemplates);
dbg && assert.bool(clearRefs);
let paths = [];
let node = content.firstChild;
if (node === null) {
return paths;};
let offset = 0;
treeWalker.currentNode = node;
do {
if (node instanceof HTMLElement) {
let attrNames = node.getAttributeNames();
for (let i = 0, n = attrNames.length; i < n; ++i) {
let name = attrNames[i];
if (name.charAt(0) === refPrefix) {
/* element reference: */
paths.push({
name : refNameToIdentifier(name),
offset,});
offset = 0;
if (clearRefs) {
node.removeAttribute(name);};
} else {
let value = node.getAttribute(name);
if (value.charAt(0) === refPrefix) {
/* attribute reference: */
paths.push({
name : refNameToIdentifier(value),
offset,
attrName : name,});
offset = 0;
if (clearRefs) {
node.setAttribute(name, ``);};
};
};
};
} else if (node instanceof Text) {
let value = node.nodeValue.trim();
if (value.charAt(0) === refPrefix) {
/* text-node reference: */
if (common.some(value, c =>
common.codePointIsWhitespace(c.codePointAt(0))))
{
/* this often happens with adjacent text-node refs */
throw new GalkError(
`reference node "${value}" contains whitespace`);
}
let id = refNameToIdentifier(value);
let subTempl = subTemplates[id];
if (subTempl !== undefined) {
dbg && assert(clearRefs,
`can't trace subtemplates without clearing refs`);
offset = traceSubTemplate(
subTempl, id, offset, treeWalker, paths, node);
} else {
if (clearRefs) {
node.nodeValue = ``;};
paths.push({
name : id,
offset,});
offset = 0;
};
} else if (value === ``
&& stripEmptyText
&& node.parentNode !== null)
{
if (treeWalker.previousNode() !== null) {
--offset;};
node.parentNode.removeChild(node);
};
};
++offset;
node = treeWalker.nextNode();
} while (node !== null);
return paths;
};
function traceSubTemplate(
templ, name, offset, treeWalker, paths, placeholderNode)
{
/* returns the new traversal offset after replacing placeholderNode
with the template content */
dbg && assert.str(name);
dbg && assert(placeholderNode instanceof Node);
if (!isPreparedTemplate(templ)) {
/* assume it's a raw subtemplate, of the form {template, ...opts} */
dbg && assert.obj(templ);
dbg && assert(templ.template instanceof HTMLTemplateElement);
/* save treeWalker's state to allow reentrancy: */
let {currentNode} = treeWalker;
templ = prepareTemplate(templ.template, templ);
treeWalker.currentNode = currentNode;
};
let {content, [kPaths] : subPaths} = templ;
dbg && assert.arr(paths);
let frag = content.cloneNode(true);
let node = frag.firstChild;
if (node === null) {
/* empty template */
if (treeWalker.previousNode() !== null) {
--offset;};
placeholderNode.remove();
return offset;
};
placeholderNode.before(frag);
/* template content becomes the target of ref `name`: */
paths.push({
name,
offset,
paths : subPaths,});
offset = 0;
treeWalker.currentNode = node;
/* advance the treewalker past the end of the subtemplate content: */
while (node !== placeholderNode) {
++offset;
node = treeWalker.nextNode();
dbg && assert(node !== null);
};
dbg && assert(treeWalker.currentNode === placeholderNode);
if (treeWalker.previousNode() !== null) {
--offset;};
placeholderNode.remove();
return offset;
};
export function refNameToIdentifier(name) {
dbg && assert.str(name);
dbg && assert(name[0] === refPrefix);
let id = ``;
for (let i = 1, n = name.length, afterSep = false; i < n; ++i) {
let c = name[i];
if (c === refSep) {
afterSep = true;
} else {
if (afterSep) {
afterSep = false;
c = c.toUpperCase();
};
id += c;
};
};
return id;
};
export function identifierToRefName(id) {
dbg && assert.str(id);
let name = refPrefix;
for (let i = 0, n = id.length; i < n; ++i) {
let c = id[i];
let lower = c.toLowerCase();
if (i > 0 && c !== lower) {
name += refSep;};
name += lower;
};
return name;
};
export function createRefNode(identifier) {
dbg && assert.str(identifier);
return document.createTextNode(identifierToRefName(identifier));
};
export function getRefs(startNode) {
dbg && assert(startNode instanceof Node);
if (startNode instanceof DocumentFragment) {
startNode = startNode.firstChild;
if (startNode === null) {
return null;};
};
let refs = startNode[kRefs];
if (typeof refs !== `object`) {
return null;};
return refs;
};
function collectAndMemoiseRefs(refPaths, startNode) {
let {refNodes, subInstances} = collectRefs(refPaths, startNode);
startNode[kRefs] = refNodes;
for (let i = 0, n = subInstances.length; i < n; ++i) {
let sub = subInstances[i];
collectAndMemoiseRefs(sub.paths, sub.node);
};
};
function collectRefs(refPaths, startNode) {
dbg && assert.arr(refPaths);
dbg && assert(startNode instanceof Node);
let refNodes = {};
let subInstances = [];
let node = startNode;
let attrs = null;
treeWalker.currentNode = node;
for (let i = 0, n = refPaths.length; i < n; ++i) {
let {name, offset, attrName, paths : subPaths} = refPaths[i];
dbg && assert.str(name);
dbg && assert.int32(offset);
if (offset > 0) {
/* moving to different node - drop cached attribute map: */
attrs = null;};
while (--offset >= 0) {
node = treeWalker.nextNode();};
dbg && assert(node === null || node instanceof Node);
if (attrName === undefined) {
/* element or text-node reference: */
refNodes[name] = node;
if (subPaths !== undefined) {
/* nested template: */
subInstances.push({node, paths : subPaths});};
} else {
/* attribute reference: */
if (attrs === null) {
attrs =
node instanceof HTMLElement
? node.attributes
: emptyAttrs;
};
dbg && assert(attrs instanceof NamedNodeMap);
dbg && assert.str(attrName);
refNodes[name] = attrs.getNamedItem(attrName);
};
};
return {refNodes, subInstances};
};
/* -------------------------------------------------------------------------- */
/*
*/
/* -------------------------------------------------------------------------- */