10. Selectors: the inferred heuristic
target, ancestry and xpath are public API (results-selectors.md). The reference's
generator is a heuristic, so we inferred it as a black box and implemented what we inferred.
The code is crates/glance-a11y/src/select/. The probe that produced the evidence is
conformance/selectors/.
10.1 Method (clean room)
The probe makes a random page (gen.mjs, seeded) or loads one of the phase-0 HAR pages. It
plants a probe id on every element, including those in open shadow roots, as an expando, so
the attributes stay unchanged. It then runs the reference with one custom rule, set up only
through the public configure: selector *, excludeHidden: false, and a check that always
passes and reports the element's probe id as its data.
It runs the reference twice: once plain, with ancestry and xpath, and once with absolutePaths. Results are keyed by probe id, and the id itself is left out of the comparison.
glance's side reads the collector's snapshot of the same page load, through a dev-only bundle
(js/dev/select-probe-entry.ts). The selectors come from the dev-only example
select_probe. Both engines read the same load because real pages generate ids at random.
Only outputs were read. To isolate each rule we made one-variable pages (the scratch tools),
for example one href value per page, 7,000 values in all.
10.2 Counting
The counts used to rank features come from the whole flattened tree: the document plus every open shadow root, with slotted children where they render and unslotted light children excluded. They are not per root.
- Evidence: a shadow-root
button[data-name="In"]is chosen even thoughbuttonis unique in its root, because the document has other buttons. The same page with per-root tag counts failsselectors_inferred. - What is counted: each tag (lower-case), each class, and each attribute feature (§10.3).
- Ids are counted per root. "Unique" means exactly one element of the root carries the non-empty id.
The uniqueness checks are DOM queries over every element of the element's own root. That includes unslotted light children (§10.7).
10.3 Features
| Rule | Evidence |
|---|---|
Never used: id, class, style, tabindex, disabled, checked, selected, xmlns, aria-activedescendant, aria-busy, aria-checked, aria-disabled, aria-expanded, aria-grabbed, aria-invalid, aria-pressed, aria-selected, aria-valuenow |
exclusion probe, one attribute per page; button:nth-child(15) over a unique button[disabled=""] (facts/focus) |
Never used: a name containing : (xlink:href) |
exclusion probe |
| Never used: a value longer than 30 UTF-16 code units | data-long at 30 and 31 characters |
| Classes are deduplicated and kept in source order | generated pages |
href and src take a URL suffix with $= (§10.5); every other attribute takes its full value with = |
7,000 isolated values |
A feature's count is the number of elements that have the same feature: the same name, the
same operator and the same value. For $= the value is the suffix, so two URLs with one
suffix count together.
10.4 Steps
One element's step:
- If the id is unique in the root, the step is
#id, and it is never positioned. - Otherwise, take the candidate features: classes, then attributes, each in source order. Keep only those whose count is lower than the tag's count, so a tag that is unique keeps none.
- Sort the kept features by count, stably, so ties stay classes first and then source order. Take at most 3.
- Add features in that order, and stop right after one whose count is 1.
- Write the tag only when no class was chosen. Classes come before attributes
(
.r13.\31 x[for="first"],img[data-role="img"][aria-label="x"]).
Positioned step: the step plus :nth-child(n), added when more than one DOM sibling
(including the element) matches the step.
10.5 URL suffixes
These rules were fitted on 7,000 isolated values. They match 6,998 of them; the two misses are in §10.7.
- A value containing
?, or starting withjavascript:ordata:, has no suffix and takes=. - Split off the fragment at the first
#. - A leading
//or a://marks a host, which runs up to the next/. With a host and no path, the candidate is the host. - Otherwise the candidate is the last path segment, extended segment by segment while it is shorter than 2 characters.
- Add the fragment back to the candidate.
- The candidate is accepted when all of these hold:
- it is at most 25 characters;
- it is at least 3 characters, or exactly 2 with a fragment of
#alone; - without a fragment, fewer than half of its characters are digits;
- without a fragment, its last
.is absent or has at least 2 characters before it.
- If rule 6 fails,
host/is used when the path is exactly/and there is no fragment. The host loses a leadingwww.here only. Sohttps://www.mozilla.org/givesmozilla.org/, andhttps://www.mozilla.orggiveswww.mozilla.org.
Matching also follows from the facts that limit features to 30 characters:
- An attribute value longer than 30 characters does not take part in uniqueness checks at all,
unless it is a URL with a suffix. A 35-character
hrefending#parsingdoes not stopa[href$="#parsing"]being unique, while a long URL endingrss.xmldoes stopa[href$="rss.xml"]. - Feature counts still count only values of 30 characters or fewer (MDN footer:
a[href$="rss.xml"]is chosen over a uniquearia-label).
10.6 Targets and paths
target, per root, evaluated against that root:
- The step, if it alone matches one element.
- Otherwise, the positioned step, if it matches one.
- Otherwise, prefix the positioned steps of DOM ancestors one at a time,
>between them, until the chain is unique. :rootwhen even the chain to the top is not unique. Observed: a shadow root's firstdivwhen another top-leveldivis also:nth-child(1).
For shadow DOM, the host chain is an array, each host computed in its own root.
absolutePaths gives the positioned step of every element from the root's top element down,
joined with >, or :root when that chain is not unique. Ancestors are steps like any
other, for example html > body > a > span:nth-child(1) > x-card[hidden=""]:nth-child(1),
not tags only.
ancestry writes tag:nth-child(n) for each step, but a bare tag for an only child and
always a bare tag for html, head and body. Tags are lower-case (clippath).
xpath takes an id step //tag[@id='v'] when exactly one element of the document carries
the id, whatever the element's own root. Two observations show this:
- a shadow
b#dd2beside a lightp#dd2still gives//b[@id='dd2']; - a shadow
ul#qwhose id the document lacks gives/ul.
Otherwise the steps are as in results-selectors.md, with lower-case tags.
10.7 Agreement
Agreement is measured per element on every element, in four fields: target, ancestry, xpath
and absolute target. The commands are node conformance/selectors/probe.mjs --har <id> and
--seeds a-b [--shape …].
| Set | Elements | target | ancestry | xpath | absolute |
|---|---|---|---|---|---|
| HAR mdn-elements + wiki-ww2 + whatwg-parsing | 34,580 | 100% | 100% | 100% | 100% |
| Rule agents' case pages: r-names 481, r-aria 522, r-struct 668 | 13,736 | 100% | 100% | 100% | 100% |
| Generated, fitting set, seeds 1–200 | 16,998 | 99.75% | 100% | 99.98% | 99.81% |
| Generated, held out, seeds 1001–1300 | 22,261 | 99.73% | 100% | 99.98% | 99.95% |
| Generated, held out, wide (depth 3, width 12), seeds 2001–2150 | 97,341 | 99.96% | 100% | 99.99% | 99.99% |
| Generated, held out, deep (depth 8, width 3), seeds 3001–3150 | 2,189 | 100% | 100% | 100% | 100% |
What is not matched:
- Unslotted light children. Every generated-page difference comes from these. On seeds
1001–1300, all 70 target and absolute differences equal what the model predicts when it
cannot see unslotted light children of shadow hosts.
- The reference's DOM queries, sibling checks and xpath id counts see those children. The collector's snapshot holds only the flattened tree, so glance does not.
- Matching them needs the snapshot to carry the unrendered elements of each root for selectors alone. That is a snapshot-schema change, recorded as a gap and not made here.
- None of the real pages or rule cases is affected.
- URL hosts with a colon. For
//.aA:/and//a:/the reference gives:root. These are protocol-relative URLs whose host contains:. - Frames were not probed here. Frame levels are prefixed by the API layer, and the differential suite's frame cases cover them.
10.8 Speed
Targets are found by narrowing posting lists, never by rescanning the root:
- The index has per-root lists by id, tag, tag plus position, class, exact attribute, and the
last two characters of each value. The last-two-characters list is what
$=checks use. - Each step checks its own posting list with an early exit at the second match.
- For each ancestor prefix, the narrowing either moves the candidates up a level or rebuilds them top-down from the new step's posting list, whichever is shorter. The single-step list is never built if a rarer prefix makes it unnecessary.
- The steps are cached per element.
| Page | Elements | Targets, including index build | per 1,000 |
|---|---|---|---|
| Wikipedia WW2 | 16,918 | 81 ms (index 23 ms) | 4.8 ms |
| WHATWG parsing | 15,024 | 76 ms (index 8 ms) | 5.0 ms |
| MDN elements | 2,638 | 15 ms | 5.5 ms |
These were measured on a release build, single-threaded, while the machine's load average was 11–40. Generated pages take 2.4–3.4 ms per 1,000.