Skip to content

Commit

Permalink
Merge ad83289 into 58e090a
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 authored Jun 7, 2021
2 parents 58e090a + ad83289 commit 1fea1a1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
4 changes: 1 addition & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
"@typescript-eslint/prefer-string-starts-ends-with": 2,
"@typescript-eslint/prefer-readonly": 2,
"@typescript-eslint/prefer-includes": 2,
"@typescript-eslint/no-unnecessary-condition": 0, // TODO
"@typescript-eslint/switch-exhaustiveness-check": 2,
"@typescript-eslint/prefer-nullish-coalescing": 2,

Expand All @@ -105,8 +104,7 @@
"files": "*.spec.ts",
"extends": "plugin:jest/recommended",
"rules": {
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/ban-ts-comment": 0
"@typescript-eslint/no-explicit-any": 0
}
}
]
Expand Down
10 changes: 10 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,16 @@ $.prototype.logHtml = function () {
$('body').logHtml(); // logs "Hello, <b>world</b>!" to the console
```

If you're using TypeScript, you should also add a type definition for your new method:

```ts
declare module 'cheerio' {
interface Cheerio<T> {
logHtml(this: Cheerio<T>): void;
}
}
```

### The "DOM Node" object

Cheerio collections are made up of objects that bear some resemblance to [browser-based DOM nodes](https://developer.mozilla.org/en-US/docs/Web/API/Node). You can expect them to define the following properties:
Expand Down
20 changes: 10 additions & 10 deletions src/cheerio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import { Cheerio } from './cheerio';
import type { Element } from 'domhandler';
import type { CheerioOptions } from './options';

declare module '.' {
interface Cheerio<T> {
myPlugin(...args: unknown[]): {
context: Cheerio<T>;
args: unknown[];
};
foo(): void;
}
}

// HTML
const script = '<script src="script.js" type="text/javascript"></script>';
const multiclass = '<p><a class="btn primary" href="#">Save</a></p>';
Expand Down Expand Up @@ -355,7 +365,6 @@ describe('cheerio', () => {
it('should honor extensions defined on `prototype` property', () => {
const $ = cheerio.load('<div>');

// @ts-ignore
$.prototype.myPlugin = function (...args: unknown[]) {
return {
context: this,
Expand All @@ -365,17 +374,13 @@ describe('cheerio', () => {

const $div = $('div');

// @ts-ignore
expect(typeof $div.myPlugin).toBe('function');
// @ts-ignore
expect($div.myPlugin().context).toBe($div);
// @ts-ignore
expect($div.myPlugin(1, 2, 3).args).toStrictEqual([1, 2, 3]);
});

it('should honor extensions defined on `fn` property', () => {
const $ = cheerio.load('<div>');
// @ts-ignore
$.fn.myPlugin = function (...args: unknown[]) {
return {
context: this,
Expand All @@ -385,24 +390,19 @@ describe('cheerio', () => {

const $div = $('div');

// @ts-ignore
expect(typeof $div.myPlugin).toBe('function');
// @ts-ignore
expect($div.myPlugin().context).toBe($div);
// @ts-ignore
expect($div.myPlugin(1, 2, 3).args).toStrictEqual([1, 2, 3]);
});

it('should isolate extensions between loaded functions', () => {
const $a = cheerio.load('<div>');
const $b = cheerio.load('<div>');

// @ts-ignore
$a.prototype.foo = function () {
/* Ignore */
};

// @ts-ignore
expect($b('div').foo).toBeUndefined();
});
});
Expand Down

0 comments on commit 1fea1a1

Please sign in to comment.