node.d.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. *
  20. * @param type The type of the node.
  21. */
  22. constructor(type: ElementType);
  23. get nodeType(): number;
  24. get parentNode(): NodeWithChildren | null;
  25. set parentNode(parent: NodeWithChildren | null);
  26. get previousSibling(): Node | null;
  27. set previousSibling(prev: Node | null);
  28. get nextSibling(): Node | null;
  29. set nextSibling(next: Node | null);
  30. /**
  31. * Clone this node, and optionally its children.
  32. *
  33. * @param recursive Clone child nodes as well.
  34. * @returns A clone of the node.
  35. */
  36. cloneNode<T extends Node>(this: T, recursive?: boolean): T;
  37. }
  38. export declare class DataNode extends Node {
  39. data: string;
  40. /**
  41. * @param type The type of the node
  42. * @param data The content of the data node
  43. */
  44. constructor(type: ElementType.Comment | ElementType.Text | ElementType.Directive, data: string);
  45. get nodeValue(): string;
  46. set nodeValue(data: string);
  47. }
  48. export declare class Text extends DataNode {
  49. constructor(data: string);
  50. }
  51. export declare class Comment extends DataNode {
  52. constructor(data: string);
  53. }
  54. export declare class ProcessingInstruction extends DataNode {
  55. name: string;
  56. constructor(name: string, data: string);
  57. "x-name"?: string;
  58. "x-publicId"?: string;
  59. "x-systemId"?: string;
  60. }
  61. /**
  62. * A `Node` that can have children.
  63. */
  64. export declare class NodeWithChildren extends Node {
  65. children: Node[];
  66. /**
  67. * @param type Type of the node.
  68. * @param children Children of the node. Only certain node types can have children.
  69. */
  70. constructor(type: ElementType.Root | ElementType.CDATA | ElementType.Script | ElementType.Style | ElementType.Tag, children: Node[]);
  71. get firstChild(): Node | null;
  72. get lastChild(): Node | null;
  73. get childNodes(): Node[];
  74. set childNodes(children: Node[]);
  75. }
  76. export declare class Document extends NodeWithChildren {
  77. constructor(children: Node[]);
  78. "x-mode"?: "no-quirks" | "quirks" | "limited-quirks";
  79. }
  80. interface Attribute {
  81. name: string;
  82. value: string;
  83. namespace?: string;
  84. prefix?: string;
  85. }
  86. export declare class Element extends NodeWithChildren {
  87. name: string;
  88. attribs: {
  89. [name: string]: string;
  90. };
  91. /**
  92. * @param name Name of the tag, eg. `div`, `span`.
  93. * @param attribs Object mapping attribute names to attribute values.
  94. * @param children Children of the node.
  95. */
  96. constructor(name: string, attribs: {
  97. [name: string]: string;
  98. }, children?: Node[], type?: ElementType.Tag | ElementType.Script | ElementType.Style);
  99. get tagName(): string;
  100. set tagName(name: string);
  101. get attributes(): Attribute[];
  102. "x-attribsNamespace"?: Record<string, string>;
  103. "x-attribsPrefix"?: Record<string, string>;
  104. }
  105. /**
  106. * @param node Node to check.
  107. * @returns `true` if the node is a `Element`, `false` otherwise.
  108. */
  109. export declare function isTag(node: Node): node is Element;
  110. /**
  111. * @param node Node to check.
  112. * @returns `true` if the node has the type `CDATA`, `false` otherwise.
  113. */
  114. export declare function isCDATA(node: Node): node is NodeWithChildren;
  115. /**
  116. * @param node Node to check.
  117. * @returns `true` if the node has the type `Text`, `false` otherwise.
  118. */
  119. export declare function isText(node: Node): node is DataNode;
  120. /**
  121. * @param node Node to check.
  122. * @returns `true` if the node has the type `Comment`, `false` otherwise.
  123. */
  124. export declare function isComment(node: Node): node is DataNode;
  125. /**
  126. * @param node Node to check.
  127. * @returns `true` if the node has the type `ProcessingInstruction`, `false` otherwise.
  128. */
  129. export declare function isDirective(node: Node): node is ProcessingInstruction;
  130. /**
  131. * @param node Node to check.
  132. * @returns `true` if the node has the type `ProcessingInstruction`, `false` otherwise.
  133. */
  134. export declare function isDocument(node: Node): node is Document;
  135. /**
  136. * @param node Node to check.
  137. * @returns `true` if the node is a `NodeWithChildren` (has children), `false` otherwise.
  138. */
  139. export declare function hasChildren(node: Node): node is NodeWithChildren;
  140. /**
  141. * Clone a node, and optionally its children.
  142. *
  143. * @param recursive Clone child nodes as well.
  144. * @returns A clone of the node.
  145. */
  146. export declare function cloneNode<T extends Node>(node: T, recursive?: boolean): T;
  147. export {};
  148. //# sourceMappingURL=node.d.ts.map