6adb15c063831a4be1642e36de6b56999f9488de66b2855d272af92cf485c1654029429cd3c7dad70bd0def9b53279d68074a16b3cbfa57f304370957b785e 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* @flow */
  2. import { isDef, isUndef } from 'shared/util'
  3. import { updateListeners } from 'core/vdom/helpers/index'
  4. import { isIE, isFF, supportsPassive, isUsingMicroTask } from 'core/util/index'
  5. import { RANGE_TOKEN, CHECKBOX_RADIO_TOKEN } from 'web/compiler/directives/model'
  6. import { currentFlushTimestamp } from 'core/observer/scheduler'
  7. // normalize v-model event tokens that can only be determined at runtime.
  8. // it's important to place the event as the first in the array because
  9. // the whole point is ensuring the v-model callback gets called before
  10. // user-attached handlers.
  11. function normalizeEvents (on) {
  12. /* istanbul ignore if */
  13. if (isDef(on[RANGE_TOKEN])) {
  14. // IE input[type=range] only supports `change` event
  15. const event = isIE ? 'change' : 'input'
  16. on[event] = [].concat(on[RANGE_TOKEN], on[event] || [])
  17. delete on[RANGE_TOKEN]
  18. }
  19. // This was originally intended to fix #4521 but no longer necessary
  20. // after 2.5. Keeping it for backwards compat with generated code from < 2.4
  21. /* istanbul ignore if */
  22. if (isDef(on[CHECKBOX_RADIO_TOKEN])) {
  23. on.change = [].concat(on[CHECKBOX_RADIO_TOKEN], on.change || [])
  24. delete on[CHECKBOX_RADIO_TOKEN]
  25. }
  26. }
  27. let target: any
  28. function createOnceHandler (event, handler, capture) {
  29. const _target = target // save current target element in closure
  30. return function onceHandler () {
  31. const res = handler.apply(null, arguments)
  32. if (res !== null) {
  33. remove(event, onceHandler, capture, _target)
  34. }
  35. }
  36. }
  37. // #9446: Firefox <= 53 (in particular, ESR 52) has incorrect Event.timeStamp
  38. // implementation and does not fire microtasks in between event propagation, so
  39. // safe to exclude.
  40. const useMicrotaskFix = isUsingMicroTask && !(isFF && Number(isFF[1]) <= 53)
  41. function add (
  42. name: string,
  43. handler: Function,
  44. capture: boolean,
  45. passive: boolean
  46. ) {
  47. // async edge case #6566: inner click event triggers patch, event handler
  48. // attached to outer element during patch, and triggered again. This
  49. // happens because browsers fire microtask ticks between event propagation.
  50. // the solution is simple: we save the timestamp when a handler is attached,
  51. // and the handler would only fire if the event passed to it was fired
  52. // AFTER it was attached.
  53. if (useMicrotaskFix) {
  54. const attachedTimestamp = currentFlushTimestamp
  55. const original = handler
  56. handler = original._wrapper = function (e) {
  57. if (
  58. // no bubbling, should always fire.
  59. // this is just a safety net in case event.timeStamp is unreliable in
  60. // certain weird environments...
  61. e.target === e.currentTarget ||
  62. // event is fired after handler attachment
  63. e.timeStamp >= attachedTimestamp ||
  64. // bail for environments that have buggy event.timeStamp implementations
  65. // #9462 iOS 9 bug: event.timeStamp is 0 after history.pushState
  66. // #9681 QtWebEngine event.timeStamp is negative value
  67. e.timeStamp <= 0 ||
  68. // #9448 bail if event is fired in another document in a multi-page
  69. // electron/nw.js app, since event.timeStamp will be using a different
  70. // starting reference
  71. e.target.ownerDocument !== document
  72. ) {
  73. return original.apply(this, arguments)
  74. }
  75. }
  76. }
  77. target.addEventListener(
  78. name,
  79. handler,
  80. supportsPassive
  81. ? { capture, passive }
  82. : capture
  83. )
  84. }
  85. function remove (
  86. name: string,
  87. handler: Function,
  88. capture: boolean,
  89. _target?: HTMLElement
  90. ) {
  91. (_target || target).removeEventListener(
  92. name,
  93. handler._wrapper || handler,
  94. capture
  95. )
  96. }
  97. function updateDOMListeners (oldVnode: VNodeWithData, vnode: VNodeWithData) {
  98. if (isUndef(oldVnode.data.on) && isUndef(vnode.data.on)) {
  99. return
  100. }
  101. const on = vnode.data.on || {}
  102. const oldOn = oldVnode.data.on || {}
  103. target = vnode.elm
  104. normalizeEvents(on)
  105. updateListeners(on, oldOn, add, remove, createOnceHandler, vnode.context)
  106. target = undefined
  107. }
  108. export default {
  109. create: updateDOMListeners,
  110. update: updateDOMListeners
  111. }