Skip to content

Commit

Permalink
fix(commons/dom): focusDisabled() behavior
Browse files Browse the repository at this point in the history
Now checks "disabled" attribute

Closes issue #3315
  • Loading branch information
dan-tripp committed Feb 21, 2022
1 parent 7c40451 commit 593aaac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/commons/dom/focus-disabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-n
import { getNodeFromTree } from '../../core/utils';
import isHiddenWithCSS from './is-hidden-with-css';

function isDisabledAttrAllowed(nodeName) {
// Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled
const allowedNodeNames = new Set(["button", "command", "fieldset", "keygen", "optgroup",
"option", "select", "textarea", "input"]);
return allowedNodeNames.has(nodeName);
}

/**
* Determines if focusing has been disabled on an element.
* @param {HTMLElement|VirtualNode} el The HTMLElement
Expand All @@ -10,7 +17,7 @@ import isHiddenWithCSS from './is-hidden-with-css';
function focusDisabled(el) {
const vNode = el instanceof AbstractVirtualNode ? el : getNodeFromTree(el);

if (vNode.hasAttr('disabled')) {
if (isDisabledAttrAllowed(vNode.props.nodeName) && vNode.hasAttr('disabled')) {
return true;
}

Expand Down
7 changes: 7 additions & 0 deletions test/checks/keyboard/no-focusable-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ describe('no-focusable-content tests', function() {
assert.isTrue(noFocusableContent(null, null, vNode));
});

it('should return false if "disabled" is specified on an element which doesn\'t allow it', function() {
var params = checkSetup(
'<button id="target"><a href="foo.html" disabled>Hello</a></button>'
);
assert.isFalse(noFocusableContent.apply(checkContext, params));
});

it('should return true on span with negative tabindex (focusable, does not have a widget role)', function() {
var vNode = queryFixture('<span id="target" role="text"> some text '
+'<span tabIndex="-1">JavaScript is able to focus this</span> '
Expand Down

0 comments on commit 593aaac

Please sign in to comment.