Skip to content

Commit 74dc2c9

Browse files
committed
Added ariadescribedbyText
1 parent ac57923 commit 74dc2c9

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Get the accessible name based on aria-describedby
3+
*
4+
* @deprecated Do not use Element directly. Pass VirtualNode instead
5+
* @param {VirtualNode|Element} element
6+
* @param {Object} context
7+
* @property {Bool} inLabelledByContext Whether or not the lookup is part of aria-describedby reference
8+
* @property {Bool} inControlContext Whether or not the lookup is part of a native label reference
9+
* @property {Element} startNode First node in accessible name computation
10+
* @property {Bool} debug Enable logging for formControlValue
11+
* @return {string} Concatenated text value for referenced elements
12+
*/
13+
function ariadescribedbyText(element, context = {}) {
14+
const { vNode } = axe.utils.nodeLookup(element);
15+
if (vNode?.props.nodeType !== 1) {
16+
return '';
17+
}
18+
19+
if (
20+
vNode.props.nodeType !== 1 ||
21+
context.inLabelledByContext ||
22+
context.inControlContext ||
23+
!vNode.attr('aria-describedby')
24+
) {
25+
return '';
26+
}
27+
28+
const refs = axe.commons.dom
29+
.idrefs(vNode, 'aria-describedby')
30+
.filter(elm => elm);
31+
return refs.reduce((accessibleName, elm) => {
32+
const accessibleNameAdd = axe.commons.text.accessibleText(elm, {
33+
// Prevent the infinite reference loop:
34+
inLabelledByContext: true,
35+
startNode: context.startNode || vNode,
36+
...context
37+
});
38+
39+
if (!accessibleName) {
40+
return accessibleNameAdd;
41+
}
42+
return `${accessibleName} ${accessibleNameAdd}`;
43+
}, '');
44+
}
45+
46+
export default ariadescribedbyText;

lib/commons/aria/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
export { default as allowedAttr } from './allowed-attr';
77
export { default as arialabelText } from './arialabel-text';
88
export { default as arialabelledbyText } from './arialabelledby-text';
9+
export { default as ariadescribedbyText } from './ariadescribedby-text';
910
export { default as getAccessibleRefs } from './get-accessible-refs';
1011
export { default as getElementUnallowedRoles } from './get-element-unallowed-roles';
1112
export { default as getExplicitRole } from './get-explicit-role';

0 commit comments

Comments
 (0)