Skip to content

Commit 7b6da48

Browse files
WilcoFiersstraker
authored andcommitted
chore(has-alt): use virtualNode instead of node (#1923)
* chore(has-alt): use virtualNode instead of node * chore(has-alt): ignore null alt
1 parent 4088d3d commit 7b6da48

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

lib/checks/shared/has-alt.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
var nn = node.nodeName.toLowerCase();
2-
return (
3-
node.hasAttribute('alt') && (nn === 'img' || nn === 'input' || nn === 'area')
4-
);
1+
const { nodeName } = virtualNode.props;
2+
if (!['img', 'input', 'area'].includes(nodeName)) {
3+
return false;
4+
}
5+
6+
return virtualNode.hasAttr('alt');

test/checks/shared/has-alt.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@ describe('has-alt', function() {
22
'use strict';
33

44
var fixture = document.getElementById('fixture');
5+
var checkSetup = axe.testUtils.checkSetup;
56

67
afterEach(function() {
78
fixture.innerHTML = '';
89
});
910

1011
it('should return true if an alt is present', function() {
11-
var node = document.createElement('img');
12-
node.setAttribute('alt', 'woohoo');
13-
fixture.appendChild(node);
12+
var checkArgs = checkSetup('<img id="target" alt="woohoo" />');
13+
assert.isTrue(checks['has-alt'].evaluate.apply(null, checkArgs));
14+
});
1415

15-
assert.isTrue(checks['has-alt'].evaluate(node));
16+
it('should return true if an empty alt is present', function() {
17+
var checkArgs = checkSetup('<img id="target" alt="" />');
18+
assert.isTrue(checks['has-alt'].evaluate.apply(null, checkArgs));
1619
});
1720

18-
it('should return false if an alt is not present', function() {
19-
var node = document.createElement('img');
20-
fixture.appendChild(node);
21+
it('should return true if a null alt is present', function() {
22+
var checkArgs = checkSetup('<img id="target" alt />');
23+
assert.isTrue(checks['has-alt'].evaluate.apply(null, checkArgs));
24+
});
2125

22-
assert.isFalse(checks['has-alt'].evaluate(node));
26+
it('should return false if an alt is not present', function() {
27+
var checkArgs = checkSetup('<img id="target" />');
28+
assert.isFalse(checks['has-alt'].evaluate.apply(null, checkArgs));
2329
});
2430
});

0 commit comments

Comments
 (0)