-
Notifications
You must be signed in to change notification settings - Fork 779
/
only-listitems.js
247 lines (206 loc) · 8.68 KB
/
only-listitems.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
describe('only-listitems', () => {
const fixture = document.getElementById('fixture');
const checkSetup = axe.testUtils.checkSetup;
const checkContext = axe.testUtils.MockCheckContext();
const checkEvaluate = axe.testUtils.getCheckEvaluate('only-listitems');
afterEach(() => {
checkContext.reset();
});
it('should return false if the list has no contents', () => {
const checkArgs = checkSetup('<ol id="target"></ol>');
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('should return false if the list has only spaces as content', () => {
const checkArgs = checkSetup('<ol id="target"> </ol>');
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('should return false if the list has whitespace', () => {
const checkArgs = checkSetup('<ol id="target"><li>Item</li> </ol>');
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('should return false if the list has only an element with role listitem', () => {
const checkArgs = checkSetup(
'<ol id="target"><div role="listitem">A list</div></ol>'
);
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('should return false if the list has only multiple mixed elements with role listitem', () => {
const checkArgs = checkSetup(
'<ol id="target"><div role="listitem">list</div><li role="listitem">list</li><div role="listitem">list</div></ol>'
);
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('should return false if the list has non-li comments', () => {
const checkArgs = checkSetup(
'<ol id="target"><li>Item</li><!--comment--></ol>'
);
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('should return true if the list has non-li text contents', () => {
const checkArgs = checkSetup(
'<ol id="target"><li>Item</li>Not an item</ol>'
);
assert.isTrue(checkEvaluate.apply(checkContext, checkArgs));
assert.deepEqual(checkContext._data, {
values: '#text'
});
});
it('should return true if the list has non-li contents', () => {
const checkArgs = checkSetup('<ol id="target"><p>Not a list</p></ol>');
assert.isTrue(checkEvaluate.apply(checkContext, checkArgs));
assert.deepEqual(checkContext._relatedNodes, [fixture.querySelector('p')]);
assert.deepEqual(checkContext._data, {
values: 'p'
});
});
it('should return false if the list has only an li with child content', () => {
const checkArgs = checkSetup('<ol id="target"><li>A <i>list</i></li></ol>');
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('should return false if the list has only an li', () => {
const checkArgs = checkSetup('<ol id="target"><li>A list</li></ol>');
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('should return true if the list has an li with other content', () => {
const checkArgs = checkSetup(
'<ol id="target"><li>A list</li><p>Not a list</p></ol>'
);
assert.isTrue(checkEvaluate.apply(checkContext, checkArgs));
assert.deepEqual(checkContext._relatedNodes, [fixture.querySelector('p')]);
assert.deepEqual(checkContext._data, {
values: 'p'
});
});
it('should return true if the list has at least one li while others have their roles changed', () => {
const checkArgs = checkSetup(
'<ol id="target"><li >A list item</li><li role="menuitem">Not a list item</li></ol>'
);
assert.isTrue(checkEvaluate.apply(checkContext, checkArgs));
assert.deepEqual(checkContext._relatedNodes, [
fixture.querySelector('[role="menuitem"]')
]);
assert.deepEqual(checkContext._data, {
values: '[role=menuitem]'
});
});
it('should return true if the list has only li items with their roles changed', () => {
const checkArgs = checkSetup(
'<ol id="target"><li id="fail1" role="menuitem">Not a list item</li><li id="fail2" role="menuitem">Not a list item</li></ol>'
);
assert.isTrue(checkEvaluate.apply(checkContext, checkArgs));
assert.deepEqual(checkContext._data, {
values: '[role=menuitem]'
});
assert.deepEqual(checkContext._relatedNodes, [
fixture.querySelector('#fail1'),
fixture.querySelector('#fail2')
]);
});
it('should return true if <link> is used along side only li items with their roles changed', () => {
const checkArgs = checkSetup(
'<ol id="target"><link rel="stylesheet" href="theme.css"><li role="menuitem">Not a list item</li></ol>'
);
assert.isTrue(checkEvaluate.apply(checkContext, checkArgs));
assert.deepEqual(checkContext._data, {
values: '[role=menuitem]'
});
});
it('should return false if <link> is used along side li', () => {
const checkArgs = checkSetup(
'<ol id="target"><link rel="stylesheet" href="theme.css"><li>A list</li></ol>'
);
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('should return false if <meta> is used along side li', () => {
const checkArgs = checkSetup(
'<ol id="target"><meta name="description" content=""><li>A list</li></ol>'
);
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('should return false if <script> is used along side li', () => {
const checkArgs = checkSetup(
'<ol id="target"><script src="script.js"></script><li>A list</li></ol>'
);
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('should return false if <style> is used along side li', () => {
const checkArgs = checkSetup(
'<ol id="target"><style></style><li>A list</li></ol>'
);
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('should return false if <template> is used along side li', () => {
const checkArgs = checkSetup(
'<ol id="target"><template></template><li>A list</li></ol>'
);
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('returns false if there are display:none elements that normally would not be allowed', () => {
const checkArgs = checkSetup(
'<ul id="target"> <li>An item</li> <h1 style="display:none">heading</h1> </ul>'
);
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('returns false if there are visibility:hidden elements that normally would not be allowed', () => {
const checkArgs = checkSetup(
'<ul id="target"> <li>An item</li> <h1 style="visibility:hidden">heading</h1> </ul>'
);
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('returns false if there are aria-hidden=true elements that normally would not be allowed', () => {
const checkArgs = checkSetup(
'<ul id="target"> <li>An item</li> <h1 aria-hidden="true">heading</h1> </ul>'
);
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('returns true if there are aria-hidden=false elements that normally would not be allowed', () => {
const checkArgs = checkSetup(
'<ul id="target"> <li>An item</li> <h1 aria-hidden="false">heading</h1> </ul>'
);
assert.isTrue(checkEvaluate.apply(checkContext, checkArgs));
assert.deepEqual(checkContext._data, {
values: 'h1'
});
});
describe('nodeNames', () => {
it('returns multiple node names', () => {
const checkArgs = checkSetup(
'<ul id="target"> <a></a><b></b><s></s> </ul>'
);
assert.isTrue(checkEvaluate.apply(checkContext, checkArgs));
assert.deepEqual(checkContext._data, {
values: 'a, b, s'
});
});
it('does only shows unique nodes names', () => {
const checkArgs = checkSetup(
'<ul id="target"> <a></a><b></b><a></a> </ul>'
);
assert.isTrue(checkEvaluate.apply(checkContext, checkArgs));
assert.deepEqual(checkContext._data, {
values: 'a, b'
});
});
});
describe('shadow DOM', () => {
it('should return false in a shadow DOM pass', () => {
const node = document.createElement('div');
node.innerHTML = '<li>My list item </li>';
const shadow = node.attachShadow({ mode: 'open' });
shadow.innerHTML = '<ul><slot></slot></ul>';
const checkArgs = checkSetup(node, 'ul');
assert.isFalse(checkEvaluate.apply(checkContext, checkArgs));
});
it('should return true in a shadow DOM fail', () => {
const node = document.createElement('div');
node.innerHTML = '<p>Not a list item</p>';
const shadow = node.attachShadow({ mode: 'open' });
shadow.innerHTML = '<ul><slot></slot></ul>';
const checkArgs = checkSetup(node, 'ul');
assert.isTrue(checkEvaluate.apply(checkContext, checkArgs));
assert.deepEqual(checkContext._data, {
values: 'p'
});
});
});
});