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

Move presentational role conflict resolution down to Role.from #273

Merged
merged 8 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 5 additions & 8 deletions packages/alfa-aria/src/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ export class Feature<N extends string = string> {
}

export namespace Feature {
export type Aspect<T, A extends Array<unknown> = []> = Mapper<
Element,
T,
A
>;
export type Aspect<T, A extends Array<unknown> = []> = Mapper<Element, T, A>;

export interface Status {
readonly obsolete: boolean;
Expand All @@ -71,7 +67,7 @@ export namespace Feature {
/**
* @internal
*/
readonly allowPresentational?: boolean;
readonly allowPresentational: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now realise that this has caused the public API to change in a way that makes it impossible to not touch internal features from the outside as this internal property is now required 🙈

}

const features = Cache.empty<Namespace, Cache<string, Feature>>();
Expand Down Expand Up @@ -309,9 +305,10 @@ Feature.register(

Feature.register(
Namespace.HTML,
Feature.of("img", (element, { allowPresentational = true }) =>
Feature.of("img", (element, { allowPresentational }) =>
Option.of(
allowPresentational && element.attribute("alt").some((alt) => alt.value === "")
allowPresentational &&
element.attribute("alt").some((alt) => alt.value === "")
? "presentation"
: "img"
)
Expand Down
41 changes: 1 addition & 40 deletions packages/alfa-aria/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,23 +233,10 @@ export namespace Node {
} else {
accessibleNode = Role.from(node)
.flatMap((role) => {
// If the element has a presentational role, but is not allowed to
// be presentational, we fall back to its implicit role by not
// considering its explicit role.
if (
role.some(isPresentational) &&
!isAllowedPresentational(node)
) {
return Role.from(node, {
explicit: false,
allowPresentational: false,
});
}

return Branched.of(role);
})
Jym77 marked this conversation as resolved.
Show resolved Hide resolved
.flatMap<Node>((role) => {
if (role.some(isPresentational)) {
if (role.some(Role.isPresentational)) {
return Branched.of(Container.of(node));
}

Expand Down Expand Up @@ -323,29 +310,3 @@ export namespace Node {
});
}
}

const isPresentational: Predicate<Role> = property(
"name",
equals("presentation", "none")
);

/**
* Determine if an element is allowed to be presentational.
*
* @see https://w3c.github.io/aria/#conflict_resolution_presentation_none
*/
const isAllowedPresentational: Predicate<dom.Element> = (element) => {
if (element.tabIndex().isSome()) {
return false;
}

return Role.lookup("roletype").some((role) => {
for (const attribute of role.characteristics.supports) {
if (element.attribute(attribute).isSome()) {
return false;
}
}

return true;
});
};
42 changes: 38 additions & 4 deletions packages/alfa-aria/src/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Feature } from "./feature";
import { Attribute } from "./attribute";

const { some } = Iterable;
const { equals, or } = Predicate;
const { equals, or, not, property, test } = Predicate;

export class Role<N extends string = string> implements Equatable {
public static of<N extends string>(
Expand Down Expand Up @@ -129,6 +129,7 @@ export namespace Role {
/**
* @see https://www.w3.org/TR/wai-aria/#roles_categorization
*/

Jym77 marked this conversation as resolved.
Show resolved Hide resolved
export enum Category {
/**
* @see https://www.w3.org/TR/wai-aria/#abstract_roles
Expand Down Expand Up @@ -233,6 +234,8 @@ export namespace Role {
): Branched<Option<Role>, Browser> {
const role = element.attribute("role").map((attr) => attr.value.trim());

const allowedPresentational = options.allowPresentational ?? isAllowedPresentational(element);

return (
Branched.of<Option<string>, Browser>(
role.map((role) => role.toLowerCase())
Expand All @@ -251,15 +254,22 @@ export namespace Role {
const role = Role.lookup(name);

if (
// If the role is not abstract…
Jym77 marked this conversation as resolved.
Show resolved Hide resolved
role.some(
(role) => role.category !== Role.Category.Abstract
) &&
// …and it's not a presentational role in a forbidden context…
Jym77 marked this conversation as resolved.
Show resolved Hide resolved
!(
role.some(Role.isPresentational) &&
!isAllowedPresentational
)
) {
// …then we got ourselves a valid explicit role…
Jym77 marked this conversation as resolved.
Show resolved Hide resolved
return role;
}
}
}

// …otherwise, default to implicit role computation.
Jym77 marked this conversation as resolved.
Show resolved Hide resolved
return None;
})
.orElse(() => {
Expand All @@ -270,7 +280,8 @@ export namespace Role {
return feature.flatMap((feature) =>
feature
.role(element, {
allowPresentational: options.allowPresentational,
allowPresentational:
allowedPresentational,
})
.flatMap(Role.lookup)
);
Expand All @@ -284,12 +295,14 @@ export namespace Role {
}

export namespace from {
export interface Options extends Feature.RoleOptions {
export interface Options extends Partial<Feature.RoleOptions> {
readonly explicit?: boolean;
readonly implicit?: boolean;
}
}

export const isPresentational = hasName("presentation", "none");

export function hasName(predicate: Predicate<string>): Predicate<Role>;

export function hasName(
Expand All @@ -313,6 +326,27 @@ export namespace Role {
}
}

/**
* Determine if an element is allowed to be presentational.
*
* @see https://w3c.github.io/aria/#conflict_resolution_presentation_none
*/
const isAllowedPresentational: Predicate<Element> = (element) => {
if (element.tabIndex().isSome()) {
return false;
}

return Role.lookup("roletype").some((role) => {
for (const attribute of role.characteristics.supports) {
if (element.attribute(attribute).isSome()) {
return false;
}
}

return true;
});
};

import "./role/separator";

import "./role/abstract/command";
Expand Down