Skip to content

Commit

Permalink
feat: Add shadow DOM to duplicate-img-label check (#443)
Browse files Browse the repository at this point in the history
* feat: Add shadow DOM to duplicate-img-label check

* fix: Use tabs
  • Loading branch information
WilcoFiers committed Jul 18, 2017
1 parent 02daad1 commit b423673
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
24 changes: 11 additions & 13 deletions lib/checks/label/duplicate-img-label.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
var imgs = node.querySelectorAll('img');
var text = axe.commons.text.visible(virtualNode, true).toLowerCase();

const text = axe.commons.text.visible(virtualNode, true).toLowerCase();
if (text === '') {
return false;
}

for (var i = 0, len = imgs.length; i < len; i++) {
var img = imgs[i];
var imgAlt = axe.commons.text.accessibleText(img).toLowerCase();
if (imgAlt === text &&
img.getAttribute('role') !== 'presentation' &&
axe.commons.dom.isVisible(img)) {
return true;
}
}
// Get all visible images in the composed tree of the current node
const images = axe.utils.querySelectorAll(virtualNode, 'img')
// Ignore hidden or role=none/presentation images
.filter(({ actualNode }) => (axe.commons.dom.isVisible(actualNode) &&
!['none', 'presentation'].includes(actualNode.getAttribute('role'))
));

return false;
// See if any of the images duplicate the node's text
return images.some(img =>
text === axe.commons.text.accessibleText(img).toLowerCase()
);
36 changes: 36 additions & 0 deletions test/checks/label/duplicate-img-label.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ describe('duplicate-img-label', function () {
'use strict';

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

afterEach(function () {
fixture.innerHTML = '';
Expand Down Expand Up @@ -64,4 +66,38 @@ describe('duplicate-img-label', function () {
var tree = axe._tree = axe.utils.getFlattenedTree(fixture);
assert.isFalse(checks['duplicate-img-label'].evaluate(node, undefined, axe.utils.getNodeFromTree(tree[0], node)));
});

(shadowSupport.v1 ? it : xit)('should return true if the img is part of a shadow tree', function () {
var button = document.createElement('div');
button.setAttribute('role', 'button');
button.innerHTML = 'My button';
var shadow = button.attachShadow({ mode: 'open' });
shadow.innerHTML = '<slot></slot><img alt="My button">';
var checkArgs = checkSetup(button);

assert.isTrue(checks['duplicate-img-label'].evaluate.apply(null, checkArgs));
});

(shadowSupport.v1 ? it : xit)('should return true if the img is a slotted element', function () {
var button = document.createElement('div');
button.setAttribute('role', 'button');
button.innerHTML = '<img alt="My button">';
var shadow = button.attachShadow({ mode: 'open' });
shadow.innerHTML = '<span>My button</span> <slot></slot>';
var checkArgs = checkSetup(button);

assert.isTrue(checks['duplicate-img-label'].evaluate.apply(null, checkArgs));
});

(shadowSupport.v1 ? it : xit)('should return false if the shadow img has a different text', function () {
var button = document.createElement('div');
button.setAttribute('role', 'button');
button.innerHTML = 'My button';
var shadow = button.attachShadow({ mode: 'open' });
shadow.innerHTML = '<slot></slot><img alt="My image">';
var checkArgs = checkSetup(button);

assert.isFalse(checks['duplicate-img-label'].evaluate.apply(null, checkArgs));
});

});
12 changes: 11 additions & 1 deletion test/testutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,18 @@ testUtils.checkSetup = function (content, options, target) {
target = options;
options = {};
}
// Normalize target, allow it to be the inserted node or '#target'
target = target || (content instanceof Node ? content : '#target');
testUtils.fixtureSetup(content);
var node = axe.utils.querySelectorAll(axe._tree[0], target || '#target')[0];

var node;
if (typeof target === 'string') {
node = axe.utils.querySelectorAll(axe._tree[0], target)[0];
} else if (target instanceof Node) {
node = axe.utils.getNodeFromTree(axe._tree[0], target);
} else {
node = target;
}
return [node.actualNode, options, node];
};

Expand Down

0 comments on commit b423673

Please sign in to comment.