Source: lib/polyfill/input_event.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.polyfill.InputEvent');
  18. goog.require('shaka.log');
  19. goog.require('shaka.polyfill.register');
  20. /**
  21. * @namespace shaka.polyfill.InputEvent
  22. *
  23. * @summary A polyfill to patch 'input' event support in IE11.
  24. */
  25. /**
  26. * Install the polyfill if needed.
  27. */
  28. shaka.polyfill.InputEvent.install = function() {
  29. shaka.log.debug('InputEvent.install');
  30. // IE11 doesn't treat the 'input' event correctly.
  31. // https://connect.microsoft.com/IE/Feedback/Details/856998
  32. // If you know a better way than a userAgent check to detect this, please
  33. // send a patch.
  34. // This matches IE11, but not Edge. Edge does not have this problem.
  35. if (navigator.userAgent.indexOf('Trident/') < 0) {
  36. // Not IE, so don't patch anything.
  37. return;
  38. }
  39. if (HTMLInputElement.prototype.addEventListener ==
  40. shaka.polyfill.InputEvent.addEventListener_) {
  41. // The polyfill was already installed.
  42. return;
  43. }
  44. shaka.log.info('Patching input event support on IE.');
  45. shaka.polyfill.InputEvent.originalAddEventListener_ =
  46. HTMLInputElement.prototype.addEventListener;
  47. HTMLInputElement.prototype['addEventListener'] =
  48. shaka.polyfill.InputEvent.addEventListener_;
  49. };
  50. /**
  51. * The original addEventListener from HTMLInputElement.
  52. * @private Function
  53. */
  54. shaka.polyfill.InputEvent.originalAddEventListener_;
  55. /**
  56. * Add an event listener to this object and translate the event types to those
  57. * that work on IE11.
  58. *
  59. * @param {string} type
  60. * @param {EventListener|function(!Event):(boolean|undefined)} listener
  61. * @param {(!AddEventListenerOptions|boolean)=} opt_options
  62. * @this {HTMLInputElement}
  63. * @private
  64. */
  65. shaka.polyfill.InputEvent.addEventListener_ =
  66. function(type, listener, opt_options) {
  67. if (type == 'input') {
  68. // Based on the type of input element, translate the HTML5 'input' event to
  69. // one that IE11 will actually dispatch.
  70. switch (this.type) {
  71. // For range inputs, we use the 'change' event.
  72. case 'range':
  73. type = 'change';
  74. break;
  75. }
  76. }
  77. shaka.polyfill.InputEvent.originalAddEventListener_.call(
  78. this, type, listener, opt_options);
  79. };
  80. shaka.polyfill.register(shaka.polyfill.InputEvent.install);