@@ -65,6 +65,18 @@ function ancestorIsVisible(element: Element, cloned = false) {
6565 return true
6666}
6767
68+ /**
69+ * Check if an element is hidden by checking the following conditions
70+ * - If the element is the body, assume it's visible and return `false`
71+ * - If the element is an input, return true if the input is `hidden`
72+ * - If the element has style return true if the style is `none`
73+ * - If the element is not a clone, does not have display:`contents`,
74+ * and has a `checkVisibility` method, return the result of that method
75+ * - Get the computed style and return true if the computed style is `none`
76+ * - Otherwise return `false`
77+ * @param element
78+ * @param cloned
79+ */
6880export function isHidden ( element : HTMLElement | Element , cloned = false ) {
6981 if ( element . tagName === 'BODY' ) {
7082 return false
@@ -78,19 +90,28 @@ export function isHidden(element: HTMLElement | Element, cloned = false) {
7890 }
7991 }
8092
81- if ( element && 'style' in element && element . style . display === 'none' ) {
82- // if the element has style return true if the style is `none`
83- return true
84- }
93+ if ( element && 'style' in element ) {
94+ if ( element . style . display === 'none' ) {
95+ // if the element has style return true if the style is `none`
96+ return true
97+ }
8598
86- // clones are not visible so we can't use checkVisibility
87- if ( ! cloned && 'checkVisibility' in element && typeof element . checkVisibility === 'function' ) {
88- // use the relatively new checkVisibility method, which returns `true` if the element is visible
89- return ! element . checkVisibility ( )
99+ if (
100+ // clones are not visible, so we can't use checkVisibility on them
101+ ! cloned &&
102+ // checkVisibility currently returns false for elements with `display: contents`
103+ // https://chromium-review.googlesource.com/c/chromium/src/+/4950634
104+ element . style . display !== 'contents' &&
105+ 'checkVisibility' in element &&
106+ typeof element . checkVisibility === 'function'
107+ ) {
108+ // use the relatively new checkVisibility method, which returns `true` if the element is visible
109+ return ! element . checkVisibility ( )
110+ }
90111 }
91112
92113 // This method is not as accurate as checkVisibility
93- // compute the style as its not immediately obvious if the element is hidden
114+ // compute the style as it's not immediately obvious if the element is hidden
94115 const computedStyle = getComputedStyle ( element )
95116
96117 if ( computedStyle . display === 'none' ) {
0 commit comments