@markuplint/ml-spec 3.0.0-alpha.3 → 3.0.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,237 @@
1
+ import type { ARIAVersion, MLMLSpec } from '../types';
2
+ import type { Category } from '../types/permitted-structres';
3
+
4
+ import { ariaSpecs } from '../specs/aria-specs';
5
+ import { isPresentational } from '../specs/is-presentational';
6
+
7
+ import { getComputedRole } from './get-computed-role';
8
+
9
+ /**
10
+ * Exposable content models and elements
11
+ *
12
+ * **WARNING**:
13
+ * This implementation is through the author's interpretation.
14
+ * Want a issue request.
15
+ * https://github.com/markuplint/markuplint/issues/new
16
+ *
17
+ * @see exposableModels https://html.spec.whatwg.org/multipage/dom.html#content-models
18
+ * @see exposableElementsThatAreNoBelongingAModel https://html.spec.whatwg.org/multipage/indices.html#elements-3
19
+ */
20
+ const exposableModels: Category[] = ['#palpable', '#SVGRenderable'];
21
+ const exposableElementsThatAreNoBelongingAModel: string[] = [
22
+ 'body',
23
+ 'dd',
24
+ 'dt',
25
+ 'figcaption',
26
+ 'html',
27
+ 'legend',
28
+ 'li',
29
+ 'optgroup',
30
+ 'option',
31
+ 'rp',
32
+ 'rt',
33
+ 'summary',
34
+ 'tbody',
35
+ 'td',
36
+ 'tfoot',
37
+ 'th',
38
+ 'thead',
39
+ 'tr',
40
+ ];
41
+
42
+ /**
43
+ * Detect including/excluding from the Accessibility Tree
44
+ *
45
+ * @see https://www.w3.org/TR/wai-aria-1.2/#accessibility_tree
46
+ *
47
+ * @param specs
48
+ * @param el
49
+ * @param version
50
+ */
51
+ export function isExposed(el: Element, specs: Readonly<MLMLSpec>, version: ARIAVersion): boolean {
52
+ // According to WAI-ARIA
53
+ if (isExcluding(el, specs, version)) {
54
+ return false;
55
+ }
56
+
57
+ // According to HTML and SVG Specs with **the author's interpretation**
58
+ {
59
+ const exposableConditions = exposableModels
60
+ .map(model => {
61
+ return specs.def['#contentModels'][model]?.join(',') || '';
62
+ })
63
+ .concat(exposableElementsThatAreNoBelongingAModel.join(','));
64
+ const exposable = exposableConditions.some(condition => {
65
+ return el.matches(condition);
66
+ });
67
+ if (!exposable) {
68
+ return false;
69
+ }
70
+ }
71
+
72
+ // According to WAI-ARIA
73
+ {
74
+ const exposable = isIncluding(el, specs, version);
75
+ if (exposable) {
76
+ return true;
77
+ }
78
+ }
79
+
80
+ // Default
81
+ return true;
82
+ }
83
+
84
+ /**
85
+ * Excluding Elements from the Accessibility Tree
86
+ *
87
+ * @see https://www.w3.org/TR/wai-aria-1.2/#tree_exclusion
88
+ *
89
+ * @param specs
90
+ * @param el
91
+ * @param version
92
+ */
93
+ function isExcluding(el: Element, specs: Readonly<MLMLSpec>, version: ARIAVersion): boolean {
94
+ /**
95
+ * The following elements are not exposed via the accessibility API and
96
+ * user agents MUST NOT include them in the accessibility tree:
97
+ * - Elements, including their descendent elements,
98
+ * that have host language semantics specifying
99
+ * that the element is not displayed, such as CSS display:none,
100
+ * visibility:hidden, or the HTML hidden attribute.
101
+ */
102
+ {
103
+ let currentEl: Element | null = el;
104
+ while (currentEl) {
105
+ if (hasDisplayNodeOrVisibilityHidden(currentEl) || currentEl.hasAttribute('hidden')) {
106
+ return true;
107
+ }
108
+ currentEl = currentEl.parentElement;
109
+ }
110
+ }
111
+
112
+ /**
113
+ * - Elements with none or presentation as the first role in the role attribute.
114
+ * However, their exclusion is conditional. In addition,
115
+ * the element's descendants and text content are generally included.
116
+ * These exceptions and conditions are documented in the presentation (role)
117
+ * section.
118
+ */
119
+ if (isPresentational((el.getAttribute('role') || '').split(/\s+/)[0])) {
120
+ return true;
121
+ }
122
+
123
+ /**
124
+ * If not already excluded from the accessibility tree per the above rules,
125
+ * user agents SHOULD NOT include the following elements
126
+ * in the accessibility tree:
127
+ * - Elements, including their descendants, that have aria-hidden set to true.
128
+ * In other words, aria-hidden="true" on a parent overrides aria-hidden="false"
129
+ * on descendants.
130
+ */
131
+ {
132
+ let currentEl: Element | null = el;
133
+ while (currentEl) {
134
+ if (currentEl.getAttribute('aria-hidden') === 'true') {
135
+ return true;
136
+ }
137
+ currentEl = currentEl.parentElement;
138
+ }
139
+ }
140
+
141
+ /**
142
+ * - Any descendants of elements that have the characteristic
143
+ * "Children Presentational: True" unless the descendant
144
+ * is not allowed to be presentational because it meets one of
145
+ * the conditions for exception described in
146
+ * Presentational Roles Conflict Resolution.
147
+ * However, the text content of any excluded descendants is included.
148
+ */
149
+ {
150
+ let currentEl = el.parentElement;
151
+ while (currentEl) {
152
+ const { role } = getComputedRole(specs, currentEl, version);
153
+ if (role?.childrenPresentational) {
154
+ return true;
155
+ }
156
+ currentEl = currentEl.parentElement;
157
+ }
158
+ }
159
+
160
+ return false;
161
+ }
162
+
163
+ /**
164
+ * Including Elements in the Accessibility Tree
165
+ *
166
+ * If not excluded from or marked as hidden in the accessibility tree
167
+ * per the rules above in Excluding Elements in the Accessibility Tree,
168
+ * user agents MUST provide an accessible object in the accessibility tree
169
+ * for DOM elements that meet any of the following criteria:
170
+ *
171
+ * @see https://www.w3.org/TR/wai-aria-1.2/#tree_inclusion
172
+ *
173
+ * @param specs
174
+ * @param el
175
+ * @param version
176
+ */
177
+ function isIncluding(el: Element, specs: Readonly<MLMLSpec>, version: ARIAVersion): boolean {
178
+ /**
179
+ * > **meet any** of the following criteria:
180
+ */
181
+ const results: boolean[] = [];
182
+
183
+ /**
184
+ * Elements that are not hidden and may fire an accessibility API event,
185
+ * including:
186
+ * - Elements that are currently focused, even if the element or one of
187
+ * its ancestor elements has its aria-hidden attribute set to true.
188
+ */
189
+ // 🚫 Can't detect the element is focused.
190
+ // results.push(true);
191
+
192
+ /**
193
+ * - Elements that are a valid target of an aria-activedescendant attribute.
194
+ */
195
+ // TODO: Compute aria-activedescendant.
196
+ // results.push(true);
197
+
198
+ /**
199
+ * Elements that have an explicit role or a global WAI-ARIA attribute and
200
+ * do not have aria-hidden set to true.
201
+ * (See Excluding Elements in the Accessibility Tree for
202
+ * additional guidance on aria-hidden.)
203
+ */
204
+ if (el.getAttribute('aria-hidden') !== 'true') {
205
+ const globalAria = ariaSpecs(specs, version).props.filter(prop => prop.isGlobal);
206
+ const { role } = getComputedRole(specs, el, version);
207
+ // Has an explicit role
208
+ if (role && !role.isImplicit) {
209
+ results.push(true);
210
+ }
211
+ // Has a global WAI-ARIA attribute
212
+ for (const attr of Array.from(el.attributes)) {
213
+ if (globalAria.some(aria => aria.name === attr.localName)) {
214
+ results.push(true);
215
+ break;
216
+ }
217
+ }
218
+ }
219
+
220
+ /**
221
+ * Elements that are not hidden and have an ID that is referenced
222
+ * by another element via a WAI-ARIA property.
223
+ */
224
+ // TODO: Compute refering ID.
225
+ // results.push(true);
226
+
227
+ return results.includes(true);
228
+ }
229
+
230
+ function hasDisplayNodeOrVisibilityHidden(el: Element) {
231
+ const style = el.getAttribute('style');
232
+ if (!style) {
233
+ return false;
234
+ }
235
+ // TODO: Improve accuracy
236
+ return /display\s*:\s*none|visibility\s*:\s*hidden/gi.test(style);
237
+ }
package/src/index.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  export * from './dom-traverse/accname-computation';
2
2
  export * from './dom-traverse/get-attr-specs';
3
+ export * from './dom-traverse/get-computed-aria-props';
3
4
  export * from './dom-traverse/get-computed-role';
4
5
  export * from './dom-traverse/get-content-model';
5
6
  export * from './dom-traverse/get-implicit-role';
6
7
  export * from './dom-traverse/get-permitted-roles';
7
8
  export * from './dom-traverse/get-spec';
8
9
  export * from './dom-traverse/has-required-owned-elements';
10
+ export * from './dom-traverse/is-exposed';
9
11
  export * from './dom-traverse/may-be-focusable';
10
12
  export * from './specs/aria-specs';
11
13
  export * from './specs/content-model-category-to-tag-names';
@@ -43,8 +43,7 @@ export type Category =
43
43
  | '#SVGStructural'
44
44
  | '#SVGStructurallyExternal'
45
45
  | '#SVGTextContent'
46
- | '#SVGTextContentChild'
47
- | '#SVGOtherXMLNamespace';
46
+ | '#SVGTextContentChild';
48
47
 
49
48
  export interface ContentModelsSchema {
50
49
  __contentModel?: ContentModel;
@@ -43,8 +43,7 @@ export type Category =
43
43
  | '#SVGStructural'
44
44
  | '#SVGStructurallyExternal'
45
45
  | '#SVGTextContent'
46
- | '#SVGTextContentChild'
47
- | '#SVGOtherXMLNamespace';
46
+ | '#SVGTextContentChild';
48
47
 
49
48
  export interface ContentModelsSchema {
50
49
  __contentModel?: ContentModel;