load.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __assign = (this && this.__assign) || function () {
  18. __assign = Object.assign || function(t) {
  19. for (var s, i = 1, n = arguments.length; i < n; i++) {
  20. s = arguments[i];
  21. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  22. t[p] = s[p];
  23. }
  24. return t;
  25. };
  26. return __assign.apply(this, arguments);
  27. };
  28. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  29. if (k2 === undefined) k2 = k;
  30. var desc = Object.getOwnPropertyDescriptor(m, k);
  31. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  32. desc = { enumerable: true, get: function() { return m[k]; } };
  33. }
  34. Object.defineProperty(o, k2, desc);
  35. }) : (function(o, m, k, k2) {
  36. if (k2 === undefined) k2 = k;
  37. o[k2] = m[k];
  38. }));
  39. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  40. Object.defineProperty(o, "default", { enumerable: true, value: v });
  41. }) : function(o, v) {
  42. o["default"] = v;
  43. });
  44. var __importStar = (this && this.__importStar) || function (mod) {
  45. if (mod && mod.__esModule) return mod;
  46. var result = {};
  47. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  48. __setModuleDefault(result, mod);
  49. return result;
  50. };
  51. Object.defineProperty(exports, "__esModule", { value: true });
  52. exports.getLoad = void 0;
  53. var options_js_1 = __importStar(require("./options.js"));
  54. var staticMethods = __importStar(require("./static.js"));
  55. var cheerio_js_1 = require("./cheerio.js");
  56. var utils_js_1 = require("./utils.js");
  57. function getLoad(parse, render) {
  58. /**
  59. * Create a querying function, bound to a document created from the provided markup.
  60. *
  61. * Note that similar to web browser contexts, this operation may introduce
  62. * `<html>`, `<head>`, and `<body>` elements; set `isDocument` to `false` to
  63. * switch to fragment mode and disable this.
  64. *
  65. * @param content - Markup to be loaded.
  66. * @param options - Options for the created instance.
  67. * @param isDocument - Allows parser to be switched to fragment mode.
  68. * @returns The loaded document.
  69. * @see {@link https://cheerio.js.org#loading} for additional usage information.
  70. */
  71. return function load(content, options, isDocument) {
  72. if (isDocument === void 0) { isDocument = true; }
  73. if (content == null) {
  74. throw new Error('cheerio.load() expects a string');
  75. }
  76. var internalOpts = __assign(__assign({}, options_js_1.default), (0, options_js_1.flatten)(options));
  77. var initialRoot = parse(content, internalOpts, isDocument, null);
  78. /** Create an extended class here, so that extensions only live on one instance. */
  79. var LoadedCheerio = /** @class */ (function (_super) {
  80. __extends(LoadedCheerio, _super);
  81. function LoadedCheerio() {
  82. return _super !== null && _super.apply(this, arguments) || this;
  83. }
  84. LoadedCheerio.prototype._make = function (selector, context) {
  85. var cheerio = initialize(selector, context);
  86. cheerio.prevObject = this;
  87. return cheerio;
  88. };
  89. LoadedCheerio.prototype._parse = function (content, options, isDocument, context) {
  90. return parse(content, options, isDocument, context);
  91. };
  92. LoadedCheerio.prototype._render = function (dom) {
  93. return render(dom, this.options);
  94. };
  95. return LoadedCheerio;
  96. }(cheerio_js_1.Cheerio));
  97. function initialize(selector, context, root, opts) {
  98. if (root === void 0) { root = initialRoot; }
  99. // $($)
  100. if (selector && (0, utils_js_1.isCheerio)(selector))
  101. return selector;
  102. var options = __assign(__assign({}, internalOpts), (0, options_js_1.flatten)(opts));
  103. var r = typeof root === 'string'
  104. ? [parse(root, options, false, null)]
  105. : 'length' in root
  106. ? root
  107. : [root];
  108. var rootInstance = (0, utils_js_1.isCheerio)(r)
  109. ? r
  110. : new LoadedCheerio(r, null, options);
  111. // Add a cyclic reference, so that calling methods on `_root` never fails.
  112. rootInstance._root = rootInstance;
  113. // $(), $(null), $(undefined), $(false)
  114. if (!selector) {
  115. return new LoadedCheerio(undefined, rootInstance, options);
  116. }
  117. var elements = typeof selector === 'string' && (0, utils_js_1.isHtml)(selector)
  118. ? // $(<html>)
  119. parse(selector, options, false, null).children
  120. : isNode(selector)
  121. ? // $(dom)
  122. [selector]
  123. : Array.isArray(selector)
  124. ? // $([dom])
  125. selector
  126. : undefined;
  127. var instance = new LoadedCheerio(elements, rootInstance, options);
  128. if (elements) {
  129. return instance;
  130. }
  131. if (typeof selector !== 'string') {
  132. throw new Error('Unexpected type of selector');
  133. }
  134. // We know that our selector is a string now.
  135. var search = selector;
  136. var searchContext = !context
  137. ? // If we don't have a context, maybe we have a root, from loading
  138. rootInstance
  139. : typeof context === 'string'
  140. ? (0, utils_js_1.isHtml)(context)
  141. ? // $('li', '<ul>...</ul>')
  142. new LoadedCheerio([parse(context, options, false, null)], rootInstance, options)
  143. : // $('li', 'ul')
  144. ((search = "".concat(context, " ").concat(search)), rootInstance)
  145. : (0, utils_js_1.isCheerio)(context)
  146. ? // $('li', $)
  147. context
  148. : // $('li', node), $('li', [nodes])
  149. new LoadedCheerio(Array.isArray(context) ? context : [context], rootInstance, options);
  150. // If we still don't have a context, return
  151. if (!searchContext)
  152. return instance;
  153. /*
  154. * #id, .class, tag
  155. */
  156. return searchContext.find(search);
  157. }
  158. // Add in static methods & properties
  159. Object.assign(initialize, staticMethods, {
  160. load: load,
  161. // `_root` and `_options` are used in static methods.
  162. _root: initialRoot,
  163. _options: internalOpts,
  164. // Add `fn` for plugins
  165. fn: LoadedCheerio.prototype,
  166. // Add the prototype here to maintain `instanceof` behavior.
  167. prototype: LoadedCheerio.prototype,
  168. });
  169. return initialize;
  170. };
  171. }
  172. exports.getLoad = getLoad;
  173. function isNode(obj) {
  174. return (!!obj.name ||
  175. obj.type === 'root' ||
  176. obj.type === 'text' ||
  177. obj.type === 'comment');
  178. }
  179. //# sourceMappingURL=load.js.map