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

Implement element.getBoundingClientRect and element.getClientRects #653

Closed
JamesMGreene opened this issue Jul 5, 2013 · 18 comments
Closed
Labels

Comments

@JamesMGreene
Copy link

Related to #61 and #135.

Not so sure this is possible based on discussion so far in those issues but I thought I'd throw it out there.

@F1LT3R
Copy link

F1LT3R commented Sep 27, 2013

I think getBoundingClientRect should at least be implemented as a dummy function that returned default values.

@domenic
Copy link
Member

domenic commented Sep 27, 2013

That seems reasonable; zeros for everything and all that. Patch welcome.

@F1LT3R
Copy link

F1LT3R commented Sep 27, 2013

#689

@JamesMGreene
Copy link
Author

Thanks, @F1LT3R and @domenic! (Although, in the meanwhile, I actually already moved on to port these tests from nodeunit to an appropriate web-based framework: QUnit). 👍

mpetrovich added a commit to nextbigsoundinc/imagely that referenced this issue Oct 9, 2015
jsdom does not implement layout-specific functionality like offsetWidth() or getBoundingClientRect() (see jsdom/jsdom#653, jsdom/jsdom#135).

The good news is that jsdom doesn't need to be used at all. Instead, the HTML file can be imported directly, and any external scripts or stylesheets can be inlined as well. PhantomJS will render the resulting HTML without issue, since it's using WebKit behind the scenes.
@hellatan
Copy link

hellatan commented Jan 8, 2016

Does getClientRects work in jsdom? According to this jquery ticket, it does not

@domenic
Copy link
Member

domenic commented Jan 8, 2016

getClientRects is not supported, only getBoundingClientRect. A PR adding getClientRects() that always returns an empty array (or maybe an array with a single rect with zeroes for all its values??) would be great.

@xjamundx
Copy link

xjamundx commented Jan 9, 2016

Yeah jquery 1.12.0 just updated its core style function to use getClientRects. We ran into the same issue. I may be able to look into doing a PR adding this on monday. In our case it was the .height() function that is now throwing.

domenic added a commit that referenced this issue Jan 17, 2016
@freshsocks
Copy link

I haven't been able to get the correct values from el.getBoundingClientRect(). The object that gets returned has all of the correct keys (height, width, top, etc) but every value is always 0. I'm interested to know if anyone else is experiencing this.

This always fails:

    it('getBoundingClientRect produces correct height', () => {
      container.style.height = '300px'
      expect(container.getBoundingClientRect().height).eql(300)
    })

My workaround to make my tests pass (which is a phrase that pains me to say) is something like this:

getComputedElHeight(el) {
    return Number(window.getComputedStyle(el).height.split('px')[0])
  }

Just thought I'd share while this issue is still open.

@F1LT3R
Copy link

F1LT3R commented Mar 14, 2016

I don't think it was implemented to have the correct values. I think it was
implemented to have the correct attributes only.
On Mar 14, 2016 9:02 AM, "lushfuture" notifications@github.com wrote:

I haven't been able to get the correct values from
el.getBoundingClientRect(). The object that gets returned has all of the
correct keys (height, width, top, etc) but every value is always 0. I'm
interested to know if anyone else is experiencing this.

This always fails:

it('getBoundingClientRect produces correct height', () => {
  container.style.height = '300px'
  expect(container.getBoundingClientRect().height).eql(300)
})

My workaround to make my tests pass (which is a phrase that pains me to
say) is something like this:

getComputedElHeight(el) {
return Number(window.getComputedStyle(el).height.split('px')[0])
}

Just thought I'd share while this issue is still open.


Reply to this email directly or view it on GitHub
#653 (comment).

@mgol
Copy link
Contributor

mgol commented Mar 14, 2016

Yes, making it return real values would essentially require implementing a whole layout engine which would be a massive undertaking. Issue #1322 covers that topic.

@hammer65
Copy link

hammer65 commented Sep 1, 2016

Is it possible in tests to override that method (for all elements) to get a usable value while testing code that uses it?

@domenic
Copy link
Member

domenic commented Sep 1, 2016

Yes, the same way you would in a browser: Element.prototype.getBoundingClientRect = function () { ... }

@hammer65
Copy link

hammer65 commented Sep 3, 2016

I ended up doing
window._core. HTMLDivElement.prototype.getBoundingClientRect

Which worked just fine.

@domenic
Copy link
Member

domenic commented Sep 3, 2016

That will break in future releases of jsdom (very soon, actually). Just remove the ._core.

@hammer65
Copy link

hammer65 commented Sep 3, 2016

Thanks I'll try that
On Sep 2, 2016 9:03 PM, "Domenic Denicola" notifications@github.com wrote:

That will break in future releases of jsdom (very soon, actually). Just
remove the _core.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#653 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AEtD9luz1uJWmJ3JlgdwuO1i5MP04_BXks5qmNWCgaJpZM4Ayv3U
.

@gre
Copy link

gre commented Dec 13, 2016

for testing, if you control the elements, you can also override per instance.

example:

function createMockDiv (width, height) {
  const div = document.createElement("div");
  Object.assign(div.style, {
    width: width+"px",
    height: height+"px",
  });
  // we have to mock this for jsdom.
  div.getBoundingClientRect = () => ({
    width,
    height,
    top: 0,
    left: 0,
    right: width,
    bottom: height,
  });
  return div;
}

@kavipriyasubramaniyan
Copy link

how to write karma code if getboundingclientrect() method is in if else condition in ts file.
can anyone please help.

@ghost
Copy link

ghost commented Mar 31, 2020

window.HTMLElement.prototype.getBoundingClientRect = function () {
  return {
    width: parseFloat(this.style.width) || 0,
    height: parseFloat(this.style.height) || 0,
    top: parseFloat(this.style.marginTop) || 0,
    left: parseFloat(this.style.marginLeft) || 0
  }
}

and also this for offset

Object.defineProperties(window.HTMLElement.prototype, {
  offsetWidth: {
    get () { return parseFloat(this.style.width) || 0 }
  },
  offsetHeight: {
    get () { return parseFloat(this.style.height) || 0 }
  },
  offsetTop: {
    get () { return parseFloat(this.style.marginTop) || 0 }
  },
  offsetLeft: {
    get () { return parseFloat(this.style.marginLeft) || 0 }
  }
})

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

No branches or pull requests

10 participants