node.d.ts 7.6 KB

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