-
Notifications
You must be signed in to change notification settings - Fork 776
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: introduce dom.isHiddenWithCSS for use in dom.isFocusable (#1211)
- Loading branch information
Showing
4 changed files
with
389 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* eslint complexity: ["error", 13], max-statements: ["error", 25] */ | ||
/* global dom */ | ||
|
||
/** | ||
* Determine whether an element is hidden based on css | ||
* @method isHiddenWithCSS | ||
* @memberof axe.commons.dom | ||
* @instance | ||
* @param {HTMLElement} el The HTML Element | ||
* @param {Boolean} descendentVisibilityValue (Optional) immediate descendant visibility value used for recursive computation | ||
* @return {Boolean} the element's hidden status | ||
*/ | ||
dom.isHiddenWithCSS = function isHiddenWithCSS(el, descendentVisibilityValue) { | ||
if (el.nodeType === 9) { | ||
// 9 === Node.DOCUMENT | ||
return false; | ||
} | ||
|
||
if (el.nodeType === 11) { | ||
// 11 === Node.DOCUMENT_FRAGMENT_NODE | ||
el = el.host; // swap to host node | ||
} | ||
|
||
if (['STYLE', 'SCRIPT'].includes(el.nodeName.toUpperCase())) { | ||
return false; | ||
} | ||
|
||
const style = window.getComputedStyle(el, null); | ||
if (!style) { | ||
throw new Error('Style does not exist for the given element.'); | ||
} | ||
|
||
const displayValue = style.getPropertyValue('display'); | ||
if (displayValue === 'none') { | ||
return true; | ||
} | ||
|
||
const HIDDEN_VISIBILITY_VALUES = ['hidden', 'collapse']; | ||
const visibilityValue = style.getPropertyValue('visibility'); | ||
if ( | ||
HIDDEN_VISIBILITY_VALUES.includes(visibilityValue) && | ||
!descendentVisibilityValue | ||
) { | ||
return true; | ||
} | ||
|
||
if ( | ||
HIDDEN_VISIBILITY_VALUES.includes(visibilityValue) && | ||
(descendentVisibilityValue && | ||
HIDDEN_VISIBILITY_VALUES.includes(descendentVisibilityValue)) | ||
) { | ||
return true; | ||
} | ||
|
||
const parent = dom.getComposedParent(el); | ||
if (parent && !HIDDEN_VISIBILITY_VALUES.includes(visibilityValue)) { | ||
return dom.isHiddenWithCSS(parent, visibilityValue); | ||
} | ||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.