html.d.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import Node from './node';
  2. import NodeType from './type';
  3. export interface KeyAttributes {
  4. id?: string;
  5. class?: string;
  6. }
  7. export interface Attributes {
  8. [key: string]: string;
  9. }
  10. export interface RawAttributes {
  11. [key: string]: string;
  12. }
  13. export declare type InsertPosition = 'beforebegin' | 'afterbegin' | 'beforeend' | 'afterend';
  14. declare class DOMTokenList {
  15. private _set;
  16. private _afterUpdate;
  17. private _validate;
  18. constructor(valuesInit?: string[], afterUpdate?: ((t: DOMTokenList) => void));
  19. add(c: string): void;
  20. replace(c1: string, c2: string): void;
  21. remove(c: string): void;
  22. toggle(c: string): void;
  23. contains(c: string): boolean;
  24. get length(): number;
  25. values(): IterableIterator<string>;
  26. get value(): string[];
  27. toString(): string;
  28. }
  29. /**
  30. * HTMLElement, which contains a set of children.
  31. *
  32. * Note: this is a minimalist implementation, no complete tree
  33. * structure provided (no parentNode, nextSibling,
  34. * previousSibling etc).
  35. * @class HTMLElement
  36. * @extends {Node}
  37. */
  38. export default class HTMLElement extends Node {
  39. private rawAttrs;
  40. private _attrs;
  41. private _rawAttrs;
  42. rawTagName: string;
  43. id: string;
  44. classList: DOMTokenList;
  45. /**
  46. * Node Type declaration.
  47. */
  48. nodeType: NodeType;
  49. /**
  50. * Quote attribute values
  51. * @param attr attribute value
  52. * @returns {string} quoted value
  53. */
  54. private quoteAttribute;
  55. /**
  56. * Creates an instance of HTMLElement.
  57. * @param keyAttrs id and class attribute
  58. * @param [rawAttrs] attributes in string
  59. *
  60. * @memberof HTMLElement
  61. */
  62. constructor(tagName: string, keyAttrs: KeyAttributes, rawAttrs: string, parentNode: HTMLElement | null);
  63. /**
  64. * Remove current element
  65. */
  66. remove(): void;
  67. /**
  68. * Remove Child element from childNodes array
  69. * @param {HTMLElement} node node to remove
  70. */
  71. removeChild(node: Node): void;
  72. /**
  73. * Exchanges given child with new child
  74. * @param {HTMLElement} oldNode node to exchange
  75. * @param {HTMLElement} newNode new node
  76. */
  77. exchangeChild(oldNode: Node, newNode: Node): void;
  78. get tagName(): string;
  79. get localName(): string;
  80. /**
  81. * Get escpaed (as-it) text value of current node and its children.
  82. * @return {string} text content
  83. */
  84. get rawText(): string;
  85. get textContent(): string;
  86. set textContent(val: string);
  87. /**
  88. * Get unescaped text value of current node and its children.
  89. * @return {string} text content
  90. */
  91. get text(): string;
  92. /**
  93. * Get structured Text (with '\n' etc.)
  94. * @return {string} structured text
  95. */
  96. get structuredText(): string;
  97. toString(): string;
  98. get innerHTML(): string;
  99. set innerHTML(content: string);
  100. set_content(content: string | Node | Node[], options?: Options): void;
  101. replaceWith(...nodes: (string | Node)[]): void;
  102. get outerHTML(): string;
  103. /**
  104. * Trim element from right (in block) after seeing pattern in a TextNode.
  105. * @param {RegExp} pattern pattern to find
  106. * @return {HTMLElement} reference to current node
  107. */
  108. trimRight(pattern: RegExp): this;
  109. /**
  110. * Get DOM structure
  111. * @return {string} strucutre
  112. */
  113. get structure(): string;
  114. /**
  115. * Remove whitespaces in this sub tree.
  116. * @return {HTMLElement} pointer to this
  117. */
  118. removeWhitespace(): this;
  119. /**
  120. * Query CSS selector to find matching nodes.
  121. * @param {string} selector Simplified CSS selector
  122. * @return {HTMLElement[]} matching elements
  123. */
  124. querySelectorAll(selector: string): HTMLElement[];
  125. /**
  126. * Query CSS Selector to find matching node.
  127. * @param {string} selector Simplified CSS selector
  128. * @return {HTMLElement} matching node
  129. */
  130. querySelector(selector: string): HTMLElement;
  131. /**
  132. * traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string. Will return itself or the matching ancestor. If no such element exists, it returns null.
  133. * @param selector a DOMString containing a selector list
  134. */
  135. closest(selector: string): Node;
  136. /**
  137. * Append a child node to childNodes
  138. * @param {Node} node node to append
  139. * @return {Node} node appended
  140. */
  141. appendChild<T extends Node = Node>(node: T): T;
  142. /**
  143. * Get first child node
  144. * @return {Node} first child node
  145. */
  146. get firstChild(): Node;
  147. /**
  148. * Get last child node
  149. * @return {Node} last child node
  150. */
  151. get lastChild(): Node;
  152. /**
  153. * Get attributes
  154. * @access private
  155. * @return {Object} parsed and unescaped attributes
  156. */
  157. get attrs(): Attributes;
  158. get attributes(): Record<string, string>;
  159. /**
  160. * Get escaped (as-it) attributes
  161. * @return {Object} parsed attributes
  162. */
  163. get rawAttributes(): RawAttributes;
  164. removeAttribute(key: string): void;
  165. hasAttribute(key: string): boolean;
  166. /**
  167. * Get an attribute
  168. * @return {string} value of the attribute
  169. */
  170. getAttribute(key: string): string | undefined;
  171. /**
  172. * Set an attribute value to the HTMLElement
  173. * @param {string} key The attribute name
  174. * @param {string} value The value to set, or null / undefined to remove an attribute
  175. */
  176. setAttribute(key: string, value: string): void;
  177. /**
  178. * Replace all the attributes of the HTMLElement by the provided attributes
  179. * @param {Attributes} attributes the new attribute set
  180. */
  181. setAttributes(attributes: Attributes): void;
  182. insertAdjacentHTML(where: InsertPosition, html: string): void;
  183. get nextSibling(): Node;
  184. get nextElementSibling(): HTMLElement;
  185. get classNames(): string;
  186. }
  187. export interface Options {
  188. lowerCaseTagName: boolean;
  189. comment: boolean;
  190. blockTextElements: {
  191. [tag: string]: boolean;
  192. };
  193. }
  194. /**
  195. * Parses HTML and returns a root element
  196. * Parse a chuck of HTML source.
  197. * @param {string} data html
  198. * @return {HTMLElement} root element
  199. */
  200. export declare function base_parse(data: string, options?: Partial<Options>): HTMLElement[];
  201. /**
  202. * Parses HTML and returns a root element
  203. * Parse a chuck of HTML source.
  204. */
  205. export declare function parse(data: string, options?: Partial<Options>): HTMLElement;
  206. export {};