-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathrenderer.ts
More file actions
519 lines (456 loc) · 16.4 KB
/
renderer.ts
File metadata and controls
519 lines (456 loc) · 16.4 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
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
/*
* Copyright (c) 2023, Salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import {
HTML_NAMESPACE,
htmlPropertyToAttribute,
isAriaAttribute,
isBooleanAttribute,
isFunction,
isNull,
isUndefined,
noop,
REFLECTIVE_GLOBAL_PROPERTY_SET,
StringToLowerCase,
} from '@lwc/shared';
import {
HostNodeType,
HostTypeKey,
HostNamespaceKey,
HostParentKey,
HostShadowRootKey,
HostAttributesKey,
HostChildrenKey,
HostValueKey,
HostHostKey,
HostContextProvidersKey,
} from './types';
import { classNameToTokenList, tokenListToClassName } from './utils/classes';
import {
reportMutation,
startTrackingMutations,
stopTrackingMutations,
} from './utils/mutation-tracking';
import { registerContextConsumer, registerContextProvider } from './context';
import type { HostNode, HostElement, HostAttribute, HostChildNode } from './types';
import type { LifecycleCallback } from '@lwc/engine-core';
function unsupportedMethod(name: string): () => never {
return function () {
throw new TypeError(`"${name}" is not supported in this environment`);
};
}
function createElement(tagName: string, namespace?: string): HostElement {
return {
[HostTypeKey]: HostNodeType.Element,
tagName,
[HostNamespaceKey]: namespace ?? HTML_NAMESPACE,
[HostParentKey]: null,
[HostShadowRootKey]: null,
[HostChildrenKey]: [],
[HostAttributesKey]: [],
[HostContextProvidersKey]: new Map(),
};
}
const isSyntheticShadowDefined: boolean = false;
type N = HostNode;
type E = HostElement;
function insert(node: N, parent: E, anchor: N | null) {
const nodeParent = node[HostParentKey];
if (nodeParent !== null && nodeParent !== parent) {
const nodeIndex = nodeParent[HostChildrenKey].indexOf(node);
nodeParent[HostChildrenKey].splice(nodeIndex, 1);
}
node[HostParentKey] = parent;
const anchorIndex = isNull(anchor) ? -1 : parent[HostChildrenKey].indexOf(anchor);
if (anchorIndex === -1) {
parent[HostChildrenKey].push(node);
} else {
parent[HostChildrenKey].splice(anchorIndex, 0, node);
}
}
function remove(node: N, parent: E) {
const nodeIndex = parent[HostChildrenKey].indexOf(node);
parent[HostChildrenKey].splice(nodeIndex, 1);
}
function cloneNode(node: HostChildNode): HostChildNode {
// Note: no need to deep clone as cloneNode is only used for nodes of type HostNodeType.Raw.
if (process.env.NODE_ENV !== 'production') {
if (node[HostTypeKey] !== HostNodeType.Raw) {
throw new TypeError(
`SSR: cloneNode was called with invalid NodeType <${node[HostTypeKey]}>, only HostNodeType.Raw is supported.`
);
}
}
return { ...node };
}
function createFragment(html: string): HostChildNode {
return {
[HostTypeKey]: HostNodeType.Raw,
[HostParentKey]: null,
[HostValueKey]: html,
};
}
function createText(content: string): HostNode {
return {
[HostTypeKey]: HostNodeType.Text,
[HostValueKey]: String(content),
[HostParentKey]: null,
};
}
function createComment(content: string): HostNode {
return {
[HostTypeKey]: HostNodeType.Comment,
[HostValueKey]: content,
[HostParentKey]: null,
};
}
function getSibling(node: N, offset: number) {
const parent = node[HostParentKey];
if (isNull(parent)) {
return null;
}
const nodeIndex = parent[HostChildrenKey].indexOf(node);
return (parent[HostChildrenKey][nodeIndex + offset] as HostNode) ?? null;
}
function nextSibling(node: N) {
return getSibling(node, 1);
}
function previousSibling(node: N) {
return getSibling(node, -1);
}
function getParentNode(node: N) {
return node[HostParentKey];
}
function attachShadow(element: E, config: ShadowRootInit) {
element[HostShadowRootKey] = {
[HostTypeKey]: HostNodeType.ShadowRoot,
[HostChildrenKey]: [],
[HostHostKey]: element,
mode: config.mode,
delegatesFocus: !!config.delegatesFocus,
};
return element[HostShadowRootKey] as any;
}
function getProperty(node: N, propName: string) {
if (propName in node) {
return (node as any)[propName];
}
if (node[HostTypeKey] === HostNodeType.Element) {
const attrName = htmlPropertyToAttribute(propName);
// Handle all the boolean properties.
if (isBooleanAttribute(attrName, node.tagName)) {
return getAttribute(node, attrName) ?? false;
}
// Handle global html attributes and AOM.
if (REFLECTIVE_GLOBAL_PROPERTY_SET.has(propName) || isAriaAttribute(attrName)) {
return getAttribute(node, attrName);
}
// Handle special elements live bindings. The checked property is already handled above
// in the boolean case.
if (node.tagName === 'input' && propName === 'value') {
return getAttribute(node, 'value') ?? '';
}
}
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line no-console
console.error(`Unexpected "${propName}" property access from the renderer`);
}
}
function setProperty(node: N, propName: string, value: any): void {
if (propName in node) {
return ((node as any)[propName] = value);
}
if (node[HostTypeKey] === HostNodeType.Element) {
const attrName = htmlPropertyToAttribute(propName);
if (propName === 'innerHTML') {
node[HostChildrenKey] = [
{
[HostTypeKey]: HostNodeType.Raw,
[HostParentKey]: node,
[HostValueKey]: value,
},
];
return;
}
// `<input checked="...">` and `<input value="...">` have a peculiar attr/prop relationship, so the engine
// has historically treated them as props rather than attributes:
// https://github.com/salesforce/lwc/blob/b584d39/packages/%40lwc/template-compiler/src/parser/attribute.ts#L217-L221
// For example, an element might be rendered as `<input type=checkbox>` but `input.checked` could
// still return true. `value` behaves similarly. `value` and `checked` behave surprisingly
// because the attributes actually represent the "default" value rather than the current one:
// - https://jakearchibald.com/2024/attributes-vs-properties/#value-on-input-fields
// - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#checked
// For this reason, we do not render these values in SSR - they are purely a runtime (prop) concern.
if (node.tagName === 'input' && (attrName === 'value' || attrName === 'checked')) {
return;
}
// Handle all the boolean properties.
if (isBooleanAttribute(attrName, node.tagName)) {
return value === true
? setAttribute(node, attrName, '')
: removeAttribute(node, attrName);
}
if (isAriaAttribute(attrName)) {
// TODO [#3284]: According to the spec, IDL nullable type values
// (null and undefined) should remove the attribute; however, we
// only do so in the case of null for historical reasons.
return isNull(value)
? removeAttribute(node, attrName)
: setAttribute(node, attrName, value);
} else if (REFLECTIVE_GLOBAL_PROPERTY_SET.has(propName)) {
// Handle global html attributes and AOM.
return setAttribute(node, attrName, value);
}
}
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line no-console
console.error(
`Unexpected attempt to set "${propName}=${value}" property from the renderer`
);
}
}
function setText(node: N, content: string) {
if (node[HostTypeKey] === HostNodeType.Text) {
node[HostValueKey] = content;
} else if (node[HostTypeKey] === HostNodeType.Element) {
node[HostChildrenKey] = [
{
[HostTypeKey]: HostNodeType.Text,
[HostParentKey]: node,
[HostValueKey]: content,
},
];
}
}
function getAttribute(element: E, name: string, namespace: string | null = null) {
const normalizedName = StringToLowerCase.call(String(name));
const attribute = element[HostAttributesKey].find(
(attr) => attr.name === normalizedName && attr[HostNamespaceKey] === namespace
);
return attribute ? attribute.value : null;
}
function setAttribute(element: E, name: string, value: unknown, namespace: string | null = null) {
const normalizedName = StringToLowerCase.call(String(name));
const normalizedValue = String(value);
reportMutation(element, normalizedName);
const attribute = element[HostAttributesKey].find(
(attr) => attr.name === normalizedName && attr[HostNamespaceKey] === namespace
);
if (isUndefined(namespace)) {
namespace = null;
}
if (isUndefined(attribute)) {
element[HostAttributesKey].push({
name: normalizedName,
[HostNamespaceKey]: namespace,
value: normalizedValue,
});
} else {
attribute.value = normalizedValue;
}
}
function removeAttribute(element: E, name: string, namespace?: string | null) {
const normalizedName = StringToLowerCase.call(String(name));
reportMutation(element, normalizedName);
element[HostAttributesKey] = element[HostAttributesKey].filter(
(attr) => attr.name !== normalizedName && attr[HostNamespaceKey] !== namespace
);
}
function getClassList(element: E) {
function getClassAttribute(): HostAttribute {
let classAttribute = element[HostAttributesKey].find(
(attr) => attr.name === 'class' && isNull(attr[HostNamespaceKey])
);
if (isUndefined(classAttribute)) {
classAttribute = {
name: 'class',
[HostNamespaceKey]: null,
value: '',
};
element[HostAttributesKey].push(classAttribute);
}
return classAttribute;
}
return {
add(...names: string[]): void {
reportMutation(element, 'class');
const classAttribute = getClassAttribute();
const tokenList = classNameToTokenList(classAttribute.value);
names.forEach((name) => tokenList.add(name));
classAttribute.value = tokenListToClassName(tokenList);
},
remove(...names: string[]): void {
reportMutation(element, 'class');
const classAttribute = getClassAttribute();
const tokenList = classNameToTokenList(classAttribute.value);
names.forEach((name) => tokenList.delete(name));
classAttribute.value = tokenListToClassName(tokenList);
},
} as DOMTokenList;
}
function setCSSStyleProperty(element: E, name: string, value: string, important: boolean) {
const styleAttribute = element[HostAttributesKey].find(
(attr) => attr.name === 'style' && isNull(attr[HostNamespaceKey])
);
const serializedProperty = `${name}: ${value}${important ? ' !important' : ''};`;
if (isUndefined(styleAttribute)) {
element[HostAttributesKey].push({
name: 'style',
[HostNamespaceKey]: null,
value: serializedProperty,
});
} else {
styleAttribute.value += ` ${serializedProperty}`;
}
}
function isConnected(node: HostNode) {
return !isNull(node[HostParentKey]);
}
function getTagName(elm: HostElement): string {
// tagName is lowercased on the server, but to align with DOM APIs, we always return uppercase
return elm.tagName.toUpperCase();
}
type CreateElementAndUpgrade = (upgradeCallback: LifecycleCallback) => HostElement;
const localRegistryRecord: Map<string, CreateElementAndUpgrade> = new Map();
function createUpgradableElementConstructor(tagName: string): CreateElementAndUpgrade {
return function Ctor(upgradeCallback: LifecycleCallback) {
const elm = createElement(tagName);
if (isFunction(upgradeCallback)) {
upgradeCallback(elm); // nothing to do with the result for now
}
return elm;
};
}
function getUpgradableElement(
tagName: string,
_isFormAssociated?: boolean
): CreateElementAndUpgrade {
let ctor = localRegistryRecord.get(tagName);
if (!isUndefined(ctor)) {
return ctor;
}
ctor = createUpgradableElementConstructor(tagName);
localRegistryRecord.set(tagName, ctor);
return ctor;
}
// Note that SSR does not have any concept of native vs synthetic custom element lifecycle
function createCustomElement(
tagName: string,
upgradeCallback: LifecycleCallback,
_useNativeLifecycle: boolean,
_isFormAssociated: boolean
): HostElement {
const UpgradableConstructor = getUpgradableElement(tagName);
return new (UpgradableConstructor as any)(upgradeCallback);
}
/** Noop in SSR */
// Noop on SSR (for now). This need to be reevaluated whenever we will implement support for
// synthetic shadow.
const insertStylesheet = noop as (
content: string,
target: ShadowRoot | undefined,
signal: AbortSignal | undefined
) => void;
const addEventListener = noop as (
target: HostNode,
type: string,
callback: EventListener,
options?: AddEventListenerOptions | boolean
) => void;
const removeEventListener = noop as (
target: HostNode,
type: string,
callback: EventListener,
options?: AddEventListenerOptions | boolean
) => void;
const assertInstanceOfHTMLElement = noop as (elm: any, msg: string) => void;
/** Unsupported methods in SSR */
const dispatchEvent = unsupportedMethod('dispatchEvent') as (target: any, event: Event) => boolean;
const getStyle = unsupportedMethod('style') as (element: HTMLElement) => CSSStyleDeclaration;
const getBoundingClientRect = unsupportedMethod('getBoundingClientRect') as (
element: HostElement
) => DOMRect;
const querySelector = unsupportedMethod('querySelector') as (
element: HostElement,
selectors: string
) => Element | null;
const querySelectorAll = unsupportedMethod('querySelectorAll') as (
element: HostElement,
selectors: string
) => NodeList;
const getElementsByTagName = unsupportedMethod('getElementsByTagName') as (
element: HostElement,
tagNameOrWildCard: string
) => HTMLCollection;
const getElementsByClassName = unsupportedMethod('getElementsByClassName') as (
element: HostElement,
names: string
) => HTMLCollection;
const getChildren = unsupportedMethod('getChildren') as (element: HostElement) => HTMLCollection;
const getChildNodes = unsupportedMethod('getChildNodes') as (element: HostElement) => NodeList;
const getFirstChild = unsupportedMethod('getFirstChild') as (
element: HostElement
) => HostNode | null;
const getFirstElementChild = unsupportedMethod('getFirstElementChild') as (
element: HostElement
) => HostElement | null;
const getLastChild = unsupportedMethod('getLastChild') as (element: HostElement) => HostNode | null;
const getLastElementChild = unsupportedMethod('getLastElementChild') as (
element: HostElement
) => HostElement | null;
const ownerDocument = unsupportedMethod('ownerDocument') as (element: HostElement) => Document;
const attachInternals = unsupportedMethod('attachInternals') as (
elm: HTMLElement
) => ElementInternals;
export const renderer = {
isSyntheticShadowDefined,
insert,
remove,
cloneNode,
createFragment,
createElement,
createText,
createComment,
createCustomElement,
nextSibling,
previousSibling,
attachShadow,
getProperty,
setProperty,
setText,
getAttribute,
setAttribute,
removeAttribute,
addEventListener,
removeEventListener,
dispatchEvent,
getClassList,
setCSSStyleProperty,
getBoundingClientRect,
querySelector,
querySelectorAll,
getElementsByTagName,
getElementsByClassName,
getChildren,
getChildNodes,
getFirstChild,
getFirstElementChild,
getLastChild,
getLastElementChild,
getTagName,
getStyle,
isConnected,
insertStylesheet,
assertInstanceOfHTMLElement,
ownerDocument,
registerContextProvider,
registerContextConsumer,
attachInternals,
defineCustomElement: getUpgradableElement,
getParentNode,
startTrackingMutations,
stopTrackingMutations,
};