|
| 1 | +import { dom, elementRoles, roles } from 'aria-query'; |
| 2 | + |
| 3 | +import { AXObjects, elementAXObjects } from 'axobject-query'; |
| 4 | + |
| 5 | +import { attributesComparator } from './attributesComparator'; |
| 6 | +import { ElementAst } from '@angular/compiler'; |
| 7 | + |
| 8 | +const domKeys = <string[]>Array.from(dom.keys()); |
| 9 | +const roleKeys: any = <string[]>Array.from(roles.keys()); |
| 10 | +const elementRoleEntries = Array.from(elementRoles); |
| 11 | + |
| 12 | +const nonInteractiveRoles = new Set( |
| 13 | + roleKeys.filter(name => { |
| 14 | + const role = roles.get(name); |
| 15 | + return !role.abstract && !role.superClass.some(classes => classes.indexOf('widget') !== 0); |
| 16 | + }) |
| 17 | +); |
| 18 | + |
| 19 | +const interactiveRoles: any = new Set( |
| 20 | + [ |
| 21 | + ...roleKeys, |
| 22 | + // 'toolbar' does not descend from widget, but it does support |
| 23 | + // aria-activedescendant, thus in practice we treat it as a widget. |
| 24 | + 'toolbar' |
| 25 | + ].filter(name => { |
| 26 | + const role = roles.get(name); |
| 27 | + return !role.abstract && role.superClass.some(classes => classes.indexOf('widget') !== 0); |
| 28 | + }) |
| 29 | +); |
| 30 | + |
| 31 | +const nonInteractiveElementRoleSchemas = elementRoleEntries.reduce((accumulator: any, [elementSchema, roleSet]: any) => { |
| 32 | + if (Array.from(roleSet).every((role): boolean => nonInteractiveRoles.has(role))) { |
| 33 | + accumulator.push(elementSchema); |
| 34 | + } |
| 35 | + return accumulator; |
| 36 | +}, []); |
| 37 | + |
| 38 | +const interactiveElementRoleSchemas = elementRoleEntries.reduce((accumulator: any, [elementSchema, roleSet]: any) => { |
| 39 | + if (Array.from(roleSet).some((role): boolean => interactiveRoles.has(role))) { |
| 40 | + accumulator.push(elementSchema); |
| 41 | + } |
| 42 | + return accumulator; |
| 43 | +}, []); |
| 44 | + |
| 45 | +const interactiveAXObjects = new Set(Array.from(AXObjects.keys()).filter(name => AXObjects.get(name).type === 'widget')); |
| 46 | + |
| 47 | +const interactiveElementAXObjectSchemas = Array.from(elementAXObjects).reduce((accumulator: any, [elementSchema, AXObjectSet]: any) => { |
| 48 | + if (Array.from(AXObjectSet).every((role): boolean => interactiveAXObjects.has(role))) { |
| 49 | + accumulator.push(elementSchema); |
| 50 | + } |
| 51 | + return accumulator; |
| 52 | +}, []); |
| 53 | + |
| 54 | +function checkIsInteractiveElement(el: ElementAst): boolean { |
| 55 | + function elementSchemaMatcher(elementSchema) { |
| 56 | + return el.name === elementSchema.name && attributesComparator(elementSchema.attributes, el); |
| 57 | + } |
| 58 | + // Check in elementRoles for inherent interactive role associations for |
| 59 | + // this element. |
| 60 | + const isInherentInteractiveElement = interactiveElementRoleSchemas.some(elementSchemaMatcher); |
| 61 | + |
| 62 | + if (isInherentInteractiveElement) { |
| 63 | + return true; |
| 64 | + } |
| 65 | + // Check in elementRoles for inherent non-interactive role associations for |
| 66 | + // this element. |
| 67 | + const isInherentNonInteractiveElement = nonInteractiveElementRoleSchemas.some(elementSchemaMatcher); |
| 68 | + |
| 69 | + if (isInherentNonInteractiveElement) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + // Check in elementAXObjects for AX Tree associations for this element. |
| 73 | + const isInteractiveAXElement = interactiveElementAXObjectSchemas.some(elementSchemaMatcher); |
| 74 | + |
| 75 | + if (isInteractiveAXElement) { |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + return false; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Returns boolean indicating whether the given element is |
| 84 | + * interactive on the DOM or not. Usually used when an element |
| 85 | + * has a dynamic handler on it and we need to discern whether or not |
| 86 | + * it's intention is to be interacted with on the DOM. |
| 87 | + */ |
| 88 | +export const isInteractiveElement = (el: ElementAst): boolean => { |
| 89 | + if (domKeys.indexOf(el.name) === -1) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + return checkIsInteractiveElement(el); |
| 94 | +}; |
0 commit comments