load.js 6.3 KB

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