-
Notifications
You must be signed in to change notification settings - Fork 776
/
subtree-text.js
82 lines (74 loc) · 2.83 KB
/
subtree-text.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import accessibleTextVirtual from './accessible-text-virtual';
import namedFromContents from '../aria/named-from-contents';
import getOwnedVirtual from '../aria/get-owned-virtual';
import getRole from '../aria/get-role';
import getElementsByContentType from '../standards/get-elements-by-content-type';
import getElementSpec from '../standards/get-element-spec';
import { controlValueRoles } from './form-control-value';
/**
* Get the accessible text for an element that can get its name from content
*
* @param {VirtualNode} element
* @param {Object} context
* @property {Bool} strict Should the name computation strictly follow AccName 1.1
* @return {String} Accessible text
*/
function subtreeText(virtualNode, context = {}) {
const { alreadyProcessed } = accessibleTextVirtual;
context.startNode = context.startNode || virtualNode;
const { strict, inControlContext, inLabelledByContext } = context;
const role = getRole(virtualNode);
const { contentTypes } = getElementSpec(virtualNode, {
noMatchAccessibleName: true
});
if (
alreadyProcessed(virtualNode, context) ||
virtualNode.props.nodeType !== 1 ||
contentTypes?.includes('embedded') || // canvas, video, etc
controlValueRoles.includes(role)
) {
return '';
}
if (
!context.subtreeDescendant &&
!context.inLabelledByContext &&
!namedFromContents(virtualNode, { strict })
) {
return '';
}
/**
* Note: Strictly speaking if a child isn't named from content and it has no accessible name
* accName says to ignore it. Browsers do this fairly consistently, but screen readers have
* chosen to ignore this, but only for direct content, not for labels / aria-labelledby.
* That way in `a[href] > article > #text` the text is used for the accessible name,
* See: https://github.com/dequelabs/axe-core/issues/1461
* See: https://github.com/w3c/accname/issues/120
*/
if (!strict) {
const subtreeDescendant = !inControlContext && !inLabelledByContext;
context = { subtreeDescendant, ...context };
}
return getOwnedVirtual(virtualNode).reduce((contentText, child) => {
return appendAccessibleText(contentText, child, context);
}, '');
}
const phrasingElements = getElementsByContentType('phrasing').concat(['#text']);
function appendAccessibleText(contentText, virtualNode, context) {
const nodeName = virtualNode.props.nodeName;
let contentTextAdd = accessibleTextVirtual(virtualNode, context);
if (!contentTextAdd) {
return contentText;
}
if (!phrasingElements.includes(nodeName)) {
// Append space, if necessary
if (contentTextAdd[0] !== ' ') {
contentTextAdd += ' ';
}
// Prepend space, if necessary
if (contentText && contentText[contentText.length - 1] !== ' ') {
contentTextAdd = ' ' + contentTextAdd;
}
}
return contentText + contentTextAdd;
}
export default subtreeText;