123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900 |
- "use strict";
- /**
- * Methods for traversing the DOM structure.
- *
- * @module cheerio/traversing
- */
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
- if (k2 === undefined) k2 = k;
- var desc = Object.getOwnPropertyDescriptor(m, k);
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
- desc = { enumerable: true, get: function() { return m[k]; } };
- }
- Object.defineProperty(o, k2, desc);
- }) : (function(o, m, k, k2) {
- if (k2 === undefined) k2 = k;
- o[k2] = m[k];
- }));
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
- Object.defineProperty(o, "default", { enumerable: true, value: v });
- }) : function(o, v) {
- o["default"] = v;
- });
- var __importStar = (this && this.__importStar) || function (mod) {
- if (mod && mod.__esModule) return mod;
- var result = {};
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
- __setModuleDefault(result, mod);
- return result;
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.children = exports.siblings = exports.prevUntil = exports.prevAll = exports.prev = exports.nextUntil = exports.nextAll = exports.next = exports.parentsUntil = exports.parents = exports.parent = void 0;
- exports.find = find;
- exports._findBySelector = _findBySelector;
- exports.closest = closest;
- exports.contents = contents;
- exports.each = each;
- exports.map = map;
- exports.filter = filter;
- exports.filterArray = filterArray;
- exports.is = is;
- exports.not = not;
- exports.has = has;
- exports.first = first;
- exports.last = last;
- exports.eq = eq;
- exports.get = get;
- exports.toArray = toArray;
- exports.index = index;
- exports.slice = slice;
- exports.end = end;
- exports.add = add;
- exports.addBack = addBack;
- const domhandler_1 = require("domhandler");
- const select = __importStar(require("cheerio-select"));
- const utils_js_1 = require("../utils.js");
- const static_js_1 = require("../static.js");
- const domutils_1 = require("domutils");
- const reSiblingSelector = /^\s*[+~]/;
- /**
- * Get the descendants of each element in the current set of matched elements,
- * filtered by a selector, jQuery object, or element.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('#fruits').find('li').length;
- * //=> 3
- * $('#fruits').find($('.apple')).length;
- * //=> 1
- * ```
- *
- * @param selectorOrHaystack - Element to look for.
- * @returns The found elements.
- * @see {@link https://api.jquery.com/find/}
- */
- function find(selectorOrHaystack) {
- if (!selectorOrHaystack) {
- return this._make([]);
- }
- if (typeof selectorOrHaystack !== 'string') {
- const haystack = (0, utils_js_1.isCheerio)(selectorOrHaystack)
- ? selectorOrHaystack.toArray()
- : [selectorOrHaystack];
- const context = this.toArray();
- return this._make(haystack.filter((elem) => context.some((node) => (0, static_js_1.contains)(node, elem))));
- }
- return this._findBySelector(selectorOrHaystack, Number.POSITIVE_INFINITY);
- }
- /**
- * Find elements by a specific selector.
- *
- * @private
- * @category Traversing
- * @param selector - Selector to filter by.
- * @param limit - Maximum number of elements to match.
- * @returns The found elements.
- */
- function _findBySelector(selector, limit) {
- var _a;
- const context = this.toArray();
- const elems = reSiblingSelector.test(selector)
- ? context
- : this.children().toArray();
- const options = {
- context,
- root: (_a = this._root) === null || _a === void 0 ? void 0 : _a[0],
- // Pass options that are recognized by `cheerio-select`
- xmlMode: this.options.xmlMode,
- lowerCaseTags: this.options.lowerCaseTags,
- lowerCaseAttributeNames: this.options.lowerCaseAttributeNames,
- pseudos: this.options.pseudos,
- quirksMode: this.options.quirksMode,
- };
- return this._make(select.select(selector, elems, options, limit));
- }
- /**
- * Creates a matcher, using a particular mapping function. Matchers provide a
- * function that finds elements using a generating function, supporting
- * filtering.
- *
- * @private
- * @param matchMap - Mapping function.
- * @returns - Function for wrapping generating functions.
- */
- function _getMatcher(matchMap) {
- return function (fn, ...postFns) {
- return function (selector) {
- var _a;
- let matched = matchMap(fn, this);
- if (selector) {
- matched = filterArray(matched, selector, this.options.xmlMode, (_a = this._root) === null || _a === void 0 ? void 0 : _a[0]);
- }
- return this._make(
- // Post processing is only necessary if there is more than one element.
- this.length > 1 && matched.length > 1
- ? postFns.reduce((elems, fn) => fn(elems), matched)
- : matched);
- };
- };
- }
- /** Matcher that adds multiple elements for each entry in the input. */
- const _matcher = _getMatcher((fn, elems) => {
- let ret = [];
- for (let i = 0; i < elems.length; i++) {
- const value = fn(elems[i]);
- if (value.length > 0)
- ret = ret.concat(value);
- }
- return ret;
- });
- /** Matcher that adds at most one element for each entry in the input. */
- const _singleMatcher = _getMatcher((fn, elems) => {
- const ret = [];
- for (let i = 0; i < elems.length; i++) {
- const value = fn(elems[i]);
- if (value !== null) {
- ret.push(value);
- }
- }
- return ret;
- });
- /**
- * Matcher that supports traversing until a condition is met.
- *
- * @param nextElem - Function that returns the next element.
- * @param postFns - Post processing functions.
- * @returns A function usable for `*Until` methods.
- */
- function _matchUntil(nextElem, ...postFns) {
- // We use a variable here that is used from within the matcher.
- let matches = null;
- const innerMatcher = _getMatcher((nextElem, elems) => {
- const matched = [];
- (0, utils_js_1.domEach)(elems, (elem) => {
- for (let next; (next = nextElem(elem)); elem = next) {
- // FIXME: `matched` might contain duplicates here and the index is too large.
- if (matches === null || matches === void 0 ? void 0 : matches(next, matched.length))
- break;
- matched.push(next);
- }
- });
- return matched;
- })(nextElem, ...postFns);
- return function (selector, filterSelector) {
- // Override `matches` variable with the new target.
- matches =
- typeof selector === 'string'
- ? (elem) => select.is(elem, selector, this.options)
- : selector
- ? getFilterFn(selector)
- : null;
- const ret = innerMatcher.call(this, filterSelector);
- // Set `matches` to `null`, so we don't waste memory.
- matches = null;
- return ret;
- };
- }
- function _removeDuplicates(elems) {
- return elems.length > 1 ? Array.from(new Set(elems)) : elems;
- }
- /**
- * Get the parent of each element in the current set of matched elements,
- * optionally filtered by a selector.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('.pear').parent().attr('id');
- * //=> fruits
- * ```
- *
- * @param selector - If specified filter for parent.
- * @returns The parents.
- * @see {@link https://api.jquery.com/parent/}
- */
- exports.parent = _singleMatcher(({ parent }) => (parent && !(0, domhandler_1.isDocument)(parent) ? parent : null), _removeDuplicates);
- /**
- * Get a set of parents filtered by `selector` of each element in the current
- * set of match elements.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('.orange').parents().length;
- * //=> 2
- * $('.orange').parents('#fruits').length;
- * //=> 1
- * ```
- *
- * @param selector - If specified filter for parents.
- * @returns The parents.
- * @see {@link https://api.jquery.com/parents/}
- */
- exports.parents = _matcher((elem) => {
- const matched = [];
- while (elem.parent && !(0, domhandler_1.isDocument)(elem.parent)) {
- matched.push(elem.parent);
- elem = elem.parent;
- }
- return matched;
- }, domutils_1.uniqueSort, (elems) => elems.reverse());
- /**
- * Get the ancestors of each element in the current set of matched elements, up
- * to but not including the element matched by the selector, DOM node, or
- * cheerio object.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('.orange').parentsUntil('#food').length;
- * //=> 1
- * ```
- *
- * @param selector - Selector for element to stop at.
- * @param filterSelector - Optional filter for parents.
- * @returns The parents.
- * @see {@link https://api.jquery.com/parentsUntil/}
- */
- exports.parentsUntil = _matchUntil(({ parent }) => (parent && !(0, domhandler_1.isDocument)(parent) ? parent : null), domutils_1.uniqueSort, (elems) => elems.reverse());
- /**
- * For each element in the set, get the first element that matches the selector
- * by testing the element itself and traversing up through its ancestors in the
- * DOM tree.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('.orange').closest();
- * //=> []
- *
- * $('.orange').closest('.apple');
- * // => []
- *
- * $('.orange').closest('li');
- * //=> [<li class="orange">Orange</li>]
- *
- * $('.orange').closest('#fruits');
- * //=> [<ul id="fruits"> ... </ul>]
- * ```
- *
- * @param selector - Selector for the element to find.
- * @returns The closest nodes.
- * @see {@link https://api.jquery.com/closest/}
- */
- function closest(selector) {
- var _a;
- const set = [];
- if (!selector) {
- return this._make(set);
- }
- const selectOpts = {
- xmlMode: this.options.xmlMode,
- root: (_a = this._root) === null || _a === void 0 ? void 0 : _a[0],
- };
- const selectFn = typeof selector === 'string'
- ? (elem) => select.is(elem, selector, selectOpts)
- : getFilterFn(selector);
- (0, utils_js_1.domEach)(this, (elem) => {
- if (elem && !(0, domhandler_1.isDocument)(elem) && !(0, domhandler_1.isTag)(elem)) {
- elem = elem.parent;
- }
- while (elem && (0, domhandler_1.isTag)(elem)) {
- if (selectFn(elem, 0)) {
- // Do not add duplicate elements to the set
- if (!set.includes(elem)) {
- set.push(elem);
- }
- break;
- }
- elem = elem.parent;
- }
- });
- return this._make(set);
- }
- /**
- * Gets the next sibling of each selected element, optionally filtered by a
- * selector.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('.apple').next().hasClass('orange');
- * //=> true
- * ```
- *
- * @param selector - If specified filter for sibling.
- * @returns The next nodes.
- * @see {@link https://api.jquery.com/next/}
- */
- exports.next = _singleMatcher((elem) => (0, domutils_1.nextElementSibling)(elem));
- /**
- * Gets all the following siblings of the each selected element, optionally
- * filtered by a selector.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('.apple').nextAll();
- * //=> [<li class="orange">Orange</li>, <li class="pear">Pear</li>]
- * $('.apple').nextAll('.orange');
- * //=> [<li class="orange">Orange</li>]
- * ```
- *
- * @param selector - If specified filter for siblings.
- * @returns The next nodes.
- * @see {@link https://api.jquery.com/nextAll/}
- */
- exports.nextAll = _matcher((elem) => {
- const matched = [];
- while (elem.next) {
- elem = elem.next;
- if ((0, domhandler_1.isTag)(elem))
- matched.push(elem);
- }
- return matched;
- }, _removeDuplicates);
- /**
- * Gets all the following siblings up to but not including the element matched
- * by the selector, optionally filtered by another selector.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('.apple').nextUntil('.pear');
- * //=> [<li class="orange">Orange</li>]
- * ```
- *
- * @param selector - Selector for element to stop at.
- * @param filterSelector - If specified filter for siblings.
- * @returns The next nodes.
- * @see {@link https://api.jquery.com/nextUntil/}
- */
- exports.nextUntil = _matchUntil((el) => (0, domutils_1.nextElementSibling)(el), _removeDuplicates);
- /**
- * Gets the previous sibling of each selected element optionally filtered by a
- * selector.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('.orange').prev().hasClass('apple');
- * //=> true
- * ```
- *
- * @param selector - If specified filter for siblings.
- * @returns The previous nodes.
- * @see {@link https://api.jquery.com/prev/}
- */
- exports.prev = _singleMatcher((elem) => (0, domutils_1.prevElementSibling)(elem));
- /**
- * Gets all the preceding siblings of each selected element, optionally filtered
- * by a selector.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('.pear').prevAll();
- * //=> [<li class="orange">Orange</li>, <li class="apple">Apple</li>]
- *
- * $('.pear').prevAll('.orange');
- * //=> [<li class="orange">Orange</li>]
- * ```
- *
- * @param selector - If specified filter for siblings.
- * @returns The previous nodes.
- * @see {@link https://api.jquery.com/prevAll/}
- */
- exports.prevAll = _matcher((elem) => {
- const matched = [];
- while (elem.prev) {
- elem = elem.prev;
- if ((0, domhandler_1.isTag)(elem))
- matched.push(elem);
- }
- return matched;
- }, _removeDuplicates);
- /**
- * Gets all the preceding siblings up to but not including the element matched
- * by the selector, optionally filtered by another selector.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('.pear').prevUntil('.apple');
- * //=> [<li class="orange">Orange</li>]
- * ```
- *
- * @param selector - Selector for element to stop at.
- * @param filterSelector - If specified filter for siblings.
- * @returns The previous nodes.
- * @see {@link https://api.jquery.com/prevUntil/}
- */
- exports.prevUntil = _matchUntil((el) => (0, domutils_1.prevElementSibling)(el), _removeDuplicates);
- /**
- * Get the siblings of each element (excluding the element) in the set of
- * matched elements, optionally filtered by a selector.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('.pear').siblings().length;
- * //=> 2
- *
- * $('.pear').siblings('.orange').length;
- * //=> 1
- * ```
- *
- * @param selector - If specified filter for siblings.
- * @returns The siblings.
- * @see {@link https://api.jquery.com/siblings/}
- */
- exports.siblings = _matcher((elem) => (0, domutils_1.getSiblings)(elem).filter((el) => (0, domhandler_1.isTag)(el) && el !== elem), domutils_1.uniqueSort);
- /**
- * Gets the element children of each element in the set of matched elements.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('#fruits').children().length;
- * //=> 3
- *
- * $('#fruits').children('.pear').text();
- * //=> Pear
- * ```
- *
- * @param selector - If specified filter for children.
- * @returns The children.
- * @see {@link https://api.jquery.com/children/}
- */
- exports.children = _matcher((elem) => (0, domutils_1.getChildren)(elem).filter(domhandler_1.isTag), _removeDuplicates);
- /**
- * Gets the children of each element in the set of matched elements, including
- * text and comment nodes.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('#fruits').contents().length;
- * //=> 3
- * ```
- *
- * @returns The children.
- * @see {@link https://api.jquery.com/contents/}
- */
- function contents() {
- const elems = this.toArray().reduce((newElems, elem) => (0, domhandler_1.hasChildren)(elem) ? newElems.concat(elem.children) : newElems, []);
- return this._make(elems);
- }
- /**
- * Iterates over a cheerio object, executing a function for each matched
- * element. When the callback is fired, the function is fired in the context of
- * the DOM element, so `this` refers to the current element, which is equivalent
- * to the function parameter `element`. To break out of the `each` loop early,
- * return with `false`.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * const fruits = [];
- *
- * $('li').each(function (i, elem) {
- * fruits[i] = $(this).text();
- * });
- *
- * fruits.join(', ');
- * //=> Apple, Orange, Pear
- * ```
- *
- * @param fn - Function to execute.
- * @returns The instance itself, useful for chaining.
- * @see {@link https://api.jquery.com/each/}
- */
- function each(fn) {
- let i = 0;
- const len = this.length;
- while (i < len && fn.call(this[i], i, this[i]) !== false)
- ++i;
- return this;
- }
- /**
- * Pass each element in the current matched set through a function, producing a
- * new Cheerio object containing the return values. The function can return an
- * individual data item or an array of data items to be inserted into the
- * resulting set. If an array is returned, the elements inside the array are
- * inserted into the set. If the function returns null or undefined, no element
- * will be inserted.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('li')
- * .map(function (i, el) {
- * // this === el
- * return $(this).text();
- * })
- * .toArray()
- * .join(' ');
- * //=> "apple orange pear"
- * ```
- *
- * @param fn - Function to execute.
- * @returns The mapped elements, wrapped in a Cheerio collection.
- * @see {@link https://api.jquery.com/map/}
- */
- function map(fn) {
- let elems = [];
- for (let i = 0; i < this.length; i++) {
- const el = this[i];
- const val = fn.call(el, i, el);
- if (val != null) {
- elems = elems.concat(val);
- }
- }
- return this._make(elems);
- }
- /**
- * Creates a function to test if a filter is matched.
- *
- * @param match - A filter.
- * @returns A function that determines if a filter has been matched.
- */
- function getFilterFn(match) {
- if (typeof match === 'function') {
- return (el, i) => match.call(el, i, el);
- }
- if ((0, utils_js_1.isCheerio)(match)) {
- return (el) => Array.prototype.includes.call(match, el);
- }
- return function (el) {
- return match === el;
- };
- }
- function filter(match) {
- var _a;
- return this._make(filterArray(this.toArray(), match, this.options.xmlMode, (_a = this._root) === null || _a === void 0 ? void 0 : _a[0]));
- }
- function filterArray(nodes, match, xmlMode, root) {
- return typeof match === 'string'
- ? select.filter(match, nodes, { xmlMode, root })
- : nodes.filter(getFilterFn(match));
- }
- /**
- * Checks the current list of elements and returns `true` if _any_ of the
- * elements match the selector. If using an element or Cheerio selection,
- * returns `true` if _any_ of the elements match. If using a predicate function,
- * the function is executed in the context of the selected element, so `this`
- * refers to the current element.
- *
- * @category Traversing
- * @param selector - Selector for the selection.
- * @returns Whether or not the selector matches an element of the instance.
- * @see {@link https://api.jquery.com/is/}
- */
- function is(selector) {
- const nodes = this.toArray();
- return typeof selector === 'string'
- ? select.some(nodes.filter(domhandler_1.isTag), selector, this.options)
- : selector
- ? nodes.some(getFilterFn(selector))
- : false;
- }
- /**
- * Remove elements from the set of matched elements. Given a Cheerio object that
- * represents a set of DOM elements, the `.not()` method constructs a new
- * Cheerio object from a subset of the matching elements. The supplied selector
- * is tested against each element; the elements that don't match the selector
- * will be included in the result.
- *
- * The `.not()` method can take a function as its argument in the same way that
- * `.filter()` does. Elements for which the function returns `true` are excluded
- * from the filtered set; all other elements are included.
- *
- * @category Traversing
- * @example <caption>Selector</caption>
- *
- * ```js
- * $('li').not('.apple').length;
- * //=> 2
- * ```
- *
- * @example <caption>Function</caption>
- *
- * ```js
- * $('li').not(function (i, el) {
- * // this === el
- * return $(this).attr('class') === 'orange';
- * }).length; //=> 2
- * ```
- *
- * @param match - Value to look for, following the rules above.
- * @returns The filtered collection.
- * @see {@link https://api.jquery.com/not/}
- */
- function not(match) {
- let nodes = this.toArray();
- if (typeof match === 'string') {
- const matches = new Set(select.filter(match, nodes, this.options));
- nodes = nodes.filter((el) => !matches.has(el));
- }
- else {
- const filterFn = getFilterFn(match);
- nodes = nodes.filter((el, i) => !filterFn(el, i));
- }
- return this._make(nodes);
- }
- /**
- * Filters the set of matched elements to only those which have the given DOM
- * element as a descendant or which have a descendant that matches the given
- * selector. Equivalent to `.filter(':has(selector)')`.
- *
- * @category Traversing
- * @example <caption>Selector</caption>
- *
- * ```js
- * $('ul').has('.pear').attr('id');
- * //=> fruits
- * ```
- *
- * @example <caption>Element</caption>
- *
- * ```js
- * $('ul').has($('.pear')[0]).attr('id');
- * //=> fruits
- * ```
- *
- * @param selectorOrHaystack - Element to look for.
- * @returns The filtered collection.
- * @see {@link https://api.jquery.com/has/}
- */
- function has(selectorOrHaystack) {
- return this.filter(typeof selectorOrHaystack === 'string'
- ? // Using the `:has` selector here short-circuits searches.
- `:has(${selectorOrHaystack})`
- : (_, el) => this._make(el).find(selectorOrHaystack).length > 0);
- }
- /**
- * Will select the first element of a cheerio object.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('#fruits').children().first().text();
- * //=> Apple
- * ```
- *
- * @returns The first element.
- * @see {@link https://api.jquery.com/first/}
- */
- function first() {
- return this.length > 1 ? this._make(this[0]) : this;
- }
- /**
- * Will select the last element of a cheerio object.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('#fruits').children().last().text();
- * //=> Pear
- * ```
- *
- * @returns The last element.
- * @see {@link https://api.jquery.com/last/}
- */
- function last() {
- return this.length > 0 ? this._make(this[this.length - 1]) : this;
- }
- /**
- * Reduce the set of matched elements to the one at the specified index. Use
- * `.eq(-i)` to count backwards from the last selected element.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('li').eq(0).text();
- * //=> Apple
- *
- * $('li').eq(-1).text();
- * //=> Pear
- * ```
- *
- * @param i - Index of the element to select.
- * @returns The element at the `i`th position.
- * @see {@link https://api.jquery.com/eq/}
- */
- function eq(i) {
- var _a;
- i = +i;
- // Use the first identity optimization if possible
- if (i === 0 && this.length <= 1)
- return this;
- if (i < 0)
- i = this.length + i;
- return this._make((_a = this[i]) !== null && _a !== void 0 ? _a : []);
- }
- function get(i) {
- if (i == null) {
- return this.toArray();
- }
- return this[i < 0 ? this.length + i : i];
- }
- /**
- * Retrieve all the DOM elements contained in the jQuery set as an array.
- *
- * @example
- *
- * ```js
- * $('li').toArray();
- * //=> [ {...}, {...}, {...} ]
- * ```
- *
- * @returns The contained items.
- */
- function toArray() {
- return Array.prototype.slice.call(this);
- }
- /**
- * Search for a given element from among the matched elements.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('.pear').index();
- * //=> 2 $('.orange').index('li');
- * //=> 1
- * $('.apple').index($('#fruit, li'));
- * //=> 1
- * ```
- *
- * @param selectorOrNeedle - Element to look for.
- * @returns The index of the element.
- * @see {@link https://api.jquery.com/index/}
- */
- function index(selectorOrNeedle) {
- let $haystack;
- let needle;
- if (selectorOrNeedle == null) {
- $haystack = this.parent().children();
- needle = this[0];
- }
- else if (typeof selectorOrNeedle === 'string') {
- $haystack = this._make(selectorOrNeedle);
- needle = this[0];
- }
- else {
- // eslint-disable-next-line @typescript-eslint/no-this-alias, unicorn/no-this-assignment
- $haystack = this;
- needle = (0, utils_js_1.isCheerio)(selectorOrNeedle)
- ? selectorOrNeedle[0]
- : selectorOrNeedle;
- }
- return Array.prototype.indexOf.call($haystack, needle);
- }
- /**
- * Gets the elements matching the specified range (0-based position).
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('li').slice(1).eq(0).text();
- * //=> 'Orange'
- *
- * $('li').slice(1, 2).length;
- * //=> 1
- * ```
- *
- * @param start - A position at which the elements begin to be selected. If
- * negative, it indicates an offset from the end of the set.
- * @param end - A position at which the elements stop being selected. If
- * negative, it indicates an offset from the end of the set. If omitted, the
- * range continues until the end of the set.
- * @returns The elements matching the specified range.
- * @see {@link https://api.jquery.com/slice/}
- */
- function slice(start, end) {
- return this._make(Array.prototype.slice.call(this, start, end));
- }
- /**
- * End the most recent filtering operation in the current chain and return the
- * set of matched elements to its previous state.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('li').eq(0).end().length;
- * //=> 3
- * ```
- *
- * @returns The previous state of the set of matched elements.
- * @see {@link https://api.jquery.com/end/}
- */
- function end() {
- var _a;
- return (_a = this.prevObject) !== null && _a !== void 0 ? _a : this._make([]);
- }
- /**
- * Add elements to the set of matched elements.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('.apple').add('.orange').length;
- * //=> 2
- * ```
- *
- * @param other - Elements to add.
- * @param context - Optionally the context of the new selection.
- * @returns The combined set.
- * @see {@link https://api.jquery.com/add/}
- */
- function add(other, context) {
- const selection = this._make(other, context);
- const contents = (0, domutils_1.uniqueSort)([...this.get(), ...selection.get()]);
- return this._make(contents);
- }
- /**
- * Add the previous set of elements on the stack to the current set, optionally
- * filtered by a selector.
- *
- * @category Traversing
- * @example
- *
- * ```js
- * $('li').eq(0).addBack('.orange').length;
- * //=> 2
- * ```
- *
- * @param selector - Selector for the elements to add.
- * @returns The combined set.
- * @see {@link https://api.jquery.com/addBack/}
- */
- function addBack(selector) {
- return this.prevObject
- ? this.add(selector ? this.prevObject.filter(selector) : this.prevObject)
- : this;
- }
- //# sourceMappingURL=traversing.js.map
|