import { type InsertableContent } from './dom'; import { BQueryElement } from './element'; /** * Wrapper for multiple DOM elements. * Provides batch operations on a collection of elements with chainable API. * * This class enables jQuery-like operations across multiple elements: * - All mutating methods apply to every element in the collection * - Getter methods return data from the first element * - Supports iteration via forEach, map, filter, and reduce * * @example * ```ts * $$('.items') * .addClass('highlight') * .css({ opacity: '0.8' }) * .on('click', () => console.log('clicked')); * ``` */ export declare class BQueryCollection { readonly elements: Element[]; /** * Stores delegated event handlers for cleanup via undelegate(). * Outer map: element -> (key -> (handler -> wrapper)) * Key format: `${event}:${selector}` * @internal */ private readonly delegatedHandlers; /** * Creates a new collection wrapper. * @param elements - Array of DOM elements to wrap */ constructor(elements: Element[]); /** * Gets the number of elements in the collection. */ get length(): number; /** * Gets the first element in the collection, if any. * @internal */ private first; /** * Gets a single element as a BQueryElement wrapper. * * @param index - Zero-based index of the element * @returns BQueryElement wrapper or undefined if out of range */ eq(index: number): BQueryElement | undefined; /** * Gets the first element as a BQueryElement wrapper. * * @returns BQueryElement wrapper or undefined if empty */ firstEl(): BQueryElement | undefined; /** * Gets the last element as a BQueryElement wrapper. * * @returns BQueryElement wrapper or undefined if empty */ lastEl(): BQueryElement | undefined; /** * Iterates over each element in the collection. * * @param callback - Function to call for each wrapped element * @returns The instance for method chaining */ each(callback: (element: BQueryElement, index: number) => void): this; /** * Maps each element to a new value. * * @param callback - Function to transform each element * @returns Array of transformed values */ map(callback: (element: Element, index: number) => T): T[]; /** * Filters elements based on a predicate. * * @param predicate - Function to test each element * @returns New BQueryCollection with matching elements */ filter(predicate: (element: Element, index: number) => boolean): BQueryCollection; /** * Reduces the collection to a single value. * * @param callback - Reducer function * @param initialValue - Initial accumulator value * @returns Accumulated result */ reduce(callback: (accumulator: T, element: Element, index: number) => T, initialValue: T): T; /** * Converts the collection to an array of BQueryElement wrappers. * * @returns Array of BQueryElement instances */ toArray(): BQueryElement[]; /** Add one or more classes to all elements. */ addClass(...classNames: string[]): this; /** Remove one or more classes from all elements. */ removeClass(...classNames: string[]): this; /** Toggle a class on all elements. */ toggleClass(className: string, force?: boolean): this; /** * Sets an attribute on all elements or gets from first. * * @param name - Attribute name * @param value - Value to set (optional) * @returns Attribute value when getting, instance when setting */ attr(name: string, value?: string): string | this; /** * Removes an attribute from all elements. * * @param name - Attribute name to remove * @returns The instance for method chaining */ removeAttr(name: string): this; /** Toggle an attribute on all elements. */ toggleAttr(name: string, force?: boolean): this; /** * Sets text content on all elements or gets from first. * * @param value - Text to set (optional) * @returns Text content when getting, instance when setting */ text(value?: string): string | this; /** * Sets sanitized HTML on all elements or gets from first. * * @param value - HTML to set (optional, will be sanitized) * @returns HTML content when getting, instance when setting */ html(value?: string): string | this; /** * Sets HTML on all elements without sanitization. * * @param value - Raw HTML to set * @returns The instance for method chaining * @warning Bypasses XSS protection */ htmlUnsafe(value: string): this; /** Append content to all elements. */ append(content: InsertableContent): this; /** Prepend content to all elements. */ prepend(content: InsertableContent): this; /** Insert content before all elements. */ before(content: InsertableContent): this; /** Insert content after all elements. */ after(content: InsertableContent): this; /** * Gets or sets CSS styles on all elements. * When getting, returns the computed style value from the first element. * * @param property - Property name or object of properties * @param value - Value when setting single property * @returns The computed style value when getting, instance when setting */ css(property: string): string; css(property: string, value: string): this; css(property: Record): this; /** Wrap each element with a wrapper element or tag. */ wrap(wrapper: string | Element): this; /** * Remove the parent element of each element, keeping the elements in place. * * **Important**: This method unwraps ALL children of each parent element, * not just the elements in the collection. If you call `unwrap()` on a * collection containing only some children of a parent, all siblings will * also be unwrapped. This behavior is consistent with jQuery's `.unwrap()`. * * @returns The collection for chaining * * @example * ```ts * // HTML:
AB
* const spans = $$('span'); * spans.unwrap(); // Removes
, both spans move to
* // Result:
AB
* ``` */ unwrap(): this; /** Replace each element with provided content. */ replaceWith(content: string | Element): BQueryCollection; /** * Removes all elements from the DOM while keeping the wrapped nodes available * for later reuse. * * @returns The instance for method chaining */ detach(): this; /** * Gets the zero-based sibling index of the first element in the collection. * * @returns Index of the first element, or -1 when unavailable */ index(): number; /** * Returns the child nodes of the first element, including text nodes and comments. * * @returns Array of child nodes from the first element */ contents(): ChildNode[]; /** * Gets the offset parent of the first element in the collection. * * @returns Offset parent element, or null when unavailable */ offsetParent(): Element | null; /** * Gets the position of the first element relative to its offset parent. * * @returns Position object with top and left coordinates */ position(): { top: number; left: number; }; /** * Gets the inner width of the first element (content + padding, excluding border). * * @returns Inner width in pixels, or 0 when the collection is empty */ innerWidth(): number; /** * Gets the inner height of the first element (content + padding, excluding border). * * @returns Inner height in pixels, or 0 when the collection is empty */ innerHeight(): number; /** * Gets the outer width of the first element, optionally including margins. * * @param includeMargin - When true, include horizontal margins * @returns Outer width in pixels */ outerWidth(includeMargin?: boolean): number; /** * Gets the outer height of the first element, optionally including margins. * * @param includeMargin - When true, include vertical margins * @returns Outer height in pixels */ outerHeight(includeMargin?: boolean): number; /** * Shows all elements. * * @param display - Optional display value (default: '') * @returns The instance for method chaining */ show(display?: string): this; /** * Hides all elements. * * @returns The instance for method chaining */ hide(): this; /** * Adds an event listener to all elements. * * @param event - Event type * @param handler - Event handler * @returns The instance for method chaining */ on(event: string, handler: EventListenerOrEventListenerObject): this; /** * Adds a one-time event listener to all elements. * * @param event - Event type * @param handler - Event handler * @returns The instance for method chaining */ once(event: string, handler: EventListener): this; /** * Removes an event listener from all elements. * * @param event - Event type * @param handler - The handler to remove * @returns The instance for method chaining */ off(event: string, handler: EventListenerOrEventListenerObject): this; /** * Triggers a custom event on all elements. * * @param event - Event type * @param detail - Optional event detail * @returns The instance for method chaining */ trigger(event: string, detail?: unknown): this; /** * Adds a delegated event listener to all elements. * Events are delegated to matching descendants. * * Use `undelegate()` to remove the listener later. * * @param event - Event type to listen for * @param selector - CSS selector to match against event targets * @param handler - Event handler function * @returns The instance for method chaining * * @example * ```ts * const handler = (e, target) => console.log('Clicked:', target.textContent); * $$('.container').delegate('click', '.item', handler); * * // Later, remove the delegated listener: * $$('.container').undelegate('click', '.item', handler); * ``` */ delegate(event: string, selector: string, handler: (event: Event, target: Element) => void): this; /** * Removes a delegated event listener previously added with `delegate()`. * * @param event - Event type that was registered * @param selector - CSS selector that was used * @param handler - The original handler function passed to delegate() * @returns The instance for method chaining * * @example * ```ts * const handler = (e, target) => console.log('Clicked:', target.textContent); * $$('.container').delegate('click', '.item', handler); * * // Remove the delegated listener: * $$('.container').undelegate('click', '.item', handler); * ``` */ undelegate(event: string, selector: string, handler: (event: Event, target: Element) => void): this; /** * Finds all descendant elements matching the selector across all elements * in the collection. Returns a new BQueryCollection with the results. * * @param selector - CSS selector to match * @returns A new BQueryCollection with all matching descendants * * @example * ```ts * $$('.container').find('.item').addClass('highlight'); * ``` */ find(selector: string): BQueryCollection; /** * Gets the closest element or ancestor matching a selector for each element in * the collection, including the element itself. Duplicates are removed from the * result. * * @param selector - CSS selector to match * @returns A new BQueryCollection with matching elements or ancestors * * @example * ```ts * $$('.item').closest('.container'); * ``` */ closest(selector: string): BQueryCollection; /** * Gets the parent element of each element in the collection. * Duplicates are removed (e.g. siblings sharing a parent). * * @returns A new BQueryCollection with unique parent elements * * @example * ```ts * $$('.item').parent().addClass('has-items'); * ``` */ parent(): BQueryCollection; /** * Gets the direct children of every element in the collection. * Duplicates are removed from the result. * * @returns A new BQueryCollection with child elements * * @example * ```ts * $$('.list').children().addClass('child'); * ``` */ children(): BQueryCollection; /** * Gets all siblings of every element in the collection (excluding the * elements themselves). Duplicates are removed. * * @returns A new BQueryCollection with sibling elements * * @example * ```ts * $$('.active').siblings().removeClass('active'); * ``` */ siblings(): BQueryCollection; /** * Gets the next sibling element of each element in the collection. * Elements without a next sibling are skipped. * * @returns A new BQueryCollection with next sibling elements * * @example * ```ts * $$('.current').next().addClass('upcoming'); * ``` */ next(): BQueryCollection; /** * Gets the previous sibling element of each element in the collection. * Elements without a previous sibling are skipped. * * @returns A new BQueryCollection with previous sibling elements * * @example * ```ts * $$('.current').prev().addClass('previous'); * ``` */ prev(): BQueryCollection; /** * Removes all elements from the DOM. * * @returns The instance for method chaining */ remove(): this; /** * Clears all child nodes from all elements. * * @returns The instance for method chaining */ empty(): this; /** @internal */ private insertAll; } //# sourceMappingURL=collection.d.ts.map