-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SIA-R111 and SIA-R113 applicability improvements (#1594)
* Update applicability to reject paragraph subtrees * Add function for getting all targets needed for spacing calculation * Use generator function * Add cache again * Add changeset * Update slimy-socks-cry.md * Update .changeset/slimy-socks-cry.md Co-authored-by: Jean-Yves Moyen <jym@siteimprove.com> * Factor target predicate * Add tests --------- Co-authored-by: Jean-Yves Moyen <jym@siteimprove.com>
- Loading branch information
1 parent
ffcacbc
commit 62e778e
Showing
5 changed files
with
206 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
"@siteimprove/alfa-rules": patch | ||
--- | ||
|
||
**Fixed:** R111 and R113 are no longer applicable to invisible targets and targets inside a paragraph |
93 changes: 76 additions & 17 deletions
93
packages/alfa-rules/src/common/applicability/targets-of-pointer-events.ts
This file contains 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 |
---|---|---|
@@ -1,38 +1,97 @@ | ||
import { DOM } from "@siteimprove/alfa-aria"; | ||
import { Cache } from "@siteimprove/alfa-cache"; | ||
import { Device } from "@siteimprove/alfa-device"; | ||
import { Document, Element, Node, Query } from "@siteimprove/alfa-dom"; | ||
import { Document, Element, Node } from "@siteimprove/alfa-dom"; | ||
import { Predicate } from "@siteimprove/alfa-predicate"; | ||
import { Sequence } from "@siteimprove/alfa-sequence"; | ||
import { Style } from "@siteimprove/alfa-style"; | ||
import { Query } from "@siteimprove/alfa-dom"; | ||
|
||
const { hasRole } = DOM; | ||
const { hasComputedStyle, isFocusable } = Style; | ||
const { hasComputedStyle, isFocusable, isVisible } = Style; | ||
|
||
const { and, not } = Predicate; | ||
|
||
const { and } = Predicate; | ||
const { getElementDescendants } = Query; | ||
|
||
const cache = Cache.empty<Document, Cache<Device, Sequence<Element>>>(); | ||
const applicabilityCache = Cache.empty< | ||
Document, | ||
Cache<Device, Sequence<Element>> | ||
>(); | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function targetsOfPointerEvents( | ||
export function applicableTargetsOfPointerEvents( | ||
document: Document, | ||
device: Device, | ||
): Sequence<Element> { | ||
return cache.get(document, Cache.empty).get(device, () => | ||
getElementDescendants(document, Node.fullTree).filter( | ||
and( | ||
hasComputedStyle( | ||
"pointer-events", | ||
(keyword) => keyword.value !== "none", | ||
device, | ||
), | ||
isFocusable(device), | ||
hasRole(device, (role) => role.isWidget()), | ||
(target) => target.getBoundingBox(device).isSome(), | ||
), | ||
return applicabilityCache.get(document, Cache.empty).get(device, () => { | ||
const isParagraph = hasRole(device, "paragraph"); | ||
const isArea = (element: Element) => element.name === "area"; | ||
|
||
function* visit(node: Node): Iterable<Element> { | ||
if (Element.isElement(node)) { | ||
if (isParagraph(node)) { | ||
// If we encounter a paragraph, we can skip the entire subtree | ||
return; | ||
} | ||
|
||
// TODO: It's not enough to reject paragraphs, we need to reject all text blocks in order to avoid false positives | ||
|
||
if (and(isTarget(device), not(isArea))(node)) { | ||
yield node; | ||
} | ||
} | ||
|
||
for (const child of node.children(Node.fullTree)) { | ||
yield* visit(child); | ||
} | ||
} | ||
|
||
return Sequence.from(visit(document)); | ||
}); | ||
} | ||
|
||
const allTargetsCache = Cache.empty< | ||
Document, | ||
Cache<Device, Sequence<Element>> | ||
>(); | ||
|
||
/** | ||
* @internal | ||
* | ||
* @privateRemarks | ||
* This function is not used in the applicability of R111 or R113, | ||
* but in the expectation of R113 since all other targets are needed | ||
* to determine if an applicable target is underspaced. | ||
* It's kept here since it's closely related to the applicability. | ||
*/ | ||
export function allTargetsOfPointerEvents( | ||
document: Document, | ||
device: Device, | ||
): Sequence<Element> { | ||
return allTargetsCache | ||
.get(document, Cache.empty) | ||
.get(device, () => | ||
getElementDescendants(document, Node.fullTree).filter(isTarget(device)), | ||
); | ||
} | ||
|
||
function isTarget(device: Device): Predicate<Element> { | ||
return and( | ||
hasComputedStyle( | ||
"pointer-events", | ||
(keyword) => keyword.value !== "none", | ||
device, | ||
), | ||
isFocusable(device), | ||
isVisible(device), | ||
hasRole(device, (role) => role.isWidget()), | ||
hasBoundingBox(device), | ||
); | ||
} | ||
|
||
function hasBoundingBox(device: Device): Predicate<Element> { | ||
return (element) => element.getBoundingBox(device).isSome(); | ||
} |
This file contains 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 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 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