Skip to content

Commit

Permalink
fix: use virtualNode in duplicate-img-label
Browse files Browse the repository at this point in the history
Closes #386
  • Loading branch information
Marcy Sutton committed Jun 30, 2017
1 parent dbbc544 commit 82e51bc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/checks/label/duplicate-img-label.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var imgs = node.querySelectorAll('img');
var text = axe.commons.text.visible(node, true).toLowerCase();
var text = axe.commons.text.visible(virtualNode, true).toLowerCase();

if (text === '') {
return false;
Expand Down
26 changes: 18 additions & 8 deletions test/checks/label/duplicate-img-label.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,63 @@ describe('duplicate-img-label', function () {

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

it('should return false if no img is present', function () {
fixture.innerHTML = '<button id="target">Plain text</button>';
var node = fixture.querySelector('#target');
assert.isFalse(checks['duplicate-img-label'].evaluate(node));
var tree = axe._tree = axe.utils.getFlattenedTree(fixture);
assert.isFalse(checks['duplicate-img-label'].evaluate(node, undefined, axe.utils.getNodeFromTree(tree[0], node)));
});

it('should return false if no text is present', function () {
fixture.innerHTML = '<button id="target"><img alt="Plain text"></button>';
var node = fixture.querySelector('#target');
assert.isFalse(checks['duplicate-img-label'].evaluate(node));
var tree = axe._tree = axe.utils.getFlattenedTree(fixture);
var result = checks['duplicate-img-label'].evaluate(node, undefined, axe.utils.getNodeFromTree(tree[0], node));
assert.isFalse(result);
});

it('should return false if aria-label duplicates img alt', function () {
fixture.innerHTML = '<button id="target" aria-label="Plain text"><img alt="Plain text"></button>';
var node = fixture.querySelector('#target');
assert.isFalse(checks['duplicate-img-label'].evaluate(node));
var tree = axe._tree = axe.utils.getFlattenedTree(fixture);
assert.isFalse(checks['duplicate-img-label'].evaluate(node, undefined, axe.utils.getNodeFromTree(tree[0], node)));
});

it('should return false if img and text have different text', function () {
fixture.innerHTML = '<button id="target"><img alt="Alt text">Plain text</button>';
var node = fixture.querySelector('#target');
assert.isFalse(checks['duplicate-img-label'].evaluate(node));
var tree = axe._tree = axe.utils.getFlattenedTree(fixture);
assert.isFalse(checks['duplicate-img-label'].evaluate(node, undefined, axe.utils.getNodeFromTree(tree[0], node)));
});

it('should return true if img and text have the same text', function () {
fixture.innerHTML = '<button id="target"><img alt="Plain text">Plain text</button>';
var node = fixture.querySelector('#target');
assert.isTrue(checks['duplicate-img-label'].evaluate(node));
var tree = axe._tree = axe.utils.getFlattenedTree(fixture);
assert.isTrue(checks['duplicate-img-label'].evaluate(node, undefined, axe.utils.getNodeFromTree(tree[0], node)));
});

it('should return true if img has ARIA label with the same text', function () {
fixture.innerHTML = '<button id="target"><img aria-label="Plain text">Plain text</button>';
var node = fixture.querySelector('#target');
assert.isTrue(checks['duplicate-img-label'].evaluate(node));
var tree = axe._tree = axe.utils.getFlattenedTree(fixture);
assert.isTrue(checks['duplicate-img-label'].evaluate(node, undefined, axe.utils.getNodeFromTree(tree[0], node)));
});

it('should return false if img and text are both blank', function () {
fixture.innerHTML = '<button id="target"><img alt=""></button>';
var node = fixture.querySelector('#target');
assert.isFalse(checks['duplicate-img-label'].evaluate(node));
var tree = axe._tree = axe.utils.getFlattenedTree(fixture);
assert.isFalse(checks['duplicate-img-label'].evaluate(node, undefined, axe.utils.getNodeFromTree(tree[0], node)));
});

it('should return false if img and text have superset/subset text', function () {
fixture.innerHTML = '<button id="target"><img alt="Plain text and more">Plain text</button>';
var node = fixture.querySelector('#target');
assert.isFalse(checks['duplicate-img-label'].evaluate(node));
var tree = axe._tree = axe.utils.getFlattenedTree(fixture);
assert.isFalse(checks['duplicate-img-label'].evaluate(node, undefined, axe.utils.getNodeFromTree(tree[0], node)));
});
});

0 comments on commit 82e51bc

Please sign in to comment.