Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(commons/dom): change isFocusable to check visibility in screenreader mode #655

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/commons/dom/is-focusable.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dom.isNativelyFocusable = function(el) {

if (!el ||
el.disabled ||
(!dom.isVisible(el) && el.nodeName.toUpperCase() !== 'AREA')) {
(!dom.isVisible(el, true) && el.nodeName.toUpperCase() !== 'AREA')) {
return false;
}

Expand Down
79 changes: 77 additions & 2 deletions test/commons/dom/is-natively-focusable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ describe('dom.isNativelyFocusable', function () {

var fixture = document.getElementById('fixture');

function hideByClipping (el) {
el.style.cssText = 'position: absolute !important;' +
' clip: rect(0px 0px 0px 0px); /* IE6, IE7 */' +
' clip: rect(0px, 0px, 0px, 0px);';
}

function hideByMovingOffScreen (el) {
el.style.cssText = 'position:absolute;' +
' left:-10000px;' +
' top:auto;' +
' width:1px;' +
' height:1px;' +
' overflow:hidden;';
}

afterEach(function () {
document.getElementById('fixture').innerHTML = '';
});
Expand Down Expand Up @@ -72,14 +87,74 @@ describe('dom.isNativelyFocusable', function () {

});

it('should return false for non-visible elements', function () {
fixture.innerHTML = '<input type="text" id="target" style="display: none">';
it('should return false for elements hidden with display:none', function () {
fixture.innerHTML = '<button id="target" style="display: none">button</button>';
var el = document.getElementById('target');

assert.isFalse(axe.commons.dom.isNativelyFocusable(el));

});

it('should return false for elements hidden with visibility:hidden', function () {
fixture.innerHTML = '<button id="target" style="visibility: hidden">button</button>';
var el = document.getElementById('target');

assert.isFalse(axe.commons.dom.isNativelyFocusable(el));

});

it('should return true for clipped elements', function () {
fixture.innerHTML = '<button id="target">button</button>';
var el = document.getElementById('target');
hideByClipping(el);

assert.isTrue(axe.commons.dom.isNativelyFocusable(el));

});

it('should return true for elements positioned off screen', function () {
fixture.innerHTML = '<button id="target">button</button>';
var el = document.getElementById('target');
hideByMovingOffScreen(el);

assert.isTrue(axe.commons.dom.isNativelyFocusable(el));

});

it('should return false for elements hidden with display:none on an ancestor', function () {
fixture.innerHTML = '<div id="parent" style="display:none"><button id="target">button</button></div>';
var el = document.getElementById('target');

assert.isFalse(axe.commons.dom.isNativelyFocusable(el));

});

it('should return false for elements hidden with visibility:hidden on an ancestor', function () {
fixture.innerHTML = '<div id="parent" style="visibility: hidden"><button id="target">button</button></div>';
var el = document.getElementById('target');

assert.isFalse(axe.commons.dom.isNativelyFocusable(el));

});

it('should return true for elements with a clipped ancestor', function () {
fixture.innerHTML = '<div id="parent"><button id="target">button</button></div>';
hideByClipping(document.getElementById('parent'));
var el = document.getElementById('target');

assert.isTrue(axe.commons.dom.isNativelyFocusable(el));

});

it('should return true for elements off-screened by an ancestor', function () {
fixture.innerHTML = '<div id="parent"><button id="target">button</button></div>';
hideByMovingOffScreen(document.getElementById('parent'));
var el = document.getElementById('target');

assert.isTrue(axe.commons.dom.isNativelyFocusable(el));

});

it('should return true for an anchor with an href', function () {
fixture.innerHTML = '<a href="something.html" id="target"></a>';
var el = document.getElementById('target');
Expand Down