|
| 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; |
0 commit comments