-
Notifications
You must be signed in to change notification settings - Fork 187
/
serialize-builder.ts
138 lines (114 loc) · 3.94 KB
/
serialize-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
import type { Bounds, Environment, Option, ElementBuilder, Maybe } from '@glimmer/interfaces';
import { ConcreteBounds, NewElementBuilder } from '@glimmer/runtime';
import { RemoteLiveBlock } from '@glimmer/runtime';
import type { SimpleElement, SimpleNode, SimpleText } from '@simple-dom/interface';
import { Cache } from '@glimmer/validator';
const TEXT_NODE = 3;
const NEEDS_EXTRA_CLOSE = new WeakMap<SimpleNode>();
function currentNode(
cursor: ElementBuilder | { element: SimpleElement; nextSibling: SimpleNode }
): Option<SimpleNode> {
let { element, nextSibling } = cursor;
if (nextSibling === null) {
return element.lastChild;
} else {
return nextSibling.previousSibling;
}
}
class SerializeBuilder extends NewElementBuilder implements ElementBuilder {
private serializeBlockDepth = 0;
__openBlock(): void {
let { tagName } = this.element;
if (tagName !== 'TITLE' && tagName !== 'SCRIPT' && tagName !== 'STYLE') {
let depth = this.serializeBlockDepth++;
this.__appendComment(`%+b:${depth}%`);
}
super.__openBlock();
}
__closeBlock(): void {
let { tagName } = this.element;
super.__closeBlock();
if (tagName !== 'TITLE' && tagName !== 'SCRIPT' && tagName !== 'STYLE') {
let depth = --this.serializeBlockDepth;
this.__appendComment(`%-b:${depth}%`);
}
}
__appendHTML(html: string): Bounds {
let { tagName } = this.element;
if (tagName === 'TITLE' || tagName === 'SCRIPT' || tagName === 'STYLE') {
return super.__appendHTML(html);
}
// Do we need to run the html tokenizer here?
let first = this.__appendComment('%glmr%');
if (tagName === 'TABLE') {
let openIndex = html.indexOf('<');
if (openIndex > -1) {
let tr = html.slice(openIndex + 1, openIndex + 3);
if (tr === 'tr') {
html = `<tbody>${html}</tbody>`;
}
}
}
if (html === '') {
this.__appendComment('% %');
} else {
super.__appendHTML(html);
}
let last = this.__appendComment('%glmr%');
return new ConcreteBounds(this.element, first, last);
}
__appendText(string: string): SimpleText {
let { tagName } = this.element;
let current = currentNode(this);
if (tagName === 'TITLE' || tagName === 'SCRIPT' || tagName === 'STYLE') {
return super.__appendText(string);
} else if (string === '') {
return (this.__appendComment('% %') as any) as SimpleText;
} else if (current && current.nodeType === TEXT_NODE) {
this.__appendComment('%|%');
}
return super.__appendText(string);
}
closeElement(): Option<Cache[]> {
if (NEEDS_EXTRA_CLOSE.has(this.element)) {
NEEDS_EXTRA_CLOSE.delete(this.element);
super.closeElement();
}
return super.closeElement();
}
openElement(tag: string) {
if (tag === 'tr') {
if (
this.element.tagName !== 'TBODY' &&
this.element.tagName !== 'THEAD' &&
this.element.tagName !== 'TFOOT'
) {
this.openElement('tbody');
// This prevents the closeBlock comment from being re-parented
// under the auto inserted tbody. Rehydration builder needs to
// account for the insertion since it is injected here and not
// really in the template.
NEEDS_EXTRA_CLOSE.set(this.constructing!, true);
this.flushElement(null);
}
}
return super.openElement(tag);
}
pushRemoteElement(
element: SimpleElement,
cursorId: string,
insertBefore: Maybe<SimpleNode> = null
): Option<RemoteLiveBlock> {
let { dom } = this;
let script = dom.createElement('script');
script.setAttribute('glmr', cursorId);
dom.insertBefore(element, script, insertBefore);
return super.pushRemoteElement(element, cursorId, insertBefore);
}
}
export function serializeBuilder(
env: Environment,
cursor: { element: SimpleElement; nextSibling: Option<SimpleNode> }
): ElementBuilder {
return SerializeBuilder.forInitialRender(env, cursor);
}