node.d.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import { ElementType } from "domelementtype";
  2. interface 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. }
  16. interface TagSourceCodeLocation extends SourceCodeLocation {
  17. startTag?: SourceCodeLocation;
  18. endTag?: SourceCodeLocation;
  19. }
  20. export declare type ParentNode = Document | Element | CDATA;
  21. export declare type ChildNode = Text | Comment | ProcessingInstruction | Element | CDATA | Document;
  22. export 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. */
  27. export 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. */
  80. export 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. */
  96. export declare class Text extends DataNode {
  97. type: ElementType.Text;
  98. get nodeType(): 3;
  99. }
  100. /**
  101. * Comments within the document.
  102. */
  103. export declare class Comment extends DataNode {
  104. type: ElementType.Comment;
  105. get nodeType(): 8;
  106. }
  107. /**
  108. * Processing instructions, including doc types.
  109. */
  110. export 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. */
  125. export 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. }
  142. export declare class CDATA extends NodeWithChildren {
  143. type: ElementType.CDATA;
  144. get nodeType(): 4;
  145. }
  146. /**
  147. * The root node of the document.
  148. */
  149. export 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. */
  158. interface Attribute {
  159. name: string;
  160. value: string;
  161. namespace?: string;
  162. prefix?: string;
  163. }
  164. /**
  165. * An element within the DOM.
  166. */
  167. export 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. */
  206. export 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. */
  211. export 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. */
  216. export 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. */
  221. export 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. */
  226. export 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. */
  231. export 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. */
  236. export 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. */
  243. export declare function cloneNode<T extends Node>(node: T, recursive?: boolean): T;
  244. export {};
  245. //# sourceMappingURL=node.d.ts.map