index.d.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. export = CSSselect;
  2. /**
  3. * Alias for CSSselect.selectAll(query, elems, options).
  4. * @see [CSSselect.compile] for supported selector queries.
  5. */
  6. declare function CSSselect<Node, ElementNode extends Node>(
  7. query: CSSselect.Query,
  8. elems: Array<ElementNode> | ElementNode,
  9. options?: CSSselect.Options<Node, ElementNode>
  10. ): Array<ElementNode>;
  11. declare namespace CSSselect {
  12. type Predicate<Value> = (v: Value) => boolean;
  13. interface Adapter<Node, ElementNode extends Node> {
  14. /**
  15. * is the node a tag?
  16. */
  17. isTag(node: Node): node is ElementNode;
  18. /**
  19. * Does at least one of passed element nodes pass the test predicate?
  20. */
  21. existsOne(test: Predicate<ElementNode>, elems: Array<ElementNode>): boolean;
  22. /**
  23. * get the attribute value.
  24. */
  25. getAttributeValue(elem: ElementNode, name: string): string;
  26. /**
  27. * get the node's children
  28. */
  29. getChildren(node: Node): Array<Node>;
  30. /**
  31. * get the name of the tag
  32. */
  33. getName(elem: ElementNode): string;
  34. /**
  35. * get the parent of the node
  36. */
  37. getParent(node: Node): Node;
  38. /*
  39. Get the siblings of the node. Note that unlike jQuery's `siblings` method,
  40. this is expected to include the current node as well
  41. */
  42. getSiblings(node: Node): Array<Node>;
  43. /*
  44. * Get the text content of the node, and its children if it has any.
  45. */
  46. getText(node: Node): string;
  47. /**
  48. * Does the element have the named attribute?
  49. */
  50. hasAttrib(elem: ElementNode, name: string): boolean;
  51. /**
  52. * takes an array of nodes, and removes any duplicates, as well as any
  53. * nodes whose ancestors are also in the array.
  54. */
  55. removeSubsets(nodes: Array<Node>): Array<Node>;
  56. /**
  57. * finds all of the element nodes in the array that match the test predicate,
  58. * as well as any of their children that match it.
  59. */
  60. findAll(test: Predicate<ElementNode>, nodes: Array<Node>): Array<ElementNode>;
  61. /**
  62. * finds the first node in the array that matches the test predicate, or one
  63. * of its children.
  64. */
  65. findOne(test: Predicate<ElementNode>, elems: Array<ElementNode>): ElementNode | undefined,
  66. /**
  67. The adapter can also optionally include an equals method, if your DOM
  68. structure needs a custom equality test to compare two objects which refer
  69. to the same underlying node. If not provided, `css-select` will fall back to
  70. `a === b`.
  71. */
  72. equals?: (a: Node, b: Node) => boolean;
  73. /**
  74. * is the element in hovered state?
  75. */
  76. isHovered?: (elem: ElementNode) => boolean;
  77. /**
  78. * is the element in visited state?
  79. */
  80. isVisited?: (elem: ElementNode) => boolean;
  81. /**
  82. * is the element in active state?
  83. */
  84. isActive?: (elem: ElementNode) => boolean;
  85. }
  86. // TODO default types to the domutil/httpparser2 types
  87. interface Options<Node, ElementNode extends Node> {
  88. /**
  89. * When enabled, tag names will be case-sensitive. Default: false.
  90. */
  91. xmlMode?: boolean;
  92. /**
  93. * Limits the module to only use CSS3 selectors. Default: false.
  94. */
  95. strict?: boolean;
  96. /**
  97. * The last function in the stack, will be called with the last element
  98. * that's looked at. Should return true.
  99. */
  100. rootFunc?: (element: ElementNode) => true;
  101. /**
  102. * The adapter to use when interacting with the backing DOM structure. By
  103. * default it uses domutils.
  104. */
  105. adapter?: Adapter<Node, ElementNode>;
  106. }
  107. type CompiledQuery = (node: any) => boolean;
  108. type Query = string | CompiledQuery;
  109. /**
  110. * Compiles the query, returns a function.
  111. *
  112. * Supported simple selectors:
  113. * * Universal (*)
  114. * * Tag (<tagname>)
  115. * * Attribute ([attr=foo]), with supported comparisons:
  116. * * [attr] (existential)
  117. * * =
  118. * * ~=
  119. * * |=
  120. * * *=
  121. * * ^=
  122. * * $=
  123. * * !=
  124. * * Can be case insensitive (E.g. [attr=foo i])
  125. * * Pseudos:
  126. * * :not
  127. * * :root
  128. * * :empty
  129. * * :[first|last]-child[-of-type]
  130. * * :only-of-type, :only-child
  131. * * :nth-[last-]child[-of-type]
  132. * * :link, :visited (the latter doesn't match any elements)
  133. * * :checked
  134. * * :enabled, :disabled
  135. * * :required, :optional
  136. * * Nonstandard Pseudos (available when strict mode is not enabled):
  137. * * `:contains`
  138. * * `:icontains` (case-insensitive version of :contains)
  139. * * `:has`
  140. * * `:parent`
  141. * * `:selected`
  142. * * `:header, :button, :input, :text, :checkbox, :file, :password, :reset, :radio etc.
  143. * * :matches
  144. *
  145. * Supported Combinators:
  146. *
  147. * * Descendant (` `)
  148. * * Child (`>`)
  149. * * Parent (`<`) (when strict mode is not enabled)
  150. * * Sibling (`~`)
  151. * * Adjacent (`+`)
  152. */
  153. function compile(query: string): CompiledQuery;
  154. /**
  155. * @template Node The generic Node type for the DOM adapter being used.
  156. * @template ElementNode The Node type for elements for the DOM adapter being used.
  157. * @param elems Elements to query. If it is an element, its children will be queried..
  158. * @param query can be either a CSS selector string or a compiled query function.
  159. * @param [options] options for querying the document.
  160. * @see CSSselect.compile for supported selector queries.
  161. * @returns All matching elements.
  162. */
  163. function selectAll<Node, ElementNode extends Node>(
  164. query: Query,
  165. elems: Array<ElementNode> | ElementNode,
  166. options?: Options<Node, ElementNode>
  167. ): Array<ElementNode>;
  168. /**
  169. * @template Node The generic Node type for the DOM adapter being used.
  170. * @template ElementNode The Node type for elements for the DOM adapter being used.
  171. * @param elems Elements to query. If it is an element, its children will be queried..
  172. * @param query can be either a CSS selector string or a compiled query function.
  173. * @param [options] options for querying the document.
  174. * @see CSSselect.compile for supported selector queries.
  175. * @returns the first match, or null if there was no match.
  176. */
  177. function selectOne<Node, ElementNode extends Node>(
  178. query: Query,
  179. elems: Array<ElementNode> | ElementNode,
  180. options?: Options<Node, ElementNode>
  181. ): ElementNode | null;
  182. /**
  183. * Tests whether or not an element is matched by query.
  184. *
  185. * @template Node The generic Node type for the DOM adapter being used.
  186. * @template ElementNode The Node type for elements for the DOM adapter being used.
  187. * @param elem The element to test if it matches the query.
  188. * @param query can be either a CSS selector string or a compiled query function.
  189. * @param [options] options for querying the document.
  190. * @see CSSselect.compile for supported selector queries.
  191. * @returns
  192. */
  193. function is<Node, ElementNode extends Node>(
  194. elem: ElementNode,
  195. query: Query,
  196. options?: Options<Node, ElementNode>
  197. ): boolean;
  198. }