UNPKG

25.1 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2017 Google LLC
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6import type { Directive } from './directive.js';
7import type { TrustedHTML } from 'trusted-types/lib';
8/**
9 * Contains types that are part of the unstable debug API.
10 *
11 * Everything in this API is not stable and may change or be removed in the future,
12 * even on patch releases.
13 */
14export declare namespace LitUnstable {
15 /**
16 * When Lit is running in dev mode and `window.emitLitDebugLogEvents` is true,
17 * we will emit 'lit-debug' events to window, with live details about the update and render
18 * lifecycle. These can be useful for writing debug tooling and visualizations.
19 *
20 * Please be aware that running with window.emitLitDebugLogEvents has performance overhead,
21 * making certain operations that are normally very cheap (like a no-op render) much slower,
22 * because we must copy data and dispatch events.
23 */
24 namespace DebugLog {
25 type Entry = TemplatePrep | TemplateInstantiated | TemplateInstantiatedAndUpdated | TemplateUpdating | BeginRender | EndRender | CommitPartEntry | SetPartValue;
26 interface TemplatePrep {
27 kind: 'template prep';
28 template: Template;
29 strings: TemplateStringsArray;
30 clonableTemplate: HTMLTemplateElement;
31 parts: TemplatePart[];
32 }
33 interface BeginRender {
34 kind: 'begin render';
35 id: number;
36 value: unknown;
37 container: HTMLElement | DocumentFragment;
38 options: RenderOptions | undefined;
39 part: ChildPart | undefined;
40 }
41 interface EndRender {
42 kind: 'end render';
43 id: number;
44 value: unknown;
45 container: HTMLElement | DocumentFragment;
46 options: RenderOptions | undefined;
47 part: ChildPart;
48 }
49 interface TemplateInstantiated {
50 kind: 'template instantiated';
51 template: Template | CompiledTemplate;
52 instance: TemplateInstance;
53 options: RenderOptions | undefined;
54 fragment: Node;
55 parts: Array<Part | undefined>;
56 values: unknown[];
57 }
58 interface TemplateInstantiatedAndUpdated {
59 kind: 'template instantiated and updated';
60 template: Template | CompiledTemplate;
61 instance: TemplateInstance;
62 options: RenderOptions | undefined;
63 fragment: Node;
64 parts: Array<Part | undefined>;
65 values: unknown[];
66 }
67 interface TemplateUpdating {
68 kind: 'template updating';
69 template: Template | CompiledTemplate;
70 instance: TemplateInstance;
71 options: RenderOptions | undefined;
72 parts: Array<Part | undefined>;
73 values: unknown[];
74 }
75 interface SetPartValue {
76 kind: 'set part';
77 part: Part;
78 value: unknown;
79 valueIndex: number;
80 values: unknown[];
81 templateInstance: TemplateInstance;
82 }
83 type CommitPartEntry = CommitNothingToChildEntry | CommitText | CommitNode | CommitAttribute | CommitProperty | CommitBooleanAttribute | CommitEventListener | CommitToElementBinding;
84 interface CommitNothingToChildEntry {
85 kind: 'commit nothing to child';
86 start: ChildNode;
87 end: ChildNode | null;
88 parent: Disconnectable | undefined;
89 options: RenderOptions | undefined;
90 }
91 interface CommitText {
92 kind: 'commit text';
93 node: Text;
94 value: unknown;
95 options: RenderOptions | undefined;
96 }
97 interface CommitNode {
98 kind: 'commit node';
99 start: Node;
100 parent: Disconnectable | undefined;
101 value: Node;
102 options: RenderOptions | undefined;
103 }
104 interface CommitAttribute {
105 kind: 'commit attribute';
106 element: Element;
107 name: string;
108 value: unknown;
109 options: RenderOptions | undefined;
110 }
111 interface CommitProperty {
112 kind: 'commit property';
113 element: Element;
114 name: string;
115 value: unknown;
116 options: RenderOptions | undefined;
117 }
118 interface CommitBooleanAttribute {
119 kind: 'commit boolean attribute';
120 element: Element;
121 name: string;
122 value: boolean;
123 options: RenderOptions | undefined;
124 }
125 interface CommitEventListener {
126 kind: 'commit event listener';
127 element: Element;
128 name: string;
129 value: unknown;
130 oldListener: unknown;
131 options: RenderOptions | undefined;
132 removeListener: boolean;
133 addListener: boolean;
134 }
135 interface CommitToElementBinding {
136 kind: 'commit to element binding';
137 element: Element;
138 value: unknown;
139 options: RenderOptions | undefined;
140 }
141 }
142}
143/**
144 * Used to sanitize any value before it is written into the DOM. This can be
145 * used to implement a security policy of allowed and disallowed values in
146 * order to prevent XSS attacks.
147 *
148 * One way of using this callback would be to check attributes and properties
149 * against a list of high risk fields, and require that values written to such
150 * fields be instances of a class which is safe by construction. Closure's Safe
151 * HTML Types is one implementation of this technique (
152 * https://github.com/google/safe-html-types/blob/master/doc/safehtml-types.md).
153 * The TrustedTypes polyfill in API-only mode could also be used as a basis
154 * for this technique (https://github.com/WICG/trusted-types).
155 *
156 * @param node The HTML node (usually either a #text node or an Element) that
157 * is being written to. Note that this is just an exemplar node, the write
158 * may take place against another instance of the same class of node.
159 * @param name The name of an attribute or property (for example, 'href').
160 * @param type Indicates whether the write that's about to be performed will
161 * be to a property or a node.
162 * @return A function that will sanitize this class of writes.
163 */
164export type SanitizerFactory = (node: Node, name: string, type: 'property' | 'attribute') => ValueSanitizer;
165/**
166 * A function which can sanitize values that will be written to a specific kind
167 * of DOM sink.
168 *
169 * See SanitizerFactory.
170 *
171 * @param value The value to sanitize. Will be the actual value passed into
172 * the lit-html template literal, so this could be of any type.
173 * @return The value to write to the DOM. Usually the same as the input value,
174 * unless sanitization is needed.
175 */
176export type ValueSanitizer = (value: unknown) => unknown;
177/** TemplateResult types */
178declare const HTML_RESULT = 1;
179declare const SVG_RESULT = 2;
180declare const MATHML_RESULT = 3;
181type ResultType = typeof HTML_RESULT | typeof SVG_RESULT | typeof MATHML_RESULT;
182declare const ATTRIBUTE_PART = 1;
183declare const CHILD_PART = 2;
184declare const PROPERTY_PART = 3;
185declare const BOOLEAN_ATTRIBUTE_PART = 4;
186declare const EVENT_PART = 5;
187declare const ELEMENT_PART = 6;
188declare const COMMENT_PART = 7;
189/**
190 * The return type of the template tag functions, {@linkcode html} and
191 * {@linkcode svg} when it hasn't been compiled by @lit-labs/compiler.
192 *
193 * A `TemplateResult` object holds all the information about a template
194 * expression required to render it: the template strings, expression values,
195 * and type of template (html or svg).
196 *
197 * `TemplateResult` objects do not create any DOM on their own. To create or
198 * update DOM you need to render the `TemplateResult`. See
199 * [Rendering](https://lit.dev/docs/components/rendering) for more information.
200 *
201 */
202export type UncompiledTemplateResult<T extends ResultType = ResultType> = {
203 ['_$litType$']: T;
204 strings: TemplateStringsArray;
205 values: unknown[];
206};
207/**
208 * This is a template result that may be either uncompiled or compiled.
209 *
210 * In the future, TemplateResult will be this type. If you want to explicitly
211 * note that a template result is potentially compiled, you can reference this
212 * type and it will continue to behave the same through the next major version
213 * of Lit. This can be useful for code that wants to prepare for the next
214 * major version of Lit.
215 */
216export type MaybeCompiledTemplateResult<T extends ResultType = ResultType> = UncompiledTemplateResult<T> | CompiledTemplateResult;
217/**
218 * The return type of the template tag functions, {@linkcode html} and
219 * {@linkcode svg}.
220 *
221 * A `TemplateResult` object holds all the information about a template
222 * expression required to render it: the template strings, expression values,
223 * and type of template (html or svg).
224 *
225 * `TemplateResult` objects do not create any DOM on their own. To create or
226 * update DOM you need to render the `TemplateResult`. See
227 * [Rendering](https://lit.dev/docs/components/rendering) for more information.
228 *
229 * In Lit 4, this type will be an alias of
230 * MaybeCompiledTemplateResult, so that code will get type errors if it assumes
231 * that Lit templates are not compiled. When deliberately working with only
232 * one, use either {@linkcode CompiledTemplateResult} or
233 * {@linkcode UncompiledTemplateResult} explicitly.
234 */
235export type TemplateResult<T extends ResultType = ResultType> = UncompiledTemplateResult<T>;
236export type HTMLTemplateResult = TemplateResult<typeof HTML_RESULT>;
237export type SVGTemplateResult = TemplateResult<typeof SVG_RESULT>;
238export type MathMLTemplateResult = TemplateResult<typeof MATHML_RESULT>;
239/**
240 * A TemplateResult that has been compiled by @lit-labs/compiler, skipping the
241 * prepare step.
242 */
243export interface CompiledTemplateResult {
244 ['_$litType$']: CompiledTemplate;
245 values: unknown[];
246}
247export interface CompiledTemplate extends Omit<Template, 'el'> {
248 el?: HTMLTemplateElement;
249 h: TemplateStringsArray;
250}
251/**
252 * Interprets a template literal as an HTML template that can efficiently
253 * render to and update a container.
254 *
255 * ```ts
256 * const header = (title: string) => html`<h1>${title}</h1>`;
257 * ```
258 *
259 * The `html` tag returns a description of the DOM to render as a value. It is
260 * lazy, meaning no work is done until the template is rendered. When rendering,
261 * if a template comes from the same expression as a previously rendered result,
262 * it's efficiently updated instead of replaced.
263 */
264export declare const html: (strings: TemplateStringsArray, ...values: unknown[]) => TemplateResult<1>;
265/**
266 * Interprets a template literal as an SVG fragment that can efficiently render
267 * to and update a container.
268 *
269 * ```ts
270 * const rect = svg`<rect width="10" height="10"></rect>`;
271 *
272 * const myImage = html`
273 * <svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">
274 * ${rect}
275 * </svg>`;
276 * ```
277 *
278 * The `svg` *tag function* should only be used for SVG fragments, or elements
279 * that would be contained **inside** an `<svg>` HTML element. A common error is
280 * placing an `<svg>` *element* in a template tagged with the `svg` tag
281 * function. The `<svg>` element is an HTML element and should be used within a
282 * template tagged with the {@linkcode html} tag function.
283 *
284 * In LitElement usage, it's invalid to return an SVG fragment from the
285 * `render()` method, as the SVG fragment will be contained within the element's
286 * shadow root and thus not be properly contained within an `<svg>` HTML
287 * element.
288 */
289export declare const svg: (strings: TemplateStringsArray, ...values: unknown[]) => TemplateResult<2>;
290/**
291 * Interprets a template literal as MathML fragment that can efficiently render
292 * to and update a container.
293 *
294 * ```ts
295 * const num = mathml`<mn>1</mn>`;
296 *
297 * const eq = html`
298 * <math>
299 * ${num}
300 * </math>`;
301 * ```
302 *
303 * The `mathml` *tag function* should only be used for MathML fragments, or
304 * elements that would be contained **inside** a `<math>` HTML element. A common
305 * error is placing a `<math>` *element* in a template tagged with the `mathml`
306 * tag function. The `<math>` element is an HTML element and should be used
307 * within a template tagged with the {@linkcode html} tag function.
308 *
309 * In LitElement usage, it's invalid to return an MathML fragment from the
310 * `render()` method, as the MathML fragment will be contained within the
311 * element's shadow root and thus not be properly contained within a `<math>`
312 * HTML element.
313 */
314export declare const mathml: (strings: TemplateStringsArray, ...values: unknown[]) => TemplateResult<3>;
315/**
316 * A sentinel value that signals that a value was handled by a directive and
317 * should not be written to the DOM.
318 */
319export declare const noChange: unique symbol;
320/**
321 * A sentinel value that signals a ChildPart to fully clear its content.
322 *
323 * ```ts
324 * const button = html`${
325 * user.isAdmin
326 * ? html`<button>DELETE</button>`
327 * : nothing
328 * }`;
329 * ```
330 *
331 * Prefer using `nothing` over other falsy values as it provides a consistent
332 * behavior between various expression binding contexts.
333 *
334 * In child expressions, `undefined`, `null`, `''`, and `nothing` all behave the
335 * same and render no nodes. In attribute expressions, `nothing` _removes_ the
336 * attribute, while `undefined` and `null` will render an empty string. In
337 * property expressions `nothing` becomes `undefined`.
338 */
339export declare const nothing: unique symbol;
340/**
341 * Object specifying options for controlling lit-html rendering. Note that
342 * while `render` may be called multiple times on the same `container` (and
343 * `renderBefore` reference node) to efficiently update the rendered content,
344 * only the options passed in during the first render are respected during
345 * the lifetime of renders to that unique `container` + `renderBefore`
346 * combination.
347 */
348export interface RenderOptions {
349 /**
350 * An object to use as the `this` value for event listeners. It's often
351 * useful to set this to the host component rendering a template.
352 */
353 host?: object;
354 /**
355 * A DOM node before which to render content in the container.
356 */
357 renderBefore?: ChildNode | null;
358 /**
359 * Node used for cloning the template (`importNode` will be called on this
360 * node). This controls the `ownerDocument` of the rendered DOM, along with
361 * any inherited context. Defaults to the global `document`.
362 */
363 creationScope?: {
364 importNode(node: Node, deep?: boolean): Node;
365 };
366 /**
367 * The initial connected state for the top-level part being rendered. If no
368 * `isConnected` option is set, `AsyncDirective`s will be connected by
369 * default. Set to `false` if the initial render occurs in a disconnected tree
370 * and `AsyncDirective`s should see `isConnected === false` for their initial
371 * render. The `part.setConnected()` method must be used subsequent to initial
372 * render to change the connected state of the part.
373 */
374 isConnected?: boolean;
375}
376export interface DirectiveParent {
377 _$parent?: DirectiveParent;
378 _$isConnected: boolean;
379 __directive?: Directive;
380 __directives?: Array<Directive | undefined>;
381}
382declare class Template {
383 parts: Array<TemplatePart>;
384 constructor({ strings, ['_$litType$']: type }: UncompiledTemplateResult, options?: RenderOptions);
385 /** @nocollapse */
386 static createElement(html: TrustedHTML, _options?: RenderOptions): HTMLTemplateElement;
387}
388export interface Disconnectable {
389 _$parent?: Disconnectable;
390 _$disconnectableChildren?: Set<Disconnectable>;
391 _$isConnected: boolean;
392}
393declare function resolveDirective(part: ChildPart | AttributePart | ElementPart, value: unknown, parent?: DirectiveParent, attributeIndex?: number): unknown;
394export type { TemplateInstance };
395/**
396 * An updateable instance of a Template. Holds references to the Parts used to
397 * update the template instance.
398 */
399declare class TemplateInstance implements Disconnectable {
400 _$template: Template;
401 _$parts: Array<Part | undefined>;
402 constructor(template: Template, parent: ChildPart);
403 get parentNode(): Node;
404 get _$isConnected(): boolean;
405 _clone(options: RenderOptions | undefined): Node;
406 _update(values: Array<unknown>): void;
407}
408type AttributeTemplatePart = {
409 readonly type: typeof ATTRIBUTE_PART;
410 readonly index: number;
411 readonly name: string;
412 readonly ctor: typeof AttributePart;
413 readonly strings: ReadonlyArray<string>;
414};
415type ChildTemplatePart = {
416 readonly type: typeof CHILD_PART;
417 readonly index: number;
418};
419type ElementTemplatePart = {
420 readonly type: typeof ELEMENT_PART;
421 readonly index: number;
422};
423type CommentTemplatePart = {
424 readonly type: typeof COMMENT_PART;
425 readonly index: number;
426};
427/**
428 * A TemplatePart represents a dynamic part in a template, before the template
429 * is instantiated. When a template is instantiated Parts are created from
430 * TemplateParts.
431 */
432type TemplatePart = ChildTemplatePart | AttributeTemplatePart | ElementTemplatePart | CommentTemplatePart;
433export type Part = ChildPart | AttributePart | PropertyPart | BooleanAttributePart | ElementPart | EventPart;
434export type { ChildPart };
435declare class ChildPart implements Disconnectable {
436 readonly type = 2;
437 readonly options: RenderOptions | undefined;
438 _$committedValue: unknown;
439 private _textSanitizer;
440 get _$isConnected(): boolean;
441 constructor(startNode: ChildNode, endNode: ChildNode | null, parent: TemplateInstance | ChildPart | undefined, options: RenderOptions | undefined);
442 /**
443 * The parent node into which the part renders its content.
444 *
445 * A ChildPart's content consists of a range of adjacent child nodes of
446 * `.parentNode`, possibly bordered by 'marker nodes' (`.startNode` and
447 * `.endNode`).
448 *
449 * - If both `.startNode` and `.endNode` are non-null, then the part's content
450 * consists of all siblings between `.startNode` and `.endNode`, exclusively.
451 *
452 * - If `.startNode` is non-null but `.endNode` is null, then the part's
453 * content consists of all siblings following `.startNode`, up to and
454 * including the last child of `.parentNode`. If `.endNode` is non-null, then
455 * `.startNode` will always be non-null.
456 *
457 * - If both `.endNode` and `.startNode` are null, then the part's content
458 * consists of all child nodes of `.parentNode`.
459 */
460 get parentNode(): Node;
461 /**
462 * The part's leading marker node, if any. See `.parentNode` for more
463 * information.
464 */
465 get startNode(): Node | null;
466 /**
467 * The part's trailing marker node, if any. See `.parentNode` for more
468 * information.
469 */
470 get endNode(): Node | null;
471 _$setValue(value: unknown, directiveParent?: DirectiveParent): void;
472 private _insert;
473 private _commitNode;
474 private _commitText;
475 private _commitTemplateResult;
476 private _commitIterable;
477}
478/**
479 * A top-level `ChildPart` returned from `render` that manages the connected
480 * state of `AsyncDirective`s created throughout the tree below it.
481 */
482export interface RootPart extends ChildPart {
483 /**
484 * Sets the connection state for `AsyncDirective`s contained within this root
485 * ChildPart.
486 *
487 * lit-html does not automatically monitor the connectedness of DOM rendered;
488 * as such, it is the responsibility of the caller to `render` to ensure that
489 * `part.setConnected(false)` is called before the part object is potentially
490 * discarded, to ensure that `AsyncDirective`s have a chance to dispose of
491 * any resources being held. If a `RootPart` that was previously
492 * disconnected is subsequently re-connected (and its `AsyncDirective`s should
493 * re-connect), `setConnected(true)` should be called.
494 *
495 * @param isConnected Whether directives within this tree should be connected
496 * or not
497 */
498 setConnected(isConnected: boolean): void;
499}
500export type { AttributePart };
501declare class AttributePart implements Disconnectable {
502 readonly type: typeof ATTRIBUTE_PART | typeof PROPERTY_PART | typeof BOOLEAN_ATTRIBUTE_PART | typeof EVENT_PART;
503 readonly element: HTMLElement;
504 readonly name: string;
505 readonly options: RenderOptions | undefined;
506 /**
507 * If this attribute part represents an interpolation, this contains the
508 * static strings of the interpolation. For single-value, complete bindings,
509 * this is undefined.
510 */
511 readonly strings?: ReadonlyArray<string>;
512 protected _sanitizer: ValueSanitizer | undefined;
513 get tagName(): string;
514 get _$isConnected(): boolean;
515 constructor(element: HTMLElement, name: string, strings: ReadonlyArray<string>, parent: Disconnectable, options: RenderOptions | undefined);
516}
517export type { PropertyPart };
518declare class PropertyPart extends AttributePart {
519 readonly type = 3;
520}
521export type { BooleanAttributePart };
522declare class BooleanAttributePart extends AttributePart {
523 readonly type = 4;
524}
525/**
526 * An AttributePart that manages an event listener via add/removeEventListener.
527 *
528 * This part works by adding itself as the event listener on an element, then
529 * delegating to the value passed to it. This reduces the number of calls to
530 * add/removeEventListener if the listener changes frequently, such as when an
531 * inline function is used as a listener.
532 *
533 * Because event options are passed when adding listeners, we must take case
534 * to add and remove the part as a listener when the event options change.
535 */
536export type { EventPart };
537declare class EventPart extends AttributePart {
538 readonly type = 5;
539 constructor(element: HTMLElement, name: string, strings: ReadonlyArray<string>, parent: Disconnectable, options: RenderOptions | undefined);
540 handleEvent(event: Event): void;
541}
542export type { ElementPart };
543declare class ElementPart implements Disconnectable {
544 element: Element;
545 readonly type = 6;
546 _$committedValue: undefined;
547 options: RenderOptions | undefined;
548 constructor(element: Element, parent: Disconnectable, options: RenderOptions | undefined);
549 get _$isConnected(): boolean;
550 _$setValue(value: unknown): void;
551}
552/**
553 * END USERS SHOULD NOT RELY ON THIS OBJECT.
554 *
555 * Private exports for use by other Lit packages, not intended for use by
556 * external users.
557 *
558 * We currently do not make a mangled rollup build of the lit-ssr code. In order
559 * to keep a number of (otherwise private) top-level exports mangled in the
560 * client side code, we export a _$LH object containing those members (or
561 * helper methods for accessing private fields of those members), and then
562 * re-export them for use in lit-ssr. This keeps lit-ssr agnostic to whether the
563 * client-side code is being used in `dev` mode or `prod` mode.
564 *
565 * This has a unique name, to disambiguate it from private exports in
566 * lit-element, which re-exports all of lit-html.
567 *
568 * @private
569 */
570export declare const _$LH: {
571 _boundAttributeSuffix: string;
572 _marker: string;
573 _markerMatch: string;
574 _HTML_RESULT: number;
575 _getTemplateHtml: (strings: TemplateStringsArray, type: ResultType) => [TrustedHTML, Array<string>];
576 _TemplateInstance: typeof TemplateInstance;
577 _isIterable: (value: unknown) => value is Iterable<unknown>;
578 _resolveDirective: typeof resolveDirective;
579 _ChildPart: typeof ChildPart;
580 _AttributePart: typeof AttributePart;
581 _BooleanAttributePart: typeof BooleanAttributePart;
582 _EventPart: typeof EventPart;
583 _PropertyPart: typeof PropertyPart;
584 _ElementPart: typeof ElementPart;
585};
586/**
587 * Renders a value, usually a lit-html TemplateResult, to the container.
588 *
589 * This example renders the text "Hello, Zoe!" inside a paragraph tag, appending
590 * it to the container `document.body`.
591 *
592 * ```js
593 * import {html, render} from 'lit';
594 *
595 * const name = "Zoe";
596 * render(html`<p>Hello, ${name}!</p>`, document.body);
597 * ```
598 *
599 * @param value Any [renderable
600 * value](https://lit.dev/docs/templates/expressions/#child-expressions),
601 * typically a {@linkcode TemplateResult} created by evaluating a template tag
602 * like {@linkcode html} or {@linkcode svg}.
603 * @param container A DOM container to render to. The first render will append
604 * the rendered value to the container, and subsequent renders will
605 * efficiently update the rendered value if the same result type was
606 * previously rendered there.
607 * @param options See {@linkcode RenderOptions} for options documentation.
608 * @see
609 * {@link https://lit.dev/docs/libraries/standalone-templates/#rendering-lit-html-templates| Rendering Lit HTML Templates}
610 */
611export declare const render: {
612 (value: unknown, container: HTMLElement | DocumentFragment, options?: RenderOptions): RootPart;
613 setSanitizer: (newSanitizer: SanitizerFactory) => void;
614 createSanitizer: SanitizerFactory;
615 _testOnlyClearSanitizerFactoryDoNotCallOrElse: () => void;
616};
617//# sourceMappingURL=lit-html.d.ts.map
\No newline at end of file