browserfeature.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2010 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Browser capability checks for the events package.
  16. *
  17. */
  18. goog.provide('goog.events.BrowserFeature');
  19. goog.require('goog.userAgent');
  20. goog.scope(function() {
  21. /**
  22. * Enum of browser capabilities.
  23. * @enum {boolean}
  24. */
  25. goog.events.BrowserFeature = {
  26. /**
  27. * Whether the button attribute of the event is W3C compliant. False in
  28. * Internet Explorer prior to version 9; document-version dependent.
  29. */
  30. HAS_W3C_BUTTON:
  31. !goog.userAgent.IE || goog.userAgent.isDocumentModeOrHigher(9),
  32. /**
  33. * Whether the browser supports full W3C event model.
  34. */
  35. HAS_W3C_EVENT_SUPPORT:
  36. !goog.userAgent.IE || goog.userAgent.isDocumentModeOrHigher(9),
  37. /**
  38. * To prevent default in IE7-8 for certain keydown events we need set the
  39. * keyCode to -1.
  40. */
  41. SET_KEY_CODE_TO_PREVENT_DEFAULT:
  42. goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9'),
  43. /**
  44. * Whether the {@code navigator.onLine} property is supported.
  45. */
  46. HAS_NAVIGATOR_ONLINE_PROPERTY:
  47. !goog.userAgent.WEBKIT || goog.userAgent.isVersionOrHigher('528'),
  48. /**
  49. * Whether HTML5 network online/offline events are supported.
  50. */
  51. HAS_HTML5_NETWORK_EVENT_SUPPORT:
  52. goog.userAgent.GECKO && goog.userAgent.isVersionOrHigher('1.9b') ||
  53. goog.userAgent.IE && goog.userAgent.isVersionOrHigher('8') ||
  54. goog.userAgent.OPERA && goog.userAgent.isVersionOrHigher('9.5') ||
  55. goog.userAgent.WEBKIT && goog.userAgent.isVersionOrHigher('528'),
  56. /**
  57. * Whether HTML5 network events fire on document.body, or otherwise the
  58. * window.
  59. */
  60. HTML5_NETWORK_EVENTS_FIRE_ON_BODY:
  61. goog.userAgent.GECKO && !goog.userAgent.isVersionOrHigher('8') ||
  62. goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9'),
  63. /**
  64. * Whether touch is enabled in the browser.
  65. */
  66. TOUCH_ENABLED:
  67. ('ontouchstart' in goog.global ||
  68. !!(goog.global['document'] && document.documentElement &&
  69. 'ontouchstart' in document.documentElement) ||
  70. // IE10 uses non-standard touch events, so it has a different check.
  71. !!(goog.global['navigator'] &&
  72. goog.global['navigator']['msMaxTouchPoints'])),
  73. /**
  74. * Whether addEventListener supports {passive: true}.
  75. * https://developers.google.com/web/updates/2016/06/passive-event-listeners
  76. */
  77. PASSIVE_EVENTS: purify(function() {
  78. // If we're in a web worker or other custom environment, we can't tell.
  79. if (!goog.global.addEventListener || !Object.defineProperty) { // IE 8
  80. return false;
  81. }
  82. var passive = false;
  83. var options = Object.defineProperty({}, 'passive', {
  84. get: function() {
  85. passive = true;
  86. }
  87. });
  88. goog.global.addEventListener('test', null, options);
  89. goog.global.removeEventListener('test', null, options);
  90. return passive;
  91. })
  92. };
  93. /**
  94. * Tricks Closure Compiler into believing that a function is pure. The compiler
  95. * assumes that any `valueOf` function is pure, without analyzing its contents.
  96. *
  97. * @param {function(): T} fn
  98. * @return {T}
  99. * @template T
  100. */
  101. function purify(fn) {
  102. return ({valueOf: fn}).valueOf();
  103. }
  104. }); // goog.scope