Skip to content

Commit

Permalink
refactor(index.js): Made "isDisabledFromFieldset" a bit more concise (#…
Browse files Browse the repository at this point in the history
…594)

Made "isDisabledFromFieldset" a bit more concise and readable. The regex may even be faster than the multiple conditions, since they access "node.tagName" multiple times.
  • Loading branch information
DaviDevMod authored Feb 26, 2022
1 parent efff33d commit b341412
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-grapes-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tabbable': patch
---

Made "isDisabledFromFieldset" more readable and concise (even marginally faster).
19 changes: 4 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,7 @@ const isHidden = function (node, displayCheck) {
// unless they are in the _first_ <legend> element of the top-most disabled
// fieldset
const isDisabledFromFieldset = function (node) {
if (
isInput(node) ||
node.tagName === 'SELECT' ||
node.tagName === 'TEXTAREA' ||
node.tagName === 'BUTTON'
) {
if (/^(INPUT|BUTTON|SELECT|TEXTAREA)$/.test(node.tagName)) {
let parentNode = node.parentElement;
while (parentNode) {
if (parentNode.tagName === 'FIELDSET' && parentNode.disabled) {
Expand All @@ -186,17 +181,11 @@ const isDisabledFromFieldset = function (node) {
for (let i = 0; i < parentNode.children.length; i++) {
const child = parentNode.children.item(i);
if (child.tagName === 'LEGEND') {
if (child.contains(node)) {
return false;
}

// the node isn't in the first legend (in doc order), so no matter
// where it is now, it'll be disabled
return true;
// return whether `node` is a descendant of the first <legend> found
return !child.contains(node);
}
}

// the node isn't in a legend, so no matter where it is now, it'll be disabled
// the disabled <fieldset> containing the `node` has no <legend> at all
return true;
}

Expand Down

0 comments on commit b341412

Please sign in to comment.