UNPKG

23.7 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2017 Google LLC
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6/// <reference types="trusted-types" />
7import type { Directive } from './directive.js';
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;
180type ResultType = typeof HTML_RESULT | typeof SVG_RESULT;
181declare const ATTRIBUTE_PART = 1;
182declare const CHILD_PART = 2;
183declare const ELEMENT_PART = 6;
184declare const COMMENT_PART = 7;
185/**
186 * The return type of the template tag functions, {@linkcode html} and
187 * {@linkcode svg} when it hasn't been compiled by @lit-labs/compiler.
188 *
189 * A `TemplateResult` object holds all the information about a template
190 * expression required to render it: the template strings, expression values,
191 * and type of template (html or svg).
192 *
193 * `TemplateResult` objects do not create any DOM on their own. To create or
194 * update DOM you need to render the `TemplateResult`. See
195 * [Rendering](https://lit.dev/docs/components/rendering) for more information.
196 *
197 */
198export type UncompiledTemplateResult<T extends ResultType = ResultType> = {
199 ['_$litType$']: T;
200 strings: TemplateStringsArray;
201 values: unknown[];
202};
203/**
204 * This is a template result that may be either uncompiled or compiled.
205 *
206 * In the future, TemplateResult will be this type. If you want to explicitly
207 * note that a template result is potentially compiled, you can reference this
208 * type and it will continue to behave the same through the next major version
209 * of Lit. This can be useful for code that wants to prepare for the next
210 * major version of Lit.
211 */
212export type MaybeCompiledTemplateResult<T extends ResultType = ResultType> = UncompiledTemplateResult<T> | CompiledTemplateResult;
213/**
214 * The return type of the template tag functions, {@linkcode html} and
215 * {@linkcode svg}.
216 *
217 * A `TemplateResult` object holds all the information about a template
218 * expression required to render it: the template strings, expression values,
219 * and type of template (html or svg).
220 *
221 * `TemplateResult` objects do not create any DOM on their own. To create or
222 * update DOM you need to render the `TemplateResult`. See
223 * [Rendering](https://lit.dev/docs/components/rendering) for more information.
224 *
225 * In Lit 4, this type will be an alias of
226 * MaybeCompiledTemplateResult, so that code will get type errors if it assumes
227 * that Lit templates are not compiled. When deliberately working with only
228 * one, use either {@linkcode CompiledTemplateResult} or
229 * {@linkcode UncompiledTemplateResult} explicitly.
230 */
231export type TemplateResult<T extends ResultType = ResultType> = UncompiledTemplateResult<T>;
232export type HTMLTemplateResult = TemplateResult<typeof HTML_RESULT>;
233export type SVGTemplateResult = TemplateResult<typeof SVG_RESULT>;
234/**
235 * A TemplateResult that has been compiled by @lit-labs/compiler, skipping the
236 * prepare step.
237 */
238export interface CompiledTemplateResult {
239 ['_$litType$']: CompiledTemplate;
240 values: unknown[];
241}
242export interface CompiledTemplate extends Omit<Template, 'el'> {
243 el?: HTMLTemplateElement;
244 h: TemplateStringsArray;
245}
246/**
247 * Interprets a template literal as an HTML template that can efficiently
248 * render to and update a container.
249 *
250 * ```ts
251 * const header = (title: string) => html`<h1>${title}</h1>`;
252 * ```
253 *
254 * The `html` tag returns a description of the DOM to render as a value. It is
255 * lazy, meaning no work is done until the template is rendered. When rendering,
256 * if a template comes from the same expression as a previously rendered result,
257 * it's efficiently updated instead of replaced.
258 */
259export declare const html: (strings: TemplateStringsArray, ...values: unknown[]) => TemplateResult<1>;
260/**
261 * Interprets a template literal as an SVG fragment that can efficiently
262 * render to and update a container.
263 *
264 * ```ts
265 * const rect = svg`<rect width="10" height="10"></rect>`;
266 *
267 * const myImage = html`
268 * <svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">
269 * ${rect}
270 * </svg>`;
271 * ```
272 *
273 * The `svg` *tag function* should only be used for SVG fragments, or elements
274 * that would be contained **inside** an `<svg>` HTML element. A common error is
275 * placing an `<svg>` *element* in a template tagged with the `svg` tag
276 * function. The `<svg>` element is an HTML element and should be used within a
277 * template tagged with the {@linkcode html} tag function.
278 *
279 * In LitElement usage, it's invalid to return an SVG fragment from the
280 * `render()` method, as the SVG fragment will be contained within the element's
281 * shadow root and thus cannot be used within an `<svg>` HTML element.
282 */
283export declare const svg: (strings: TemplateStringsArray, ...values: unknown[]) => TemplateResult<2>;
284/**
285 * A sentinel value that signals that a value was handled by a directive and
286 * should not be written to the DOM.
287 */
288export declare const noChange: unique symbol;
289/**
290 * A sentinel value that signals a ChildPart to fully clear its content.
291 *
292 * ```ts
293 * const button = html`${
294 * user.isAdmin
295 * ? html`<button>DELETE</button>`
296 * : nothing
297 * }`;
298 * ```
299 *
300 * Prefer using `nothing` over other falsy values as it provides a consistent
301 * behavior between various expression binding contexts.
302 *
303 * In child expressions, `undefined`, `null`, `''`, and `nothing` all behave the
304 * same and render no nodes. In attribute expressions, `nothing` _removes_ the
305 * attribute, while `undefined` and `null` will render an empty string. In
306 * property expressions `nothing` becomes `undefined`.
307 */
308export declare const nothing: unique symbol;
309/**
310 * Object specifying options for controlling lit-html rendering. Note that
311 * while `render` may be called multiple times on the same `container` (and
312 * `renderBefore` reference node) to efficiently update the rendered content,
313 * only the options passed in during the first render are respected during
314 * the lifetime of renders to that unique `container` + `renderBefore`
315 * combination.
316 */
317export interface RenderOptions {
318 /**
319 * An object to use as the `this` value for event listeners. It's often
320 * useful to set this to the host component rendering a template.
321 */
322 host?: object;
323 /**
324 * A DOM node before which to render content in the container.
325 */
326 renderBefore?: ChildNode | null;
327 /**
328 * Node used for cloning the template (`importNode` will be called on this
329 * node). This controls the `ownerDocument` of the rendered DOM, along with
330 * any inherited context. Defaults to the global `document`.
331 */
332 creationScope?: {
333 importNode(node: Node, deep?: boolean): Node;
334 };
335 /**
336 * The initial connected state for the top-level part being rendered. If no
337 * `isConnected` option is set, `AsyncDirective`s will be connected by
338 * default. Set to `false` if the initial render occurs in a disconnected tree
339 * and `AsyncDirective`s should see `isConnected === false` for their initial
340 * render. The `part.setConnected()` method must be used subsequent to initial
341 * render to change the connected state of the part.
342 */
343 isConnected?: boolean;
344}
345export interface DirectiveParent {
346 _$parent?: DirectiveParent;
347 _$isConnected: boolean;
348 __directive?: Directive;
349 __directives?: Array<Directive | undefined>;
350}
351declare class Template {
352 parts: Array<TemplatePart>;
353 constructor({ strings, ['_$litType$']: type }: UncompiledTemplateResult, options?: RenderOptions);
354 /** @nocollapse */
355 static createElement(html: TrustedHTML, _options?: RenderOptions): HTMLTemplateElement;
356}
357export interface Disconnectable {
358 _$parent?: Disconnectable;
359 _$disconnectableChildren?: Set<Disconnectable>;
360 _$isConnected: boolean;
361}
362declare function resolveDirective(part: ChildPart | AttributePart | ElementPart, value: unknown, parent?: DirectiveParent, attributeIndex?: number): unknown;
363export type { TemplateInstance };
364/**
365 * An updateable instance of a Template. Holds references to the Parts used to
366 * update the template instance.
367 */
368declare class TemplateInstance implements Disconnectable {
369 _$template: Template;
370 _$parts: Array<Part | undefined>;
371 constructor(template: Template, parent: ChildPart);
372 get parentNode(): Node;
373 get _$isConnected(): boolean;
374 _clone(options: RenderOptions | undefined): Node;
375 _update(values: Array<unknown>): void;
376}
377type AttributeTemplatePart = {
378 readonly type: typeof ATTRIBUTE_PART;
379 readonly index: number;
380 readonly name: string;
381 readonly ctor: typeof AttributePart;
382 readonly strings: ReadonlyArray<string>;
383};
384type ChildTemplatePart = {
385 readonly type: typeof CHILD_PART;
386 readonly index: number;
387};
388type ElementTemplatePart = {
389 readonly type: typeof ELEMENT_PART;
390 readonly index: number;
391};
392type CommentTemplatePart = {
393 readonly type: typeof COMMENT_PART;
394 readonly index: number;
395};
396/**
397 * A TemplatePart represents a dynamic part in a template, before the template
398 * is instantiated. When a template is instantiated Parts are created from
399 * TemplateParts.
400 */
401type TemplatePart = ChildTemplatePart | AttributeTemplatePart | ElementTemplatePart | CommentTemplatePart;
402export type Part = ChildPart | AttributePart | PropertyPart | BooleanAttributePart | ElementPart | EventPart;
403export type { ChildPart };
404declare class ChildPart implements Disconnectable {
405 readonly type = 2;
406 readonly options: RenderOptions | undefined;
407 _$committedValue: unknown;
408 private _textSanitizer;
409 get _$isConnected(): boolean;
410 constructor(startNode: ChildNode, endNode: ChildNode | null, parent: TemplateInstance | ChildPart | undefined, options: RenderOptions | undefined);
411 /**
412 * The parent node into which the part renders its content.
413 *
414 * A ChildPart's content consists of a range of adjacent child nodes of
415 * `.parentNode`, possibly bordered by 'marker nodes' (`.startNode` and
416 * `.endNode`).
417 *
418 * - If both `.startNode` and `.endNode` are non-null, then the part's content
419 * consists of all siblings between `.startNode` and `.endNode`, exclusively.
420 *
421 * - If `.startNode` is non-null but `.endNode` is null, then the part's
422 * content consists of all siblings following `.startNode`, up to and
423 * including the last child of `.parentNode`. If `.endNode` is non-null, then
424 * `.startNode` will always be non-null.
425 *
426 * - If both `.endNode` and `.startNode` are null, then the part's content
427 * consists of all child nodes of `.parentNode`.
428 */
429 get parentNode(): Node;
430 /**
431 * The part's leading marker node, if any. See `.parentNode` for more
432 * information.
433 */
434 get startNode(): Node | null;
435 /**
436 * The part's trailing marker node, if any. See `.parentNode` for more
437 * information.
438 */
439 get endNode(): Node | null;
440 _$setValue(value: unknown, directiveParent?: DirectiveParent): void;
441 private _insert;
442 private _commitNode;
443 private _commitText;
444 private _commitTemplateResult;
445 private _commitIterable;
446}
447/**
448 * A top-level `ChildPart` returned from `render` that manages the connected
449 * state of `AsyncDirective`s created throughout the tree below it.
450 */
451export interface RootPart extends ChildPart {
452 /**
453 * Sets the connection state for `AsyncDirective`s contained within this root
454 * ChildPart.
455 *
456 * lit-html does not automatically monitor the connectedness of DOM rendered;
457 * as such, it is the responsibility of the caller to `render` to ensure that
458 * `part.setConnected(false)` is called before the part object is potentially
459 * discarded, to ensure that `AsyncDirective`s have a chance to dispose of
460 * any resources being held. If a `RootPart` that was previously
461 * disconnected is subsequently re-connected (and its `AsyncDirective`s should
462 * re-connect), `setConnected(true)` should be called.
463 *
464 * @param isConnected Whether directives within this tree should be connected
465 * or not
466 */
467 setConnected(isConnected: boolean): void;
468}
469export type { AttributePart };
470declare class AttributePart implements Disconnectable {
471 readonly type: 1 | 3 | 4 | 5;
472 readonly element: HTMLElement;
473 readonly name: string;
474 readonly options: RenderOptions | undefined;
475 /**
476 * If this attribute part represents an interpolation, this contains the
477 * static strings of the interpolation. For single-value, complete bindings,
478 * this is undefined.
479 */
480 readonly strings?: ReadonlyArray<string>;
481 protected _sanitizer: ValueSanitizer | undefined;
482 get tagName(): string;
483 get _$isConnected(): boolean;
484 constructor(element: HTMLElement, name: string, strings: ReadonlyArray<string>, parent: Disconnectable, options: RenderOptions | undefined);
485}
486export type { PropertyPart };
487declare class PropertyPart extends AttributePart {
488 readonly type = 3;
489}
490export type { BooleanAttributePart };
491declare class BooleanAttributePart extends AttributePart {
492 readonly type = 4;
493}
494/**
495 * An AttributePart that manages an event listener via add/removeEventListener.
496 *
497 * This part works by adding itself as the event listener on an element, then
498 * delegating to the value passed to it. This reduces the number of calls to
499 * add/removeEventListener if the listener changes frequently, such as when an
500 * inline function is used as a listener.
501 *
502 * Because event options are passed when adding listeners, we must take case
503 * to add and remove the part as a listener when the event options change.
504 */
505export type { EventPart };
506declare class EventPart extends AttributePart {
507 readonly type = 5;
508 constructor(element: HTMLElement, name: string, strings: ReadonlyArray<string>, parent: Disconnectable, options: RenderOptions | undefined);
509 handleEvent(event: Event): void;
510}
511export type { ElementPart };
512declare class ElementPart implements Disconnectable {
513 element: Element;
514 readonly type = 6;
515 _$committedValue: undefined;
516 options: RenderOptions | undefined;
517 constructor(element: Element, parent: Disconnectable, options: RenderOptions | undefined);
518 get _$isConnected(): boolean;
519 _$setValue(value: unknown): void;
520}
521/**
522 * END USERS SHOULD NOT RELY ON THIS OBJECT.
523 *
524 * Private exports for use by other Lit packages, not intended for use by
525 * external users.
526 *
527 * We currently do not make a mangled rollup build of the lit-ssr code. In order
528 * to keep a number of (otherwise private) top-level exports mangled in the
529 * client side code, we export a _$LH object containing those members (or
530 * helper methods for accessing private fields of those members), and then
531 * re-export them for use in lit-ssr. This keeps lit-ssr agnostic to whether the
532 * client-side code is being used in `dev` mode or `prod` mode.
533 *
534 * This has a unique name, to disambiguate it from private exports in
535 * lit-element, which re-exports all of lit-html.
536 *
537 * @private
538 */
539export declare const _$LH: {
540 _boundAttributeSuffix: string;
541 _marker: string;
542 _markerMatch: string;
543 _HTML_RESULT: number;
544 _getTemplateHtml: (strings: TemplateStringsArray, type: ResultType) => [TrustedHTML, Array<string>];
545 _TemplateInstance: typeof TemplateInstance;
546 _isIterable: (value: unknown) => value is Iterable<unknown>;
547 _resolveDirective: typeof resolveDirective;
548 _ChildPart: typeof ChildPart;
549 _AttributePart: typeof AttributePart;
550 _BooleanAttributePart: typeof BooleanAttributePart;
551 _EventPart: typeof EventPart;
552 _PropertyPart: typeof PropertyPart;
553 _ElementPart: typeof ElementPart;
554};
555/**
556 * Renders a value, usually a lit-html TemplateResult, to the container.
557 *
558 * This example renders the text "Hello, Zoe!" inside a paragraph tag, appending
559 * it to the container `document.body`.
560 *
561 * ```js
562 * import {html, render} from 'lit';
563 *
564 * const name = "Zoe";
565 * render(html`<p>Hello, ${name}!</p>`, document.body);
566 * ```
567 *
568 * @param value Any [renderable
569 * value](https://lit.dev/docs/templates/expressions/#child-expressions),
570 * typically a {@linkcode TemplateResult} created by evaluating a template tag
571 * like {@linkcode html} or {@linkcode svg}.
572 * @param container A DOM container to render to. The first render will append
573 * the rendered value to the container, and subsequent renders will
574 * efficiently update the rendered value if the same result type was
575 * previously rendered there.
576 * @param options See {@linkcode RenderOptions} for options documentation.
577 * @see
578 * {@link https://lit.dev/docs/libraries/standalone-templates/#rendering-lit-html-templates| Rendering Lit HTML Templates}
579 */
580export declare const render: {
581 (value: unknown, container: HTMLElement | DocumentFragment, options?: RenderOptions): RootPart;
582 setSanitizer: (newSanitizer: SanitizerFactory) => void;
583 createSanitizer: SanitizerFactory;
584 _testOnlyClearSanitizerFactoryDoNotCallOrElse: () => void;
585};
586//# sourceMappingURL=lit-html.d.ts.map
\No newline at end of file