cheerio.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import * as Attributes from './api/attributes.js';
  2. import * as Traversing from './api/traversing.js';
  3. import * as Manipulation from './api/manipulation.js';
  4. import * as Css from './api/css.js';
  5. import * as Forms from './api/forms.js';
  6. import * as Extract from './api/extract.js';
  7. /**
  8. * The cheerio class is the central class of the library. It wraps a set of
  9. * elements and provides an API for traversing, modifying, and interacting with
  10. * the set.
  11. *
  12. * Loading a document will return the Cheerio class bound to the root element of
  13. * the document. The class will be instantiated when querying the document (when
  14. * calling `$('selector')`).
  15. *
  16. * @example This is the HTML markup we will be using in all of the API examples:
  17. *
  18. * ```html
  19. * <ul id="fruits">
  20. * <li class="apple">Apple</li>
  21. * <li class="orange">Orange</li>
  22. * <li class="pear">Pear</li>
  23. * </ul>
  24. * ```
  25. */
  26. export class Cheerio {
  27. /**
  28. * Instance of cheerio. Methods are specified in the modules. Usage of this
  29. * constructor is not recommended. Please use `$.load` instead.
  30. *
  31. * @private
  32. * @param elements - The new selection.
  33. * @param root - Sets the root node.
  34. * @param options - Options for the instance.
  35. */
  36. constructor(elements, root, options) {
  37. this.length = 0;
  38. this.options = options;
  39. this._root = root;
  40. if (elements) {
  41. for (let idx = 0; idx < elements.length; idx++) {
  42. this[idx] = elements[idx];
  43. }
  44. this.length = elements.length;
  45. }
  46. }
  47. }
  48. /** Set a signature of the object. */
  49. Cheerio.prototype.cheerio = '[cheerio object]';
  50. /*
  51. * Make cheerio an array-like object
  52. */
  53. Cheerio.prototype.splice = Array.prototype.splice;
  54. // Support for (const element of $(...)) iteration:
  55. Cheerio.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
  56. // Plug in the API
  57. Object.assign(Cheerio.prototype, Attributes, Traversing, Manipulation, Css, Forms, Extract);
  58. //# sourceMappingURL=cheerio.js.map