Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(has-alt): use virtualNode instead of node #1923

Merged
merged 2 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/checks/shared/has-alt.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var nn = node.nodeName.toLowerCase();
return (
node.hasAttribute('alt') && (nn === 'img' || nn === 'input' || nn === 'area')
);
const { nodeName } = virtualNode.props;
if (['img', 'input', 'area'].includes(nodeName) === false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not !['img', 'input', 'area'].includes(nodeName)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easier to spot the negation this way. Doesn't really matter I guess.

return false;
}

return typeof virtualNode.attr('alt') === 'string';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VirtualNode has a hasAttr function, any reason why you're not using it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right you are!

17 changes: 9 additions & 8 deletions test/checks/shared/has-alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ describe('has-alt', function() {
'use strict';

var fixture = document.getElementById('fixture');
var checkSetup = axe.testUtils.checkSetup;

afterEach(function() {
fixture.innerHTML = '';
});

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

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

it('should return false if an alt is not present', function() {
var node = document.createElement('img');
fixture.appendChild(node);

assert.isFalse(checks['has-alt'].evaluate(node));
var checkArgs = checkSetup('<img id="target" />');
assert.isFalse(checks['has-alt'].evaluate.apply(null, checkArgs));
});
});