options.d.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import type { DomHandlerOptions } from 'domhandler';
  2. import type { ParserOptions as HTMLParser2ParserOptions } from 'htmlparser2';
  3. import type { ParserOptions as Parse5ParserOptions } from 'parse5';
  4. import type { Htmlparser2TreeAdapterMap } from 'parse5-htmlparser2-tree-adapter';
  5. import type { Options as SelectOptions } from 'cheerio-select';
  6. /**
  7. * Options accepted by htmlparser2, the default parser for XML.
  8. *
  9. * @see https://github.com/fb55/htmlparser2/wiki/Parser-options
  10. */
  11. export interface HTMLParser2Options extends DomHandlerOptions, HTMLParser2ParserOptions {
  12. }
  13. /**
  14. * Options accepted by Cheerio.
  15. *
  16. * Please note that parser-specific options are _only recognized_ if the
  17. * relevant parser is used.
  18. */
  19. export interface CheerioOptions extends Parse5ParserOptions<Htmlparser2TreeAdapterMap> {
  20. /**
  21. * Recommended way of configuring htmlparser2 when wanting to parse XML.
  22. *
  23. * This will switch Cheerio to use htmlparser2.
  24. *
  25. * @default false
  26. */
  27. xml?: HTMLParser2Options | boolean;
  28. /**
  29. * Enable xml mode, which will switch Cheerio to use htmlparser2.
  30. *
  31. * @deprecated Please use the `xml` option instead.
  32. * @default false
  33. */
  34. xmlMode?: boolean;
  35. /** The base URI for the document. Used to resolve the `href` and `src` props. */
  36. baseURI?: string | URL;
  37. /**
  38. * Is the document in quirks mode?
  39. *
  40. * This will lead to `.className` and `#id` being case-insensitive.
  41. *
  42. * @default false
  43. */
  44. quirksMode?: SelectOptions['quirksMode'];
  45. /**
  46. * Extension point for pseudo-classes.
  47. *
  48. * Maps from names to either strings of functions.
  49. *
  50. * - A string value is a selector that the element must match to be selected.
  51. * - A function is called with the element as its first argument, and optional
  52. * parameters second. If it returns true, the element is selected.
  53. *
  54. * @example
  55. *
  56. * ```js
  57. * const $ = cheerio.load(
  58. * '<div class="foo"></div><div data-bar="boo"></div>',
  59. * {
  60. * pseudos: {
  61. * // `:foo` is an alias for `div.foo`
  62. * foo: 'div.foo',
  63. * // `:bar(val)` is equivalent to `[data-bar=val s]`
  64. * bar: (el, val) => el.attribs['data-bar'] === val,
  65. * },
  66. * },
  67. * );
  68. *
  69. * $(':foo').length; // 1
  70. * $('div:bar(boo)').length; // 1
  71. * $('div:bar(baz)').length; // 0
  72. * ```
  73. */
  74. pseudos?: SelectOptions['pseudos'];
  75. }
  76. /** Internal options for Cheerio. */
  77. export interface InternalOptions extends HTMLParser2Options, Omit<CheerioOptions, 'xml'> {
  78. /**
  79. * Whether to use htmlparser2.
  80. *
  81. * This is set to true if `xml` is set to true.
  82. */
  83. _useHtmlParser2?: boolean;
  84. }
  85. /**
  86. * Flatten the options for Cheerio.
  87. *
  88. * This will set `_useHtmlParser2` to true if `xml` is set to true.
  89. *
  90. * @param options - The options to flatten.
  91. * @param baseOptions - The base options to use.
  92. * @returns The flattened options.
  93. */
  94. export declare function flattenOptions(options?: CheerioOptions | null, baseOptions?: InternalOptions): InternalOptions;
  95. //# sourceMappingURL=options.d.ts.map