Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix accessible name calculation bug related to nested <label>s #1616

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/little-pets-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@siteimprove/alfa-aria": patch
---

**Fixed:** A bug in the accessible name computation where nested labels would cause a crash or incorrect name has been fixed.
8 changes: 4 additions & 4 deletions packages/alfa-aria/src/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ const nameFromLabel = (element: Element, device: Device, state: State) => {
),
);

const names = references.collect((element) =>
const names = references.collect((label) =>
Name.fromNode(
element,
label,
device,
state.reference(element, element).recurse(true).descend(false),
).map((name) => [name, element] as const),
state.reference(element, label).recurse(true).descend(false),
).map((name) => [name, label] as const),
);

const name = names
Expand Down
13 changes: 11 additions & 2 deletions packages/alfa-aria/src/name/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ export class State implements Equatable, Serializable<State.JSON> {

/**
* The element that referenced the name computation.
* (this is the element on which aria-labelledby is set)
* E.g the element on which aria-labelledby is set
* or the <input> element being labelled by a <label> element.
*/
public get referrer(): Option<Element> {
return this._referrer;
}

/**
* The element that is referenced during the name computation.
* (this is the target of the aria-labelledby attribute)
* E.g the target of the aria-labelledby attribute
* or the <label> element that labels an <input> element.
*/
public get referred(): Option<Element> {
return this._referred;
Expand Down Expand Up @@ -126,6 +128,13 @@ export class State implements Equatable, Serializable<State.JSON> {
* @remarks
* We currently have no way to clear references since we currently have no
* use for it.
*
* @param referrer The element that is source of a labelled-by relation,
* e.g. an <input> element or an element with an aria-labelledby attribute.
*
* @param referred The element that is target of a labelled-by relation,
* e.g. a <label> element or an element referenced by an aria-labelled-by attribute
* on some other element.
*/
public reference(referrer: Element, referred: Element): State {
if (
Expand Down
Loading