isEventSupported.js 1.8 KB

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