-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrule.ts
60 lines (54 loc) · 1.81 KB
/
rule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Diagnostic, Rule } from "@siteimprove/alfa-act";
import type { Attribute} from "@siteimprove/alfa-dom";
import { Element } from "@siteimprove/alfa-dom";
import { Language } from "@siteimprove/alfa-iana";
import { Predicate } from "@siteimprove/alfa-predicate";
import { Err, Ok } from "@siteimprove/alfa-result";
import { String } from "@siteimprove/alfa-string";
import { Criterion, Technique } from "@siteimprove/alfa-wcag";
import type { Page } from "@siteimprove/alfa-web";
import { expectation } from "../common/act/expectation.js";
import { Scope, Stability } from "../tags/index.js";
const { hasAttribute, isDocumentElement } = Element;
const { not } = Predicate;
export default Rule.Atomic.of<Page, Attribute>({
uri: "https://alfa.siteimprove.com/rules/sia-r5",
requirements: [Criterion.of("3.1.1"), Technique.of("H57")],
tags: [Scope.Page, Stability.Stable],
evaluate({ document }) {
return {
applicability() {
return (
document
.children()
.filter(isDocumentElement)
.filter(hasAttribute("lang", not(String.isWhitespace)))
// The previous filter ensures that lang exists
.map((element) => element.attribute("lang").getUnsafe())
);
},
expectations(target) {
return {
1: expectation(
Language.parse(target.value).isOk(),
() => Outcomes.HasValidLanguage,
() => Outcomes.HasNoValidLanguage,
),
};
},
};
},
});
/**
* @public
*/
export namespace Outcomes {
export const HasValidLanguage = Ok.of(
Diagnostic.of(`The \`lang\` attribute has a valid primary language tag`),
);
export const HasNoValidLanguage = Err.of(
Diagnostic.of(
`The \`lang\` attribute does not have a valid primary language tag`,
),
);
}