index.d.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // TODO default types to the domutil/httpparser2 types
  75. interface Options<Node, ElementNode extends Node> {
  76. /**
  77. * When enabled, tag names will be case-sensitive. Default: false.
  78. */
  79. xmlMode?: boolean;
  80. /**
  81. * Limits the module to only use CSS3 selectors. Default: false.
  82. */
  83. strict?: boolean;
  84. /**
  85. * The last function in the stack, will be called with the last element
  86. * that's looked at. Should return true.
  87. */
  88. rootFunc?: (element: ElementNode) => true;
  89. /**
  90. * The adapter to use when interacting with the backing DOM structure. By
  91. * default it uses domutils.
  92. */
  93. adapter?: Adapter<Node, ElementNode>;
  94. }
  95. type CompiledQuery = (node: any) => boolean;
  96. type Query = string | CompiledQuery;
  97. /**
  98. * Compiles the query, returns a function.
  99. *
  100. * Supported simple selectors:
  101. * * Universal (*)
  102. * * Tag (<tagname>)
  103. * * Attribute ([attr=foo]), with supported comparisons:
  104. * * [attr] (existential)
  105. * * =
  106. * * ~=
  107. * * |=
  108. * * *=
  109. * * ^=
  110. * * $=
  111. * * !=
  112. * * Can be case insensitive (E.g. [attr=foo i])
  113. * * Pseudos:
  114. * * :not
  115. * * :root
  116. * * :empty
  117. * * :[first|last]-child[-of-type]
  118. * * :only-of-type, :only-child
  119. * * :nth-[last-]child[-of-type]
  120. * * :link, :visited (the latter doesn't match any elements)
  121. * * :checked
  122. * * :enabled, :disabled
  123. * * :required, :optional
  124. * * Nonstandard Pseudos (available when strict mode is not enabled):
  125. * * `:contains`
  126. * * `:icontains` (case-insensitive version of :contains)
  127. * * `:has`
  128. * * `:parent`
  129. * * `:selected`
  130. * * `:header, :button, :input, :text, :checkbox, :file, :password, :reset, :radio etc.
  131. * * :matches
  132. *
  133. * Supported Combinators:
  134. *
  135. * * Descendant (` `)
  136. * * Child (`>`)
  137. * * Parent (`<`) (when strict mode is not enabled)
  138. * * Sibling (`~`)
  139. * * Adjacent (`+`)
  140. */
  141. function compile(query: string): CompiledQuery;
  142. /**
  143. * @template Node The generic Node type for the DOM adapter being used.
  144. * @template ElementNode The Node type for elements for the DOM adapter being used.
  145. * @param elems Elements to query. If it is an element, its children will be queried..
  146. * @param query can be either a CSS selector string or a compiled query function.
  147. * @param [options] options for querying the document.
  148. * @see CSSselect.compile for supported selector queries.
  149. * @returns All matching elements.
  150. */
  151. function selectAll<Node, ElementNode extends Node>(
  152. query: Query,
  153. elems: Array<ElementNode> | ElementNode,
  154. options?: Options<Node, ElementNode>
  155. ): Array<ElementNode>;
  156. /**
  157. * @template Node The generic Node type for the DOM adapter being used.
  158. * @template ElementNode The Node type for elements for the DOM adapter being used.
  159. * @param elems Elements to query. If it is an element, its children will be queried..
  160. * @param query can be either a CSS selector string or a compiled query function.
  161. * @param [options] options for querying the document.
  162. * @see CSSselect.compile for supported selector queries.
  163. * @returns the first match, or null if there was no match.
  164. */
  165. function selectOne<Node, ElementNode extends Node>(
  166. query: Query,
  167. elems: Array<ElementNode> | ElementNode,
  168. options?: Options<Node, ElementNode>
  169. ): ElementNode | null;
  170. /**
  171. * Tests whether or not an element is matched by query.
  172. *
  173. * @template Node The generic Node type for the DOM adapter being used.
  174. * @template ElementNode The Node type for elements for the DOM adapter being used.
  175. * @param elem The element to test if it matches the query.
  176. * @param query can be either a CSS selector string or a compiled query function.
  177. * @param [options] options for querying the document.
  178. * @see CSSselect.compile for supported selector queries.
  179. * @returns
  180. */
  181. function is<Node, ElementNode extends Node>(
  182. elem: ElementNode,
  183. query: Query,
  184. options?: Options<Node, ElementNode>
  185. ): boolean;
  186. }