Skip to content

Commit

Permalink
feat(duplicate-img-label): add option for parentSelector (#2216)
Browse files Browse the repository at this point in the history
* feat(duplicate-img-label): add option for parentSelector

* move options to metadata file

* typo
  • Loading branch information
straker committed May 14, 2020
1 parent b5f4168 commit 8906806
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
12 changes: 3 additions & 9 deletions lib/checks/label/duplicate-img-label-evaluate.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
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) {
if (['none', 'presentation'].includes(getRole(node))) {
return false;
}
const parentVNode = closest(virtualNode, options.parentSelector);

const parent = findUpVirtual(
virtualNode,
'button, [role="button"], a[href], p, li, td, th'
);

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

const parentVNode = getNodeFromTree(parent);
const visibleText = visibleVirtual(parentVNode, true).toLowerCase();
if (visibleText === '') {
return false;
Expand Down
3 changes: 3 additions & 0 deletions lib/checks/label/duplicate-img-label.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"id": "duplicate-img-label",
"evaluate": "duplicate-img-label-evaluate",
"options": {
"parentSelector": "button, [role=button], a[href], p, li, td, th"
},
"metadata": {
"impact": "minor",
"messages": {
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 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(
axe.testUtils.getCheckEvaluate('duplicate-img-label')(
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

0 comments on commit 8906806

Please sign in to comment.