-
Notifications
You must be signed in to change notification settings - Fork 779
/
native-text-methods.js
135 lines (117 loc) · 3.34 KB
/
native-text-methods.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import titleText from './title-text';
import subtreeText from './subtree-text';
import labelText from './label-text';
import accessibleText from './accessible-text';
const defaultButtonValues = {
submit: 'Submit',
image: 'Submit',
reset: 'Reset',
button: '' // No default for "button"
};
const nativeTextMethods = {
/**
* Return the value of a DOM node
* @param {VirtualNode} element
* @return {String} value text
*/
valueText: vNode => vNode.props.value || '',
/**
* Return default value of a button
* @param {VirtualNode} element
* @return {String} default button text
*/
buttonDefaultText: vNode => defaultButtonValues[vNode.props.type] || '',
/**
* Return caption text of an HTML table element
* @param {VirtualNode} element
* @param {Object} context
* @return {String} caption text
*/
tableCaptionText: descendantText.bind(null, 'caption'),
/**
* Return figcaption text of an HTML figure element
* @param {VirtualNode} element
* @param {Object} context
* @return {String} figcaption text
*/
figureText: descendantText.bind(null, 'figcaption'),
/**
* Return figcaption text of an HTML figure element
* @param {VirtualNode} element
* @param {Object} context
* @return {String} figcaption text
*/
svgTitleText: descendantText.bind(null, 'title'),
/**
* Return legend text of an HTML fieldset element
* @param {VirtualNode} element
* @param {Object} context
* @return {String} legend text
*/
fieldsetLegendText: descendantText.bind(null, 'legend'),
/**
* Return the alt text
* @param {VirtualNode} element
* @return {String} alt text
*/
altText: attrText.bind(null, 'alt'),
/**
* Return summary text for an HTML table element
* @param {VirtualNode} element
* @return {String} summary text
*/
tableSummaryText: attrText.bind(null, 'summary'),
/**
* Return the title text
* @param {VirtualNode} element
* @return {String} title text
*/
titleText,
/**
* Return accessible text of the subtree
* @param {VirtualNode} element
* @param {Object} context
* @return {String} Subtree text
*/
subtreeText,
/**
* Return accessible text for an implicit and/or explicit HTML label element
* @param {VirtualNode} element
* @param {Object} context
* @return {String} Label text
*/
labelText,
/**
* Return a single space
* @return {String} Returns ` `
*/
singleSpace: function singleSpace() {
return ' ';
},
/**
* Return the placeholder text
* @param {VirtualNode} element
* @return {String} placeholder text
*/
placeholderText: attrText.bind(null, 'placeholder')
};
function attrText(attr, vNode) {
return vNode.attr(attr) || '';
}
/**
* Get the accessible text of first matching node
* IMPORTANT: This method does not look at the composed tree
* @private
*/
function descendantText(nodeName, { actualNode }, context) {
nodeName = nodeName.toLowerCase();
// Prevent accidently getting the nested element, like:
// fieldset > fielset > legend (1st fieldset has no legend)
const nodeNames = [nodeName, actualNode.nodeName.toLowerCase()].join(',');
const candidate = actualNode.querySelector(nodeNames);
if (!candidate || candidate.nodeName.toLowerCase() !== nodeName) {
return '';
}
return accessibleText(candidate, context);
}
export default nativeTextMethods;