|
1 | | -import { uniqueArray, closest, isHtmlElement } from '../../core/utils'; |
| 1 | +import { uniqueArray, isHtmlElement } from '../../core/utils'; |
2 | 2 | import { getRole, allowedAttr, validateAttr } from '../../commons/aria'; |
3 | 3 | import { isFocusable } from '../../commons/dom'; |
4 | | -import cache from '../../core/base/cache'; |
5 | 4 |
|
6 | 5 | /** |
7 | 6 | * Check if each ARIA attribute on an element is allowed for its semantic role. |
@@ -30,62 +29,31 @@ import cache from '../../core/base/cache'; |
30 | 29 | export default function ariaAllowedAttrEvaluate(node, options, virtualNode) { |
31 | 30 | const invalid = []; |
32 | 31 | const role = getRole(virtualNode); |
33 | | - const attrs = virtualNode.attrNames; |
34 | 32 | let allowed = allowedAttr(role); |
| 33 | + |
35 | 34 | // @deprecated: allowed attr options to pass more attrs. |
36 | 35 | // configure the standards spec instead |
37 | 36 | if (Array.isArray(options[role])) { |
38 | 37 | allowed = uniqueArray(options[role].concat(allowed)); |
39 | 38 | } |
40 | 39 |
|
41 | | - const tableMap = cache.get('aria-allowed-attr-table', () => new WeakMap()); |
42 | | - |
43 | | - function validateRowAttrs() { |
44 | | - // check if the parent exists otherwise a TypeError will occur (virtual-nodes specifically) |
45 | | - if (virtualNode.parent && role === 'row') { |
46 | | - const table = closest( |
47 | | - virtualNode, |
48 | | - 'table, [role="treegrid"], [role="table"], [role="grid"]' |
49 | | - ); |
50 | | - |
51 | | - let tableRole = tableMap.get(table); |
52 | | - if (table && !tableRole) { |
53 | | - tableRole = getRole(table); |
54 | | - tableMap.set(table, tableRole); |
55 | | - } |
56 | | - if (['table', 'grid'].includes(tableRole) && role === 'row') { |
57 | | - return true; |
58 | | - } |
59 | | - } |
60 | | - } |
61 | | - // Allows options to be mapped to object e.g. {'aria-level' : validateRowAttrs} |
62 | | - const ariaAttr = Array.isArray(options.validTreeRowAttrs) |
63 | | - ? options.validTreeRowAttrs |
64 | | - : []; |
65 | | - const preChecks = {}; |
66 | | - ariaAttr.forEach(attr => { |
67 | | - preChecks[attr] = validateRowAttrs; |
68 | | - }); |
69 | | - if (allowed) { |
70 | | - for (let i = 0; i < attrs.length; i++) { |
71 | | - const attrName = attrs[i]; |
72 | | - if (validateAttr(attrName) && preChecks[attrName]?.()) { |
73 | | - invalid.push(attrName + '="' + virtualNode.attr(attrName) + '"'); |
74 | | - } else if (validateAttr(attrName) && !allowed.includes(attrName)) { |
75 | | - invalid.push(attrName + '="' + virtualNode.attr(attrName) + '"'); |
76 | | - } |
| 40 | + // Unknown ARIA attributes are tested in aria-valid-attr |
| 41 | + for (const attrName of virtualNode.attrNames) { |
| 42 | + if (validateAttr(attrName) && !allowed.includes(attrName)) { |
| 43 | + invalid.push(attrName); |
77 | 44 | } |
78 | 45 | } |
79 | 46 |
|
80 | | - if (invalid.length) { |
81 | | - this.data(invalid); |
| 47 | + if (!invalid.length) { |
| 48 | + return true; |
| 49 | + } |
82 | 50 |
|
83 | | - if (!isHtmlElement(virtualNode) && !role && !isFocusable(virtualNode)) { |
84 | | - return undefined; |
85 | | - } |
| 51 | + this.data( |
| 52 | + invalid.map(attrName => attrName + '="' + virtualNode.attr(attrName) + '"') |
| 53 | + ); |
86 | 54 |
|
87 | | - return false; |
| 55 | + if (!role && !isHtmlElement(virtualNode) && !isFocusable(virtualNode)) { |
| 56 | + return undefined; |
88 | 57 | } |
89 | | - |
90 | | - return true; |
| 58 | + return false; |
91 | 59 | } |
0 commit comments