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

feat: Add shadow DOM to duplicate-img-label check #443

Merged
merged 2 commits into from
Jul 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be !!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

array.some returns a boolean.

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') {
Copy link
Contributor

Choose a reason for hiding this comment

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

We needed this testutils file after all 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this is very nice to have hanging around.

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