-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterhtml.js
271 lines (244 loc) · 7.36 KB
/
interhtml.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
/* Example UI library (InterHTML) that can be built with Template Extensions */
/* Adapted from https://github.com/github/jtml - MIT - Keith Cirkel */
/* Demo: https://template-extensions.vercel.app/examples/counter.html */
import {
TemplateInstance,
AssignedTemplateInstance,
ChildNodePart,
AttrPart,
} from '../src/index.js';
export function render(result, element) {
result.renderInto(element);
}
export function enhance(result, element) {
result.enhanceInto(element);
}
export function html(strings, ...values) {
return new TemplateResult(strings, values, defaultProcessor);
}
const templateCache = new WeakMap();
const htmlCache = new WeakMap();
const renderedTemplates = new WeakMap();
const renderedTemplateInstances = new WeakMap();
class TemplateResult {
strings;
values;
#processor;
constructor(strings, values, processor) {
this.strings = strings;
this.values = values;
this.#processor = processor;
}
renderInto(element) {
const template = this.template;
if (renderedTemplates.get(element) !== template) {
renderedTemplates.set(element, template);
const instance = new TemplateInstance(
template,
this.values,
this.#processor
);
renderedTemplateInstances.set(element, instance);
if (element instanceof ChildNodePart) {
element.replace(...instance.children);
} else {
element.append(instance);
}
return;
}
const templateInstance = renderedTemplateInstances.get(element);
if (templateInstance) {
templateInstance.update(this.values);
}
}
enhanceInto(element) {
const template = this.template;
if (renderedTemplates.get(element) !== template) {
renderedTemplates.set(element, template);
const instance = new AssignedTemplateInstance(
element,
template,
this.values,
this.#processor
);
renderedTemplateInstances.set(element, instance);
return;
}
const templateInstance = renderedTemplateInstances.get(element);
if (templateInstance) {
templateInstance.update(this.values);
}
}
get template() {
if (templateCache.has(this.strings)) {
return templateCache.get(this.strings);
} else {
const template = document.createElement('template');
template.innerHTML = this.templateHTML;
templateCache.set(this.strings, template);
return template;
}
}
get templateHTML() {
if (htmlCache.has(this.strings)) {
return htmlCache.get(this.strings);
} else {
let html = this.strings[0];
for (let i = 0; i < this.values.length; i++)
html += `{{${i}}}` + this.strings[i + 1];
htmlCache.set(this.strings, html);
return html;
}
}
toString() {
let html = this.strings[0];
for (let i = 0; i < this.values.length; i++)
html += stringifyValue(this.values[i]) + this.strings[i + 1];
return html;
}
}
function stringifyValue(value) {
if (Array.isArray(value)) value = value.join('');
else if (typeof value === 'function') value = '';
else value += '';
return value;
}
const defaultProcessor = {
processCallback(instance, parts, state) {
if (!state) return;
for (const [expression, part] of parts) {
if (expression in state) {
const value = state[expression];
processBooleanAttribute(instance, part, value) ||
processInput(instance, part, value) ||
processEvent(instance, part, value) ||
processSubTemplate(instance, part, value) ||
processDocumentFragment(instance, part, value) ||
processIterable(instance, part, value) ||
processPropertyIdentity(instance, part, value);
}
}
},
};
function processSubTemplate(instance, part, value) {
if (value instanceof TemplateResult && part instanceof ChildNodePart) {
if (instance.assign) {
value.enhanceInto(part.parentNode);
} else {
value.renderInto(part);
}
return true;
}
}
export function processDocumentFragment(instance, part, value) {
if (value instanceof DocumentFragment && part instanceof ChildNodePart) {
if (value.childNodes.length) part.replace(...value.childNodes);
return true;
}
}
function processEvent(instance, part, value) {
if (part instanceof AttrPart && part.attributeName.startsWith('on')) {
let name = part.attributeName.slice(2).toLowerCase();
if (value) {
part.element.addEventListener(name, eventProxy);
} else {
part.element.removeEventListener(name, eventProxy);
}
(part.element._listeners || (part.element._listeners = {}))[name] = value;
part.element.removeAttributeNS(part.attributeNamespace, part.attributeName);
return true;
}
}
function eventProxy(e) {
return this._listeners && this._listeners[e.type](e);
}
function processBooleanAttribute(instance, part, value) {
if (
typeof value === 'boolean' &&
part instanceof AttrPart
// can't use this because on custom elements the props are always undefined
// typeof part.element[part.attributeName] === 'boolean'
) {
const ns = part.attributeNamespace;
const oldValue = part.element.hasAttributeNS(ns, part.attributeName);
if (value !== oldValue) {
part.booleanValue = value;
}
return true;
}
}
function processInput(instance, part, value) {
if (
part instanceof AttrPart &&
part.element.localName === 'input' &&
part.attributeName === 'value'
) {
part.element.value = value;
}
}
export function processPropertyIdentity(instance, part, value) {
if (part instanceof AttrPart) {
const ns = part.attributeNamespace;
const oldValue = part.element.getAttributeNS(ns, part.attributeName);
if (String(value) !== oldValue) {
part.value = String(value);
}
return true;
}
if (String(value) !== part.value) {
part.value = String(value);
}
return true;
}
function isIterable(value) {
return typeof value === 'object' && Symbol.iterator in value;
}
function processIterable(instance, part, value) {
if (!isIterable(value)) return false;
if (part instanceof ChildNodePart) {
const nodes = [];
let index = 0;
for (const item of value) {
if (item instanceof TemplateResult) {
if (instance.assign) {
const { childNodes } = item.template.content;
const len = item.length ?? getContentChildNodes(childNodes).length;
let fragment = getContentChildNodes(part.replacementNodes).slice(
index,
index + len
);
item.enhanceInto({ childNodes: fragment });
index += len;
} else {
const fragment = document.createDocumentFragment();
item.renderInto(fragment);
nodes.push(...fragment.childNodes);
}
} else if (item instanceof DocumentFragment) {
if (!instance.assign) {
nodes.push(...item.childNodes);
}
} else {
if (!instance.assign) {
nodes.push(String(item));
}
}
}
if (nodes.length) part.replace(...nodes);
return true;
} else {
part.value = Array.from(value).join(' ');
return true;
}
}
function getContentChildNodes(childNodes) {
let nodes = [];
for (let n of childNodes) if (!isIgnorable(n)) nodes.push(n);
return nodes;
}
const reAnyChars = /[^\t\n\r ]/;
function isIgnorable(node) {
return (
node.nodeType === 3 && !reAnyChars.test(node.data) // a text node, all ws
);
}