@markuplint/ml-spec 3.0.0-alpha.4 → 3.0.0-alpha.6

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