# why
selector interpretation was spread across the locator resolver, iframe
routing, & snapshot scoping, with duplicated XPath tokenization. this PR
centralizes that logic so future query engines can build on one internal
boundary while preserving existing selector behavior
# what changed
- moved CSS, XPath, & `text=` interpretation and iframe-hop planning
into `selectorQuery.ts`, a dependency-free module with typed internal
queries
- updated existing resolver entry points to delegate to it, and shared
XPath tokenization with snapshot scoping while retaining their different
frame-entry rules.
- added shared characterization fixtures for prefixes, quoting, `>>`
iframe hops, generated XPath, and positional selection. tests preserve
existing quirks, including case-sensitive XPath frame routing &
splitting `>>` inside quotes
# test plan
added `selectorNormalization.test.ts` & `selectorQuery.test.ts`, using
shared fixtures to check the pure helpers & existing resolver entry
points against the same expected behavior:
- CSS, XPath, and text prefixes preserve case handling, whitespace,
quoting, slash literals, and existing treatment of empty or malformed
inputs.
- `>>` and generated XPath plan the expected nested iframe hops and
final selector; a trailing iframe remains an element target for
locators.
- Existing routing quirks stay unchanged, including `>>` inside quoted
strings and the different handling of uppercase XPath prefixes and
parenthesized XPath.
- `first()` and `nth()` select within the final frame without changing
indices embedded in selectors or mutating the original locator. Internal
fractional indices are floored, and negative/non-finite indices are
rejected.
- Shared XPath helpers preserve axes, predicates, and reconstructed
paths through both the query module and snapshot compatibility exports.
<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Centralizes selector interpretation into a new dependency-free
`selectorQuery.ts` module so the locator resolver, iframe routing, and
snapshot scoping share one internal boundary instead of duplicating
parsing logic.
- Moves CSS, XPath, and `text=` parsing, iframe-hop planning, and XPath
tokenization into `selectorQuery.ts`; existing entry points now delegate
to it.
- Preserves existing selector behavior, including quirks like
case-sensitive XPath frame routing and splitting `>>` inside quoted
strings.
- Adds shared characterization fixtures plus
`selectorNormalization.test.ts` and `selectorQuery.test.ts` covering
prefixes, quoting, iframe hops, generated XPath, and positional
selection.
<sup>Written for commit dcb1eb17332e5cc282557bd8cd9a579a11032535.
Summary will update on new commits.</sup>
<a
href="https://cubic.dev/pr/browserbase/stagehand/pull/3529?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="View guided diff"
src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<a
href="https://www.cubic.dev/action/auto-fix/pr/browserbase/stagehand/3529?returnTo=https%3A%2F%2Fgithub.com%2Fbrowserbase%2Fstagehand%2Fpull%2F3529&source=description"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"><img
alt="Turn on auto-fix"
src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->