static.js 6.5 KB

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