isEventSupported.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * Copyright 2013-2015, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. * @providesModule isEventSupported
  10. */
  11. 'use strict';
  12. var ExecutionEnvironment = require('./ExecutionEnvironment');
  13. var useHasFeature;
  14. if (ExecutionEnvironment.canUseDOM) {
  15. useHasFeature =
  16. document.implementation &&
  17. document.implementation.hasFeature &&
  18. // always returns true in newer browsers as per the standard.
  19. // @see http://dom.spec.whatwg.org/#dom-domimplementation-hasfeature
  20. document.implementation.hasFeature('', '') !== true;
  21. }
  22. /**
  23. * Checks if an event is supported in the current execution environment.
  24. *
  25. * NOTE: This will not work correctly for non-generic events such as `change`,
  26. * `reset`, `load`, `error`, and `select`.
  27. *
  28. * Borrows from Modernizr.
  29. *
  30. * @param {string} eventNameSuffix Event name, e.g. "click".
  31. * @param {?boolean} capture Check if the capture phase is supported.
  32. * @return {boolean} True if the event is supported.
  33. * @internal
  34. * @license Modernizr 3.0.0pre (Custom Build) | MIT
  35. */
  36. function isEventSupported(eventNameSuffix, capture) {
  37. if (!ExecutionEnvironment.canUseDOM ||
  38. capture && !('addEventListener' in document)) {
  39. return false;
  40. }
  41. var eventName = 'on' + eventNameSuffix;
  42. var isSupported = eventName in document;
  43. if (!isSupported) {
  44. var element = document.createElement('div');
  45. element.setAttribute(eventName, 'return;');
  46. isSupported = typeof element[eventName] === 'function';
  47. }
  48. if (!isSupported && useHasFeature && eventNameSuffix === 'wheel') {
  49. // This is the only way to test support for the `wheel` event in IE9+.
  50. isSupported = document.implementation.hasFeature('Events.wheel', '3.0');
  51. }
  52. return isSupported;
  53. }
  54. module.exports = isEventSupported;