diff --git a/lib/checks/label/duplicate-img-label-evaluate.js b/lib/checks/label/duplicate-img-label-evaluate.js index 8e04752b25..f8b786c186 100644 --- a/lib/checks/label/duplicate-img-label-evaluate.js +++ b/lib/checks/label/duplicate-img-label-evaluate.js @@ -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; diff --git a/lib/checks/label/duplicate-img-label.json b/lib/checks/label/duplicate-img-label.json index 28bc29ef60..938996544e 100644 --- a/lib/checks/label/duplicate-img-label.json +++ b/lib/checks/label/duplicate-img-label.json @@ -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": { diff --git a/test/checks/label/duplicate-img-label.js b/test/checks/label/duplicate-img-label.js index d99097c747..20fe667ef5 100644 --- a/test/checks/label/duplicate-img-label.js +++ b/test/checks/label/duplicate-img-label.js @@ -119,6 +119,20 @@ describe('duplicate-img-label', function() { ); }); + it('should support options.parentSelector', function() { + fixture.innerHTML = + '
'; + 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() {