UNPKG

8.05 kBTypeScriptView Raw
1import { ElementType } from "domelementtype";
2interface SourceCodeLocation {
3 /** One-based line index of the first character. */
4 startLine: number;
5 /** One-based column index of the first character. */
6 startCol: number;
7 /** Zero-based first character index. */
8 startOffset: number;
9 /** One-based line index of the last character. */
10 endLine: number;
11 /** One-based column index of the last character. Points directly *after* the last character. */
12 endCol: number;
13 /** Zero-based last character index. Points directly *after* the last character. */
14 endOffset: number;
15}
16interface TagSourceCodeLocation extends SourceCodeLocation {
17 startTag?: SourceCodeLocation;
18 endTag?: SourceCodeLocation;
19}
20export declare type ParentNode = Document | Element | CDATA;
21export declare type ChildNode = Text | Comment | ProcessingInstruction | Element | CDATA | Document;
22export declare type AnyNode = ParentNode | ChildNode;
23/**
24 * This object will be used as the prototype for Nodes when creating a
25 * DOM-Level-1-compliant structure.
26 */
27export declare abstract class Node {
28 /** The type of the node. */
29 abstract readonly type: ElementType;
30 /** Parent of the node */
31 parent: ParentNode | null;
32 /** Previous sibling */
33 prev: ChildNode | null;
34 /** Next sibling */
35 next: ChildNode | null;
36 /** The start index of the node. Requires `withStartIndices` on the handler to be `true. */
37 startIndex: number | null;
38 /** The end index of the node. Requires `withEndIndices` on the handler to be `true. */
39 endIndex: number | null;
40 /**
41 * `parse5` source code location info.
42 *
43 * Available if parsing with parse5 and location info is enabled.
44 */
45 sourceCodeLocation?: SourceCodeLocation | null;
46 /**
47 * [DOM spec](https://dom.spec.whatwg.org/#dom-node-nodetype)-compatible
48 * node {@link type}.
49 */
50 abstract readonly nodeType: number;
51 /**
52 * Same as {@link parent}.
53 * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
54 */
55 get parentNode(): ParentNode | null;
56 set parentNode(parent: ParentNode | null);
57 /**
58 * Same as {@link prev}.
59 * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
60 */
61 get previousSibling(): ChildNode | null;
62 set previousSibling(prev: ChildNode | null);
63 /**
64 * Same as {@link next}.
65 * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
66 */
67 get nextSibling(): ChildNode | null;
68 set nextSibling(next: ChildNode | null);
69 /**
70 * Clone this node, and optionally its children.
71 *
72 * @param recursive Clone child nodes as well.
73 * @returns A clone of the node.
74 */
75 cloneNode<T extends Node>(this: T, recursive?: boolean): T;
76}
77/**
78 * A node that contains some data.
79 */
80export declare abstract class DataNode extends Node {
81 data: string;
82 /**
83 * @param data The content of the data node
84 */
85 constructor(data: string);
86 /**
87 * Same as {@link data}.
88 * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
89 */
90 get nodeValue(): string;
91 set nodeValue(data: string);
92}
93/**
94 * Text within the document.
95 */
96export declare class Text extends DataNode {
97 type: ElementType.Text;
98 get nodeType(): 3;
99}
100/**
101 * Comments within the document.
102 */
103export declare class Comment extends DataNode {
104 type: ElementType.Comment;
105 get nodeType(): 8;
106}
107/**
108 * Processing instructions, including doc types.
109 */
110export declare class ProcessingInstruction extends DataNode {
111 name: string;
112 type: ElementType.Directive;
113 constructor(name: string, data: string);
114 get nodeType(): 1;
115 /** If this is a doctype, the document type name (parse5 only). */
116 "x-name"?: string;
117 /** If this is a doctype, the document type public identifier (parse5 only). */
118 "x-publicId"?: string;
119 /** If this is a doctype, the document type system identifier (parse5 only). */
120 "x-systemId"?: string;
121}
122/**
123 * A `Node` that can have children.
124 */
125export declare abstract class NodeWithChildren extends Node {
126 children: ChildNode[];
127 /**
128 * @param children Children of the node. Only certain node types can have children.
129 */
130 constructor(children: ChildNode[]);
131 /** First child of the node. */
132 get firstChild(): ChildNode | null;
133 /** Last child of the node. */
134 get lastChild(): ChildNode | null;
135 /**
136 * Same as {@link children}.
137 * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
138 */
139 get childNodes(): ChildNode[];
140 set childNodes(children: ChildNode[]);
141}
142export declare class CDATA extends NodeWithChildren {
143 type: ElementType.CDATA;
144 get nodeType(): 4;
145}
146/**
147 * The root node of the document.
148 */
149export declare class Document extends NodeWithChildren {
150 type: ElementType.Root;
151 get nodeType(): 9;
152 /** [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks) (parse5 only). */
153 "x-mode"?: "no-quirks" | "quirks" | "limited-quirks";
154}
155/**
156 * The description of an individual attribute.
157 */
158interface Attribute {
159 name: string;
160 value: string;
161 namespace?: string;
162 prefix?: string;
163}
164/**
165 * An element within the DOM.
166 */
167export declare class Element extends NodeWithChildren {
168 name: string;
169 attribs: {
170 [name: string]: string;
171 };
172 type: ElementType.Tag | ElementType.Script | ElementType.Style;
173 /**
174 * @param name Name of the tag, eg. `div`, `span`.
175 * @param attribs Object mapping attribute names to attribute values.
176 * @param children Children of the node.
177 */
178 constructor(name: string, attribs: {
179 [name: string]: string;
180 }, children?: ChildNode[], type?: ElementType.Tag | ElementType.Script | ElementType.Style);
181 get nodeType(): 1;
182 /**
183 * `parse5` source code location info, with start & end tags.
184 *
185 * Available if parsing with parse5 and location info is enabled.
186 */
187 sourceCodeLocation?: TagSourceCodeLocation | null;
188 /**
189 * Same as {@link name}.
190 * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
191 */
192 get tagName(): string;
193 set tagName(name: string);
194 get attributes(): Attribute[];
195 /** Element namespace (parse5 only). */
196 namespace?: string;
197 /** Element attribute namespaces (parse5 only). */
198 "x-attribsNamespace"?: Record<string, string>;
199 /** Element attribute namespace-related prefixes (parse5 only). */
200 "x-attribsPrefix"?: Record<string, string>;
201}
202/**
203 * @param node Node to check.
204 * @returns `true` if the node is a `Element`, `false` otherwise.
205 */
206export declare function isTag(node: Node): node is Element;
207/**
208 * @param node Node to check.
209 * @returns `true` if the node has the type `CDATA`, `false` otherwise.
210 */
211export declare function isCDATA(node: Node): node is CDATA;
212/**
213 * @param node Node to check.
214 * @returns `true` if the node has the type `Text`, `false` otherwise.
215 */
216export declare function isText(node: Node): node is Text;
217/**
218 * @param node Node to check.
219 * @returns `true` if the node has the type `Comment`, `false` otherwise.
220 */
221export declare function isComment(node: Node): node is Comment;
222/**
223 * @param node Node to check.
224 * @returns `true` if the node has the type `ProcessingInstruction`, `false` otherwise.
225 */
226export declare function isDirective(node: Node): node is ProcessingInstruction;
227/**
228 * @param node Node to check.
229 * @returns `true` if the node has the type `ProcessingInstruction`, `false` otherwise.
230 */
231export declare function isDocument(node: Node): node is Document;
232/**
233 * @param node Node to check.
234 * @returns `true` if the node has children, `false` otherwise.
235 */
236export declare function hasChildren(node: Node): node is ParentNode;
237/**
238 * Clone a node, and optionally its children.
239 *
240 * @param recursive Clone child nodes as well.
241 * @returns A clone of the node.
242 */
243export declare function cloneNode<T extends Node>(node: T, recursive?: boolean): T;
244export {};
245//# sourceMappingURL=node.d.ts.map
\No newline at end of file