/
render-value.ts
811 lines (757 loc) · 25.9 KB
/
render-value.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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/// <reference lib="dom" />
/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
import type {TemplateResult, ChildPart} from 'lit';
import type {
Directive,
DirectiveClass,
DirectiveResult,
} from 'lit/directive.js';
import {nothing, noChange} from 'lit';
import {PartType} from 'lit/directive.js';
import {isTemplateResult, getDirectiveClass} from 'lit/directive-helpers.js';
import {_$LH} from 'lit-html/private-ssr-support.js';
const {
getTemplateHtml,
marker,
markerMatch,
boundAttributeSuffix,
overrideDirectiveResolve,
setDirectiveClass,
getAttributePartCommittedValue,
resolveDirective,
AttributePart,
PropertyPart,
BooleanAttributePart,
EventPart,
connectedDisconnectable,
} = _$LH;
import {digestForTemplateResult} from 'lit/experimental-hydrate.js';
import {
ElementRenderer,
ElementRendererConstructor,
getElementRenderer,
} from './element-renderer.js';
import {escapeHtml} from './util/escape-html.js';
import {parseFragment} from 'parse5';
import {isElementNode, isCommentNode, traverse} from '@parse5/tools';
import {isRenderLightDirective} from '@lit-labs/ssr-client/directives/render-light.js';
import {reflectedAttributeName} from './reflected-attributes.js';
import type {RenderResult} from './render-result.js';
declare module 'parse5/dist/tree-adapters/default.js' {
interface Element {
isDefinedCustomElement?: boolean;
}
}
const patchedDirectiveCache: WeakMap<DirectiveClass, DirectiveClass> =
new Map();
/**
* Looks for values of type `DirectiveResult` and replaces its Directive class
* with a subclass that calls `render` rather than `update`
*/
const patchIfDirective = (value: unknown) => {
// This property needs to remain unminified.
const directiveCtor = getDirectiveClass(value);
if (directiveCtor !== undefined) {
let patchedCtor = patchedDirectiveCache.get(directiveCtor);
if (patchedCtor === undefined) {
patchedCtor = overrideDirectiveResolve(
directiveCtor,
(directive: Directive, values: unknown[]) => {
// Since the return value may also be a directive result in the case of
// nested directives, we may need to patch that as well
return patchIfDirective(directive.render(...values));
}
);
patchedDirectiveCache.set(directiveCtor, patchedCtor);
}
// This property needs to remain unminified.
setDirectiveClass(value as DirectiveResult, patchedCtor);
}
return value;
};
/**
* Patches `DirectiveResult` `Directive` classes for AttributePart values, which
* may be an array
*/
const patchAnyDirectives = (
part: InstanceType<typeof AttributePart>,
value: unknown,
valueIndex: number
) => {
if (part.strings !== undefined) {
for (let i = 0; i < part.strings.length - 1; i++) {
patchIfDirective((value as unknown[])[valueIndex + i]);
}
} else {
patchIfDirective(value);
}
};
const templateCache = new Map<TemplateStringsArray, Array<Op>>();
/**
* Operation to output static text
*/
type TextOp = {
type: 'text';
value: string;
};
/**
* Operation to output dynamic text from the associated template result value
*/
type ChildPartOp = {
type: 'child-part';
index: number;
useCustomElementInstance?: boolean;
};
/**
* Operation to output an attribute with bindings. Includes all bindings for an
* attribute.
*/
type AttributePartOp = {
type: 'attribute-part';
index: number;
name: string;
ctor: typeof AttributePart;
strings: Array<string>;
tagName: string;
useCustomElementInstance?: boolean;
};
/**
* Operation for an element binding. Although we only support directives in
* element position which cannot emit anything, the opcode needs to index past
* the part value
*/
type ElementPartOp = {
type: 'element-part';
index: number;
};
/**
* Operator to create a custom element instance.
*/
type CustomElementOpenOp = {
type: 'custom-element-open';
tagName: string;
ctor: {new (): HTMLElement};
staticAttributes: Map<string, string>;
};
/**
* Operation to render a custom element's attributes. This is separate from
* `custom-element-open` because attribute/property parts go in between and need
* to run and be set on the instance before we render the element's final
* attributes.
*/
type CustomElementAttributesOp = {
type: 'custom-element-attributes';
};
/**
* Operation to render a custom element's children, usually its shadow root.
*/
type CustomElementShadowOp = {
type: 'custom-element-shadow';
};
/**
* Operation to close a custom element so that its no longer available for
* bindings.
*/
type CustomElementClosedOp = {
type: 'custom-element-close';
};
/**
* Operation to possibly emit the `<!--lit-node-->` marker; the operation
* always emits if there were attribute parts, and may emit if the node
* was a custom element and it needed `defer-hydration` because it was
* rendered in the shadow root of another custom element host; we don't
* know the latter at opcode generation time, and so that test is done at
* runtime in the opcode.
*/
type PossibleNodeMarkerOp = {
type: 'possible-node-marker';
boundAttributesCount: number;
nodeIndex: number;
};
type Op =
| TextOp
| ChildPartOp
| AttributePartOp
| ElementPartOp
| CustomElementOpenOp
| CustomElementAttributesOp
| CustomElementShadowOp
| CustomElementClosedOp
| PossibleNodeMarkerOp;
/**
* For a given TemplateResult, generates and/or returns a cached list of opcodes
* for the associated Template. Opcodes are designed to allow emitting
* contiguous static text from the template as much as possible, with specific
* non-`text` opcodes interleaved to perform dynamic work, such as emitting
* values for ChildParts or AttributeParts, and handling custom elements.
*
* For the following example template, an opcode list may look like this:
*
* ```js
* html`<div><span>Hello</span><span class=${'bold'}>${template()}</span></div>`
* ```
*
* - `text`
* - Emit run of static text: `<div><span>Hello</span>`
* - `possible-node-marker`
* - Emit `<!--lit-node n-->` marker since there are attribute parts
* - `text`
* - Emit run of static text: `<span`
* - `attribute-part`
* - Emit an AttributePart's value, e.g. ` class="bold"`
* - `text`
* - Emit run of static text: `>`
* - `child-part`
* - Emit the ChildPart's value, in this case a TemplateResult, thus we
* recurse into that template's opcodes
* - `text`
* - Emit run of static text: `/span></div>`
*
* When a custom-element is encountered, the flow looks like this:
*
* ```js
* html`<x-foo staticAttr dynamicAttr=${value}><div>child</div>...</x-foo>`
* ```
*
* - `possible-node-marker`
* - Emit `<!--lit-node n-->` marker since there are attribute parts and we
* may emit the `defer-hydration` attribute on the node that follows
* - `text`
* - Emit open tag `<x-foo`
* - `custom-element-open`
* - Create the CE `instance`+`renderer` and put on
* `customElementInstanceStack`
* - Call `renderer.setAttribute()` for any `staticAttributes` (e.g.
* 'staticAttr`)
* - `attribute-part`(s)
* - Call `renderer.setAttribute()` or `renderer.setProperty()` for
* `AttributePart`/`PropertyPart`s (e.g. for `dynamicAttr`)
* - `custom-element-attributes`
* - Call `renderer.connectedCallback()`
* - Emit `renderer.renderAttributes()`
* - `text`
* - Emit end of of open tag `>`
* - `custom-element-shadow`
* - Emit `renderer.renderShadow()` (emits `<template shadowroot>` +
* recurses to emit `render()`)
* - `text`
* - Emit run of static text within tag: `<div>child</div>...`
* - ...(recurse to render more parts/children)...
* - `custom-element-close`
* - Pop the CE `instance`+`renderer` off the `customElementInstanceStack`
*/
const getTemplateOpcodes = (result: TemplateResult) => {
const template = templateCache.get(result.strings);
if (template !== undefined) {
return template;
}
// The property '_$litType$' needs to remain unminified.
const [html, attrNames] = getTemplateHtml(
result.strings,
result['_$litType$']
);
/**
* The html string is parsed into a parse5 AST with source code information
* on; this lets us skip over certain ast nodes by string character position
* while walking the AST.
*/
const ast = parseFragment(String(html), {
sourceCodeLocationInfo: true,
});
const ops: Array<Op> = [];
/* The last offset of html written to the stream */
let lastOffset: number | undefined = 0;
/* Current attribute part index, for indexing attrNames */
let attrIndex = 0;
/**
* Sets `lastOffset` to `offset`, skipping a range of characters. This is
* useful for skipping and re-writing lit-html marker nodes, bound attribute
* suffix, etc.
*/
const skipTo = (offset: number) => {
if (lastOffset === undefined) {
throw new Error('lastOffset is undefined');
}
if (offset < lastOffset) {
throw new Error(`offset must be greater than lastOffset.
offset: ${offset}
lastOffset: ${lastOffset}
`);
}
lastOffset = offset;
};
/**
* Records the given string to the output, either by appending to the current
* opcode (if already `text`) or by creating a new `text` opcode (if the
* previous opcode was not `text)
*/
const flush = (value: string) => {
const op = getLast(ops);
if (op !== undefined && op.type === 'text') {
op.value += value;
} else {
ops.push({
type: 'text',
value,
});
}
};
/**
* Creates or appends to a text opcode with a substring of the html from the
* `lastOffset` flushed to `offset`.
*/
const flushTo = (offset?: number) => {
if (lastOffset === undefined) {
throw new Error('lastOffset is undefined');
}
const previousLastOffset = lastOffset;
lastOffset = offset;
const value = String(html).substring(previousLastOffset, offset);
flush(value);
};
// Depth-first node index, counting only comment and element nodes, to match
// client-side lit-html.
let nodeIndex = 0;
traverse(ast, {
'pre:node'(node, parent) {
if (isCommentNode(node)) {
if (node.data === markerMatch) {
flushTo(node.sourceCodeLocation!.startOffset);
skipTo(node.sourceCodeLocation!.endOffset);
ops.push({
type: 'child-part',
index: nodeIndex,
useCustomElementInstance:
parent && isElementNode(parent) && parent.isDefinedCustomElement,
});
}
nodeIndex++;
} else if (isElementNode(node)) {
let boundAttributesCount = 0;
const tagName = node.tagName;
if (tagName.indexOf('-') !== -1) {
// Looking up the constructor here means that custom elements must be
// registered before rendering the first template that contains them.
const ctor = customElements.get(tagName);
if (ctor !== undefined) {
// Mark that this is a custom element
node.isDefinedCustomElement = true;
ops.push({
type: 'custom-element-open',
tagName,
ctor,
staticAttributes: new Map(
node.attrs
.filter((attr) => !attr.name.endsWith(boundAttributeSuffix))
.map((attr) => [attr.name, attr.value])
),
});
}
}
if (node.attrs.length > 0) {
const attrInfo = [] as Array<
[boolean, boolean, typeof node.attrs[0]]
>;
for (const attr of node.attrs) {
const isAttrBinding = attr.name.endsWith(boundAttributeSuffix);
const isElementBinding = attr.name.startsWith(marker);
if (isAttrBinding || isElementBinding) {
boundAttributesCount += 1;
}
attrInfo.push([isAttrBinding, isElementBinding, attr]);
}
if (boundAttributesCount > 0 || node.isDefinedCustomElement) {
// We (may) need to emit a `<!-- lit-node -->` comment marker to
// indicate the following node needs to be identified during
// hydration when it has bindings or if it is a custom element (and
// thus may need its `defer-hydration` to be removed, depending on
// the `deferHydration` setting). The marker is emitted as a
// previous sibling before the node in question, to avoid issues
// with void elements (which do not have children) and raw text
// elements (whose children are intepreted as text).
flushTo(node.sourceCodeLocation!.startTag!.startOffset);
ops.push({
type: 'possible-node-marker',
boundAttributesCount,
nodeIndex,
});
}
for (const [isAttrBinding, isElementBinding, attr] of attrInfo) {
if (isAttrBinding || isElementBinding) {
// Note that although we emit a lit-node comment marker for any
// nodes with bindings, we don't account for it in the nodeIndex because
// that will not be injected into the client template
const strings = attr.value.split(marker);
// We store the case-sensitive name from `attrNames` (generated
// while parsing the template strings); note that this assumes
// parse5 attribute ordering matches string ordering
const name = attrNames[attrIndex++];
const attrSourceLocation =
node.sourceCodeLocation!.attrs![attr.name]!;
const attrNameStartOffset = attrSourceLocation.startOffset;
const attrEndOffset = attrSourceLocation.endOffset;
flushTo(attrNameStartOffset);
if (isAttrBinding) {
const [, prefix, caseSensitiveName] = /([.?@])?(.*)/.exec(
name as string
)!;
ops.push({
type: 'attribute-part',
index: nodeIndex,
name: caseSensitiveName,
ctor:
prefix === '.'
? PropertyPart
: prefix === '?'
? BooleanAttributePart
: prefix === '@'
? EventPart
: AttributePart,
strings,
tagName: tagName.toUpperCase(),
useCustomElementInstance: node.isDefinedCustomElement,
});
} else {
ops.push({
type: 'element-part',
index: nodeIndex,
});
}
skipTo(attrEndOffset);
} else if (node.isDefinedCustomElement) {
// For custom elements, all static attributes are stored along
// with the `custom-element-open` opcode so that we can set them
// into the custom element instance, and then serialize them back
// out along with any manually-reflected attributes. As such, we
// skip over static attribute text here.
const attrSourceLocation =
node.sourceCodeLocation!.attrs![attr.name]!;
flushTo(attrSourceLocation.startOffset);
skipTo(attrSourceLocation.endOffset);
}
}
}
if (node.isDefinedCustomElement) {
// For custom elements, add an opcode to write out attributes,
// close the tag, and then add an opcode to write the shadow
// root
flushTo(node.sourceCodeLocation!.startTag!.endOffset - 1);
ops.push({
type: 'custom-element-attributes',
});
flush('>');
skipTo(node.sourceCodeLocation!.startTag!.endOffset);
ops.push({
type: 'custom-element-shadow',
});
}
nodeIndex++;
}
},
node(node) {
if (isElementNode(node) && node.isDefinedCustomElement) {
ops.push({
type: 'custom-element-close',
});
}
},
});
// Flush remaining static text in the template (e.g. closing tags)
flushTo();
templateCache.set(result.strings, ops);
return ops;
};
export type RenderInfo = {
/**
* Element renderers to use
*/
elementRenderers: ElementRendererConstructor[];
/**
* Stack of open custom elements (in light dom or shadow dom)
*/
customElementInstanceStack: Array<ElementRenderer | undefined>;
/**
* Stack of open host custom elements (n-1 will be n's host)
*/
customElementHostStack: Array<ElementRenderer | undefined>;
/**
* An optional callback to notify when a custom element has been rendered.
*
* This allows servers to know what specific tags were rendered for a given
* template, even in the case of conditional templates.
*/
customElementRendered?: (tagName: string) => void;
/**
* Flag to defer hydration of top level custom element. Defaults to false.
*/
deferHydration: boolean;
};
declare global {
interface Array<T> {
flat(depth: number): Array<T>;
}
}
export function* renderValue(
value: unknown,
renderInfo: RenderInfo
): RenderResult {
patchIfDirective(value);
if (isRenderLightDirective(value)) {
// If a value was produced with renderLight(), we want to call and render
// the renderLight() method.
const instance = getLast(renderInfo.customElementInstanceStack);
if (instance !== undefined) {
const renderLightResult = instance.renderLight(renderInfo);
if (renderLightResult !== undefined) {
yield* renderLightResult;
}
}
value = null;
} else {
value = resolveDirective(
connectedDisconnectable({type: PartType.CHILD}) as ChildPart,
value
);
}
if (value != null && isTemplateResult(value)) {
yield `<!--lit-part ${digestForTemplateResult(value as TemplateResult)}-->`;
yield* renderTemplateResult(value as TemplateResult, renderInfo);
} else {
yield `<!--lit-part-->`;
if (
value === undefined ||
value === null ||
value === nothing ||
value === noChange
) {
// yield nothing
} else if (Array.isArray(value)) {
for (const item of value) {
yield* renderValue(item, renderInfo);
}
} else {
yield escapeHtml(String(value));
}
}
yield `<!--/lit-part-->`;
}
function* renderTemplateResult(
result: TemplateResult,
renderInfo: RenderInfo
): RenderResult {
// In order to render a TemplateResult we have to handle and stream out
// different parts of the result separately:
// - Literal sections of the template
// - Defined custom element within the literal sections
// - Values in the result
//
// This means we can't just iterate through the template literals and values,
// we must parse and traverse the template's HTML. But we don't want to pay
// the cost of serializing the HTML node-by-node when we already have the
// template in string form. So we parse with location info turned on and use
// that to index into the HTML string generated by TemplateResult.getHTML().
// During the tree walk we will handle expression marker nodes and custom
// elements. For each we will record the offset of the node, and output the
// previous span of HTML.
const ops = getTemplateOpcodes(result);
/* The next value in result.values to render */
let partIndex = 0;
for (const op of ops) {
switch (op.type) {
case 'text':
yield op.value;
break;
case 'child-part': {
const value = result.values[partIndex++];
yield* renderValue(value, renderInfo);
break;
}
case 'attribute-part': {
const statics = op.strings;
const part = new op.ctor(
// Passing only object with tagName for the element is fine since the
// directive only gets PartInfo without the node available in the
// constructor
{tagName: op.tagName} as HTMLElement,
op.name,
statics,
connectedDisconnectable(),
{}
);
const value =
part.strings === undefined ? result.values[partIndex] : result.values;
patchAnyDirectives(part, value, partIndex);
let committedValue: unknown = noChange;
// Values for EventParts are never emitted
if (!(part.type === PartType.EVENT)) {
committedValue = getAttributePartCommittedValue(
part,
value,
partIndex
);
}
// We don't emit anything on the server when value is `noChange` or
// `nothing`
if (committedValue !== noChange) {
const instance = op.useCustomElementInstance
? getLast(renderInfo.customElementInstanceStack)
: undefined;
if (part.type === PartType.PROPERTY) {
yield* renderPropertyPart(instance, op, committedValue);
} else if (part.type === PartType.BOOLEAN_ATTRIBUTE) {
// Boolean attribute binding
yield* renderBooleanAttributePart(instance, op, committedValue);
} else {
yield* renderAttributePart(instance, op, committedValue);
}
}
partIndex += statics.length - 1;
break;
}
case 'element-part': {
// We don't emit anything for element parts (since we only support
// directives for now; since they can't render, we don't even bother
// running them), but we still need to advance the part index
partIndex++;
break;
}
case 'custom-element-open': {
// Instantiate the element and its renderer
const instance = getElementRenderer(
renderInfo,
op.tagName,
op.ctor,
op.staticAttributes
);
// Set static attributes to the element renderer
for (const [name, value] of op.staticAttributes) {
instance.setAttribute(name, value);
}
renderInfo.customElementInstanceStack.push(instance);
renderInfo.customElementRendered?.(op.tagName);
break;
}
case 'custom-element-attributes': {
const instance = getLast(renderInfo.customElementInstanceStack);
if (instance === undefined) {
throw new Error(
`Internal error: ${op.type} outside of custom element context`
);
}
// Perform any connect-time work via the renderer (e.g. reflecting any
// properties to attributes, for example)
if (instance.connectedCallback) {
instance.connectedCallback();
}
// Render out any attributes on the instance (both static and those
// that may have been dynamically set by the renderer)
yield* instance.renderAttributes();
// If deferHydration flag is true or if this element is nested in
// another, add the `defer-hydration` attribute, so that it does not
// enable before the host element hydrates
if (
renderInfo.deferHydration ||
renderInfo.customElementHostStack.length > 0
) {
yield ' defer-hydration';
}
break;
}
case 'possible-node-marker': {
// Add a node marker if this element had attribute bindings or if it
// was nested in another and we rendered the `defer-hydration` attribute
// since the hydration node walk will need to stop at this element
// to hydrate it
if (
op.boundAttributesCount > 0 ||
renderInfo.customElementHostStack.length > 0
) {
yield `<!--lit-node ${op.nodeIndex}-->`;
}
break;
}
case 'custom-element-shadow': {
const instance = getLast(renderInfo.customElementInstanceStack);
if (instance === undefined) {
throw new Error(
`Internal error: ${op.type} outside of custom element context`
);
}
renderInfo.customElementHostStack.push(instance);
const shadowContents = instance.renderShadow(renderInfo);
// Only emit a DSR if renderShadow() emitted something (returning
// undefined allows effectively no-op rendering the element)
if (shadowContents !== undefined) {
const {mode = 'open', delegatesFocus} =
instance.shadowRootOptions ?? {};
// `delegatesFocus` is intentionally allowed to coerce to boolean to
// match web platform behavior.
const delegatesfocusAttr = delegatesFocus
? ' shadowrootdelegatesfocus'
: '';
yield `<template shadowroot="${mode}" shadowrootmode="${mode}"${delegatesfocusAttr}>`;
yield* shadowContents;
yield '</template>';
}
renderInfo.customElementHostStack.pop();
break;
}
case 'custom-element-close':
renderInfo.customElementInstanceStack.pop();
break;
default:
throw new Error('internal error');
}
}
if (partIndex !== result.values.length) {
throw new Error(
`unexpected final partIndex: ${partIndex} !== ${result.values.length}`
);
}
}
function* renderPropertyPart(
instance: ElementRenderer | undefined,
op: AttributePartOp,
value: unknown
) {
value = value === nothing ? undefined : value;
// Property should be reflected to attribute
const reflectedName = reflectedAttributeName(op.tagName, op.name);
if (instance !== undefined) {
instance.setProperty(op.name, value);
}
if (reflectedName !== undefined) {
yield `${reflectedName}="${escapeHtml(String(value))}"`;
}
}
function* renderBooleanAttributePart(
instance: ElementRenderer | undefined,
op: AttributePartOp,
value: unknown
) {
if (value && value !== nothing) {
if (instance !== undefined) {
instance.setAttribute(op.name, '');
} else {
yield op.name;
}
}
}
function* renderAttributePart(
instance: ElementRenderer | undefined,
op: AttributePartOp,
value: unknown
) {
if (value !== nothing) {
if (instance !== undefined) {
instance.setAttribute(op.name, String(value ?? ''));
} else {
yield `${op.name}="${escapeHtml(String(value ?? ''))}"`;
}
}
}
const getLast = <T>(a: Array<T>) => a[a.length - 1];