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
Make jquery :hidden and :visible selectors work #1048
Comments
Works with jQuery v1. Looks like we might run into some compat issues which were removed with v2. |
There we go: |
Thank you, it's better but I just bumped into another problem: if the tested element isn't hidden itself but is inside a hidden one, it doesn't work. Updated code: var jsdom = require("jsdom");
jsdom.env({
html: '<div style="display: none;"><span /></div>',
scripts: [ "http://code.jquery.com/jquery-1.11.2.min.js" ],
done: function (errors, window) {
if (errors) throw errors;
console.log(window.$('span').is(':hidden')); // prints false
}
}); |
Yes, we won't be able so solve that without proper layouting (to properly calculate |
I'll do that check with some other way, thanks for your inputs :) |
for anyone interested, this workaround looks okay with jQuery v1: jQueryElt.is(':hidden') || jQueryElt.parents().is(':hidden'); |
Given this is a jsdom environment assertions using jQuery's :visible have been changed to check the display property for visibility. See jsdom/jsdom#1048 Change-Id: Ifad8067c0b50053a94ac977ee1f1f5a3066bfa16
Deleting +1 comments as they are not helpful; if they persist we will lock this issue. |
For what it's worth, doing something similar to what's suggested in #135 (comment) works, e.g.:
Depending on your use-case it might more or less sense of course. |
In jQuery 3 the visible pseudo selector look like this: jQuery.expr.pseudos.visible = function( elem ) {
return !!( elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length );
}; so I've used this code: window.Element.prototype.getClientRects = function() {
var node = this;
while(node) {
if(node === document) {
break;
}
// don't know why but style is sometimes undefined
if (!node.style || node.style.display === 'none' || node.style.visibility === 'hidden') {
return [];
}
node = node.parentNode;
}
var self = $(this);
return [{width: self.width(), height: self.height()}];
}; |
I received an error of maximum stack I guessed right and modified something: I changed this part |
expanding a little bit on the previous workaround I ended up with this, which seems to work also in the case of nested elements window.Element.prototype.getClientRects = function() {
let node = this;
let visible = false;
while(node) {
if(node === document) {
break;
}
// don't know why but style is sometimes undefined
if (!node.style || node.style.display === 'none' || node.style.visibility === 'hidden') {
return [];
}
visible = true;
node = node.parentNode;
}
return visible ? [{width: 111, height: 111}] : [];
}; the aim is of course just to unblock |
@OmbraDiFenice this function is not recursive, that visible variable is kind of useless. You will never end with the second branch of the conditional operator. Unless you call it on |
you're right @jcubic, my attempt wasn't to stop the recursion but rather to try to return the empty array in cases where a parent element wasn't visible. But regardless, my Sorry for the confusion, in my defense I put together this "visible" stuff in like 5 minutes but I should know better by now and avoid such rookie mistakes 😅 |
Hello,
I'm probably missing something here but I can't make jQuery
:hidden
selector work.Shouldn't this print
true
?(jsdom version is 3.1.2)
The text was updated successfully, but these errors were encountered: