Skip to content

fix(record): resolve aria-labelledby inside the target's own tree - #774

Open
openviking-superagent wants to merge 1 commit into
Tencent:mainfrom
openviking-superagent:fix/646-shadow-aria-labelledby-scope
Open

openviking-superagent wants to merge 1 commit into
Tencent:mainfrom
openviking-superagent:fix/646-shadow-aria-labelledby-scope

Conversation

@openviking-superagent

Copy link
Copy Markdown

Fixes #646.

A Shadow DOM button labelled by a sibling in the same shadow root lost its name during recording. labelledByText() resolved every aria-labelledby IDREF with document.getElementById, so an inner label was invisible, and when the outer page happened to use the same id the descriptor took the unrelated outer text. Chromium's AX tree reports the local label in both cases.

What changed

  • labelledByText() now resolves each IDREF in el.getRootNode() (the element's own tree, as ARIA does) and falls back to the outer document only when that tree has no such id. This is duck-typed on getElementById rather than instanceof Document/ShadowRoot, which also keeps it working in test environments where the global constructors differ from the document's.
  • No change to the accessible-name preference order or to any other descriptor field.

Verification

New cases in src/lib/__tests__/describe-target.test.ts:

  • a shadow button labelled inside its root records name: "Save" (previously: no step at all);
  • with a same-id element in the outer document, the shadow label still wins (previously: Delete account);
  • the light-DOM path is unchanged.

vitest run src/lib/__tests__/describe-target.test.ts — 15/15. tsc --noEmit and biome check pass on both files.

labelledByText was the reported path; associatedLabelText() has the same document-scope assumption for label[for] inside a shadow root and is left for a separate change so this stays reviewable.

A Shadow DOM button that labels itself with a sibling in the same shadow
root lost its accessible name: labelledByText looked the id up on
document, so the inner label was invisible, and when the outer page used
the same id the descriptor took the unrelated outer text.

Resolve IDREFs in el.getRootNode() first and fall back to the outer
document only when the element's own tree has no such id. Add regression
tests for both reported scenarios plus the light-DOM path.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

录制解析 Shadow DOM 按钮的 aria-labelledby 时错误使用 document 作用域

1 participant