You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
5.2 KiB

  1. import Node from './node';
  2. import NodeType from './type';
  3. import TextNode from './text';
  4. import Matcher from '../matcher';
  5. export interface KeyAttributes {
  6. id?: string;
  7. class?: string;
  8. }
  9. export interface Attributes {
  10. [key: string]: string;
  11. }
  12. export interface RawAttributes {
  13. [key: string]: string;
  14. }
  15. export declare type InsertPosition = 'beforebegin' | 'afterbegin' | 'beforeend' | 'afterend';
  16. /**
  17. * HTMLElement, which contains a set of children.
  18. *
  19. * Note: this is a minimalist implementation, no complete tree
  20. * structure provided (no parentNode, nextSibling,
  21. * previousSibling etc).
  22. * @class HTMLElement
  23. * @extends {Node}
  24. */
  25. export default class HTMLElement extends Node {
  26. private rawAttrs;
  27. parentNode: Node;
  28. private _attrs;
  29. private _rawAttrs;
  30. private _tag_name;
  31. id: string;
  32. classNames: string[];
  33. /**
  34. * Node Type declaration.
  35. */
  36. nodeType: NodeType;
  37. /**
  38. * Creates an instance of HTMLElement.
  39. * @param keyAttrs id and class attribute
  40. * @param [rawAttrs] attributes in string
  41. *
  42. * @memberof HTMLElement
  43. */
  44. constructor(tagName: string, keyAttrs: KeyAttributes, rawAttrs?: string, parentNode?: Node);
  45. /**
  46. * Remove Child element from childNodes array
  47. * @param {HTMLElement} node node to remove
  48. */
  49. removeChild(node: Node): void;
  50. /**
  51. * Exchanges given child with new child
  52. * @param {HTMLElement} oldNode node to exchange
  53. * @param {HTMLElement} newNode new node
  54. */
  55. exchangeChild(oldNode: Node, newNode: Node): void;
  56. get tagName(): string;
  57. /**
  58. * Get escpaed (as-it) text value of current node and its children.
  59. * @return {string} text content
  60. */
  61. get rawText(): string;
  62. /**
  63. * Get unescaped text value of current node and its children.
  64. * @return {string} text content
  65. */
  66. get text(): string;
  67. /**
  68. * Get structured Text (with '\n' etc.)
  69. * @return {string} structured text
  70. */
  71. get structuredText(): string;
  72. toString(): string;
  73. get innerHTML(): string;
  74. set_content(content: string | Node | Node[], options?: Options): void;
  75. get outerHTML(): string;
  76. /**
  77. * Trim element from right (in block) after seeing pattern in a TextNode.
  78. * @param {RegExp} pattern pattern to find
  79. * @return {HTMLElement} reference to current node
  80. */
  81. trimRight(pattern: RegExp): this;
  82. /**
  83. * Get DOM structure
  84. * @return {string} strucutre
  85. */
  86. get structure(): string;
  87. /**
  88. * Remove whitespaces in this sub tree.
  89. * @return {HTMLElement} pointer to this
  90. */
  91. removeWhitespace(): this;
  92. /**
  93. * Query CSS selector to find matching nodes.
  94. * @param {string} selector Simplified CSS selector
  95. * @param {Matcher} selector A Matcher instance
  96. * @return {HTMLElement[]} matching elements
  97. */
  98. querySelectorAll(selector: string | Matcher): HTMLElement[];
  99. /**
  100. * Query CSS Selector to find matching node.
  101. * @param {string} selector Simplified CSS selector
  102. * @param {Matcher} selector A Matcher instance
  103. * @return {HTMLElement} matching node
  104. */
  105. querySelector(selector: string | Matcher): HTMLElement;
  106. /**
  107. * Append a child node to childNodes
  108. * @param {Node} node node to append
  109. * @return {Node} node appended
  110. */
  111. appendChild<T extends Node = Node>(node: T): T;
  112. /**
  113. * Get first child node
  114. * @return {Node} first child node
  115. */
  116. get firstChild(): Node;
  117. /**
  118. * Get last child node
  119. * @return {Node} last child node
  120. */
  121. get lastChild(): Node;
  122. /**
  123. * Get attributes
  124. * @return {Object} parsed and unescaped attributes
  125. */
  126. get attributes(): Attributes;
  127. /**
  128. * Get escaped (as-it) attributes
  129. * @return {Object} parsed attributes
  130. */
  131. get rawAttributes(): RawAttributes;
  132. removeAttribute(key: string): void;
  133. hasAttribute(key: string): boolean;
  134. /**
  135. * Get an attribute
  136. * @return {string} value of the attribute
  137. */
  138. getAttribute(key: string): string | undefined;
  139. /**
  140. * Set an attribute value to the HTMLElement
  141. * @param {string} key The attribute name
  142. * @param {string} value The value to set, or null / undefined to remove an attribute
  143. */
  144. setAttribute(key: string, value: string): void;
  145. /**
  146. * Replace all the attributes of the HTMLElement by the provided attributes
  147. * @param {Attributes} attributes the new attribute set
  148. */
  149. setAttributes(attributes: Attributes): void;
  150. insertAdjacentHTML(where: InsertPosition, html: string): void;
  151. }
  152. export interface Options {
  153. lowerCaseTagName?: boolean;
  154. script?: boolean;
  155. style?: boolean;
  156. pre?: boolean;
  157. comment?: boolean;
  158. }
  159. /**
  160. * Parses HTML and returns a root element
  161. * Parse a chuck of HTML source.
  162. * @param {string} data html
  163. * @return {HTMLElement} root element
  164. */
  165. export declare function parse(data: string, options?: Options): HTMLElement & {
  166. valid: boolean;
  167. };
  168. export declare function parse(data: string, options?: Options & {
  169. noFix: false;
  170. }): HTMLElement & {
  171. valid: boolean;
  172. };
  173. export declare function parse(data: string, options?: Options & {
  174. noFix: true;
  175. }): (HTMLElement | TextNode) & {
  176. valid: boolean;
  177. };