UNPKG

14 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 * Used to sanitize any value before it is written into the DOM. This can be
10 * used to implement a security policy of allowed and disallowed values in
11 * order to prevent XSS attacks.
12 *
13 * One way of using this callback would be to check attributes and properties
14 * against a list of high risk fields, and require that values written to such
15 * fields be instances of a class which is safe by construction. Closure's Safe
16 * HTML Types is one implementation of this technique (
17 * https://github.com/google/safe-html-types/blob/master/doc/safehtml-types.md).
18 * The TrustedTypes polyfill in API-only mode could also be used as a basis
19 * for this technique (https://github.com/WICG/trusted-types).
20 *
21 * @param node The HTML node (usually either a #text node or an Element) that
22 * is being written to. Note that this is just an exemplar node, the write
23 * may take place against another instance of the same class of node.
24 * @param name The name of an attribute or property (for example, 'href').
25 * @param type Indicates whether the write that's about to be performed will
26 * be to a property or a node.
27 * @return A function that will sanitize this class of writes.
28 */
29export declare type SanitizerFactory = (node: Node, name: string, type: 'property' | 'attribute') => ValueSanitizer;
30/**
31 * A function which can sanitize values that will be written to a specific kind
32 * of DOM sink.
33 *
34 * See SanitizerFactory.
35 *
36 * @param value The value to sanitize. Will be the actual value passed into
37 * the lit-html template literal, so this could be of any type.
38 * @return The value to write to the DOM. Usually the same as the input value,
39 * unless sanitization is needed.
40 */
41export declare type ValueSanitizer = (value: unknown) => unknown;
42/** TemplateResult types */
43declare const HTML_RESULT = 1;
44declare const SVG_RESULT = 2;
45declare type ResultType = typeof HTML_RESULT | typeof SVG_RESULT;
46/**
47 * The return type of the template tag functions.
48 */
49export declare type TemplateResult<T extends ResultType = ResultType> = {
50 ['_$litType$']: T;
51 strings: TemplateStringsArray;
52 values: unknown[];
53};
54export declare type HTMLTemplateResult = TemplateResult<typeof HTML_RESULT>;
55export declare type SVGTemplateResult = TemplateResult<typeof SVG_RESULT>;
56export interface CompiledTemplateResult {
57 ['_$litType$']: CompiledTemplate;
58 values: unknown[];
59}
60export interface CompiledTemplate extends Omit<Template, 'el'> {
61 el?: HTMLTemplateElement;
62 h: TrustedHTML;
63}
64/**
65 * Interprets a template literal as an HTML template that can efficiently
66 * render to and update a container.
67 *
68 * ```ts
69 * const header = (title: string) => html`<h1>${title}</h1>`;
70 * ```
71 *
72 * The `html` tag returns a description of the DOM to render as a value. It is
73 * lazy, meaning no work is done until the template is rendered. When rendering,
74 * if a template comes from the same expression as a previously rendered result,
75 * it's efficiently updated instead of replaced.
76 */
77export declare const html: (strings: TemplateStringsArray, ...values: unknown[]) => TemplateResult<1>;
78/**
79 * Interprets a template literal as an SVG template that can efficiently
80 * render to and update a container.
81 */
82export declare const svg: (strings: TemplateStringsArray, ...values: unknown[]) => TemplateResult<2>;
83/**
84 * A sentinel value that signals that a value was handled by a directive and
85 * should not be written to the DOM.
86 */
87export declare const noChange: unique symbol;
88/**
89 * A sentinel value that signals a ChildPart to fully clear its content.
90 *
91 * ```ts
92 * const button = html`${
93 * user.isAdmin
94 * ? html`<button>DELETE</button>`
95 * : nothing
96 * }`;
97 * ```
98 *
99 * Prefer using `nothing` over other falsy values as it provides a consistent
100 * behavior between various expression binding contexts.
101 *
102 * In child expressions, `undefined`, `null`, `''`, and `nothing` all behave the
103 * same and render no nodes. In attribute expressions, `nothing` _removes_ the
104 * attribute, while `undefined` and `null` will render an empty string. In
105 * property expressions `nothing` becomes `undefined`.
106 */
107export declare const nothing: unique symbol;
108/**
109 * Object specifying options for controlling lit-html rendering. Note that
110 * while `render` may be called multiple times on the same `container` (and
111 * `renderBefore` reference node) to efficiently update the rendered content,
112 * only the options passed in during the first render are respected during
113 * the lifetime of renders to that unique `container` + `renderBefore`
114 * combination.
115 */
116export interface RenderOptions {
117 /**
118 * An object to use as the `this` value for event listeners. It's often
119 * useful to set this to the host component rendering a template.
120 */
121 host?: object;
122 /**
123 * A DOM node before which to render content in the container.
124 */
125 renderBefore?: ChildNode | null;
126 /**
127 * Node used for cloning the template (`importNode` will be called on this
128 * node). This controls the `ownerDocument` of the rendered DOM, along with
129 * any inherited context. Defaults to the global `document`.
130 */
131 creationScope?: {
132 importNode(node: Node, deep?: boolean): Node;
133 };
134 /**
135 * The initial connected state for the top-level part being rendered. If no
136 * `isConnected` option is set, `AsyncDirective`s will be connected by
137 * default. Set to `false` if the initial render occurs in a disconnected tree
138 * and `AsyncDirective`s should see `isConnected === false` for their initial
139 * render. The `part.setConnected()` method must be used subsequent to initial
140 * render to change the connected state of the part.
141 */
142 isConnected?: boolean;
143}
144/**
145 * Renders a value, usually a lit-html TemplateResult, to the container.
146 * @param value
147 * @param container
148 * @param options
149 */
150export declare const render: {
151 (value: unknown, container: HTMLElement | DocumentFragment, options?: RenderOptions | undefined): RootPart;
152 setSanitizer: (newSanitizer: SanitizerFactory) => void;
153 createSanitizer: SanitizerFactory;
154 _testOnlyClearSanitizerFactoryDoNotCallOrElse: () => void;
155};
156export interface DirectiveParent {
157 _$parent?: DirectiveParent;
158 _$isConnected: boolean;
159 __directive?: Directive;
160 __directives?: Array<Directive | undefined>;
161}
162declare class Template {
163 constructor({ strings, ['_$litType$']: type }: TemplateResult, options?: RenderOptions);
164 /** @nocollapse */
165 static createElement(html: TrustedHTML, _options?: RenderOptions): HTMLTemplateElement;
166}
167export interface Disconnectable {
168 _$parent?: Disconnectable;
169 _$disconnectableChildren?: Set<Disconnectable>;
170 _$isConnected: boolean;
171}
172declare function resolveDirective(part: ChildPart | AttributePart | ElementPart, value: unknown, parent?: DirectiveParent, attributeIndex?: number): unknown;
173/**
174 * An updateable instance of a Template. Holds references to the Parts used to
175 * update the template instance.
176 */
177declare class TemplateInstance implements Disconnectable {
178 constructor(template: Template, parent: ChildPart);
179 get parentNode(): Node;
180 get _$isConnected(): boolean;
181 _clone(options: RenderOptions | undefined): Node;
182 _update(values: Array<unknown>): void;
183}
184export declare type Part = ChildPart | AttributePart | PropertyPart | BooleanAttributePart | ElementPart | EventPart;
185export type { ChildPart };
186declare class ChildPart implements Disconnectable {
187 readonly type = 2;
188 readonly options: RenderOptions | undefined;
189 _$committedValue: unknown;
190 private _textSanitizer;
191 get _$isConnected(): boolean;
192 constructor(startNode: ChildNode, endNode: ChildNode | null, parent: TemplateInstance | ChildPart | undefined, options: RenderOptions | undefined);
193 /**
194 * The parent node into which the part renders its content.
195 *
196 * A ChildPart's content consists of a range of adjacent child nodes of
197 * `.parentNode`, possibly bordered by 'marker nodes' (`.startNode` and
198 * `.endNode`).
199 *
200 * - If both `.startNode` and `.endNode` are non-null, then the part's content
201 * consists of all siblings between `.startNode` and `.endNode`, exclusively.
202 *
203 * - If `.startNode` is non-null but `.endNode` is null, then the part's
204 * content consists of all siblings following `.startNode`, up to and
205 * including the last child of `.parentNode`. If `.endNode` is non-null, then
206 * `.startNode` will always be non-null.
207 *
208 * - If both `.endNode` and `.startNode` are null, then the part's content
209 * consists of all child nodes of `.parentNode`.
210 */
211 get parentNode(): Node;
212 /**
213 * The part's leading marker node, if any. See `.parentNode` for more
214 * information.
215 */
216 get startNode(): Node | null;
217 /**
218 * The part's trailing marker node, if any. See `.parentNode` for more
219 * information.
220 */
221 get endNode(): Node | null;
222 _$setValue(value: unknown, directiveParent?: DirectiveParent): void;
223 private _insert;
224 private _commitNode;
225 private _commitText;
226 private _commitTemplateResult;
227 private _commitIterable;
228}
229/**
230 * A top-level `ChildPart` returned from `render` that manages the connected
231 * state of `AsyncDirective`s created throughout the tree below it.
232 */
233export interface RootPart extends ChildPart {
234 /**
235 * Sets the connection state for `AsyncDirective`s contained within this root
236 * ChildPart.
237 *
238 * lit-html does not automatically monitor the connectedness of DOM rendered;
239 * as such, it is the responsibility of the caller to `render` to ensure that
240 * `part.setConnected(false)` is called before the part object is potentially
241 * discarded, to ensure that `AsyncDirective`s have a chance to dispose of
242 * any resources being held. If a `RootPart` that was prevously
243 * disconnected is subsequently re-connected (and its `AsyncDirective`s should
244 * re-connect), `setConnected(true)` should be called.
245 *
246 * @param isConnected Whether directives within this tree should be connected
247 * or not
248 */
249 setConnected(isConnected: boolean): void;
250}
251export type { AttributePart };
252declare class AttributePart implements Disconnectable {
253 readonly type: 1 | 3 | 4 | 5;
254 readonly element: HTMLElement;
255 readonly name: string;
256 readonly options: RenderOptions | undefined;
257 /**
258 * If this attribute part represents an interpolation, this contains the
259 * static strings of the interpolation. For single-value, complete bindings,
260 * this is undefined.
261 */
262 readonly strings?: ReadonlyArray<string>;
263 protected _sanitizer: ValueSanitizer | undefined;
264 get tagName(): string;
265 get _$isConnected(): boolean;
266 constructor(element: HTMLElement, name: string, strings: ReadonlyArray<string>, parent: Disconnectable, options: RenderOptions | undefined);
267}
268export type { PropertyPart };
269declare class PropertyPart extends AttributePart {
270 readonly type = 3;
271}
272export type { BooleanAttributePart };
273declare class BooleanAttributePart extends AttributePart {
274 readonly type = 4;
275}
276/**
277 * An AttributePart that manages an event listener via add/removeEventListener.
278 *
279 * This part works by adding itself as the event listener on an element, then
280 * delegating to the value passed to it. This reduces the number of calls to
281 * add/removeEventListener if the listener changes frequently, such as when an
282 * inline function is used as a listener.
283 *
284 * Because event options are passed when adding listeners, we must take case
285 * to add and remove the part as a listener when the event options change.
286 */
287export type { EventPart };
288declare class EventPart extends AttributePart {
289 readonly type = 5;
290 constructor(element: HTMLElement, name: string, strings: ReadonlyArray<string>, parent: Disconnectable, options: RenderOptions | undefined);
291 handleEvent(event: Event): void;
292}
293export type { ElementPart };
294declare class ElementPart implements Disconnectable {
295 element: Element;
296 readonly type = 6;
297 _$committedValue: undefined;
298 options: RenderOptions | undefined;
299 constructor(element: Element, parent: Disconnectable, options: RenderOptions | undefined);
300 get _$isConnected(): boolean;
301 _$setValue(value: unknown): void;
302}
303/**
304 * END USERS SHOULD NOT RELY ON THIS OBJECT.
305 *
306 * Private exports for use by other Lit packages, not intended for use by
307 * external users.
308 *
309 * We currently do not make a mangled rollup build of the lit-ssr code. In order
310 * to keep a number of (otherwise private) top-level exports mangled in the
311 * client side code, we export a _$LH object containing those members (or
312 * helper methods for accessing private fields of those members), and then
313 * re-export them for use in lit-ssr. This keeps lit-ssr agnostic to whether the
314 * client-side code is being used in `dev` mode or `prod` mode.
315 *
316 * This has a unique name, to disambiguate it from private exports in
317 * lit-element, which re-exports all of lit-html.
318 *
319 * @private
320 */
321export declare const _$LH: {
322 _boundAttributeSuffix: string;
323 _marker: string;
324 _markerMatch: string;
325 _HTML_RESULT: number;
326 _getTemplateHtml: (strings: TemplateStringsArray, type: ResultType) => [TrustedHTML, Array<string | undefined>];
327 _TemplateInstance: typeof TemplateInstance;
328 _isIterable: (value: unknown) => value is Iterable<unknown>;
329 _resolveDirective: typeof resolveDirective;
330 _ChildPart: typeof ChildPart;
331 _AttributePart: typeof AttributePart;
332 _BooleanAttributePart: typeof BooleanAttributePart;
333 _EventPart: typeof EventPart;
334 _PropertyPart: typeof PropertyPart;
335 _ElementPart: typeof ElementPart;
336};
337//# sourceMappingURL=lit-html.d.ts.map
\No newline at end of file