load.js 5.5 KB

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