load.js 5.1 KB

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