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(duplicate-img-label): add option for parentSelector #2216

Merged
merged 4 commits into from
May 14, 2020
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
16 changes: 7 additions & 9 deletions lib/checks/label/duplicate-img-label-evaluate.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { getRole } from '../../commons/aria';
import { visibleVirtual, accessibleTextVirtual } from '../../commons/text';
import { findUpVirtual } from '../../commons/dom';
import { getNodeFromTree } from '../../core/utils';
import { closest } from '../../core/utils';

function duplicateImgLabelEvaluate(node, options, virtualNode) {
function duplicateImgLabelEvaluate(node, options = {}, virtualNode) {
if (['none', 'presentation'].includes(getRole(node))) {
return false;
}

const parent = findUpVirtual(
virtualNode,
'button, [role="button"], a[href], p, li, td, th'
);
const {
parentSelector = 'button, [role="button"], a[href], p, li, td, th'
straker marked this conversation as resolved.
Show resolved Hide resolved
} = options;
const parentVNode = closest(virtualNode, parentSelector);

if (!parent) {
if (!parentVNode) {
return false;
}

const parentVNode = getNodeFromTree(parent);
const visibleText = visibleVirtual(parentVNode, true).toLowerCase();
if (visibleText === '') {
return false;
Expand Down
14 changes: 14 additions & 0 deletions test/checks/label/duplicate-img-label.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ describe('duplicate-img-label', function() {
);
});

it('should support a options.parentSelector', function() {
fixture.innerHTML =
'<div aria-label="Plain text"><img id="target" alt="Plain text"></div>';
var node = fixture.querySelector('#target');
axe.testUtils.flatTreeSetup(fixture);
assert.isFalse(
checks['duplicate-img-label'].evaluate(
node,
{ parentSelector: 'div' },
axe.utils.getNodeFromTree(node)
)
);
});

(shadowSupport.v1 ? it : xit)(
'should return true if the img is part of a shadow tree',
function() {
Expand Down