static.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import { describe, it, expect, beforeEach } from 'vitest';
  2. import { cheerio, food, eleven } from './__fixtures__/fixtures.js';
  3. import { type CheerioAPI } from './index.js';
  4. describe('cheerio', () => {
  5. describe('.html', () => {
  6. it('() : should return innerHTML; $.html(obj) should return outerHTML', () => {
  7. const $div = cheerio(
  8. 'div',
  9. '<div><span>foo</span><span>bar</span></div>',
  10. );
  11. const span = $div.children()[1];
  12. expect(cheerio(span).html()).toBe('bar');
  13. expect(cheerio.html(span)).toBe('<span>bar</span>');
  14. });
  15. it('(<obj>) : should accept an object, an array, or a cheerio object', () => {
  16. const $span = cheerio('<span>foo</span>');
  17. expect(cheerio.html($span[0])).toBe('<span>foo</span>');
  18. expect(cheerio.html($span)).toBe('<span>foo</span>');
  19. });
  20. it('(<value>) : should be able to set to an empty string', () => {
  21. const $elem = cheerio('<span>foo</span>').html('');
  22. expect(cheerio.html($elem)).toBe('<span></span>');
  23. });
  24. it('(<root>) : does not render the root element', () => {
  25. const $ = cheerio.load('');
  26. expect(cheerio.html($.root())).toBe(
  27. '<html><head></head><body></body></html>',
  28. );
  29. });
  30. it('(<elem>, <root>, <elem>) : does not render the root element', () => {
  31. const $ = cheerio.load('<div>a div</div><span>a span</span>');
  32. const $collection = $('div').add($.root()).add('span');
  33. const expected =
  34. '<html><head></head><body><div>a div</div><span>a span</span></body></html><div>a div</div><span>a span</span>';
  35. expect(cheerio.html($collection)).toBe(expected);
  36. });
  37. it('() : does not crash with `null` as `this` value', () => {
  38. const { html } = cheerio;
  39. expect(html.call(null as never)).toBe('');
  40. expect(html.call(null as never, '#nothing')).toBe('');
  41. });
  42. });
  43. describe('.text', () => {
  44. it('(cheerio object) : should return the text contents of the specified elements', () => {
  45. const $ = cheerio.load('<a>This is <em>content</em>.</a>');
  46. expect(cheerio.text($('a'))).toBe('This is content.');
  47. });
  48. it('(cheerio object) : should omit comment nodes', () => {
  49. const $ = cheerio.load(
  50. '<a>This is <!-- a comment --> not a comment.</a>',
  51. );
  52. expect(cheerio.text($('a'))).toBe('This is not a comment.');
  53. });
  54. it('(cheerio object) : should include text contents of children recursively', () => {
  55. const $ = cheerio.load(
  56. '<a>This is <div>a child with <span>another child and <!-- a comment --> not a comment</span> followed by <em>one last child</em> and some final</div> text.</a>',
  57. );
  58. expect(cheerio.text($('a'))).toBe(
  59. 'This is a child with another child and not a comment followed by one last child and some final text.',
  60. );
  61. });
  62. it('() : should return the rendered text content of the root', () => {
  63. const $ = cheerio.load(
  64. '<a>This is <div>a child with <span>another child and <!-- a comment --> not a comment</span> followed by <em>one last child</em> and some final</div> text.</a>',
  65. );
  66. expect(cheerio.text($.root())).toBe(
  67. 'This is a child with another child and not a comment followed by one last child and some final text.',
  68. );
  69. });
  70. it('(cheerio object) : should not omit script tags', () => {
  71. const $ = cheerio.load('<script>console.log("test")</script>');
  72. expect(cheerio.text($.root())).toBe('console.log("test")');
  73. });
  74. it('(cheerio object) : should omit style tags', () => {
  75. const $ = cheerio.load(
  76. '<style type="text/css">.cf-hidden { display: none; }</style>',
  77. );
  78. expect($.text()).toBe('.cf-hidden { display: none; }');
  79. });
  80. it('() : does not crash with `null` as `this` value', () => {
  81. const { text } = cheerio;
  82. expect(text.call(null as never)).toBe('');
  83. });
  84. });
  85. describe('.parseHTML', () => {
  86. const $ = cheerio.load('');
  87. it('() : returns null', () => {
  88. expect($.parseHTML()).toBe(null);
  89. });
  90. it('(null) : returns null', () => {
  91. expect($.parseHTML(null)).toBe(null);
  92. });
  93. it('("") : returns null', () => {
  94. expect($.parseHTML('')).toBe(null);
  95. });
  96. it('(largeHtmlString) : parses large HTML strings', () => {
  97. const html = '<div></div>'.repeat(10);
  98. const nodes = $.parseHTML(html);
  99. expect(nodes.length).toBe(10);
  100. expect(nodes).toBeInstanceOf(Array);
  101. });
  102. it('("<script>") : ignores scripts by default', () => {
  103. const html = '<script>undefined()</script>';
  104. expect($.parseHTML(html)).toHaveLength(0);
  105. });
  106. it('("<script>", true) : preserves scripts when requested', () => {
  107. const html = '<script>undefined()</script>';
  108. expect($.parseHTML(html, true)[0]).toHaveProperty('tagName', 'script');
  109. });
  110. it('("scriptAndNonScript) : preserves non-script nodes', () => {
  111. const html = '<script>undefined()</script><div></div>';
  112. expect($.parseHTML(html)[0]).toHaveProperty('tagName', 'div');
  113. });
  114. it('(scriptAndNonScript, true) : Preserves script position', () => {
  115. const html = '<script>undefined()</script><div></div>';
  116. expect($.parseHTML(html, true)[0]).toHaveProperty('tagName', 'script');
  117. });
  118. it('(text) : returns a text node', () => {
  119. expect($.parseHTML('text')[0].type).toBe('text');
  120. });
  121. it('(<tab>>text) : preserves leading whitespace', () => {
  122. expect($.parseHTML('\t<div></div>')[0]).toHaveProperty('data', '\t');
  123. });
  124. it('( text) : Leading spaces are treated as text nodes', () => {
  125. expect($.parseHTML(' <div/> ')[0].type).toBe('text');
  126. });
  127. it('(html) : should preserve content', () => {
  128. const html = '<div>test div</div>';
  129. expect(cheerio($.parseHTML(html)[0]).html()).toBe('test div');
  130. });
  131. it('(malformedHtml) : should not break', () => {
  132. expect($.parseHTML('<span><span>')).toHaveLength(1);
  133. });
  134. it('(garbageInput) : should not cause an error', () => {
  135. expect(
  136. $.parseHTML('<#if><tr><p>This is a test.</p></tr><#/if>'),
  137. ).toBeTruthy();
  138. });
  139. it('(text) : should return an array that is not effected by DOM manipulation methods', () => {
  140. const $div = cheerio.load('<div>');
  141. const elems = $div.parseHTML('<b></b><i></i>');
  142. $div('div').append(elems);
  143. expect(elems).toHaveLength(2);
  144. });
  145. it('(html, context) : should ignore context argument', () => {
  146. const $div = cheerio.load('<div>');
  147. const elems = $div.parseHTML('<script>foo</script><a>', { foo: 123 });
  148. $div('div').append(elems);
  149. expect(elems).toHaveLength(1);
  150. });
  151. it('(html, context, keepScripts) : should ignore context argument', () => {
  152. const $div = cheerio.load('<div>');
  153. const elems = $div.parseHTML(
  154. '<script>foo</script><a>',
  155. { foo: 123 },
  156. true,
  157. );
  158. $div('div').append(elems);
  159. expect(elems).toHaveLength(2);
  160. });
  161. });
  162. describe('.merge', () => {
  163. const $ = cheerio.load('');
  164. it('should be a function', () => {
  165. expect(typeof $.merge).toBe('function');
  166. });
  167. it('(arraylike, arraylike) : should modify the first array, but not the second', () => {
  168. const arr1 = [1, 2, 3];
  169. const arr2 = [4, 5, 6];
  170. const ret = $.merge(arr1, arr2);
  171. expect(typeof ret).toBe('object');
  172. expect(Array.isArray(ret)).toBe(true);
  173. expect(ret).toBe(arr1);
  174. expect(arr1).toHaveLength(6);
  175. expect(arr2).toHaveLength(3);
  176. });
  177. it('(arraylike, arraylike) : should handle objects that arent arrays, but are arraylike', () => {
  178. const arr1: ArrayLike<string> = {
  179. length: 3,
  180. 0: 'a',
  181. 1: 'b',
  182. 2: 'c',
  183. };
  184. const arr2 = {
  185. length: 3,
  186. 0: 'd',
  187. 1: 'e',
  188. 2: 'f',
  189. };
  190. $.merge(arr1, arr2);
  191. expect(arr1).toHaveLength(6);
  192. expect(arr1[3]).toBe('d');
  193. expect(arr1[4]).toBe('e');
  194. expect(arr1[5]).toBe('f');
  195. expect(arr2).toHaveLength(3);
  196. });
  197. it('(?, ?) : should gracefully reject invalid inputs', () => {
  198. expect($.merge([4], 3 as never)).toBeFalsy();
  199. expect($.merge({} as never, {} as never)).toBeFalsy();
  200. expect($.merge([], {} as never)).toBeFalsy();
  201. expect($.merge({} as never, [])).toBeFalsy();
  202. const fakeArray1 = { length: 3, 0: 'a', 1: 'b', 3: 'd' };
  203. expect($.merge(fakeArray1, [])).toBeFalsy();
  204. expect($.merge([], fakeArray1)).toBeFalsy();
  205. expect($.merge({ length: '7' } as never, [])).toBeFalsy();
  206. expect($.merge({ length: -1 }, [])).toBeFalsy();
  207. });
  208. it('(?, ?) : should no-op on invalid inputs', () => {
  209. const fakeArray1 = { length: 3, 0: 'a', 1: 'b', 3: 'd' };
  210. $.merge(fakeArray1, []);
  211. expect(fakeArray1).toHaveLength(3);
  212. expect(fakeArray1[0]).toBe('a');
  213. expect(fakeArray1[1]).toBe('b');
  214. expect(fakeArray1[3]).toBe('d');
  215. $.merge([], fakeArray1);
  216. expect(fakeArray1).toHaveLength(3);
  217. expect(fakeArray1[0]).toBe('a');
  218. expect(fakeArray1[1]).toBe('b');
  219. expect(fakeArray1[3]).toBe('d');
  220. });
  221. });
  222. describe('.contains', () => {
  223. let $: CheerioAPI;
  224. beforeEach(() => {
  225. $ = cheerio.load(food);
  226. });
  227. it('(container, contained) : should correctly detect the provided element', () => {
  228. const $food = $('#food');
  229. const $fruits = $('#fruits');
  230. const $apple = $('.apple');
  231. expect($.contains($food[0], $fruits[0])).toBe(true);
  232. expect($.contains($food[0], $apple[0])).toBe(true);
  233. });
  234. it('(container, other) : should not detect elements that are not contained', () => {
  235. const $fruits = $('#fruits');
  236. const $vegetables = $('#vegetables');
  237. const $apple = $('.apple');
  238. expect($.contains($vegetables[0], $apple[0])).toBe(false);
  239. expect($.contains($fruits[0], $vegetables[0])).toBe(false);
  240. expect($.contains($vegetables[0], $fruits[0])).toBe(false);
  241. expect($.contains($fruits[0], $fruits[0])).toBe(false);
  242. expect($.contains($vegetables[0], $vegetables[0])).toBe(false);
  243. });
  244. });
  245. describe('.root', () => {
  246. it('() : should return a cheerio-wrapped root object', () => {
  247. const $ = cheerio.load('<html><head></head><body>foo</body></html>');
  248. $.root().append('<div id="test"></div>');
  249. expect($.html()).toBe(
  250. '<html><head></head><body>foo</body></html><div id="test"></div>',
  251. );
  252. });
  253. });
  254. describe('.extract', () => {
  255. it('() : should extract values for selectors', () => {
  256. const $ = cheerio.load(eleven);
  257. expect(
  258. $.extract({
  259. red: [{ selector: '.red', value: 'outerHTML' }],
  260. }),
  261. ).toStrictEqual({
  262. red: [
  263. '<li class="red">Four</li>',
  264. '<li class="red">Five</li>',
  265. '<li class="red sel">Nine</li>',
  266. ],
  267. });
  268. });
  269. });
  270. });