mirror of
https://github.com/cheeriojs/cheerio.git
synced 2026-02-21 20:31:35 +00:00
Remove deprecated exports (#3974)
This commit is contained in:
4
.github/dependabot.yml
vendored
4
.github/dependabot.yml
vendored
@@ -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:
|
||||
|
||||
@@ -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('<h2 class="title">Hello world</h2>');
|
||||
|
||||
$('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:
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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 = [
|
||||
'<ul id="fruits">',
|
||||
'<li class="apple">Apple</li>',
|
||||
|
||||
@@ -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('<div><p>Some <a>text</a>.</p></div>');
|
||||
|
||||
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('<li class="pear">Pear</li>');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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('<foo><bar><bar></bar><bar></bar></bar></foo>');
|
||||
const q = load('<foo><bar><bar></bar><bar></bar></bar></foo>');
|
||||
expect(q('foo').find('> bar')).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should find siblings', () => {
|
||||
const q = cheerio.load('<p class=a><p class=b></p>');
|
||||
const q = load('<p class=a><p class=b></p>');
|
||||
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('<caseSenSitive allTheWay>', { xml: true });
|
||||
const q = load('<caseSenSitive allTheWay>', { 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<Element>;
|
||||
|
||||
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);
|
||||
|
||||
@@ -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('<div>test div</div>'))).toBe(
|
||||
'<div>test div</div>',
|
||||
);
|
||||
});
|
||||
|
||||
it('should have a functional `xml` that is bound to the default instance', () => {
|
||||
expect(cheerio.xml(cheerio.default('<div>test div</div>'))).toBe(
|
||||
'<div>test div</div>',
|
||||
);
|
||||
});
|
||||
|
||||
it('should have a functional `text` that is bound to the default instance', () => {
|
||||
expect(cheerio.text(cheerio.default('<div>test div</div>'))).toBe(
|
||||
'test div',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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
|
||||
* `<html>`, `<head>`, and `<body>` 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<AnyNode>,
|
||||
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<AnyNode>) => 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 `<script>` and `<style>` elements. To avoid this, use
|
||||
* `.prop('innerText')` instead.
|
||||
*
|
||||
* @deprecated Use `text` on the loaded instance instead.
|
||||
* @category Deprecated
|
||||
* @param elements - Elements to render.
|
||||
* @returns The rendered document.
|
||||
*/
|
||||
export const text: (elements: ArrayLike<AnyNode>) => string =
|
||||
staticMethods.text.bind(defaultInstance);
|
||||
|
||||
/**
|
||||
* The `.parseHTML` method exported by the Cheerio module is deprecated.
|
||||
*
|
||||
* In order to promote consistency with the jQuery library, users are encouraged
|
||||
* to instead use the static method of the same name as it is defined on the
|
||||
* "loaded" Cheerio factory function.
|
||||
*
|
||||
* @deprecated Use `parseHTML` on the loaded instance instead.
|
||||
* @category Deprecated
|
||||
* @example
|
||||
*
|
||||
* ```js
|
||||
* const $ = cheerio.load('');
|
||||
* $.parseHTML('<b>markup</b>');
|
||||
* ```
|
||||
*/
|
||||
export const parseHTML = staticMethods.parseHTML.bind(
|
||||
defaultInstance,
|
||||
) as typeof staticMethods.parseHTML;
|
||||
|
||||
/**
|
||||
* The `.root` method exported by the Cheerio module is deprecated.
|
||||
*
|
||||
* Users seeking to access the top-level element of a parsed document should
|
||||
* instead use the `root` static method of a "loaded" Cheerio function.
|
||||
*
|
||||
* @deprecated Use `root` on the loaded instance instead.
|
||||
* @category Deprecated
|
||||
* @example
|
||||
*
|
||||
* ```js
|
||||
* const $ = cheerio.load('');
|
||||
* $.root();
|
||||
* ```
|
||||
*/
|
||||
export const root = staticMethods.root.bind(
|
||||
defaultInstance,
|
||||
) as typeof staticMethods.root;
|
||||
@@ -1,7 +1,7 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { parseDOM } from 'htmlparser2';
|
||||
import cheerio, { type Cheerio } from './index.js';
|
||||
import { fruits, food, noscript } from './__fixtures__/fixtures.js';
|
||||
import { type Cheerio } from './index.js';
|
||||
import { cheerio, fruits, food, noscript } from './__fixtures__/fixtures.js';
|
||||
import type { Element } from 'domhandler';
|
||||
|
||||
declare module './index.js' {
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
export * from './base-exports.js';
|
||||
// TODO: Remove this
|
||||
export { default } from './base-exports.js';
|
||||
export type { Cheerio } from './cheerio.js';
|
||||
export type * from './types.js';
|
||||
export type { CheerioOptions, HTMLParser2Options } from './options.js';
|
||||
export type { CheerioAPI } from './load.js';
|
||||
|
||||
export * from './load-parse.js';
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import * as cheerio from './index-browser.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('<div>test div</div>'))).toBe(
|
||||
'<div>test div</div>',
|
||||
);
|
||||
});
|
||||
|
||||
it('should have a functional `xml` that is bound to the default instance', () => {
|
||||
expect(cheerio.xml(cheerio.default('<div>test div</div>'))).toBe(
|
||||
'<div>test div</div>',
|
||||
);
|
||||
});
|
||||
|
||||
it('should have a functional `text` that is bound to the default instance', () => {
|
||||
expect(cheerio.text(cheerio.default('<div>test div</div>'))).toBe(
|
||||
'test div',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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
|
||||
* `<html>`, `<head>`, and `<body>` 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<AnyNode>,
|
||||
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<AnyNode>) => 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 `<script>` and `<style>` elements. To avoid this, use
|
||||
* `.prop('innerText')` instead.
|
||||
*
|
||||
* @deprecated Use `text` on the loaded instance instead.
|
||||
* @category Deprecated
|
||||
* @param elements - Elements to render.
|
||||
* @returns The rendered document.
|
||||
*/
|
||||
export const text: (elements: ArrayLike<AnyNode>) => string =
|
||||
staticMethods.text.bind(defaultInstance);
|
||||
|
||||
/**
|
||||
* The `.parseHTML` method exported by the Cheerio module is deprecated.
|
||||
*
|
||||
* In order to promote consistency with the jQuery library, users are encouraged
|
||||
* to instead use the static method of the same name as it is defined on the
|
||||
* "loaded" Cheerio factory function.
|
||||
*
|
||||
* @deprecated Use `parseHTML` on the loaded instance instead.
|
||||
* @category Deprecated
|
||||
* @example
|
||||
*
|
||||
* ```js
|
||||
* const $ = cheerio.load('');
|
||||
* $.parseHTML('<b>markup</b>');
|
||||
* ```
|
||||
*/
|
||||
export const parseHTML = staticMethods.parseHTML.bind(
|
||||
defaultInstance,
|
||||
) as typeof staticMethods.parseHTML;
|
||||
|
||||
/**
|
||||
* The `.root` method exported by the Cheerio module is deprecated.
|
||||
*
|
||||
* Users seeking to access the top-level element of a parsed document should
|
||||
* instead use the `root` static method of a "loaded" Cheerio function.
|
||||
*
|
||||
* @deprecated Use `root` on the loaded instance instead.
|
||||
* @category Deprecated
|
||||
* @example
|
||||
*
|
||||
* ```js
|
||||
* const $ = cheerio.load('');
|
||||
* $.root();
|
||||
* ```
|
||||
*/
|
||||
export const root = staticMethods.root.bind(
|
||||
defaultInstance,
|
||||
) as typeof staticMethods.root;
|
||||
20
src/index.ts
20
src/index.ts
@@ -3,17 +3,16 @@
|
||||
* convenience methods for loading documents from various sources.
|
||||
*/
|
||||
|
||||
export * from './base-exports.js';
|
||||
// TODO: Remove this
|
||||
export { default } from './base-exports.js';
|
||||
export * from './load-parse.js';
|
||||
|
||||
export type { Cheerio } from './cheerio.js';
|
||||
export type * from './types.js';
|
||||
export type { CheerioOptions, HTMLParser2Options } from './options.js';
|
||||
export type { CheerioAPI } from './load.js';
|
||||
|
||||
/* eslint-disable n/no-unsupported-features/node-builtins */
|
||||
|
||||
import type { CheerioAPI, CheerioOptions } from './base-exports.js';
|
||||
import { load } from './base-exports.js';
|
||||
import { flattenOptions, type InternalOptions } from './options.js';
|
||||
import { adapter as htmlparser2Adapter } from 'parse5-htmlparser2-tree-adapter';
|
||||
|
||||
import * as htmlparser2 from 'htmlparser2';
|
||||
import { ParserStream as Parse5Stream } from 'parse5-parser-stream';
|
||||
import {
|
||||
@@ -24,6 +23,13 @@ import {
|
||||
import * as undici from 'undici';
|
||||
import MIMEType from 'whatwg-mimetype';
|
||||
import { Writable, finished } from 'node:stream';
|
||||
import type { CheerioAPI } from './load.js';
|
||||
import {
|
||||
flattenOptions,
|
||||
type InternalOptions,
|
||||
type CheerioOptions,
|
||||
} from './options.js';
|
||||
import { load } from './load-parse.js';
|
||||
|
||||
/**
|
||||
* Sniffs the encoding of a buffer, then creates a querying function bound to a
|
||||
|
||||
41
src/load-parse.ts
Normal file
41
src/load-parse.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { type CheerioAPI, getLoad } from './load.js';
|
||||
import { getParse } from './parse.js';
|
||||
import { renderWithParse5, parseWithParse5 } from './parsers/parse5-adapter.js';
|
||||
import type { CheerioOptions } from './options.js';
|
||||
import renderWithHtmlparser2 from 'dom-serializer';
|
||||
import { parseDocument as parseWithHtmlparser2 } from 'htmlparser2';
|
||||
import type { AnyNode } from 'domhandler';
|
||||
|
||||
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
|
||||
* `<html>`, `<head>`, and `<body>` 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),
|
||||
);
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import * as fixtures from './__fixtures__/fixtures.js';
|
||||
import cheerio, { type CheerioAPI } from './index.js';
|
||||
import { cheerio, food, eleven } from './__fixtures__/fixtures.js';
|
||||
import { type CheerioAPI } from './index.js';
|
||||
|
||||
describe('cheerio', () => {
|
||||
describe('.html', () => {
|
||||
@@ -270,7 +270,7 @@ describe('cheerio', () => {
|
||||
let $: CheerioAPI;
|
||||
|
||||
beforeEach(() => {
|
||||
$ = cheerio.load(fixtures.food);
|
||||
$ = cheerio.load(food);
|
||||
});
|
||||
|
||||
it('(container, contained) : should correctly detect the provided element', () => {
|
||||
@@ -307,7 +307,7 @@ describe('cheerio', () => {
|
||||
|
||||
describe('.extract', () => {
|
||||
it('() : should extract values for selectors', () => {
|
||||
const $ = cheerio.load(fixtures.eleven);
|
||||
const $ = cheerio.load(eleven);
|
||||
|
||||
expect(
|
||||
$.extract({
|
||||
|
||||
Reference in New Issue
Block a user