load.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { default as defaultOptions, flatten as flattenOptions, } from './options.js';
  2. import * as staticMethods from './static.js';
  3. import { Cheerio } from './cheerio.js';
  4. import { isHtml, isCheerio } from './utils.js';
  5. export function getLoad(parse, render) {
  6. /**
  7. * Create a querying function, bound to a document created from the provided markup.
  8. *
  9. * Note that similar to web browser contexts, this operation may introduce
  10. * `<html>`, `<head>`, and `<body>` elements; set `isDocument` to `false` to
  11. * switch to fragment mode and disable this.
  12. *
  13. * @param content - Markup to be loaded.
  14. * @param options - Options for the created instance.
  15. * @param isDocument - Allows parser to be switched to fragment mode.
  16. * @returns The loaded document.
  17. * @see {@link https://cheerio.js.org#loading} for additional usage information.
  18. */
  19. return function load(content, options, isDocument = true) {
  20. if (content == null) {
  21. throw new Error('cheerio.load() expects a string');
  22. }
  23. const internalOpts = { ...defaultOptions, ...flattenOptions(options) };
  24. const initialRoot = parse(content, internalOpts, isDocument, null);
  25. /** Create an extended class here, so that extensions only live on one instance. */
  26. class LoadedCheerio extends Cheerio {
  27. _make(selector, context) {
  28. const cheerio = initialize(selector, context);
  29. cheerio.prevObject = this;
  30. return cheerio;
  31. }
  32. _parse(content, options, isDocument, context) {
  33. return parse(content, options, isDocument, context);
  34. }
  35. _render(dom) {
  36. return render(dom, this.options);
  37. }
  38. }
  39. function initialize(selector, context, root = initialRoot, opts) {
  40. // $($)
  41. if (selector && isCheerio(selector))
  42. return selector;
  43. const options = {
  44. ...internalOpts,
  45. ...flattenOptions(opts),
  46. };
  47. const r = typeof root === 'string'
  48. ? [parse(root, options, false, null)]
  49. : 'length' in root
  50. ? root
  51. : [root];
  52. const rootInstance = isCheerio(r)
  53. ? r
  54. : new LoadedCheerio(r, null, options);
  55. // Add a cyclic reference, so that calling methods on `_root` never fails.
  56. rootInstance._root = rootInstance;
  57. // $(), $(null), $(undefined), $(false)
  58. if (!selector) {
  59. return new LoadedCheerio(undefined, rootInstance, options);
  60. }
  61. const elements = typeof selector === 'string' && isHtml(selector)
  62. ? // $(<html>)
  63. parse(selector, options, false, null).children
  64. : isNode(selector)
  65. ? // $(dom)
  66. [selector]
  67. : Array.isArray(selector)
  68. ? // $([dom])
  69. selector
  70. : undefined;
  71. const instance = new LoadedCheerio(elements, rootInstance, options);
  72. if (elements) {
  73. return instance;
  74. }
  75. if (typeof selector !== 'string') {
  76. throw new Error('Unexpected type of selector');
  77. }
  78. // We know that our selector is a string now.
  79. let search = selector;
  80. const searchContext = !context
  81. ? // If we don't have a context, maybe we have a root, from loading
  82. rootInstance
  83. : typeof context === 'string'
  84. ? isHtml(context)
  85. ? // $('li', '<ul>...</ul>')
  86. new LoadedCheerio([parse(context, options, false, null)], rootInstance, options)
  87. : // $('li', 'ul')
  88. ((search = `${context} ${search}`), rootInstance)
  89. : isCheerio(context)
  90. ? // $('li', $)
  91. context
  92. : // $('li', node), $('li', [nodes])
  93. new LoadedCheerio(Array.isArray(context) ? context : [context], rootInstance, options);
  94. // If we still don't have a context, return
  95. if (!searchContext)
  96. return instance;
  97. /*
  98. * #id, .class, tag
  99. */
  100. return searchContext.find(search);
  101. }
  102. // Add in static methods & properties
  103. Object.assign(initialize, staticMethods, {
  104. load,
  105. // `_root` and `_options` are used in static methods.
  106. _root: initialRoot,
  107. _options: internalOpts,
  108. // Add `fn` for plugins
  109. fn: LoadedCheerio.prototype,
  110. // Add the prototype here to maintain `instanceof` behavior.
  111. prototype: LoadedCheerio.prototype,
  112. });
  113. return initialize;
  114. };
  115. }
  116. function isNode(obj) {
  117. return (!!obj.name ||
  118. obj.type === 'root' ||
  119. obj.type === 'text' ||
  120. obj.type === 'comment');
  121. }
  122. //# sourceMappingURL=load.js.map