From 5e0098b0262614db20fc9d6689a5d5b95c153d8d Mon Sep 17 00:00:00 2001 From: Cassey Lottman Date: Tue, 29 Jun 2021 11:43:50 -0500 Subject: [PATCH] fix(page-no-duplicate): don't count elements hidden from screenreaders as potential duplicates (#3051) --- .../generic/page-no-duplicate-evaluate.js | 2 +- test/checks/keyboard/page-no-duplicate.js | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/checks/generic/page-no-duplicate-evaluate.js b/lib/checks/generic/page-no-duplicate-evaluate.js index 8a50c71e77..5d87ec0277 100644 --- a/lib/checks/generic/page-no-duplicate-evaluate.js +++ b/lib/checks/generic/page-no-duplicate-evaluate.js @@ -18,7 +18,7 @@ function pageNoDuplicateEvaluate(node, options, virtualNode) { cache.set(key, true); let elms = querySelectorAllFilter(axe._tree[0], options.selector, elm => - isVisible(elm.actualNode) + isVisible(elm.actualNode, true) ); // Filter elements that, within certain contexts, don't map their role. diff --git a/test/checks/keyboard/page-no-duplicate.js b/test/checks/keyboard/page-no-duplicate.js index 8d9d74f3f4..44f82ccefb 100644 --- a/test/checks/keyboard/page-no-duplicate.js +++ b/test/checks/keyboard/page-no-duplicate.js @@ -59,6 +59,15 @@ describe('page-no-duplicate', function() { assert.isTrue(check.evaluate.apply(checkContext, params)); }); + it('should return true if there is more than one element matching the selector but only one is visible to screenreaders', function() { + var options = { selector: 'main' }; + var params = checkSetup( + '
', + options + ); + assert.isTrue(check.evaluate.apply(checkContext, params)); + }); + (shadowSupported ? it : xit)( 'should return false if there is a second matching element inside the shadow dom', function() { @@ -81,6 +90,30 @@ describe('page-no-duplicate', function() { ]); } ); + + (shadowSupported ? it : xit)( + 'should return true if there is a second matching element inside the shadow dom but only one is visible to screenreaders', + function() { + var options = { selector: 'main' }; + var div = document.createElement('div'); + div.innerHTML = + '
'; + + var shadow = div + .querySelector('#shadow') + .attachShadow({ mode: 'open' }); + shadow.innerHTML = '
'; + axe.testUtils.fixtureSetup(div); + var vNode = axe.utils.querySelectorAll(axe._tree, '#target')[0]; + + assert.isTrue( + check.evaluate.call(checkContext, vNode.actualNode, options, vNode) + ); + assert.deepEqual(checkContext._relatedNodes, [ + shadow.querySelector('main') + ]); + } + ); }); describe('option.nativeScopeFilter', function() {