isEventSupported.js.flow 1.8 KB

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