123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- /**
- * Methods for getting and modifying attributes.
- *
- * @module cheerio/attributes
- */
- import type { Node, Element } from 'domhandler';
- import type { Cheerio } from '../cheerio';
- /**
- * Method for getting attributes. Gets the attribute value for only the first
- * element in the matched set.
- *
- * @category Attributes
- * @example
- *
- * ```js
- * $('ul').attr('id');
- * //=> fruits
- * ```
- *
- * @param name - Name of the attribute.
- * @returns The attribute's value.
- * @see {@link https://api.jquery.com/attr/}
- */
- export declare function attr<T extends Node>(this: Cheerio<T>, name: string): string | undefined;
- /**
- * Method for getting all attributes and their values of the first element in
- * the matched set.
- *
- * @category Attributes
- * @example
- *
- * ```js
- * $('ul').attr();
- * //=> { id: 'fruits' }
- * ```
- *
- * @returns The attribute's values.
- * @see {@link https://api.jquery.com/attr/}
- */
- export declare function attr<T extends Node>(this: Cheerio<T>): Record<string, string>;
- /**
- * Method for setting attributes. Sets the attribute value for only the first
- * element in the matched set. If you set an attribute's value to `null`, you
- * remove that attribute. You may also pass a `map` and `function`.
- *
- * @category Attributes
- * @example
- *
- * ```js
- * $('.apple').attr('id', 'favorite').html();
- * //=> <li class="apple" id="favorite">Apple</li>
- * ```
- *
- * @param name - Name of the attribute.
- * @param value - The new value of the attribute.
- * @returns The instance itself.
- * @see {@link https://api.jquery.com/attr/}
- */
- export declare function attr<T extends Node>(this: Cheerio<T>, name: string, value?: string | null | ((this: Element, i: number, attrib: string) => string | null)): Cheerio<T>;
- /**
- * Method for setting multiple attributes at once. Sets the attribute value for
- * only the first element in the matched set. If you set an attribute's value to
- * `null`, you remove that attribute.
- *
- * @category Attributes
- * @example
- *
- * ```js
- * $('.apple').attr({ id: 'favorite' }).html();
- * //=> <li class="apple" id="favorite">Apple</li>
- * ```
- *
- * @param values - Map of attribute names and values.
- * @returns The instance itself.
- * @see {@link https://api.jquery.com/attr/}
- */
- export declare function attr<T extends Node>(this: Cheerio<T>, values: Record<string, string | null>): Cheerio<T>;
- interface StyleProp {
- length: number;
- [key: string]: string | number;
- [index: number]: string;
- }
- /**
- * Method for getting and setting properties. Gets the property value for only
- * the first element in the matched set.
- *
- * @category Attributes
- * @example
- *
- * ```js
- * $('input[type="checkbox"]').prop('checked');
- * //=> false
- *
- * $('input[type="checkbox"]').prop('checked', true).val();
- * //=> ok
- * ```
- *
- * @param name - Name of the property.
- * @param value - If specified set the property to this.
- * @returns If `value` is specified the instance itself, otherwise the prop's value.
- * @see {@link https://api.jquery.com/prop/}
- */
- export declare function prop<T extends Node>(this: Cheerio<T>, name: 'tagName' | 'nodeName'): T extends Element ? string : undefined;
- export declare function prop<T extends Node>(this: Cheerio<T>, name: 'innerHTML' | 'outerHTML'): string | null;
- export declare function prop<T extends Node>(this: Cheerio<T>, name: 'style'): StyleProp;
- export declare function prop<T extends Node, K extends keyof Element>(this: Cheerio<T>, name: K): Element[K];
- export declare function prop<T extends Node, K extends keyof Element>(this: Cheerio<T>, name: K, value: Element[K] | ((this: Element, i: number, prop: K) => Element[keyof Element])): Cheerio<T>;
- export declare function prop<T extends Node>(this: Cheerio<T>, name: Record<string, string | Element[keyof Element] | boolean>): Cheerio<T>;
- export declare function prop<T extends Node>(this: Cheerio<T>, name: string, value: string | boolean | null | ((this: Element, i: number, prop: string) => string | boolean)): Cheerio<T>;
- export declare function prop<T extends Node>(this: Cheerio<T>, name: string): string;
- /**
- * Method for getting data attributes, for only the first element in the matched set.
- *
- * @category Attributes
- * @example
- *
- * ```js
- * $('<div data-apple-color="red"></div>').data('apple-color');
- * //=> 'red'
- * ```
- *
- * @param name - Name of the data attribute.
- * @returns The data attribute's value.
- * @see {@link https://api.jquery.com/data/}
- */
- export declare function data<T extends Node>(this: Cheerio<T>, name: string): unknown | undefined;
- /**
- * Method for getting all of an element's data attributes, for only the first
- * element in the matched set.
- *
- * @category Attributes
- * @example
- *
- * ```js
- * $('<div data-apple-color="red"></div>').data();
- * //=> { appleColor: 'red' }
- * ```
- *
- * @returns The data attribute's values.
- * @see {@link https://api.jquery.com/data/}
- */
- export declare function data<T extends Node>(this: Cheerio<T>): Record<string, unknown>;
- /**
- * Method for setting data attributes, for only the first element in the matched set.
- *
- * @category Attributes
- * @example
- *
- * ```js
- * const apple = $('.apple').data('kind', 'mac');
- *
- * apple.data('kind');
- * //=> 'mac'
- * ```
- *
- * @param name - Name of the data attribute.
- * @param value - The new value.
- * @returns The instance itself.
- * @see {@link https://api.jquery.com/data/}
- */
- export declare function data<T extends Node>(this: Cheerio<T>, name: string, value: unknown): Cheerio<T>;
- /**
- * Method for setting multiple data attributes at once, for only the first
- * element in the matched set.
- *
- * @category Attributes
- * @example
- *
- * ```js
- * const apple = $('.apple').data({ kind: 'mac' });
- *
- * apple.data('kind');
- * //=> 'mac'
- * ```
- *
- * @param values - Map of names to values.
- * @returns The instance itself.
- * @see {@link https://api.jquery.com/data/}
- */
- export declare function data<T extends Node>(this: Cheerio<T>, values: Record<string, unknown>): Cheerio<T>;
- /**
- * Method for getting the value of input, select, and textarea. Note: Support
- * for `map`, and `function` has not been added yet.
- *
- * @category Attributes
- * @example
- *
- * ```js
- * $('input[type="text"]').val();
- * //=> input_text
- * ```
- *
- * @returns The value.
- * @see {@link https://api.jquery.com/val/}
- */
- export declare function val<T extends Node>(this: Cheerio<T>): string | undefined | string[];
- /**
- * Method for setting the value of input, select, and textarea. Note: Support
- * for `map`, and `function` has not been added yet.
- *
- * @category Attributes
- * @example
- *
- * ```js
- * $('input[type="text"]').val('test').html();
- * //=> <input type="text" value="test"/>
- * ```
- *
- * @param value - The new value.
- * @returns The instance itself.
- * @see {@link https://api.jquery.com/val/}
- */
- export declare function val<T extends Node>(this: Cheerio<T>, value: string | string[]): Cheerio<T>;
- /**
- * Method for removing attributes by `name`.
- *
- * @category Attributes
- * @example
- *
- * ```js
- * $('.pear').removeAttr('class').html();
- * //=> <li>Pear</li>
- *
- * $('.apple').attr('id', 'favorite');
- * $('.apple').removeAttr('id class').html();
- * //=> <li>Apple</li>
- * ```
- *
- * @param name - Name of the attribute.
- * @returns The instance itself.
- * @see {@link https://api.jquery.com/removeAttr/}
- */
- export declare function removeAttr<T extends Node>(this: Cheerio<T>, name: string): Cheerio<T>;
- /**
- * Check to see if *any* of the matched elements have the given `className`.
- *
- * @category Attributes
- * @example
- *
- * ```js
- * $('.pear').hasClass('pear');
- * //=> true
- *
- * $('apple').hasClass('fruit');
- * //=> false
- *
- * $('li').hasClass('pear');
- * //=> true
- * ```
- *
- * @param className - Name of the class.
- * @returns Indicates if an element has the given `className`.
- * @see {@link https://api.jquery.com/hasClass/}
- */
- export declare function hasClass<T extends Node>(this: Cheerio<T>, className: string): boolean;
- /**
- * Adds class(es) to all of the matched elements. Also accepts a `function`.
- *
- * @category Attributes
- * @example
- *
- * ```js
- * $('.pear').addClass('fruit').html();
- * //=> <li class="pear fruit">Pear</li>
- *
- * $('.apple').addClass('fruit red').html();
- * //=> <li class="apple fruit red">Apple</li>
- * ```
- *
- * @param value - Name of new class.
- * @returns The instance itself.
- * @see {@link https://api.jquery.com/addClass/}
- */
- export declare function addClass<T extends Node, R extends ArrayLike<T>>(this: R, value?: string | ((this: Element, i: number, className: string) => string | undefined)): R;
- /**
- * Removes one or more space-separated classes from the selected elements. If no
- * `className` is defined, all classes will be removed. Also accepts a `function`.
- *
- * @category Attributes
- * @example
- *
- * ```js
- * $('.pear').removeClass('pear').html();
- * //=> <li class="">Pear</li>
- *
- * $('.apple').addClass('red').removeClass().html();
- * //=> <li class="">Apple</li>
- * ```
- *
- * @param name - Name of the class. If not specified, removes all elements.
- * @returns The instance itself.
- * @see {@link https://api.jquery.com/removeClass/}
- */
- export declare function removeClass<T extends Node, R extends ArrayLike<T>>(this: R, name?: string | ((this: Element, i: number, className: string) => string | undefined)): R;
- /**
- * Add or remove class(es) from the matched elements, depending on either the
- * class's presence or the value of the switch argument. Also accepts a `function`.
- *
- * @category Attributes
- * @example
- *
- * ```js
- * $('.apple.green').toggleClass('fruit green red').html();
- * //=> <li class="apple fruit red">Apple</li>
- *
- * $('.apple.green').toggleClass('fruit green red', true).html();
- * //=> <li class="apple green fruit red">Apple</li>
- * ```
- *
- * @param value - Name of the class. Can also be a function.
- * @param stateVal - If specified the state of the class.
- * @returns The instance itself.
- * @see {@link https://api.jquery.com/toggleClass/}
- */
- export declare function toggleClass<T extends Node, R extends ArrayLike<T>>(this: R, value?: string | ((this: Element, i: number, className: string, stateVal?: boolean) => string), stateVal?: boolean): R;
- export {};
- //# sourceMappingURL=attributes.d.ts.map
|