load.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.load = void 0;
  4. var tslib_1 = require("tslib");
  5. var options_1 = tslib_1.__importStar(require("./options"));
  6. var staticMethods = tslib_1.__importStar(require("./static"));
  7. var cheerio_1 = require("./cheerio");
  8. var parse_1 = tslib_1.__importDefault(require("./parse"));
  9. /**
  10. * Create a querying function, bound to a document created from the provided
  11. * markup. Note that similar to web browser contexts, this operation may
  12. * introduce `<html>`, `<head>`, and `<body>` elements; set `isDocument` to
  13. * `false` to 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#loading} for additional usage information.
  20. */
  21. function load(content, options, isDocument) {
  22. if (isDocument === void 0) { isDocument = true; }
  23. if (content == null) {
  24. throw new Error('cheerio.load() expects a string');
  25. }
  26. var internalOpts = tslib_1.__assign(tslib_1.__assign({}, options_1.default), options_1.flatten(options));
  27. var root = parse_1.default(content, internalOpts, isDocument);
  28. /** Create an extended class here, so that extensions only live on one instance. */
  29. var LoadedCheerio = /** @class */ (function (_super) {
  30. tslib_1.__extends(LoadedCheerio, _super);
  31. function LoadedCheerio() {
  32. return _super !== null && _super.apply(this, arguments) || this;
  33. }
  34. return LoadedCheerio;
  35. }(cheerio_1.Cheerio));
  36. function initialize(selector, context, r, opts) {
  37. if (r === void 0) { r = root; }
  38. return new LoadedCheerio(selector, context, r, tslib_1.__assign(tslib_1.__assign({}, internalOpts), options_1.flatten(opts)));
  39. }
  40. // Add in static methods & properties
  41. Object.assign(initialize, staticMethods, {
  42. load: load,
  43. // `_root` and `_options` are used in static methods.
  44. _root: root,
  45. _options: internalOpts,
  46. // Add `fn` for plugins
  47. fn: LoadedCheerio.prototype,
  48. // Add the prototype here to maintain `instanceof` behavior.
  49. prototype: LoadedCheerio.prototype,
  50. });
  51. return initialize;
  52. }
  53. exports.load = load;