-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy patharia.ts
More file actions
135 lines (122 loc) · 4.05 KB
/
aria.ts
File metadata and controls
135 lines (122 loc) · 4.05 KB
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
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { create, forEach, StringReplace, StringToLowerCase } from './language';
/**
* According to the following list, there are 48 aria attributes of which two (ariaDropEffect and
* ariaGrabbed) are deprecated:
* https://www.w3.org/TR/wai-aria-1.1/#x6-6-definitions-of-states-and-properties-all-aria-attributes
*
* The above list of 46 aria attributes is consistent with the following resources:
* https://github.com/w3c/aria/pull/708/files#diff-eacf331f0ffc35d4b482f1d15a887d3bR11060
* https://wicg.github.io/aom/spec/aria-reflection.html
*
* NOTE: If you update this list, please update test files that implicitly reference this list!
* Searching the codebase for `aria-flowto` and `ariaFlowTo` should be good enough to find all usages.
*/
const AriaPropertyNames = [
'ariaActiveDescendant',
'ariaAtomic',
'ariaAutoComplete',
'ariaBusy',
'ariaChecked',
'ariaColCount',
'ariaColIndex',
'ariaColIndexText',
'ariaColSpan',
'ariaControls',
'ariaCurrent',
'ariaDescribedBy',
'ariaDescription',
'ariaDetails',
'ariaDisabled',
'ariaErrorMessage',
'ariaExpanded',
'ariaFlowTo',
'ariaHasPopup',
'ariaHidden',
'ariaInvalid',
'ariaKeyShortcuts',
'ariaLabel',
'ariaLabelledBy',
'ariaLevel',
'ariaLive',
'ariaModal',
'ariaMultiLine',
'ariaMultiSelectable',
'ariaOrientation',
'ariaOwns',
'ariaPlaceholder',
'ariaPosInSet',
'ariaPressed',
'ariaReadOnly',
'ariaRelevant',
'ariaRequired',
'ariaRoleDescription',
'ariaRowCount',
'ariaRowIndex',
'ariaRowIndexText',
'ariaRowSpan',
'ariaSelected',
'ariaSetSize',
'ariaSort',
'ariaValueMax',
'ariaValueMin',
'ariaValueNow',
'ariaValueText',
'ariaBrailleLabel',
'ariaBrailleRoleDescription',
'role',
] as const;
type AriaProperty = (typeof AriaPropertyNames)[number];
export type AccessibleElementProperties = {
[Prop in AriaProperty]: string | null;
};
type AriaPropToAttrMap = {
[Prop in AriaProperty]: Prop extends `aria${infer S}` ? `aria-${Lowercase<S>}` : Prop;
};
type AriaAttribute = AriaPropToAttrMap[AriaProperty];
type AriaAttrToPropMap = {
[Prop in AriaProperty as AriaPropToAttrMap[Prop]]: Prop;
};
const { AriaAttrNameToPropNameMap, AriaPropNameToAttrNameMap } = /*@__PURE__*/ (() => {
const AriaAttrNameToPropNameMap: AriaAttrToPropMap = create(null);
const AriaPropNameToAttrNameMap: AriaPropToAttrMap = create(null);
// Synthetic creation of all AOM property descriptors for Custom Elements
forEach.call(AriaPropertyNames, (propName) => {
const attrName = StringToLowerCase.call(
StringReplace.call(propName, /^aria/, () => 'aria-')
) as AriaAttribute;
// These type assertions are because the map types are a 1:1 mapping of ariaX to aria-x.
// TypeScript knows we have one of ariaX | ariaY and one of aria-x | aria-y, and tries to
// prevent us from doing ariaX: aria-y, but we that it's safe.
(AriaAttrNameToPropNameMap[attrName] as AriaProperty) = propName;
(AriaPropNameToAttrNameMap[propName] as AriaAttribute) = attrName;
});
return { AriaAttrNameToPropNameMap, AriaPropNameToAttrNameMap };
})();
/**
*
* @param attrName
*/
export function isAriaAttribute(attrName: string): boolean {
return attrName in AriaAttrNameToPropNameMap;
}
// These attributes take either an ID or a list of IDs as values.
// This includes aria-* attributes as well as the special non-ARIA "for" attribute
export const ID_REFERENCING_ATTRIBUTES_SET: Set<string> = /*@__PURE__*/ new Set([
'aria-activedescendant',
'aria-controls',
'aria-describedby',
'aria-details',
'aria-errormessage',
'aria-flowto',
'aria-labelledby',
'aria-owns',
'for',
'popovertarget',
]);
export { AriaAttrNameToPropNameMap, AriaPropNameToAttrNameMap };