From 593aaac5ffee3b56b31e30e28d4fe2799defcead Mon Sep 17 00:00:00 2001 From: Dan Tripp Date: Fri, 11 Feb 2022 19:50:06 -0500 Subject: [PATCH] fix(commons/dom): focusDisabled() behavior Now checks "disabled" attribute Closes issue #3315 --- lib/commons/dom/focus-disabled.js | 9 ++++++++- test/checks/keyboard/no-focusable-content.js | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/commons/dom/focus-disabled.js b/lib/commons/dom/focus-disabled.js index e1288f1be8..1153d01518 100644 --- a/lib/commons/dom/focus-disabled.js +++ b/lib/commons/dom/focus-disabled.js @@ -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 @@ -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; } diff --git a/test/checks/keyboard/no-focusable-content.js b/test/checks/keyboard/no-focusable-content.js index d642bbd15e..e10b4679b0 100644 --- a/test/checks/keyboard/no-focusable-content.js +++ b/test/checks/keyboard/no-focusable-content.js @@ -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( + '' + ); + 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(' some text ' +'JavaScript is able to focus this '