forked from glimmerjs/glimmer-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement-builder.ts
544 lines (434 loc) · 13.9 KB
/
element-builder.ts
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import type {
AttrNamespace,
Bounds,
Cursor,
CursorStackSymbol,
ElementBuilder,
ElementOperations,
Environment,
GlimmerTreeChanges,
GlimmerTreeConstruction,
LiveBlock,
Maybe,
ModifierInstance,
Nullable,
SimpleComment,
SimpleDocumentFragment,
SimpleElement,
SimpleNode,
SimpleText,
UpdatableBlock,
} from '@glimmer/interfaces';
import { destroy, registerDestructor } from '@glimmer/destroyable';
import { assert, expect, Stack } from '@glimmer/util';
import type { DynamicAttribute } from './attributes/dynamic';
import { clear, ConcreteBounds, CursorImpl } from '../bounds';
import { dynamicAttribute } from './attributes/dynamic';
export interface FirstNode {
firstNode(): SimpleNode;
}
export interface LastNode {
lastNode(): SimpleNode;
}
class First {
constructor(private node: SimpleNode) {}
firstNode(): SimpleNode {
return this.node;
}
}
class Last {
constructor(private node: SimpleNode) {}
lastNode(): SimpleNode {
return this.node;
}
}
export class Fragment implements Bounds {
private bounds: Bounds;
constructor(bounds: Bounds) {
this.bounds = bounds;
}
parentElement(): SimpleElement {
return this.bounds.parentElement();
}
firstNode(): SimpleNode {
return this.bounds.firstNode();
}
lastNode(): SimpleNode {
return this.bounds.lastNode();
}
}
export const CURSOR_STACK: CursorStackSymbol = Symbol('CURSOR_STACK') as CursorStackSymbol;
export class NewElementBuilder implements ElementBuilder {
public dom: GlimmerTreeConstruction;
public updateOperations: GlimmerTreeChanges;
public constructing: Nullable<SimpleElement> = null;
public operations: Nullable<ElementOperations> = null;
private env: Environment;
[CURSOR_STACK] = new Stack<Cursor>();
private modifierStack = new Stack<Nullable<ModifierInstance[]>>();
private blockStack = new Stack<LiveBlock>();
static forInitialRender(env: Environment, cursor: CursorImpl) {
return new this(env, cursor.element, cursor.nextSibling).initialize();
}
static resume(env: Environment, block: UpdatableBlock): NewElementBuilder {
let parentNode = block.parentElement();
let nextSibling = block.reset(env);
let stack = new this(env, parentNode, nextSibling).initialize();
stack.pushLiveBlock(block);
return stack;
}
constructor(env: Environment, parentNode: SimpleElement, nextSibling: Nullable<SimpleNode>) {
this.pushElement(parentNode, nextSibling);
this.env = env;
this.dom = env.getAppendOperations();
this.updateOperations = env.getDOM();
}
protected initialize(): this {
this.pushSimpleBlock();
return this;
}
debugBlocks(): LiveBlock[] {
return this.blockStack.toArray();
}
get element(): SimpleElement {
return this[CURSOR_STACK].current!.element;
}
get nextSibling(): Nullable<SimpleNode> {
return this[CURSOR_STACK].current!.nextSibling;
}
get hasBlocks() {
return this.blockStack.size > 0;
}
protected block(): LiveBlock {
return expect(this.blockStack.current, 'Expected a current live block');
}
popElement() {
this[CURSOR_STACK].pop();
expect(this[CURSOR_STACK].current, "can't pop past the last element");
}
pushSimpleBlock(): LiveBlock {
return this.pushLiveBlock(new SimpleLiveBlock(this.element));
}
pushUpdatableBlock(): UpdatableBlockImpl {
return this.pushLiveBlock(new UpdatableBlockImpl(this.element));
}
pushBlockList(list: LiveBlock[]): LiveBlockList {
return this.pushLiveBlock(new LiveBlockList(this.element, list));
}
protected pushLiveBlock<T extends LiveBlock>(block: T, isRemote = false): T {
let current = this.blockStack.current;
if (current !== null) {
if (!isRemote) {
current.didAppendBounds(block);
}
}
this.__openBlock();
this.blockStack.push(block);
return block;
}
popBlock(): LiveBlock {
this.block().finalize(this);
this.__closeBlock();
return expect(this.blockStack.pop(), 'Expected popBlock to return a block');
}
__openBlock(): void {}
__closeBlock(): void {}
// todo return seems unused
openElement(tag: string): SimpleElement {
let element = this.__openElement(tag);
this.constructing = element;
return element;
}
__openElement(tag: string): SimpleElement {
return this.dom.createElement(tag, this.element);
}
flushElement(modifiers: Nullable<ModifierInstance[]>) {
let parent = this.element;
let element = expect(
this.constructing,
`flushElement should only be called when constructing an element`
);
this.__flushElement(parent, element);
this.constructing = null;
this.operations = null;
this.pushModifiers(modifiers);
this.pushElement(element, null);
this.didOpenElement(element);
}
__flushElement(parent: SimpleElement, constructing: SimpleElement) {
this.dom.insertBefore(parent, constructing, this.nextSibling);
}
closeElement(): Nullable<ModifierInstance[]> {
this.willCloseElement();
this.popElement();
return this.popModifiers();
}
pushRemoteElement(
element: SimpleElement,
guid: string,
insertBefore: Maybe<SimpleNode>
): RemoteLiveBlock {
return this.__pushRemoteElement(element, guid, insertBefore);
}
__pushRemoteElement(
element: SimpleElement,
_guid: string,
insertBefore: Maybe<SimpleNode>
): RemoteLiveBlock {
this.pushElement(element, insertBefore);
if (insertBefore === undefined) {
while (element.lastChild) {
element.removeChild(element.lastChild);
}
}
let block = new RemoteLiveBlock(element);
return this.pushLiveBlock(block, true);
}
popRemoteElement(): RemoteLiveBlock {
const block = this.popBlock();
assert(block instanceof RemoteLiveBlock, '[BUG] expecting a RemoteLiveBlock');
this.popElement();
return block;
}
protected pushElement(element: SimpleElement, nextSibling: Maybe<SimpleNode> = null): void {
this[CURSOR_STACK].push(new CursorImpl(element, nextSibling));
}
private pushModifiers(modifiers: Nullable<ModifierInstance[]>): void {
this.modifierStack.push(modifiers);
}
private popModifiers(): Nullable<ModifierInstance[]> {
return this.modifierStack.pop();
}
didAppendBounds(bounds: Bounds): Bounds {
this.block().didAppendBounds(bounds);
return bounds;
}
didAppendNode<T extends SimpleNode>(node: T): T {
this.block().didAppendNode(node);
return node;
}
didOpenElement(element: SimpleElement): SimpleElement {
this.block().openElement(element);
return element;
}
willCloseElement(): void {
this.block().closeElement();
}
appendText(string: string): SimpleText {
return this.didAppendNode(this.__appendText(string));
}
__appendText(text: string): SimpleText {
let { dom, element, nextSibling } = this;
let node = dom.createTextNode(text);
dom.insertBefore(element, node, nextSibling);
return node;
}
__appendNode(node: SimpleNode): SimpleNode {
this.dom.insertBefore(this.element, node, this.nextSibling);
return node;
}
__appendFragment(fragment: SimpleDocumentFragment): Bounds {
let first = fragment.firstChild;
if (first) {
let ret = new ConcreteBounds(this.element, first, fragment.lastChild!);
this.dom.insertBefore(this.element, fragment, this.nextSibling);
return ret;
} else {
const comment = this.__appendComment('');
return new ConcreteBounds(this.element, comment, comment);
}
}
__appendHTML(html: string): Bounds {
return this.dom.insertHTMLBefore(this.element, this.nextSibling, html);
}
appendDynamicHTML(value: string): void {
let bounds = this.trustedContent(value);
this.didAppendBounds(bounds);
}
appendDynamicText(value: string): SimpleText {
let node = this.untrustedContent(value);
this.didAppendNode(node);
return node;
}
appendDynamicFragment(value: SimpleDocumentFragment): void {
let bounds = this.__appendFragment(value);
this.didAppendBounds(bounds);
}
appendDynamicNode(value: SimpleNode): void {
let node = this.__appendNode(value);
let bounds = new ConcreteBounds(this.element, node, node);
this.didAppendBounds(bounds);
}
private trustedContent(value: string): Bounds {
return this.__appendHTML(value);
}
private untrustedContent(value: string): SimpleText {
return this.__appendText(value);
}
appendComment(string: string): SimpleComment {
return this.didAppendNode(this.__appendComment(string));
}
__appendComment(string: string): SimpleComment {
let { dom, element, nextSibling } = this;
let node = dom.createComment(string);
dom.insertBefore(element, node, nextSibling);
return node;
}
__setAttribute(name: string, value: string, namespace: Nullable<AttrNamespace>): void {
this.dom.setAttribute(this.constructing!, name, value, namespace);
}
__setProperty(name: string, value: unknown): void {
(this.constructing! as any)[name] = value;
}
setStaticAttribute(name: string, value: string, namespace: Nullable<AttrNamespace>): void {
this.__setAttribute(name, value, namespace);
}
setDynamicAttribute(
name: string,
value: unknown,
trusting: boolean,
namespace: Nullable<AttrNamespace>
): DynamicAttribute {
let element = this.constructing!;
let attribute = dynamicAttribute(element, name, namespace, trusting);
attribute.set(this, value, this.env);
return attribute;
}
}
export class SimpleLiveBlock implements LiveBlock {
protected first: Nullable<FirstNode> = null;
protected last: Nullable<LastNode> = null;
protected nesting = 0;
constructor(private parent: SimpleElement) {}
parentElement() {
return this.parent;
}
firstNode(): SimpleNode {
let first = expect(
this.first,
'cannot call `firstNode()` while `SimpleLiveBlock` is still initializing'
);
return first.firstNode();
}
lastNode(): SimpleNode {
let last = expect(
this.last,
'cannot call `lastNode()` while `SimpleLiveBlock` is still initializing'
);
return last.lastNode();
}
openElement(element: SimpleElement) {
this.didAppendNode(element);
this.nesting++;
}
closeElement() {
this.nesting--;
}
didAppendNode(node: SimpleNode) {
if (this.nesting !== 0) return;
if (!this.first) {
this.first = new First(node);
}
this.last = new Last(node);
}
didAppendBounds(bounds: Bounds) {
if (this.nesting !== 0) return;
if (!this.first) {
this.first = bounds;
}
this.last = bounds;
}
finalize(stack: ElementBuilder) {
if (this.first === null) {
stack.appendComment('');
}
}
}
export class RemoteLiveBlock extends SimpleLiveBlock {
constructor(parent: SimpleElement) {
super(parent);
registerDestructor(this, () => {
// In general, you only need to clear the root of a hierarchy, and should never
// need to clear any child nodes. This is an important constraint that gives us
// a strong guarantee that clearing a subtree is a single DOM operation.
//
// Because remote blocks are not normally physically nested inside of the tree
// that they are logically nested inside, we manually clear remote blocks when
// a logical parent is cleared.
//
// HOWEVER, it is currently possible for a remote block to be physically nested
// inside of the block it is logically contained inside of. This happens when
// the remote block is appended to the end of the application's entire element.
//
// The problem with that scenario is that Glimmer believes that it owns more of
// the DOM than it actually does. The code is attempting to write past the end
// of the Glimmer-managed root, but Glimmer isn't aware of that.
//
// The correct solution to that problem is for Glimmer to be aware of the end
// of the bounds that it owns, and once we make that change, this check could
// be removed.
//
// For now, a more targeted fix is to check whether the node was already removed
// and avoid clearing the node if it was. In most cases this shouldn't happen,
// so this might hide bugs where the code clears nested nodes unnecessarily,
// so we should eventually try to do the correct fix.
if (this.parentElement() === this.firstNode().parentNode) {
clear(this);
}
});
}
}
export class UpdatableBlockImpl extends SimpleLiveBlock implements UpdatableBlock {
reset(): Nullable<SimpleNode> {
destroy(this);
let nextSibling = clear(this);
this.first = null;
this.last = null;
this.nesting = 0;
return nextSibling;
}
}
// FIXME: All the noops in here indicate a modelling problem
export class LiveBlockList implements LiveBlock {
constructor(
private readonly parent: SimpleElement,
public boundList: LiveBlock[]
) {
this.parent = parent;
this.boundList = boundList;
}
parentElement() {
return this.parent;
}
firstNode(): SimpleNode {
let head = expect(
this.boundList[0],
'cannot call `firstNode()` while `LiveBlockList` is still initializing'
);
return head.firstNode();
}
lastNode(): SimpleNode {
let boundList = this.boundList;
let tail = expect(
boundList[boundList.length - 1],
'cannot call `lastNode()` while `LiveBlockList` is still initializing'
);
return tail.lastNode();
}
openElement(_element: SimpleElement) {
assert(false, 'Cannot openElement directly inside a block list');
}
closeElement() {
assert(false, 'Cannot closeElement directly inside a block list');
}
didAppendNode(_node: SimpleNode) {
assert(false, 'Cannot create a new node directly inside a block list');
}
didAppendBounds(_bounds: Bounds) {}
finalize(_stack: ElementBuilder) {
assert(this.boundList.length > 0, 'boundsList cannot be empty');
}
}
export function clientBuilder(env: Environment, cursor: CursorImpl): ElementBuilder {
return NewElementBuilder.forInitialRender(env, cursor);
}