load.js 7.1 KB

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