-
Notifications
You must be signed in to change notification settings - Fork 17
[A11Y-12318] Add SIA-R120. #2175
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| "@siteimprove/alfa-rules": minor | ||
| --- | ||
|
|
||
| **Added:** The new rule R120 checks that the access keys an element declares are usable. | ||
|
|
||
| It makes three checks. No other element may declare the same key, since two elements competing for one key means at most one of them can be reached and which one wins is left to the user agent. Each key must be a single character, since anything longer is not a key a user can press. And an element may not declare the same key twice. | ||
|
|
||
| Case is folded throughout, so `A` and `a` are the same key. Two elements declaring them compete, and `accesskey="a A"` on one element declares that key twice and is reported for the repeat. That is stricter than the W3C checker, which compares the tokens of a single value literally and accepts it. | ||
|
|
||
| The attribute holds a set of space-separated tokens, so `accesskey="a b"` declares two keys and each is checked separately. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| import { Diagnostic, Rule } from "@siteimprove/alfa-act"; | ||
| import { Element, Namespace, Node, Query } from "@siteimprove/alfa-dom"; | ||
| import { Predicate } from "@siteimprove/alfa-predicate"; | ||
| import { Err, Ok } from "@siteimprove/alfa-result"; | ||
| import { Sequence } from "@siteimprove/alfa-sequence"; | ||
| import { Style } from "@siteimprove/alfa-style"; | ||
| import type { Page } from "@siteimprove/alfa-web"; | ||
|
|
||
| import { expectation } from "../common/act/index.ts"; | ||
| import { BestPractice } from "../requirements/index.ts"; | ||
|
|
||
| import { Scope, Stability } from "../tags/index.ts"; | ||
|
|
||
| const { hasNamespace } = Element; | ||
| const { and } = Predicate; | ||
| const { isRendered } = Style; | ||
| const { getElementDescendants } = Query; | ||
|
|
||
| /** | ||
| * This rule checks that the access keys an element declares are usable: that no | ||
| * other element declares the same key, that each key is a single character, and | ||
| * that the element does not declare the same key twice. | ||
| * | ||
| * Two elements competing for one key means at most one of them can be reached | ||
| * by it, and which one wins is left to the user agent. | ||
| * | ||
| * Case is folded throughout, since `A` and `a` reach the same physical key, so | ||
| * `accesskey="a A"` declares one key twice and is reported for the repeat. That | ||
| * is stricter than the specification, which asks only that the tokens not be | ||
| * "identical to another token", a literal comparison the W3C checker | ||
| * implements with a case-sensitive equality. | ||
| * | ||
| * Elements that are not rendered are left out, since their access keys cannot | ||
| * be activated, and a key declared only on such an element competes with | ||
| * nothing. | ||
| * | ||
| * {@link https://html.spec.whatwg.org/multipage/interaction.html#the-accesskey-attribute} | ||
| */ | ||
| export default Rule.Atomic.of<Page, Element>({ | ||
| uri: "https://alfa.siteimprove.com/rules/sia-r120", | ||
| requirements: [BestPractice.of("accesskey-unique")], | ||
| tags: [Scope.Page, Stability.Stable], | ||
| evaluate({ device, document }) { | ||
| const elements = getElementDescendants(document, Node.fullTree) | ||
| .filter(and(hasNamespace(Namespace.HTML), declaresAccesskey)) | ||
| .filter(isRendered(device)); | ||
|
|
||
| const accessKeyElementPairs = new Map<string, Array<Element>>(); | ||
|
|
||
| for (const element of elements) { | ||
| for (const key of accesskeys(element)) { | ||
| const elementsForKey = accessKeyElementPairs.get(key) ?? []; | ||
|
|
||
| elementsForKey.push(element); | ||
| accessKeyElementPairs.set(key, elementsForKey); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| applicability() { | ||
| return elements; | ||
| }, | ||
|
|
||
| expectations(target) { | ||
| const contested = accesskeys(target).filter( | ||
| (key) => (accessKeyElementPairs.get(key)?.length ?? 0) > 1, | ||
| ); | ||
| const tooLong = tokens(target).reject((token) => token.length === 1); | ||
|
|
||
| const duplicated = repeatedTokens(target); | ||
|
|
||
| return { | ||
| 1: expectation( | ||
| contested.isEmpty(), | ||
| () => Outcomes.HasUniqueAccesskeys, | ||
| () => Outcomes.HasNonUniqueAccesskeys(contested), | ||
| ), | ||
| 2: expectation( | ||
| tooLong.isEmpty(), | ||
| () => Outcomes.HasSingleCharacterAccesskeys, | ||
| () => Outcomes.HasMultiCharacterAccesskeys(tooLong), | ||
| ), | ||
| 3: expectation( | ||
| duplicated.isEmpty(), | ||
| () => Outcomes.HasDistinctAccesskeys, | ||
| () => Outcomes.HasRepeatedAccesskeys(duplicated), | ||
| ), | ||
| }; | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| function tokens(element: Element): Sequence<string> { | ||
| return Sequence.from(element.attribute("accesskey")).flatMap((attribute) => | ||
| attribute.tokens(), | ||
| ); | ||
| } | ||
|
|
||
| function accesskeys(element: Element): Sequence<string> { | ||
| return tokens(element) | ||
| .map((token) => token.toLowerCase()) | ||
| .distinct(); | ||
| } | ||
|
|
||
| function repeatedTokens(element: Element): Sequence<string> { | ||
| const seen = new Set<string>(); | ||
| const repeated = new Set<string>(); | ||
|
|
||
| for (const token of tokens(element).map((token) => token.toLowerCase())) { | ||
| if (seen.has(token)) { | ||
| repeated.add(token); | ||
| } else { | ||
| seen.add(token); | ||
| } | ||
| } | ||
|
|
||
| return Sequence.from(repeated); | ||
| } | ||
|
|
||
| const declaresAccesskey: Predicate<Element> = (element) => | ||
| !tokens(element).isEmpty(); | ||
|
|
||
| /** | ||
| * @public | ||
| */ | ||
| export namespace Outcomes { | ||
| export const HasUniqueAccesskeys = Ok.of( | ||
| Diagnostic.of( | ||
| `No access key of the element is declared by another element.`, | ||
| ), | ||
| ); | ||
|
|
||
| export const HasNonUniqueAccesskeys = (keys: Iterable<string>) => | ||
| Err.of( | ||
| Diagnostic.of( | ||
| many(keys) | ||
| ? `More than one element declares the access keys ${list(keys)}.` | ||
| : `More than one element declares the access key ${list(keys)}.`, | ||
| ), | ||
| ); | ||
|
|
||
| export const HasSingleCharacterAccesskeys = Ok.of( | ||
| Diagnostic.of(`Every access key of the element is a single character.`), | ||
| ); | ||
|
|
||
| export const HasMultiCharacterAccesskeys = (keys: Iterable<string>) => | ||
| Err.of( | ||
| Diagnostic.of( | ||
| many(keys) | ||
| ? `The access keys ${list(keys)} are not single characters.` | ||
| : `The access key ${list(keys)} is not a single character.`, | ||
| ), | ||
| ); | ||
|
|
||
| export const HasDistinctAccesskeys = Ok.of( | ||
| Diagnostic.of(`The element declares each of its access keys once.`), | ||
| ); | ||
|
|
||
| export const HasRepeatedAccesskeys = (keys: Iterable<string>) => | ||
| Err.of( | ||
| Diagnostic.of( | ||
| many(keys) | ||
| ? `The element declares the access keys ${list(keys)} more than once.` | ||
| : `The element declares the access key ${list(keys)} more than once.`, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| function list(keys: Iterable<string>): string { | ||
| return Sequence.from(keys) | ||
| .map((key) => `"${key}"`) | ||
| .join(", "); | ||
| } | ||
|
|
||
| function many(keys: Iterable<string>): boolean { | ||
| return Sequence.from(keys).size > 1; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot you mentioned "access keys are scoped to their individual Document." and that ", matching keys in a parent document and an
<iframe>are incorrectly reported as competing". Please provide the link to online documentation where it has been mentioned that the keys in a parent document and an<iframe>must be unique with no overlap across.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn’t find a source that explicitly says a parent document and an
<iframe>may or may not reuse the sameaccesskey. The references I was relying on were the HTML Standard’saccesskeysection and the browsing-context/document model:So my earlier note was an inference from those sections, not a directly quoted requirement.