-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Add feature test for passive event listener support #1894
Comments
Yes please! Happy to guide you through it, too
|
Here's the test (and use) for event listener options and its passive option: // assume the feature isn't supported
var supportsPassive = false;
// create options object with a getter to see if its passive property is accessed
var opts = Object.defineProperty && Object.defineProperty({}, 'passive', { get: function(){ supportsPassive = true }});
// create a throwaway element & event and (synchronously) test out our options
document.addEventListener('test', function() {}, opts);
// Use our detect's results. passive applied if supported, capture will be false either way.
elem.addEventListener('touchstart', fn, supportsPassive ? { passive: true } : false); The And a more terse version, assuming a modern runtime: var supportsPassive = false;
document.createElement("div").addEventListener("test", _ => {}, { get passive() { supportsPassive = true }});
elem.addEventListener('touchstart', fn, supportsPassive ? { passive: true } : false); |
The terse option seems better we can just wrap in a |
Since the listener argument is nullable and var supportsPassive = false;
try {
document.addEventListener("test", null, { get passive() { supportsPassive = true }});
} catch(e) {} |
(If brevity is a plus, one can also omit |
IE8 has |
Good call. Thx Latest version is now over here: WICG/EventListenerOptions#30 (comment) |
@ryanseddon You can't test syntax using try-catch, the whole thing will just not parse. |
@mgol yep, we did |
Should I do something to get this listed in https://modernizr.com/docs? |
it'll be listed automatically once I cut a new release, which will probably be tonight |
Great, thanks! |
thank YOU |
Any word on getting this into a release? |
From a quick look, I didn't find a mention of passive event listener in https://modernizr.com/docs Thanks in advance! |
This isn't released yet. I dropped the
into our Modernizr build locally for now. When will this be released? @patrickkettner @paulirish |
How about cleaning up the event listener after adding finishing the test? Modernizr.addTest('passiveeventlisteners', function() {
var supportsPassiveOption = false;
try {
var opts = Object.defineProperty({}, 'passive', {
get: function() {
supportsPassiveOption = true;
}
});
window.addEventListener('test', null, opts);
window.removeEventListener('test', null, opts);
} catch (e) {}
return supportsPassiveOption;
}); |
@Bamieh ... cleaning a |
@WebReflection browsers will always disappoint you! no but seriously, nothing in the spec mentions testing the EventListener interface for null, hence the listener will stay registered. (1.3.1.) |
Does it throw if it's triggered? What do specs say about dealing with null listener on dispatch? Anyway, i'm for cleaning up too, I just think in this case might be effectively unnecessary. |
Okay after further investigation, |
Spec is here. Step 2: "If callback is null, terminate these steps". I.e. this is defined to be a no-op and AFAIK all browsers implement it as such. |
Looking at the spec here step 3 If I'm interpreting this correctly, this feature detection isn't spec compliant and likely most browsers aren't either given that this works in practice. Perhaps the spec should be changed to explicitly allow for feature detection in this way? |
That's incorrect; it's accessed by the Web IDL bindings which convert the incoming object into a dictionary, and take place before any of the algorithm steps. I can appreciate that being pretty subtle though for those not familiar with Web IDL. |
thanks for the quick response! |
Thanks for clarifying. That means that this snippet works, unsurprisingly. What about the spec being misleading in the way that step 2 and 3 is ordered? The spec is describing the flattening behavior that is occurring in Web IDL. Should we open a spec issue to re-order these steps to make it explicit that flattening should occur before the early return on a null callback? |
The Web IDL conversion from object to dictionary and the "flattening" are orthogonal. Flattening operates on either a boolean or a dictionary; objects are never involved. There's no need to reorder the steps. It's not misleading to people who understand Web IDL, which again, I understand is not too many non-implementers. But you're looking at the algorithm for implementers, anyway. |
Ohh I understand now. Thank you for clarifying! |
hey, if you assign a variable like this Moreover, this is not a "most-likely", this is a certain statement:
there is no browser on earth that would implement passive events without being compatible with ES5 so the code is just fine as it is |
For passive event listeners, feature detection is important but non-trivial.
I don't have much experience with Modernizr but I can try to post a feature detect for this if desired.
The text was updated successfully, but these errors were encountered: