Internal Utility Helpers
Relevant source files
The following files were used as context for generating this wiki page:
Internal utility helpers in cheerio-select provide the foundational logic for parsing, grouping, and filtering CSS selectors, specifically bridging the gap between standard CSS selectors and jQuery-style positional pseudo-selectors. These helpers facilitate the identification of advanced selectors that require specialized handling beyond the standard css-select implementation.
The primary scope of these helpers includes categorizing selectors into "plain" (standard CSS) and "filtered" (positional) groups, determining result limits based on pseudo-classes like :first, and resolving document roots for traversal-based queries.
Sources: src/index.ts, src/helpers.ts
Selector Categorization and Grouping
The system distinguishes between standard CSS selectors and those containing positional filters (e.g., :eq, :first, :lt). This categorization is essential because positional selectors operate on the result set of a query rather than individual element attributes or tree relationships.
Selector Grouping Logic
The groupSelectors function processes an array of parsed selectors (from css-what) and splits them into two distinct arrays.
- Plain Selectors: Selectors that can be handled by standard CSS engines.
- Filtered Selectors: Selectors containing at least one positional filter that necessitates post-processing.
The logic ensures that even complex selectors, such as those using :not() containing filters, are correctly identified as filtered selectors.
Sources: src/helpers.ts:18-42, src/helpers.spec.ts:6-25
Key Functions for Selector Management
| Function | Description |
|---|---|
groupSelectors | Splits a Selector[][] into [plain, filtered] groups based on the presence of positional pseudos. |
isFilter | A type guard used to check if a selector component is a positional filter. |
getDocumentRoot | Traverses upward from a node to find the top-level Document or the furthest ancestor. |
Sources: src/helpers.ts:18, src/index.ts:25, src/positionals.ts:47
Positional Logic and Result Capping
Positional helpers manage how many elements are retrieved from the DOM based on the specific requirements of the selector. This optimization prevents unnecessary traversal when only a subset of matches is required.
Result Limits
The getLimit helper calculates the maximum number of elements needed to satisfy a positional query. For example, :first or :eq(0) only requires a limit of 1, whereas :lt(5) requires 5.
Sources: src/positionals.ts:61-82
Filter Identification
The isFilter function identifies specific jQuery-style pseudos that trigger positional logic.
| Filter Name | Data Type | Description |
|---|---|---|
first, last | null | Selects boundary elements of the set. |
eq, nth | string (index) | Selects element at a specific index. |
gt, lt | string (index) | Selects elements greater than or less than an index. |
even, odd | null | Selects elements based on parity. |
not | Selector[][] | Excludes elements matching a sub-selector. |
Sources: src/positionals.ts:16-43
Context and Root Resolution
For selectors involving traversals (like + or ~) that appear at the start of a selector or after a positional, the internal helpers must resolve the correct context.
Document Rooting
The getDocumentRoot helper is critical for "traversal-first" queries. It ensures that the selector engine has a reference point to search from when the user provides an element rather than a full document.
export function getDocumentRoot(node: AnyNode): Document {
while (node.parent) node = node.parent;
return node as Document;
}
Sources: src/helpers.ts:10-13
Test Environment Utilities
The project utilizes specialized helpers in its test suite to simulate browser-like document methods (getElementById, createElement) on top of the domhandler structures. This allows the selector engine to be verified against the standard Sizzle/jQuery test fixtures.
Sources: test/tools/sizzle-testinit.ts:22-40
Summary
Internal Utility Helpers form the backbone of cheerio-select by extending standard CSS selection with jQuery-style positional capabilities. Through precise selector grouping in helpers.ts and limit calculation in positionals.ts, the system optimizes performance by only retrieving the minimum necessary DOM elements and correctly routing selectors to either the standard css-select engine or the custom positional filtering logic.