diff --git a/lib/checks/label/duplicate-img-label.js b/lib/checks/label/duplicate-img-label.js index f97b64a38f..58b0cc6fb5 100644 --- a/lib/checks/label/duplicate-img-label.js +++ b/lib/checks/label/duplicate-img-label.js @@ -8,6 +8,11 @@ const parent = dom.findUpVirtual( virtualNode, 'button, [role="button"], a[href], p, li, td, th' ); + +if (!parent) { + return false; +} + const parentVNode = axe.utils.getNodeFromTree(parent); const visibleText = text.visibleVirtual(parentVNode, true).toLowerCase(); if (visibleText === '') { diff --git a/test/checks/label/duplicate-img-label.js b/test/checks/label/duplicate-img-label.js index e7d3f6c71e..fb2fc34ed7 100644 --- a/test/checks/label/duplicate-img-label.js +++ b/test/checks/label/duplicate-img-label.js @@ -105,6 +105,20 @@ describe('duplicate-img-label', function() { ); }); + it('should return false if img does not have required parent', function() { + fixture.innerHTML = + '
Plain text and more

Plain text

'; + var node = fixture.querySelector('#target'); + axe.testUtils.flatTreeSetup(fixture); + assert.isFalse( + checks['duplicate-img-label'].evaluate( + node, + undefined, + axe.utils.getNodeFromTree(node) + ) + ); + }); + (shadowSupport.v1 ? it : xit)( 'should return true if the img is part of a shadow tree', function() {