-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
193 lines (177 loc) · 5.33 KB
/
index.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import informations from './data/data.js';
const versionMappings = {
'2.0': 'wcag20',
2.1: 'wcag21',
2.2: 'wcag22',
};
/**
* Returns all the available data for the specified bullet point or throws
* an error if the given bullet point doesn't exist.
*
* @throws When the success criterion not exist.
* @param {'2.0' | '2.1' | '2.2'} version
* @param {number} chapter
* @param {number} section
* @param {number} subsection
* @returns {Object | Error} All available data for the specified criterion
*
* @example
* ```
* import { getCriterionData } from 'wcag-reference';
*
* const data = getCriterionData('2.1', 2, 1, 1);
* // → {
* // id: 'keyboard',
* // handle: '2.1.1 Keyboard',
* // quickReference: 'https://www.w3.org/WAI/WCAG21/quickref/#keyboard',
* // detailedReference: 'https://www.w3.org/WAI/WCAG21/Understanding/keyboard.html',
* // level: 1, // → 1: A, 2: AA, 3: AAA
* // wcagUrl: 'https://www.w3.org/TR/WCAG21/'
* // }
* ```
*/
export function getCriterionData(version, chapter, section, subsection) {
if (!versionMappings[version]) {
throw new Error("Requested WCAG Version isn't valid!");
}
if (!informations[versionMappings[version]].informations[chapter]) {
throw new Error("Requested chapter doesn't exist!");
}
if (
!informations[versionMappings[version]].informations[chapter]
.guidelines[section]
) {
throw new Error("Requested section doesn't exist!");
}
if (
!informations[versionMappings[version]].informations[chapter]
.guidelines[section].successCriterions[subsection]
) {
throw new Error("Requested subsection doesn't exist!");
}
return Object.assign(
informations[versionMappings[version]].informations[chapter].guidelines[
section
].successCriterions[subsection],
{ wcagUrl: informations[versionMappings[version]].informations.url }
);
}
/**
* Returns a link with an anchor to the specified bullet point or throws
* an error if the given bullet point doesn't exist.
*
* @throws When the success criterion not exist.
* @param {'2.0' | '2.1' | '2.2'} version
* @param {number} chapter
* @param {number} section
* @param {number} subsection
* @returns {string} Link with an anchor pointing to the criterion
*
* @example
* ```
* import { getLinkToCriterion } from 'wcag-reference';
*
* const link = getLinkToCriterion('2.2', 3, 3, 4);
* // → 'https://www.w3.org/TR/WCAG22/#error-prevention-legal-financial-data'
* ```
*/
export function getLinkToCriterion(version, chapter, section, subsection) {
const criterionData = getCriterionData(
version,
chapter,
section,
subsection
);
return criterionData.wcagUrl + '#' + criterionData.id;
}
/**
* Returns all the available data for the specified technique or throws an
* error if the given technique doesn't exist.
*
* @throws When the technique not exist.
* @param {'2' | '2.1' | '2.2'} version
* @param {string} technique
* @returns {Object | Error} All available data for the specified technique
*
* @example
* ```
* import { getTechniqueData } from 'wcag-reference';
*
* const data21 = getTechniqueData('2.1', 'G57');
* // → {
* // text: 'G57: Ordering the content in a meaningful sequence',
* // techniquesUrl: 'https://www.w3.org/WAI/WCAG21/Techniques/',
* // groupId: 'general',
* // groupPage: undefined
* // }
* const data20 = getTechniqueData('2.0', 'G57');
* // → {
* // text: 'G57: Ordering the content in a meaningful sequence',
* // techniquesUrl: 'https://www.w3.org/TR/WCAG20-TECHS/',
* // groupId: undefined,
* // groupPage: 'general.html'
* // }
* ```
*/
export function getTechniqueData(version, technique) {
if (!versionMappings[version]) {
throw new Error("Requested WCAG Version isn't valid!");
}
// get the technique prefix ex. 'ARIA12' → 'ARIA'
const prefix = technique.replace(/\d*/g, '');
// checks if the requested technique exists
if (
!informations[versionMappings[version]].techniques[prefix] ||
!informations[versionMappings[version]].techniques[prefix].techniques[
technique
]
) {
throw new Error("Requested WCAG technique doesn't exist!");
}
return Object.assign(
informations[versionMappings[version]].techniques[prefix].techniques[
technique
],
{
techniquesUrl:
informations[versionMappings[version]].techniques.url,
groupId:
informations[versionMappings[version]].techniques[prefix].id,
groupPage:
informations[versionMappings[version]].techniques[prefix]
.onePage,
}
);
}
/**
* Returns a link with an anchor to the specified technique or throws
* an error if the given technique doesn't exist.
*
* @throws When the technique not exist.
* @param {'2' | '2.1' | '2.2'} version
* @param {string} technique
* @returns {string | Error} Link with an anchor pointing to the technique
*
* @example
* ```
* import { getLinkToTechnique } from 'wcag-reference';
*
* const link = getLinkToTechnique('2.0', 'SCR27');
* // → 'https://www.w3.org/TR/WCAG20-TECHS/SCR27.html'
* ```
*/
export function getLinkToTechnique(version, technique) {
const techniqueData = getTechniqueData(version, technique);
let section = '';
// we need the right path for the technique group for 2.1 & 2.2
if (version !== '2.0') {
section = techniqueData.groupId + '/';
}
return (
techniqueData.techniquesUrl +
section +
// we don't need ".html" for 2.1 & 2.2 but it works so no switch needed
technique +
'.html'
);
}