static.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  14. if (k2 === undefined) k2 = k;
  15. var desc = Object.getOwnPropertyDescriptor(m, k);
  16. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  17. desc = { enumerable: true, get: function() { return m[k]; } };
  18. }
  19. Object.defineProperty(o, k2, desc);
  20. }) : (function(o, m, k, k2) {
  21. if (k2 === undefined) k2 = k;
  22. o[k2] = m[k];
  23. }));
  24. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  25. Object.defineProperty(o, "default", { enumerable: true, value: v });
  26. }) : function(o, v) {
  27. o["default"] = v;
  28. });
  29. var __importStar = (this && this.__importStar) || function (mod) {
  30. if (mod && mod.__esModule) return mod;
  31. var result = {};
  32. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  33. __setModuleDefault(result, mod);
  34. return result;
  35. };
  36. Object.defineProperty(exports, "__esModule", { value: true });
  37. exports.merge = exports.contains = exports.root = exports.parseHTML = exports.text = exports.xml = exports.html = void 0;
  38. var domutils_1 = require("domutils");
  39. var options_js_1 = __importStar(require("./options.js"));
  40. /**
  41. * Helper function to render a DOM.
  42. *
  43. * @param that - Cheerio instance to render.
  44. * @param dom - The DOM to render. Defaults to `that`'s root.
  45. * @param options - Options for rendering.
  46. * @returns The rendered document.
  47. */
  48. function render(that, dom, options) {
  49. if (!that)
  50. return '';
  51. return that(dom !== null && dom !== void 0 ? dom : that._root.children, null, undefined, options).toString();
  52. }
  53. /**
  54. * Checks if a passed object is an options object.
  55. *
  56. * @param dom - Object to check if it is an options object.
  57. * @returns Whether the object is an options object.
  58. */
  59. function isOptions(dom, options) {
  60. return (!options &&
  61. typeof dom === 'object' &&
  62. dom != null &&
  63. !('length' in dom) &&
  64. !('type' in dom));
  65. }
  66. function html(dom, options) {
  67. /*
  68. * Be flexible about parameters, sometimes we call html(),
  69. * with options as only parameter
  70. * check dom argument for dom element specific properties
  71. * assume there is no 'length' or 'type' properties in the options object
  72. */
  73. var toRender = isOptions(dom) ? ((options = dom), undefined) : dom;
  74. /*
  75. * Sometimes `$.html()` is used without preloading html,
  76. * so fallback non-existing options to the default ones.
  77. */
  78. var opts = __assign(__assign(__assign({}, options_js_1.default), this === null || this === void 0 ? void 0 : this._options), (0, options_js_1.flatten)(options !== null && options !== void 0 ? options : {}));
  79. return render(this, toRender, opts);
  80. }
  81. exports.html = html;
  82. /**
  83. * Render the document as XML.
  84. *
  85. * @param dom - Element to render.
  86. * @returns THe rendered document.
  87. */
  88. function xml(dom) {
  89. var options = __assign(__assign({}, this._options), { xmlMode: true });
  90. return render(this, dom, options);
  91. }
  92. exports.xml = xml;
  93. /**
  94. * Render the document as text.
  95. *
  96. * This returns the `textContent` of the passed elements. The result will
  97. * include the contents of `script` and `stype` elements. To avoid this, use
  98. * `.prop('innerText')` instead.
  99. *
  100. * @param elements - Elements to render.
  101. * @returns The rendered document.
  102. */
  103. function text(elements) {
  104. var elems = elements ? elements : this ? this.root() : [];
  105. var ret = '';
  106. for (var i = 0; i < elems.length; i++) {
  107. ret += (0, domutils_1.textContent)(elems[i]);
  108. }
  109. return ret;
  110. }
  111. exports.text = text;
  112. function parseHTML(data, context, keepScripts) {
  113. if (keepScripts === void 0) { keepScripts = typeof context === 'boolean' ? context : false; }
  114. if (!data || typeof data !== 'string') {
  115. return null;
  116. }
  117. if (typeof context === 'boolean') {
  118. keepScripts = context;
  119. }
  120. var parsed = this.load(data, options_js_1.default, false);
  121. if (!keepScripts) {
  122. parsed('script').remove();
  123. }
  124. /*
  125. * The `children` array is used by Cheerio internally to group elements that
  126. * share the same parents. When nodes created through `parseHTML` are
  127. * inserted into previously-existing DOM structures, they will be removed
  128. * from the `children` array. The results of `parseHTML` should remain
  129. * constant across these operations, so a shallow copy should be returned.
  130. */
  131. return parsed.root()[0].children.slice();
  132. }
  133. exports.parseHTML = parseHTML;
  134. /**
  135. * Sometimes you need to work with the top-level root element. To query it, you
  136. * can use `$.root()`.
  137. *
  138. * @example
  139. *
  140. * ```js
  141. * $.root().append('<ul id="vegetables"></ul>').html();
  142. * //=> <ul id="fruits">...</ul><ul id="vegetables"></ul>
  143. * ```
  144. *
  145. * @returns Cheerio instance wrapping the root node.
  146. * @alias Cheerio.root
  147. */
  148. function root() {
  149. return this(this._root);
  150. }
  151. exports.root = root;
  152. /**
  153. * Checks to see if the `contained` DOM element is a descendant of the
  154. * `container` DOM element.
  155. *
  156. * @param container - Potential parent node.
  157. * @param contained - Potential child node.
  158. * @returns Indicates if the nodes contain one another.
  159. * @alias Cheerio.contains
  160. * @see {@link https://api.jquery.com/jQuery.contains/}
  161. */
  162. function contains(container, contained) {
  163. // According to the jQuery API, an element does not "contain" itself
  164. if (contained === container) {
  165. return false;
  166. }
  167. /*
  168. * Step up the descendants, stopping when the root element is reached
  169. * (signaled by `.parent` returning a reference to the same object)
  170. */
  171. var next = contained;
  172. while (next && next !== next.parent) {
  173. next = next.parent;
  174. if (next === container) {
  175. return true;
  176. }
  177. }
  178. return false;
  179. }
  180. exports.contains = contains;
  181. /**
  182. * $.merge().
  183. *
  184. * @param arr1 - First array.
  185. * @param arr2 - Second array.
  186. * @returns `arr1`, with elements of `arr2` inserted.
  187. * @alias Cheerio.merge
  188. * @see {@link https://api.jquery.com/jQuery.merge/}
  189. */
  190. function merge(arr1, arr2) {
  191. if (!isArrayLike(arr1) || !isArrayLike(arr2)) {
  192. return;
  193. }
  194. var newLength = arr1.length;
  195. var len = +arr2.length;
  196. for (var i = 0; i < len; i++) {
  197. arr1[newLength++] = arr2[i];
  198. }
  199. arr1.length = newLength;
  200. return arr1;
  201. }
  202. exports.merge = merge;
  203. /**
  204. * Checks if an object is array-like.
  205. *
  206. * @param item - Item to check.
  207. * @returns Indicates if the item is array-like.
  208. */
  209. function isArrayLike(item) {
  210. if (Array.isArray(item)) {
  211. return true;
  212. }
  213. if (typeof item !== 'object' ||
  214. !Object.prototype.hasOwnProperty.call(item, 'length') ||
  215. typeof item.length !== 'number' ||
  216. item.length < 0) {
  217. return false;
  218. }
  219. for (var i = 0; i < item.length; i++) {
  220. if (!(i in item)) {
  221. return false;
  222. }
  223. }
  224. return true;
  225. }
  226. //# sourceMappingURL=static.js.map