cheerio.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
  2. import type { InternalOptions } from './options.js';
  3. import type { AnyNode, Document, ParentNode } from 'domhandler';
  4. import type { BasicAcceptedElems } from './types.js';
  5. import * as Attributes from './api/attributes.js';
  6. import * as Traversing from './api/traversing.js';
  7. import * as Manipulation from './api/manipulation.js';
  8. import * as Css from './api/css.js';
  9. import * as Forms from './api/forms.js';
  10. import * as Extract from './api/extract.js';
  11. type MethodsType = typeof Attributes &
  12. typeof Traversing &
  13. typeof Manipulation &
  14. typeof Css &
  15. typeof Forms &
  16. typeof Extract;
  17. /**
  18. * The cheerio class is the central class of the library. It wraps a set of
  19. * elements and provides an API for traversing, modifying, and interacting with
  20. * the set.
  21. *
  22. * Loading a document will return the Cheerio class bound to the root element of
  23. * the document. The class will be instantiated when querying the document (when
  24. * calling `$('selector')`).
  25. *
  26. * @example This is the HTML markup we will be using in all of the API examples:
  27. *
  28. * ```html
  29. * <ul id="fruits">
  30. * <li class="apple">Apple</li>
  31. * <li class="orange">Orange</li>
  32. * <li class="pear">Pear</li>
  33. * </ul>
  34. * ```
  35. */
  36. export abstract class Cheerio<T> implements ArrayLike<T> {
  37. length = 0;
  38. [index: number]: T;
  39. options: InternalOptions;
  40. /**
  41. * The root of the document. Can be set by using the `root` argument of the
  42. * constructor.
  43. *
  44. * @private
  45. */
  46. _root: Cheerio<Document> | null;
  47. /**
  48. * Instance of cheerio. Methods are specified in the modules. Usage of this
  49. * constructor is not recommended. Please use `$.load` instead.
  50. *
  51. * @private
  52. * @param elements - The new selection.
  53. * @param root - Sets the root node.
  54. * @param options - Options for the instance.
  55. */
  56. constructor(
  57. elements: ArrayLike<T> | undefined,
  58. root: Cheerio<Document> | null,
  59. options: InternalOptions,
  60. ) {
  61. this.options = options;
  62. this._root = root;
  63. if (elements) {
  64. for (let idx = 0; idx < elements.length; idx++) {
  65. this[idx] = elements[idx];
  66. }
  67. this.length = elements.length;
  68. }
  69. }
  70. prevObject: Cheerio<any> | undefined;
  71. /**
  72. * Make a cheerio object.
  73. *
  74. * @private
  75. * @param dom - The contents of the new object.
  76. * @param context - The context of the new object.
  77. * @returns The new cheerio object.
  78. */
  79. abstract _make<T>(
  80. dom: ArrayLike<T> | T | string,
  81. context?: BasicAcceptedElems<AnyNode>,
  82. ): Cheerio<T>;
  83. /**
  84. * Parses some content.
  85. *
  86. * @private
  87. * @param content - Content to parse.
  88. * @param options - Options for parsing.
  89. * @param isDocument - Allows parser to be switched to fragment mode.
  90. * @returns A document containing the `content`.
  91. */
  92. abstract _parse(
  93. content: string | Document | AnyNode | AnyNode[] | Buffer,
  94. options: InternalOptions,
  95. isDocument: boolean,
  96. context: ParentNode | null,
  97. ): Document;
  98. /**
  99. * Render an element or a set of elements.
  100. *
  101. * @private
  102. * @param dom - DOM to render.
  103. * @returns The rendered DOM.
  104. */
  105. abstract _render(dom: AnyNode | ArrayLike<AnyNode>): string;
  106. }
  107. export interface Cheerio<T> extends MethodsType, Iterable<T> {
  108. cheerio: '[cheerio object]';
  109. splice: typeof Array.prototype.splice;
  110. }
  111. /** Set a signature of the object. */
  112. Cheerio.prototype.cheerio = '[cheerio object]';
  113. /*
  114. * Make cheerio an array-like object
  115. */
  116. Cheerio.prototype.splice = Array.prototype.splice;
  117. // Support for (const element of $(...)) iteration:
  118. Cheerio.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
  119. // Plug in the API
  120. Object.assign(
  121. Cheerio.prototype,
  122. Attributes,
  123. Traversing,
  124. Manipulation,
  125. Css,
  126. Forms,
  127. Extract,
  128. );