From d4eb8d314cda352947afe329d45e4322cea958d4 Mon Sep 17 00:00:00 2001
From: Felix Boehm <188768+fb55@users.noreply.github.com>
Date: Tue, 6 Aug 2024 01:06:55 +0100
Subject: [PATCH] Remove deprecated exports (#3974)
---
.github/dependabot.yml | 4 -
Readme.md | 4 +-
benchmark/benchmark.ts | 2 +-
src/__fixtures__/fixtures.ts | 6 ++
src/__tests__/deprecated.spec.ts | 9 +-
src/api/attributes.spec.ts | 3 +-
src/api/css.spec.ts | 4 +-
src/api/extract.spec.ts | 2 +-
src/api/forms.spec.ts | 4 +-
src/api/traversing.spec.ts | 45 +++++-----
src/base-exports.spec.ts | 30 -------
src/base-exports.ts | 148 -------------------------------
src/cheerio.spec.ts | 4 +-
src/index-browser.mts | 9 +-
src/index-browser.spec.ts | 30 -------
src/index-browser.ts | 148 -------------------------------
src/index.ts | 20 +++--
src/load-parse.ts | 41 +++++++++
src/static.spec.ts | 8 +-
19 files changed, 109 insertions(+), 412 deletions(-)
delete mode 100644 src/base-exports.spec.ts
delete mode 100644 src/base-exports.ts
delete mode 100644 src/index-browser.spec.ts
delete mode 100644 src/index-browser.ts
create mode 100644 src/load-parse.ts
diff --git a/.github/dependabot.yml b/.github/dependabot.yml
index 72084ce6..f7564c99 100644
--- a/.github/dependabot.yml
+++ b/.github/dependabot.yml
@@ -12,10 +12,6 @@ updates:
interval: daily
open-pull-requests-limit: 4
versioning-strategy: increase
- # TODO: We cannot update React to v18. See https://github.com/facebook/docusaurus/issues/7264
- ignore:
- - dependency-name: 'react'
- versions: ['18.x']
- package-ecosystem: 'github-actions'
directory: '/'
schedule:
diff --git a/Readme.md b/Readme.md
index f394ff23..fa1339ed 100644
--- a/Readme.md
+++ b/Readme.md
@@ -22,7 +22,7 @@
[中文文档 (Chinese Readme)](https://github.com/cheeriojs/cheerio/wiki/Chinese-README)
```js
-const cheerio = require('cheerio');
+import * as cheerio from 'cheerio';
const $ = cheerio.load('
Hello world
');
$('h2.title').text('Hello there!');
@@ -61,7 +61,7 @@ jQuery operates on the one, baked-in DOM. With Cheerio, we need to pass in the
HTML document.
```js
-// ES6 or TypeScript:
+// ESM or TypeScript:
import * as cheerio from 'cheerio';
// In other environments:
diff --git a/benchmark/benchmark.ts b/benchmark/benchmark.ts
index b02b830a..bb2c81e4 100755
--- a/benchmark/benchmark.ts
+++ b/benchmark/benchmark.ts
@@ -5,7 +5,7 @@ import type { Element } from 'domhandler';
import type { Cheerio } from '../src/cheerio.js';
import type { CheerioAPI } from '../src/load.js';
import { JSDOM } from 'jsdom';
-import { load } from '../src/base-exports.js';
+import { load } from '../src/load-parse.js';
const documentDir = new URL('documents/', import.meta.url);
const jQuerySrc = await fs.readFile(
diff --git a/src/__fixtures__/fixtures.ts b/src/__fixtures__/fixtures.ts
index 4487d4c5..d153fe2d 100644
--- a/src/__fixtures__/fixtures.ts
+++ b/src/__fixtures__/fixtures.ts
@@ -1,3 +1,9 @@
+import type { CheerioAPI } from '../load.js';
+import { load } from '../load-parse.js';
+
+/** A Cheerio instance with no content. */
+export const cheerio: CheerioAPI = load([]);
+
export const fruits: string = [
'',
'- Apple
',
diff --git a/src/__tests__/deprecated.spec.ts b/src/__tests__/deprecated.spec.ts
index 3468d692..493b8c7f 100644
--- a/src/__tests__/deprecated.spec.ts
+++ b/src/__tests__/deprecated.spec.ts
@@ -4,8 +4,7 @@
* maintained until that time.
*/
import { describe, it, expect, beforeEach } from 'vitest';
-import * as fixtures from '../__fixtures__/fixtures.js';
-import cheerio from '../index.js';
+import { cheerio, food, fruits } from '../__fixtures__/fixtures.js';
describe('deprecated APIs', () => {
describe('cheerio module', () => {
@@ -95,7 +94,7 @@ describe('deprecated APIs', () => {
let $: typeof cheerio;
beforeEach(() => {
- $ = cheerio.load(fixtures.food);
+ $ = cheerio.load(food);
});
it('(container, contained) : should correctly detect the provided element', () => {
@@ -131,7 +130,7 @@ describe('deprecated APIs', () => {
describe('Cheerio function', () => {
it('.load', () => {
- const $1 = cheerio.load(fixtures.fruits);
+ const $1 = cheerio.load(fruits);
const $2 = $1.load('');
expect($2('a')).toHaveLength(1);
@@ -173,7 +172,7 @@ describe('deprecated APIs', () => {
});
it('(selector) : should return the outerHTML of the selected element', () => {
- const $ = cheerio.load(fixtures.fruits);
+ const $ = cheerio.load(fruits);
expect($.html('.pear')).toBe('- Pear
');
});
});
diff --git a/src/api/attributes.spec.ts b/src/api/attributes.spec.ts
index 270c124a..9b1c64cb 100644
--- a/src/api/attributes.spec.ts
+++ b/src/api/attributes.spec.ts
@@ -1,7 +1,8 @@
import { describe, it, expect, beforeEach } from 'vitest';
-import cheerio, { load, type CheerioAPI, type Cheerio } from '../index.js';
+import { load, type CheerioAPI, type Cheerio } from '../index.js';
import type { Element } from 'domhandler';
import {
+ cheerio,
script,
fruits,
vegetables,
diff --git a/src/api/css.spec.ts b/src/api/css.spec.ts
index d9e01744..f6455c22 100644
--- a/src/api/css.spec.ts
+++ b/src/api/css.spec.ts
@@ -1,7 +1,7 @@
import { describe, it, expect, beforeEach } from 'vitest';
-import cheerio, { load, type Cheerio } from '../index.js';
+import { load, type Cheerio } from '../index.js';
import type { Element } from 'domhandler';
-import { mixedText } from '../__fixtures__/fixtures.js';
+import { cheerio, mixedText } from '../__fixtures__/fixtures.js';
describe('$(...)', () => {
describe('.css', () => {
diff --git a/src/api/extract.spec.ts b/src/api/extract.spec.ts
index 939e0752..b2432127 100644
--- a/src/api/extract.spec.ts
+++ b/src/api/extract.spec.ts
@@ -1,6 +1,6 @@
import { describe, it, expect } from 'vitest';
import * as fixtures from '../__fixtures__/fixtures.js';
-import { load } from '../base-exports.js';
+import { load } from '../load-parse.js';
interface RedSelObject {
red: string | undefined;
diff --git a/src/api/forms.spec.ts b/src/api/forms.spec.ts
index 3b134502..ddd3d758 100644
--- a/src/api/forms.spec.ts
+++ b/src/api/forms.spec.ts
@@ -1,6 +1,6 @@
import { describe, it, expect, beforeEach } from 'vitest';
-import cheerio, { type CheerioAPI } from '../index.js';
-import { forms } from '../__fixtures__/fixtures.js';
+import { type CheerioAPI } from '../index.js';
+import { cheerio, forms } from '../__fixtures__/fixtures.js';
describe('$(...)', () => {
let $: CheerioAPI;
diff --git a/src/api/traversing.spec.ts b/src/api/traversing.spec.ts
index 29764f96..3494b6f9 100644
--- a/src/api/traversing.spec.ts
+++ b/src/api/traversing.spec.ts
@@ -1,8 +1,9 @@
import { describe, it, expect, beforeEach } from 'vitest';
-import cheerio, { type CheerioAPI } from '../index.js';
+import { load, type CheerioAPI } from '../index.js';
import { Cheerio } from '../cheerio.js';
import { type AnyNode, type Element, type Text, isText } from 'domhandler';
import {
+ cheerio,
food,
fruits,
eleven,
@@ -23,13 +24,13 @@ describe('$(...)', () => {
let $: CheerioAPI;
beforeEach(() => {
- $ = cheerio.load(fruits);
+ $ = load(fruits);
});
describe('.load', () => {
it('should throw a TypeError if given invalid input', () => {
expect(() => {
- (cheerio.load as any)();
+ (load as any)();
}).toThrow('cheerio.load() expects a string');
});
});
@@ -77,12 +78,12 @@ describe('$(...)', () => {
});
it('should query immediate descendant only', () => {
- const q = cheerio.load('');
+ const q = load('');
expect(q('foo').find('> bar')).toHaveLength(1);
});
it('should find siblings', () => {
- const q = cheerio.load('');
+ const q = load('');
expect(q('.a').find('+.b')).toHaveLength(1);
expect(q('.a').find('~.b')).toHaveLength(1);
expect(q('.a').find('+.a')).toHaveLength(0);
@@ -90,7 +91,7 @@ describe('$(...)', () => {
});
it('should query case-sensitively when in xml mode', () => {
- const q = cheerio.load('', { xml: true });
+ const q = load('', { xml: true });
expect(q('caseSenSitive')).toHaveLength(1);
expect(q('[allTheWay]')).toHaveLength(1);
expect(q('casesensitive')).toHaveLength(0);
@@ -105,7 +106,7 @@ describe('$(...)', () => {
describe('(cheerio object) :', () => {
it('returns only those nodes contained within the current selection', () => {
- const q = cheerio.load(food);
+ const q = load(food);
const $selection = q('#fruits').find(q('li'));
expect($selection).toHaveLength(3);
@@ -114,7 +115,7 @@ describe('$(...)', () => {
expect($selection[2]).toBe(q('.pear')[0]);
});
it('returns only those nodes contained within any element in the current selection', () => {
- const q = cheerio.load(food);
+ const q = load(food);
const $selection = q('.apple, #vegetables').find(q('li'));
expect($selection).toHaveLength(2);
@@ -125,21 +126,21 @@ describe('$(...)', () => {
describe('(node) :', () => {
it('returns node when contained within the current selection', () => {
- const q = cheerio.load(food);
+ const q = load(food);
const $selection = q('#fruits').find(q('.apple')[0]);
expect($selection).toHaveLength(1);
expect($selection[0]).toBe(q('.apple')[0]);
});
it('returns node when contained within any element the current selection', () => {
- const q = cheerio.load(food);
+ const q = load(food);
const $selection = q('#fruits, #vegetables').find(q('.carrot')[0]);
expect($selection).toHaveLength(1);
expect($selection[0]).toBe(q('.carrot')[0]);
});
it('does not return node that is not contained within the current selection', () => {
- const q = cheerio.load(food);
+ const q = load(food);
const $selection = q('#fruits').find(q('.carrot')[0]);
expect($selection).toHaveLength(0);
@@ -176,7 +177,7 @@ describe('$(...)', () => {
describe('.contents', () => {
beforeEach(() => {
- $ = cheerio.load(text);
+ $ = load(text);
});
it('() : should get all contents', () => {
@@ -219,7 +220,7 @@ describe('$(...)', () => {
});
it('() : should return elements in order', () => {
- const result = cheerio.load(eleven)('.red').next();
+ const result = load(eleven)('.red').next();
expect(result).toHaveLength(2);
expect(result.eq(0).text()).toBe('Six');
expect(result.eq(1).text()).toBe('Ten');
@@ -384,7 +385,7 @@ describe('$(...)', () => {
});
it('() : should maintain elements order', () => {
- const sel = cheerio.load(eleven)('.sel');
+ const sel = load(eleven)('.sel');
expect(sel).toHaveLength(3);
expect(sel.eq(0).text()).toBe('Three');
expect(sel.eq(1).text()).toBe('Nine');
@@ -566,7 +567,7 @@ describe('$(...)', () => {
});
it('() : when two elements are siblings to each other they have to be included', () => {
- const result = cheerio.load(eleven)('.sel').siblings();
+ const result = load(eleven)('.sel').siblings();
expect(result).toHaveLength(7);
expect(result.eq(0).text()).toBe('One');
expect(result.eq(1).text()).toBe('Two');
@@ -578,14 +579,14 @@ describe('$(...)', () => {
});
it('(selector) : when two elements are siblings to each other they have to be included', () => {
- const result = cheerio.load(eleven)('.sel').siblings('.red');
+ const result = load(eleven)('.sel').siblings('.red');
expect(result).toHaveLength(2);
expect(result.eq(0).text()).toBe('Four');
expect(result.eq(1).text()).toBe('Nine');
});
it('(cheerio) : test filtering with cheerio object', () => {
- const doc = cheerio.load(eleven);
+ const doc = load(eleven);
const result = doc('.sel').siblings(doc(':not([class])'));
expect(result).toHaveLength(4);
expect(result.eq(0).text()).toBe('One');
@@ -597,7 +598,7 @@ describe('$(...)', () => {
describe('.parents', () => {
beforeEach(() => {
- $ = cheerio.load(food);
+ $ = load(food);
});
it('() : should get all of the parents in logical order', () => {
@@ -648,7 +649,7 @@ describe('$(...)', () => {
describe('.parentsUntil', () => {
beforeEach(() => {
- $ = cheerio.load(food);
+ $ = load(food);
});
it('() : should get all of the parents in logical order', () => {
@@ -1012,7 +1013,7 @@ describe('$(...)', () => {
describe('.has', () => {
beforeEach(() => {
- $ = cheerio.load(food);
+ $ = load(food);
});
it('(selector) : should reduce the set of matched elements to those with descendants that match the selector', () => {
@@ -1278,7 +1279,7 @@ describe('$(...)', () => {
let $pear: Cheerio;
beforeEach(() => {
- $ = cheerio.load(food);
+ $ = load(food);
$fruits = $('#fruits');
$apple = $('.apple');
$orange = $('.orange');
@@ -1579,7 +1580,7 @@ describe('$(...)', () => {
expect($selection[1]).toBe($('.apple')[0]);
});
it('includes parents and self', () => {
- const q = cheerio.load(food);
+ const q = load(food);
const $selection = q('.apple').parents().addBack();
expect($selection).toHaveLength(5);
diff --git a/src/base-exports.spec.ts b/src/base-exports.spec.ts
deleted file mode 100644
index c86ab250..00000000
--- a/src/base-exports.spec.ts
+++ /dev/null
@@ -1,30 +0,0 @@
-import { describe, it, expect } from 'vitest';
-import * as cheerio from './base-exports.js';
-import * as statics from './static.js';
-
-describe('static method re-exports', () => {
- it('should export expected static methods', () => {
- for (const key of Object.keys(statics) as (keyof typeof statics)[]) {
- if (key === 'extract') continue;
- expect(typeof cheerio[key]).toBe(typeof statics[key]);
- }
- });
-
- it('should have a functional `html` that is bound to the default instance', () => {
- expect(cheerio.html(cheerio.default('test div
'))).toBe(
- 'test div
',
- );
- });
-
- it('should have a functional `xml` that is bound to the default instance', () => {
- expect(cheerio.xml(cheerio.default('test div
'))).toBe(
- 'test div
',
- );
- });
-
- it('should have a functional `text` that is bound to the default instance', () => {
- expect(cheerio.text(cheerio.default('test div
'))).toBe(
- 'test div',
- );
- });
-});
diff --git a/src/base-exports.ts b/src/base-exports.ts
deleted file mode 100644
index 9c05c20a..00000000
--- a/src/base-exports.ts
+++ /dev/null
@@ -1,148 +0,0 @@
-import { type CheerioAPI, getLoad } from './load.js';
-import { getParse } from './parse.js';
-import { renderWithParse5, parseWithParse5 } from './parsers/parse5-adapter.js';
-import * as staticMethods from './static.js';
-import type { BasicAcceptedElems } from './types.js';
-import type { CheerioOptions } from './options.js';
-import renderWithHtmlparser2 from 'dom-serializer';
-import { parseDocument as parseWithHtmlparser2 } from 'htmlparser2';
-import type { AnyNode } from 'domhandler';
-
-/**
- * The main types of Cheerio objects.
- *
- * @category Cheerio
- */
-export type { Cheerio } from './cheerio.js';
-
-/**
- * Types used in signatures of Cheerio methods.
- *
- * @category Cheerio
- */
-export * from './types.js';
-export type { CheerioOptions, HTMLParser2Options } from './options.js';
-export type { CheerioAPI } from './load.js';
-export { contains, merge } from './static.js';
-
-const parse = getParse((content, options, isDocument, context) =>
- options._useHtmlParser2
- ? parseWithHtmlparser2(content, options)
- : parseWithParse5(content, options, isDocument, context),
-);
-
-// Duplicate docs due to https://github.com/TypeStrong/typedoc/issues/1616
-/**
- * Create a querying function, bound to a document created from the provided
- * markup.
- *
- * Note that similar to web browser contexts, this operation may introduce
- * ``, ``, and `` elements; set `isDocument` to `false` to
- * switch to fragment mode and disable this.
- *
- * @category Loading
- * @param content - Markup to be loaded.
- * @param options - Options for the created instance.
- * @param isDocument - Allows parser to be switched to fragment mode.
- * @returns The loaded document.
- * @see {@link https://cheerio.js.org#loading} for additional usage information.
- */
-export const load: (
- content: string | AnyNode | AnyNode[] | Buffer,
- options?: CheerioOptions | null,
- isDocument?: boolean,
-) => CheerioAPI = getLoad(parse, (dom, options) =>
- options._useHtmlParser2
- ? renderWithHtmlparser2(dom, options)
- : renderWithParse5(dom),
-);
-
-const defaultInstance: CheerioAPI = load([]);
-
-/**
- * The default cheerio instance.
- *
- * @deprecated Use the function returned by `load` instead. To access load, make
- * sure you are importing `* as cheerio` instead of this default export.
- * @category Deprecated
- */
-export default defaultInstance;
-
-/**
- * Renders the document.
- *
- * @deprecated Use `html` on the loaded instance instead.
- * @category Deprecated
- * @param dom - Element to render.
- * @param options - Options for the renderer.
- * @returns The rendered document.
- */
-export const html: (
- dom: BasicAcceptedElems,
- options?: CheerioOptions,
-) => string = staticMethods.html.bind(defaultInstance);
-
-/**
- * Render the document as XML.
- *
- * @deprecated Use `xml` on the loaded instance instead.
- * @category Deprecated
- * @param dom - Element to render.
- * @returns The rendered document.
- */
-export const xml: (dom: BasicAcceptedElems) => string =
- staticMethods.xml.bind(defaultInstance);
-
-/**
- * Render the document as text.
- *
- * This returns the `textContent` of the passed elements. The result will
- * include the contents of `