options.d.ts 3.2 KB

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