node.d.ts 6.8 KB

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