Background: I'm currently working on a project where my JS app is embedded into multiple other (parent) JS applications. Some parent app can show/hide my app by setting display:block/none to some element that contains my app. Other parent apps temporarily remove my app from DOM and reattach it later. Few parent apps use shadow dom, so I do not have the access to the elements in the parent app.
I need a way to check if my app is visible (inside DOM and displayed; don't care about visibility:hidden and doesn't have to be inside the viewport), without changing the parent apps, so I looked into getBoundingClientRect.
Question: At least on chrome, getBoundingClientRect returns (w:0,h:0,l:0,t:0) if an element or its ancestor has display:none or if it is removed from DOM. I could not find any documentation on the guarantee of this behavior, and I am wondering, if it is safe (in the sense the this logic will not change) to use getBoundingClientRect to check for visibility of an element.
Also I need to know if this behavior is consistent across all major browsers, including FF, IE8+, and Safari. Is this documented anywhere?
Lastly, why (0,0,0,0)? I feel like getBoundingClientRect should return null in such cases as (0,0,0,0) actually means something different. Is there a good reason for that?
Edited/Additional Question: Thanks istos for pointing out that returning null may break some unsuspecting code, such as:
console.log(clientRectObject.height);
// TypeError: Cannot read property 'height' of null
Not to complain about the current behavior, but as a design question: instead of null, can returning some illegal Rect values (such as negative width and height) be a more useful alternative?