cheerio.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Cheerio = void 0;
  4. var tslib_1 = require("tslib");
  5. var parse_1 = tslib_1.__importDefault(require("./parse"));
  6. var options_1 = tslib_1.__importDefault(require("./options"));
  7. var utils_1 = require("./utils");
  8. var Attributes = tslib_1.__importStar(require("./api/attributes"));
  9. var Traversing = tslib_1.__importStar(require("./api/traversing"));
  10. var Manipulation = tslib_1.__importStar(require("./api/manipulation"));
  11. var Css = tslib_1.__importStar(require("./api/css"));
  12. var Forms = tslib_1.__importStar(require("./api/forms"));
  13. var Cheerio = /** @class */ (function () {
  14. /**
  15. * Instance of cheerio. Methods are specified in the modules. Usage of this
  16. * constructor is not recommended. Please use $.load instead.
  17. *
  18. * @private
  19. * @param selector - The new selection.
  20. * @param context - Context of the selection.
  21. * @param root - Sets the root node.
  22. * @param options - Options for the instance.
  23. */
  24. function Cheerio(selector, context, root, options) {
  25. var _this = this;
  26. if (options === void 0) { options = options_1.default; }
  27. this.length = 0;
  28. this.options = options;
  29. // $(), $(null), $(undefined), $(false)
  30. if (!selector)
  31. return this;
  32. if (root) {
  33. if (typeof root === 'string')
  34. root = parse_1.default(root, this.options, false);
  35. this._root = new this.constructor(root, null, null, this.options);
  36. // Add a cyclic reference, so that calling methods on `_root` never fails.
  37. this._root._root = this._root;
  38. }
  39. // $($)
  40. if (utils_1.isCheerio(selector))
  41. return selector;
  42. var elements = typeof selector === 'string' && utils_1.isHtml(selector)
  43. ? // $(<html>)
  44. parse_1.default(selector, this.options, false).children
  45. : isNode(selector)
  46. ? // $(dom)
  47. [selector]
  48. : Array.isArray(selector)
  49. ? // $([dom])
  50. selector
  51. : null;
  52. if (elements) {
  53. elements.forEach(function (elem, idx) {
  54. _this[idx] = elem;
  55. });
  56. this.length = elements.length;
  57. return this;
  58. }
  59. // We know that our selector is a string now.
  60. var search = selector;
  61. var searchContext = !context
  62. ? // If we don't have a context, maybe we have a root, from loading
  63. this._root
  64. : typeof context === 'string'
  65. ? utils_1.isHtml(context)
  66. ? // $('li', '<ul>...</ul>')
  67. this._make(parse_1.default(context, this.options, false))
  68. : // $('li', 'ul')
  69. ((search = context + " " + search), this._root)
  70. : utils_1.isCheerio(context)
  71. ? // $('li', $)
  72. context
  73. : // $('li', node), $('li', [nodes])
  74. this._make(context);
  75. // If we still don't have a context, return
  76. if (!searchContext)
  77. return this;
  78. /*
  79. * #id, .class, tag
  80. */
  81. // @ts-expect-error No good way to type this — we will always return `Cheerio<Element>` here.
  82. return searchContext.find(search);
  83. }
  84. /**
  85. * Make a cheerio object.
  86. *
  87. * @private
  88. * @param dom - The contents of the new object.
  89. * @param context - The context of the new object.
  90. * @returns The new cheerio object.
  91. */
  92. Cheerio.prototype._make = function (dom, context) {
  93. var cheerio = new this.constructor(dom, context, this._root, this.options);
  94. cheerio.prevObject = this;
  95. return cheerio;
  96. };
  97. return Cheerio;
  98. }());
  99. exports.Cheerio = Cheerio;
  100. /** Set a signature of the object. */
  101. Cheerio.prototype.cheerio = '[cheerio object]';
  102. /*
  103. * Make cheerio an array-like object
  104. */
  105. Cheerio.prototype.splice = Array.prototype.splice;
  106. // Support for (const element of $(...)) iteration:
  107. Cheerio.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
  108. // Plug in the API
  109. Object.assign(Cheerio.prototype, Attributes, Traversing, Manipulation, Css, Forms);
  110. function isNode(obj) {
  111. return (!!obj.name ||
  112. obj.type === 'root' ||
  113. obj.type === 'text' ||
  114. obj.type === 'comment');
  115. }