UNPKG

9.22 kBTypeScriptView Raw
1import type { DOCUMENT_MODE, NS } from '../common/html.js';
2import type { Attribute, ElementLocation } from '../common/token.js';
3export interface TreeAdapterTypeMap<Node = unknown, ParentNode = unknown, ChildNode = unknown, Document = unknown, DocumentFragment = unknown, Element = unknown, CommentNode = unknown, TextNode = unknown, Template = unknown, DocumentType = unknown> {
4 node: Node;
5 parentNode: ParentNode;
6 childNode: ChildNode;
7 document: Document;
8 documentFragment: DocumentFragment;
9 element: Element;
10 commentNode: CommentNode;
11 textNode: TextNode;
12 template: Template;
13 documentType: DocumentType;
14}
15/**
16 * Tree adapter is a set of utility functions that provides minimal required abstraction layer beetween parser and a specific AST format.
17 * Note that `TreeAdapter` is not designed to be a general purpose AST manipulation library. You can build such library
18 * on top of existing `TreeAdapter` or use one of the existing libraries from npm.
19 *
20 * @see The default implementation {@link parse5.treeAdapters.default}
21 */
22export interface TreeAdapter<T extends TreeAdapterTypeMap = TreeAdapterTypeMap> {
23 /**
24 * Copies attributes to the given element. Only attributes that are not yet present in the element are copied.
25 *
26 * @param recipient - Element to copy attributes into.
27 * @param attrs - Attributes to copy.
28 */
29 adoptAttributes(recipient: T['element'], attrs: Attribute[]): void;
30 /**
31 * Appends a child node to the given parent node.
32 *
33 * @param parentNode - Parent node.
34 * @param newNode - Child node.
35 */
36 appendChild(parentNode: T['parentNode'], newNode: T['childNode']): void;
37 /**
38 * Creates a comment node.
39 *
40 * @param data - Comment text.
41 */
42 createCommentNode(data: string): T['commentNode'];
43 /**
44 * Creates a document node.
45 */
46 createDocument(): T['document'];
47 /**
48 * Creates a document fragment node.
49 */
50 createDocumentFragment(): T['documentFragment'];
51 /**
52 * Creates an element node.
53 *
54 * @param tagName - Tag name of the element.
55 * @param namespaceURI - Namespace of the element.
56 * @param attrs - Attribute name-value pair array. Foreign attributes may contain `namespace` and `prefix` fields as well.
57 */
58 createElement(tagName: string, namespaceURI: NS, attrs: Attribute[]): T['element'];
59 /**
60 * Removes a node from its parent.
61 *
62 * @param node - Node to remove.
63 */
64 detachNode(node: T['childNode']): void;
65 /**
66 * Returns the given element's attributes in an array, in the form of name-value pairs.
67 * Foreign attributes may contain `namespace` and `prefix` fields as well.
68 *
69 * @param element - Element.
70 */
71 getAttrList(element: T['element']): Attribute[];
72 /**
73 * Returns the given node's children in an array.
74 *
75 * @param node - Node.
76 */
77 getChildNodes(node: T['parentNode']): T['childNode'][];
78 /**
79 * Returns the given comment node's content.
80 *
81 * @param commentNode - Comment node.
82 */
83 getCommentNodeContent(commentNode: T['commentNode']): string;
84 /**
85 * Returns [document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
86 *
87 * @param document - Document node.
88 */
89 getDocumentMode(document: T['document']): DOCUMENT_MODE;
90 /**
91 * Returns the given document type node's name.
92 *
93 * @param doctypeNode - Document type node.
94 */
95 getDocumentTypeNodeName(doctypeNode: T['documentType']): string;
96 /**
97 * Returns the given document type node's public identifier.
98 *
99 * @param doctypeNode - Document type node.
100 */
101 getDocumentTypeNodePublicId(doctypeNode: T['documentType']): string;
102 /**
103 * Returns the given document type node's system identifier.
104 *
105 * @param doctypeNode - Document type node.
106 */
107 getDocumentTypeNodeSystemId(doctypeNode: T['documentType']): string;
108 /**
109 * Returns the first child of the given node.
110 *
111 * @param node - Node.
112 */
113 getFirstChild(node: T['parentNode']): T['childNode'] | null;
114 /**
115 * Returns the given element's namespace.
116 *
117 * @param element - Element.
118 */
119 getNamespaceURI(element: T['element']): NS;
120 /**
121 * Returns the given node's source code location information.
122 *
123 * @param node - Node.
124 */
125 getNodeSourceCodeLocation(node: T['node']): ElementLocation | undefined | null;
126 /**
127 * Returns the given node's parent.
128 *
129 * @param node - Node.
130 */
131 getParentNode(node: T['node']): T['parentNode'] | null;
132 /**
133 * Returns the given element's tag name.
134 *
135 * @param element - Element.
136 */
137 getTagName(element: T['element']): string;
138 /**
139 * Returns the given text node's content.
140 *
141 * @param textNode - Text node.
142 */
143 getTextNodeContent(textNode: T['textNode']): string;
144 /**
145 * Returns the `<template>` element content element.
146 *
147 * @param templateElement - `<template>` element.
148 */
149 getTemplateContent(templateElement: T['template']): T['documentFragment'];
150 /**
151 * Inserts a child node to the given parent node before the given reference node.
152 *
153 * @param parentNode - Parent node.
154 * @param newNode - Child node.
155 * @param referenceNode - Reference node.
156 */
157 insertBefore(parentNode: T['parentNode'], newNode: T['childNode'], referenceNode: T['childNode']): void;
158 /**
159 * Inserts text into a node. If the last child of the node is a text node, the provided text will be appended to the
160 * text node content. Otherwise, inserts a new text node with the given text.
161 *
162 * @param parentNode - Node to insert text into.
163 * @param text - Text to insert.
164 */
165 insertText(parentNode: T['parentNode'], text: string): void;
166 /**
167 * Inserts text into a sibling node that goes before the reference node. If this sibling node is the text node,
168 * the provided text will be appended to the text node content. Otherwise, inserts a new sibling text node with
169 * the given text before the reference node.
170 *
171 * @param parentNode - Node to insert text into.
172 * @param text - Text to insert.
173 * @param referenceNode - Node to insert text before.
174 */
175 insertTextBefore(parentNode: T['parentNode'], text: string, referenceNode: T['childNode']): void;
176 /**
177 * Determines if the given node is a comment node.
178 *
179 * @param node - Node.
180 */
181 isCommentNode(node: T['node']): node is T['commentNode'];
182 /**
183 * Determines if the given node is a document type node.
184 *
185 * @param node - Node.
186 */
187 isDocumentTypeNode(node: T['node']): node is T['documentType'];
188 /**
189 * Determines if the given node is an element.
190 *
191 * @param node - Node.
192 */
193 isElementNode(node: T['node']): node is T['element'];
194 /**
195 * Determines if the given node is a text node.
196 *
197 * @param node - Node.
198 */
199 isTextNode(node: T['node']): node is T['textNode'];
200 /**
201 * Sets the [document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks).
202 *
203 * @param document - Document node.
204 * @param mode - Document mode.
205 */
206 setDocumentMode(document: T['document'], mode: DOCUMENT_MODE): void;
207 /**
208 * Sets the document type. If the `document` already contains a document type node, the `name`, `publicId` and `systemId`
209 * properties of this node will be updated with the provided values. Otherwise, creates a new document type node
210 * with the given properties and inserts it into the `document`.
211 *
212 * @param document - Document node.
213 * @param name - Document type name.
214 * @param publicId - Document type public identifier.
215 * @param systemId - Document type system identifier.
216 */
217 setDocumentType(document: T['document'], name: string, publicId: string, systemId: string): void;
218 /**
219 * Attaches source code location information to the node.
220 *
221 * @param node - Node.
222 */
223 setNodeSourceCodeLocation(node: T['node'], location: ElementLocation | null): void;
224 /**
225 * Updates the source code location information of the node.
226 *
227 * @param node - Node.
228 */
229 updateNodeSourceCodeLocation(node: T['node'], location: Partial<ElementLocation>): void;
230 /**
231 * Sets the `<template>` element content element.
232 *
233 * @param templateElement - `<template>` element.
234 * @param contentElement - Content element.
235 */
236 setTemplateContent(templateElement: T['template'], contentElement: T['documentFragment']): void;
237 /**
238 * Optional callback for elements being pushed to the stack of open elements.
239 *
240 * @param element The element being pushed to the stack of open elements.
241 */
242 onItemPush?: (item: T['element']) => void;
243 /**
244 * Optional callback for elements being popped from the stack of open elements.
245 *
246 * @param item The element being popped.
247 */
248 onItemPop?: (item: T['element'], newTop: T['parentNode']) => void;
249}
250//# sourceMappingURL=interface.d.ts.map
\No newline at end of file