-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
213 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ npm-debug.log | |
.cache-loader | ||
/coverage | ||
/.tshy | ||
/.tshy-build | ||
/dist | ||
/website/docs/api | ||
/website/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './base-exports.js'; | ||
// TODO: Remove this | ||
export { default } from './base-exports.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"exclude": ["*.config.ts", "*.spec.ts", "scripts/*", "website/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters