mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-11 12:30:18 +00:00
cheerio: add Cheerio.cheerio: string; (#40141)
* Add `cheerio: string;` * replace `var` with `const` * add test * format with prettier
This commit is contained in:
committed by
Pranav Senthilnathan
parent
9a4f5e0d20
commit
d4bb0c69ee
@@ -3,8 +3,7 @@ import cheerio = require('cheerio');
|
||||
/*
|
||||
* LOADING
|
||||
*/
|
||||
let html =
|
||||
`<ul id="fruits">
|
||||
let html = `<ul id="fruits">
|
||||
<li class="orange">Apple</li>
|
||||
<li class="class">Orange</li>
|
||||
<li class="pear">Pear</li>
|
||||
@@ -13,22 +12,23 @@ let html =
|
||||
|
||||
// Preferred Method
|
||||
var $ = cheerio.load(html);
|
||||
|
||||
// Directly load element
|
||||
cheerio(html);
|
||||
cheerio('ul', html);
|
||||
cheerio('li', 'ul', html);
|
||||
|
||||
const $fromElement = cheerio.load($("ul").get(0));
|
||||
const $fromElement = cheerio.load($('ul').get(0));
|
||||
|
||||
if ($fromElement("ul > li").length !== 3) {
|
||||
throw new Error("Expecting 3 elements when passing `CheerioElement` to `load()`");
|
||||
if ($fromElement('ul > li').length !== 3) {
|
||||
throw new Error('Expecting 3 elements when passing `CheerioElement` to `load()`');
|
||||
}
|
||||
|
||||
$ = cheerio.load(Buffer.from(html));
|
||||
|
||||
$ = cheerio.load(html, {
|
||||
normalizeWhitespace: true,
|
||||
xmlMode: true
|
||||
xmlMode: true,
|
||||
});
|
||||
|
||||
$ = cheerio.load(html, {
|
||||
@@ -38,7 +38,7 @@ $ = cheerio.load(html, {
|
||||
lowerCaseTags: true,
|
||||
lowerCaseAttributeNames: true,
|
||||
recognizeCDATA: true,
|
||||
recognizeSelfClosing: true
|
||||
recognizeSelfClosing: true,
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -47,6 +47,8 @@ $ = cheerio.load(html, {
|
||||
var $el = $('.class');
|
||||
var $multiEl = $('selector', 'selector', 'selector');
|
||||
|
||||
$el.cheerio;
|
||||
|
||||
/**
|
||||
* Attributes
|
||||
*/
|
||||
@@ -60,8 +62,8 @@ $el.attr('id', el => el.tagName).html();
|
||||
$el.attr({ id: 'uniq', class: 'big' }).html();
|
||||
|
||||
// props
|
||||
$el.prop('style')
|
||||
$el.prop('style', 'none').html()
|
||||
$el.prop('style');
|
||||
$el.prop('style', 'none').html();
|
||||
|
||||
// data
|
||||
$el.data();
|
||||
@@ -70,7 +72,9 @@ $el.data('kind', 'mac');
|
||||
|
||||
// val
|
||||
$('input[type="text"]').val();
|
||||
$('input[type="text"]').val('test').html();
|
||||
$('input[type="text"]')
|
||||
.val('test')
|
||||
.html();
|
||||
|
||||
// removeAttr
|
||||
$el.removeAttr('class').html();
|
||||
@@ -79,14 +83,16 @@ $el.removeAttr('class').html();
|
||||
$el.addClass('class').addClass('test');
|
||||
$el.hasClass('test');
|
||||
$el.removeClass('class').removeClass('test');
|
||||
$el.addClass('red').removeClass().html();
|
||||
$el.addClass('red')
|
||||
.removeClass()
|
||||
.html();
|
||||
$el.toggleClass('fruit green red').html();
|
||||
|
||||
// is
|
||||
$el.is('#id');
|
||||
$el.is($el);
|
||||
$el.is(() => {
|
||||
return true;
|
||||
return true;
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -99,7 +105,7 @@ $('<form><input name="foo" value="bar" /></form>').serialize();
|
||||
/**
|
||||
* Traversing
|
||||
*/
|
||||
// find
|
||||
// find
|
||||
$el.find('li').length;
|
||||
$el.find($('.apple')).length;
|
||||
|
||||
@@ -144,7 +150,9 @@ $el.prevUntil();
|
||||
$el.prevUntil('.class');
|
||||
|
||||
// .slice( start, [end] )
|
||||
$el.slice(1).eq(0).text();
|
||||
$el.slice(1)
|
||||
.eq(0)
|
||||
.text();
|
||||
$el.slice(1, 2).length;
|
||||
|
||||
// .siblings([selector])
|
||||
@@ -160,13 +168,15 @@ $el.contents().length;
|
||||
|
||||
// .each( function(index, element) )
|
||||
$el.each((i, el) => {
|
||||
$(el).html();
|
||||
$(el).html();
|
||||
});
|
||||
|
||||
// .map( function(index, element) )
|
||||
$el.map((i, el) => {
|
||||
return $(el).text();
|
||||
}).get().join(' ');
|
||||
return $(el).text();
|
||||
})
|
||||
.get()
|
||||
.join(' ');
|
||||
|
||||
// .filter
|
||||
$ = cheerio.load(html);
|
||||
@@ -175,7 +185,7 @@ $el.filter($('.class')).attr('class');
|
||||
$el.filter($('.class')[0]).attr('class');
|
||||
|
||||
$el.filter((i, el) => {
|
||||
return $(el).attr('class') === 'class';
|
||||
return $(el).attr('class') === 'class';
|
||||
}).attr('class');
|
||||
|
||||
// .not
|
||||
@@ -184,7 +194,7 @@ $el.not($('.class')).length;
|
||||
$el.not($('.class')[0]).length;
|
||||
|
||||
$el.not((i, el) => {
|
||||
return $(el).attr('class') === 'class';
|
||||
return $(el).attr('class') === 'class';
|
||||
}).length;
|
||||
|
||||
// .has
|
||||
@@ -192,10 +202,14 @@ $el.has('.class').attr('id');
|
||||
$el.has($el[0]).attr('id');
|
||||
|
||||
// .first()
|
||||
$el.children().first().text();
|
||||
$el.children()
|
||||
.first()
|
||||
.text();
|
||||
|
||||
// .last()
|
||||
$el.children().last().text();
|
||||
$el.children()
|
||||
.last()
|
||||
.text();
|
||||
|
||||
// .eq( i )
|
||||
$el.eq(0).text();
|
||||
@@ -216,18 +230,18 @@ $el.index($('#fruit, li'));
|
||||
$el.eq(0).end().length;
|
||||
|
||||
// .add
|
||||
$el.add('.class').length
|
||||
$el.add('.class').length;
|
||||
|
||||
// .addBack( [filter] )
|
||||
$el.eq(0).addBack().length
|
||||
$el.eq(0).addBack('.class').length
|
||||
$el.eq(0).addBack().length;
|
||||
$el.eq(0).addBack('.class').length;
|
||||
|
||||
/**
|
||||
* Manipulation
|
||||
*/
|
||||
|
||||
$('<li class="plum">Plum</li>').appendTo($el)
|
||||
$el.prependTo($('<li class="plum">Plum</li>'))
|
||||
$('<li class="plum">Plum</li>').appendTo($el);
|
||||
$el.prependTo($('<li class="plum">Plum</li>'));
|
||||
|
||||
// .append( content, [content, ...] )
|
||||
$el.append('<li class="plum">Plum</li>').html();
|
||||
@@ -242,14 +256,18 @@ $el.after('<li class="plum">Plum</li>').html();
|
||||
$el.after('<li class="plum">Plum</li>', '<li class="plum">Plum</li>').html();
|
||||
|
||||
// .insertAfter( content )
|
||||
$('<li class="plum">Plum</li>').insertAfter('.class').html();
|
||||
$('<li class="plum">Plum</li>')
|
||||
.insertAfter('.class')
|
||||
.html();
|
||||
|
||||
// .before( content, [content, ...] )
|
||||
$el.before('<li class="plum">Plum</li>').html();
|
||||
$el.before('<li class="plum">Plum</li>', '<li class="plum">Plum</li>').html();
|
||||
|
||||
// .insertBefore( content )
|
||||
$('<li class="plum">Plum</li>').insertBefore('.class').html();
|
||||
$('<li class="plum">Plum</li>')
|
||||
.insertBefore('.class')
|
||||
.html();
|
||||
|
||||
// .remove( [selector] )
|
||||
$el.remove().html();
|
||||
@@ -296,7 +314,9 @@ $el.clone().html();
|
||||
*/
|
||||
|
||||
// $.root
|
||||
$.root().append('<ul id="vegetables"></ul>').html();
|
||||
$.root()
|
||||
.append('<ul id="vegetables"></ul>')
|
||||
.html();
|
||||
|
||||
// $.contains( container, contained )
|
||||
$.contains($el[0], $el[0]);
|
||||
|
||||
Vendored
+19
-15
@@ -21,11 +21,12 @@ interface Cheerio {
|
||||
// JQuery http://api.jquery.com
|
||||
|
||||
[index: number]: CheerioElement;
|
||||
cheerio: string;
|
||||
length: number;
|
||||
|
||||
// Attributes
|
||||
|
||||
attr(): {[attr: string]: string};
|
||||
attr(): { [attr: string]: string };
|
||||
attr(name: string): string;
|
||||
attr(name: string, value: AttrFunction): Cheerio;
|
||||
// `value` *can* be `any` here but:
|
||||
@@ -34,7 +35,7 @@ interface Cheerio {
|
||||
attr(name: string, value: string): Cheerio;
|
||||
// The map's values *can* be `any` but they'll all be cast to strings
|
||||
// regardless.
|
||||
attr(map: {[key: string]: any}): Cheerio;
|
||||
attr(map: { [key: string]: any }): Cheerio;
|
||||
|
||||
data(): any;
|
||||
data(name: string): any;
|
||||
@@ -58,7 +59,10 @@ interface Cheerio {
|
||||
toggleClass(className: string): Cheerio;
|
||||
toggleClass(className: string, toggleSwitch: boolean): Cheerio;
|
||||
toggleClass(toggleSwitch?: boolean): Cheerio;
|
||||
toggleClass(func: (index: number, className: string, toggleSwitch: boolean) => string, toggleSwitch?: boolean): Cheerio;
|
||||
toggleClass(
|
||||
func: (index: number, className: string, toggleSwitch: boolean) => string,
|
||||
toggleSwitch?: boolean,
|
||||
): Cheerio;
|
||||
|
||||
is(selector: string): boolean;
|
||||
is(element: CheerioElement): boolean;
|
||||
@@ -68,10 +72,10 @@ interface Cheerio {
|
||||
|
||||
// Form
|
||||
serialize(): string;
|
||||
serializeArray(): {name: string, value: string}[];
|
||||
serializeArray(): { name: string; value: string }[];
|
||||
|
||||
// Traversing
|
||||
|
||||
|
||||
find(selector: string): Cheerio;
|
||||
find(element: Cheerio): Cheerio;
|
||||
|
||||
@@ -145,12 +149,12 @@ interface Cheerio {
|
||||
add(elements: CheerioElement[]): Cheerio;
|
||||
add(selection: Cheerio): Cheerio;
|
||||
|
||||
addBack():Cheerio;
|
||||
addBack(filter: string):Cheerio;
|
||||
addBack(): Cheerio;
|
||||
addBack(filter: string): Cheerio;
|
||||
|
||||
// Manipulation
|
||||
appendTo(target: Cheerio) : Cheerio
|
||||
prependTo(target: Cheerio) : Cheerio
|
||||
appendTo(target: Cheerio): Cheerio;
|
||||
prependTo(target: Cheerio): Cheerio;
|
||||
|
||||
append(content: string, ...contents: any[]): Cheerio;
|
||||
append(content: Document, ...contents: any[]): Cheerio;
|
||||
@@ -269,7 +273,7 @@ interface CheerioElement {
|
||||
tagName: string;
|
||||
type: string;
|
||||
name: string;
|
||||
attribs: {[attr: string]: string};
|
||||
attribs: { [attr: string]: string };
|
||||
children: CheerioElement[];
|
||||
childNodes: CheerioElement[];
|
||||
lastChild: CheerioElement;
|
||||
@@ -286,14 +290,14 @@ interface CheerioElement {
|
||||
}
|
||||
|
||||
interface CheerioAPI extends CheerioSelector, CheerioStatic {
|
||||
load(html: string | Buffer, options?: CheerioOptionsInterface): CheerioStatic;
|
||||
load(element: CheerioElement, options?: CheerioOptionsInterface): CheerioStatic;
|
||||
load(html: string | Buffer, options?: CheerioOptionsInterface): CheerioStatic;
|
||||
load(element: CheerioElement, options?: CheerioOptionsInterface): CheerioStatic;
|
||||
}
|
||||
|
||||
interface Document { }
|
||||
interface Document {}
|
||||
|
||||
declare var cheerio:CheerioAPI;
|
||||
declare const cheerio: CheerioAPI;
|
||||
|
||||
declare module "cheerio" {
|
||||
declare module 'cheerio' {
|
||||
export = cheerio;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user