diff --git a/lib/commons/dom/get-element-stack.js b/lib/commons/dom/get-element-stack.js
index 2d955828ca..f6c3a7bc45 100644
--- a/lib/commons/dom/get-element-stack.js
+++ b/lib/commons/dom/get-element-stack.js
@@ -418,7 +418,7 @@ function createGrid(
// (we don't do this before so we can calculate stacking context
// of parents with 0 width/height)
const rect = vNode.boundingClientRect;
- if (rect.width !== 0 && rect.height !== 0) {
+ if (rect.width !== 0 && rect.height !== 0 && dom.isVisible(node)) {
addNodeToGrid(grid, vNode);
}
diff --git a/test/commons/dom/get-element-stack.js b/test/commons/dom/get-element-stack.js
index f2321900f4..f1aad360ad 100644
--- a/test/commons/dom/get-element-stack.js
+++ b/test/commons/dom/get-element-stack.js
@@ -4,6 +4,7 @@ describe('dom.getElementStack', function() {
var fixture = document.getElementById('fixture');
var getElementStack = axe.commons.dom.getElementStack;
var getClientElementStack = axe.commons.dom.getClientElementStack;
+ var isIE11 = axe.testUtils.isIE11;
var shadowSupported = axe.testUtils.shadowSupport.v1;
function mapToIDs(stack) {
@@ -291,6 +292,36 @@ describe('dom.getElementStack', function() {
assert.deepEqual(stack, ['1', 'fixture', 'target', '2']);
});
+ it('should not add hidden elements', function() {
+ fixture.innerHTML =
+ '' +
+ 'Some text
' +
+ 'Some text
' +
+ 'Hello World' +
+ '';
+ axe.testUtils.flatTreeSetup(fixture);
+ var target = fixture.querySelector('#target');
+ var stack = mapToIDs(getElementStack(target));
+ assert.deepEqual(stack, ['target', '1', 'fixture']);
+ });
+
+ // IE11 either only supports clip paths defined by url() or not at all,
+ // MDN and caniuse.com give different results...
+ (isIE11 ? it.skip : it)(
+ 'should not add hidden elements using clip-path',
+ function() {
+ fixture.innerHTML =
+ '' +
+ 'Some text
' +
+ 'Hello World' +
+ '';
+ axe.testUtils.flatTreeSetup(fixture);
+ var target = fixture.querySelector('#target');
+ var stack = mapToIDs(getElementStack(target));
+ assert.deepEqual(stack, ['target', '1', 'fixture']);
+ }
+ );
+
(shadowSupported ? it : xit)(
'should sort shadow dom elements correctly',
function() {