-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Comments
I think getBoundingClientRect should at least be implemented as a dummy function that returned default values. |
That seems reasonable; zeros for everything and all that. Patch welcome. |
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.
Does getClientRects work in jsdom? According to this jquery ticket, it does not |
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. |
Yeah jquery |
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:
My workaround to make my tests pass (which is a phrase that pains me to say) is something like this:
Just thought I'd share while this issue is still open. |
I don't think it was implemented to have the correct values. I think it was
|
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. |
Is it possible in tests to override that method (for all elements) to get a usable value while testing code that uses it? |
Yes, the same way you would in a browser: |
I ended up doing Which worked just fine. |
That will break in future releases of jsdom (very soon, actually). Just remove the |
Thanks I'll try that
|
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;
} |
how to write karma code if getboundingclientrect() method is in if else condition in ts file. |
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 }
}
}) |
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.
The text was updated successfully, but these errors were encountered: