Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IE 11 may throw an exception when calling getBoundingClientRect on detached elements #75

Closed
lynchbomb opened this issue Jan 11, 2018 · 3 comments
Assignees

Comments

@lynchbomb
Copy link
Member

lynchbomb commented Jan 11, 2018

IE 11 may throw an exception when calling .getBoundingClientRect on detached elements. These elements should (according to CSSOM spec) return an object with zero for the top, bottom, left, right, width, and height properties.

http://jsfiddle.net/jonathansampson/3vqddemh/ for the excellent solution to this.

try {
  document.createElement('i').getBoundingClientRect();
} catch(e) {
  let getBoundingClientRect = Element.prototype.getBoundingClientRect;
  Element.prototype.getBoundingClientRect = () => {
    try {
      return getBoundingClientRect.call(this);
    } catch(e) {
      return { top: 0, bottom: 0, left: 0, width: 0, height: 0, right: 0 }; 
    }
  }
}
@stefanpenner
Copy link
Member

Rather then monkey patching the Element.prototype, and doing so on all errors thrown by the above expression can we:

  • create a getBoundingClientRect helper getBoundingClientRect(element) that implements the above?
  • only do so if the caught error is the error we expect to catch. (wouldn't want to catch a ReferenceError or something

Example:

function getBoundingClientRect(element) {
   try {
     return element.getBoundingClientRect();
  } catch(e) {
   if (typoof e === 'object' && e !==nuill && /* check the e is the exception we expect */) {
      return { top: 0, bottom: 0, left: 0, width: 0, height: 0, right: 0 }; 
  } else {
    throw e; // something else went wrong, and we must surface the error
  }
}

@lynchbomb
Copy link
Member Author

lynchbomb commented Jan 16, 2018

Yep totally agree. I spiked (we can rename) spanielGetBoundingClientRect which is near identical to what you proposed; testing and will push later.

@lynchbomb
Copy link
Member Author

This PR should address this issue: #79

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants