static.js 6.2 KB

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