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

Implement SIA-R55 #835

Merged
merged 9 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
21 changes: 3 additions & 18 deletions packages/alfa-rules/src/sia-r15/rule.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Rule, Diagnostic } from "@siteimprove/alfa-act";
import { Node } from "@siteimprove/alfa-aria";
import { Element, Namespace } from "@siteimprove/alfa-dom";
import { List } from "@siteimprove/alfa-list";
import { Map } from "@siteimprove/alfa-map";
import { Option } from "@siteimprove/alfa-option";
import { Predicate } from "@siteimprove/alfa-predicate";
import { Err, Ok } from "@siteimprove/alfa-result";
import { Criterion } from "@siteimprove/alfa-wcag";
Expand Down Expand Up @@ -39,21 +36,9 @@ export default Rule.Atomic.of<Page, Group<Element>, Question>({
hasNonEmptyAccessibleName(device)
)
)
.reduce((groups, iframe) => {
const name = Node.from(iframe, device).name.map((name) =>
normalize(name.value)
);

groups = groups.set(
name,
groups
.get(name)
.getOrElse(() => List.empty<Element>())
.append(iframe)
);

return groups;
}, Map.empty<Option<string>, List<Element>>())
.groupBy((iframe) =>
Node.from(iframe, device).name.map((name) => normalize(name.value))
)
.filter((elements) => elements.size > 1)
.map(Group.of)
.values();
Expand Down
59 changes: 20 additions & 39 deletions packages/alfa-rules/src/sia-r41/rule.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { Rule, Diagnostic } from "@siteimprove/alfa-act";
import { Node } from "@siteimprove/alfa-aria";
import { Element, Namespace } from "@siteimprove/alfa-dom";
import { Iterable } from "@siteimprove/alfa-iterable";
import { List } from "@siteimprove/alfa-list";
import { Map } from "@siteimprove/alfa-map";
import { Option } from "@siteimprove/alfa-option";
import { Predicate } from "@siteimprove/alfa-predicate";
import { Err, Ok } from "@siteimprove/alfa-result";
import { Criterion } from "@siteimprove/alfa-wcag";
Expand All @@ -24,7 +20,6 @@ import { normalize } from "../common/normalize";
import { Question } from "../common/question";

const { isElement, hasNamespace } = Element;
const { flatten } = Iterable;
const { and, not } = Predicate;

export default Rule.Atomic.of<Page, Group<Element>, Question>({
Expand All @@ -33,42 +28,28 @@ export default Rule.Atomic.of<Page, Group<Element>, Question>({
evaluate({ device, document, response }) {
return {
applicability() {
return flatten(
document
.descendants({ flattened: true, nested: true })
.filter(isElement)
.filter(
and(
hasNamespace(Namespace.HTML, Namespace.SVG),
hasRole(device, (role) => role.is("link")),
not(isIgnored(device)),
hasNonEmptyAccessibleName(device)
)
return document
.descendants({ flattened: true, nested: true })
.filter(isElement)
.filter(
and(
hasNamespace(Namespace.HTML, Namespace.SVG),
hasRole(device, (role) => role.is("link")),
not(isIgnored(device)),
hasNonEmptyAccessibleName(device)
)
.groupBy((element) => element.root())
.map((elements) =>
elements
.reduce((groups, element) => {
const name = Node.from(element, device).name.map((name) =>
normalize(name.value)
);

groups = groups.set(
name,
groups
.get(name)
.getOrElse(() => List.empty<Element>())
.append(element)
);

return groups;
}, Map.empty<Option<string>, List<Element>>())
.filter((elements) => elements.size > 1)
.map(Group.of)
.values()
)
.groupBy((element) => element.root())
.flatMap((elements) =>
elements.groupBy((element) =>
Node.from(element, device).name.map((name) =>
normalize(name.value)
)
)
.values()
);
)
.filter((elements) => elements.size > 1)
.map(Group.of)
.values();
},

expectations(target) {
Expand Down
99 changes: 99 additions & 0 deletions packages/alfa-rules/src/sia-r55/rule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Diagnostic, Interview, Rule } from "@siteimprove/alfa-act";
import { Node, Role } from "@siteimprove/alfa-aria";
import { Array } from "@siteimprove/alfa-array";
import { Element, Namespace } from "@siteimprove/alfa-dom";
import { Iterable } from "@siteimprove/alfa-iterable";
import { List } from "@siteimprove/alfa-list";
import { Option } from "@siteimprove/alfa-option";
import { Predicate } from "@siteimprove/alfa-predicate";
import { Err, Ok, Result } from "@siteimprove/alfa-result";
import { Page } from "@siteimprove/alfa-web";

import { expectation } from "../common/expectation";
import { Group } from "../common/group";
import { normalize } from "../common/normalize";

import { hasRole, isIgnored } from "../common/predicate";
import { Question } from "../common/question";

const { and, equals, not } = Predicate;
const { hasNamespace } = Element;

export default Rule.Atomic.of<Page, Group<Element>, Question>({
uri: "https://siteimprove.github.io/sanshikan/rules/sia-r55.html",
evaluate({ device, document }) {
return {
applicability() {
return (
document
.descendants({ flattened: true, nested: true })
.filter(Element.isElement)
.filter(
and(
hasNamespace(equals(Namespace.HTML)),
not(isIgnored(device)),
hasRole(device, (role) => role.is("landmark"))
)
)
// We first group by name, under the assumption that duplicated
// names are less frequent than duplicated roles.
.groupBy((landmark) =>
Node.from(landmark, device).name.map((name) =>
normalize(name.value)
)
)
.filter((landmarks) => landmarks.size > 1)
// Next, we group by role.
.flatMap((sameName) =>
sameName
// We have filtered by having a role, and can safely get it.
.groupBy((landmark) => Node.from(landmark, device).role.get())
)
.filter((elements) => elements.size > 1)
.map(Group.of)
.values()
);
},

expectations(target) {
// Empty groups have been filtered out already, so we can safely get the
// first element
const role = Node.from(Iterable.first(target).get(), device).role.get()
.name;

const sameResource = Question.of(
"reference-equivalent-resources",
Jym77 marked this conversation as resolved.
Show resolved Hide resolved
"boolean",
target,
`Do these ${role} landmarks have the same or equivalent content?`
);

return {
1: sameResource.map((same) =>
expectation(
same,
() => Outcomes.SameResource(role),
() => Outcomes.DifferentResources(role)
)
),
};
},
};
},
});

export namespace Outcomes {
export const SameResource = (role: Role.Name) =>
Ok.of(
Diagnostic.of(
`No two \`${role}\` have the same name and different content.`
)
);

export const DifferentResources = (role: Role.Name) =>
Err.of(
Diagnostic.of(
`Some \`${role}\` have the same name and different content.`
)
);
}
45 changes: 9 additions & 36 deletions packages/alfa-rules/src/sia-r56/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { Array } from "@siteimprove/alfa-array";
import { Element, Namespace } from "@siteimprove/alfa-dom";
import { Iterable } from "@siteimprove/alfa-iterable";
import { List } from "@siteimprove/alfa-list";
import { Map } from "@siteimprove/alfa-map";
import { Option } from "@siteimprove/alfa-option";
import { Predicate } from "@siteimprove/alfa-predicate";
import { Err, Ok } from "@siteimprove/alfa-result";
import { Page } from "@siteimprove/alfa-web";
Expand Down Expand Up @@ -34,21 +32,7 @@ export default Rule.Atomic.of<Page, Group<Element>>({
hasRole(device, (role) => role.is("landmark"))
)
)
.reduce((groups, landmark) => {
// Since we already have filtered by having a landmark role, we can
// safely get the role.
const role = Node.from(landmark, device).role.get();

groups = groups.set(
role,
groups
.get(role)
.getOrElse(() => List.empty<Element>())
.append(landmark)
);

return groups;
}, Map.empty<Role, List<Element>>())
.groupBy((landmark) => Node.from(landmark, device).role.get())
.filter((elements) => elements.size > 1)
.map(Group.of)
.values();
Expand All @@ -57,26 +41,15 @@ export default Rule.Atomic.of<Page, Group<Element>>({
expectations(target) {
// Empty groups have been filtered out already, so we can safely get the
// first element
const role = Node.from(
Iterable.first(target).get()!,
device
).role.get().name;

const byNames = [...target]
.reduce((groups, landmark) => {
const name = Node.from(landmark, device).name.map((name) =>
const role = Node.from(Iterable.first(target).get(), device).role.get()
.name;

const byNames = List.from(target)
.groupBy((landmark) =>
Node.from(landmark, device).name.map((name) =>
normalize(name.value)
);
groups = groups.set(
name,
groups
.get(name)
.getOrElse(() => List.empty<Element>())
.append(landmark)
);

return groups;
}, Map.empty<Option<string>, List<Element>>())
)
)
.filter((landmarks) => landmarks.size > 1);

return {
Expand Down
63 changes: 22 additions & 41 deletions packages/alfa-rules/src/sia-r81/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import { Rule, Diagnostic } from "@siteimprove/alfa-act";
import { Node } from "@siteimprove/alfa-aria";
import { Device } from "@siteimprove/alfa-device";
import { Element, Namespace } from "@siteimprove/alfa-dom";
import { Iterable } from "@siteimprove/alfa-iterable";
import { List } from "@siteimprove/alfa-list";
import { Map } from "@siteimprove/alfa-map";
import { Option } from "@siteimprove/alfa-option";
import { Predicate } from "@siteimprove/alfa-predicate";
import { Err, Ok } from "@siteimprove/alfa-result";
import { Set } from "@siteimprove/alfa-set";
Expand All @@ -28,7 +24,6 @@ import { normalize } from "../common/normalize";
import { Question } from "../common/question";

const { isElement, hasName, hasNamespace, hasId } = Element;
const { flatten } = Iterable;
const { and, not, equals } = Predicate;

export default Rule.Atomic.of<Page, Group<Element>, Question>({
Expand All @@ -37,44 +32,30 @@ export default Rule.Atomic.of<Page, Group<Element>, Question>({
evaluate({ device, document, response }) {
return {
applicability() {
return flatten(
document
.descendants({ flattened: true, nested: true })
.filter(isElement)
.filter(
and(
hasNamespace(Namespace.HTML, Namespace.SVG),
hasRole(device, (role) => role.is("link")),
not(isIgnored(device)),
hasNonEmptyAccessibleName(device)
)
)
.groupBy((element) =>
linkContext(element, device).add(element.root())
return document
.descendants({ flattened: true, nested: true })
.filter(isElement)
.filter(
and(
hasNamespace(Namespace.HTML, Namespace.SVG),
hasRole(device, (role) => role.is("link")),
not(isIgnored(device)),
hasNonEmptyAccessibleName(device)
)
.map((elements) =>
elements
.reduce((groups, element) => {
const name = Node.from(element, device).name.map((name) =>
normalize(name.value)
);

groups = groups.set(
name,
groups
.get(name)
.getOrElse(() => List.empty<Element>())
.append(element)
);

return groups;
}, Map.empty<Option<string>, List<Element>>())
.filter((elements) => elements.size > 1)
.map(Group.of)
.values()
)
.groupBy((element) =>
linkContext(element, device).add(element.root())
)
.flatMap((elements) =>
elements.groupBy((element) =>
Node.from(element, device).name.map((name) =>
normalize(name.value)
)
)
.values()
);
)
.filter((elements) => elements.size > 1)
.map(Group.of)
.values();
},

expectations(target) {
Expand Down
Loading