| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/input/BoundaryEventDispatcher.h" | 5 #include "core/input/BoundaryEventDispatcher.h" |
| 6 | 6 |
| 7 #include "core/dom/Node.h" | 7 #include "core/dom/Node.h" |
| 8 #include "core/dom/shadow/FlatTreeTraversal.h" | 8 #include "core/dom/shadow/FlatTreeTraversal.h" |
| 9 | 9 |
| 10 namespace blink { | 10 namespace blink { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 dispatchOut(exitedTarget, enteredTarget); | 67 dispatchOut(exitedTarget, enteredTarget); |
| 68 | 68 |
| 69 // Create lists of all exited/entered ancestors, locate the common ancestor | 69 // Create lists of all exited/entered ancestors, locate the common ancestor |
| 70 // Based on httparchive, in more than 97% cases the depth of DOM is less | 70 // Based on httparchive, in more than 97% cases the depth of DOM is less |
| 71 // than 20. | 71 // than 20. |
| 72 HeapVector<Member<Node>, 20> exitedAncestors; | 72 HeapVector<Member<Node>, 20> exitedAncestors; |
| 73 HeapVector<Member<Node>, 20> enteredAncestors; | 73 HeapVector<Member<Node>, 20> enteredAncestors; |
| 74 size_t exitedAncestorsCommonParentIndex = 0; | 74 size_t exitedAncestorsCommonParentIndex = 0; |
| 75 size_t enteredAncestorsCommonParentIndex = 0; | 75 size_t enteredAncestorsCommonParentIndex = 0; |
| 76 | 76 |
| 77 // A note on mouseenter and mouseleave: These are non-bubbling events, and the
y are dispatched if there | 77 // A note on mouseenter and mouseleave: These are non-bubbling events, and |
| 78 // is a capturing event handler on an ancestor or a normal event handler on th
e element itself. This special | 78 // they are dispatched if there is a capturing event handler on an ancestor or |
| 79 // handling is necessary to avoid O(n^2) capturing event handler checks. | 79 // a normal event handler on the element itself. This special handling is |
| 80 // necessary to avoid O(n^2) capturing event handler checks. |
| 80 // | 81 // |
| 81 // Note, however, that this optimization can possibly cause some unanswered/
missing/redundant mouseenter or | 82 // Note, however, that this optimization can possibly cause some |
| 82 // mouseleave events in certain contrived eventhandling scenarios, e.g., when: | 83 // unanswered/missing/redundant mouseenter or mouseleave events in certain |
| 83 // - the mouseleave handler for a node sets the only capturing-mouseleave-list
ener in its ancestor, or | 84 // contrived eventhandling scenarios, e.g., when: |
| 84 // - DOM mods in any mouseenter/mouseleave handler changes the common ancestor
of exited & entered nodes, etc. | 85 // - the mouseleave handler for a node sets the only |
| 85 // We think the spec specifies a "frozen" state to avoid such corner cases (ch
eck the discussion on "candidate event | 86 // capturing-mouseleave-listener in its ancestor, or |
| 86 // listeners" at http://www.w3.org/TR/uievents), but our code below preserves
one such behavior from past only to | 87 // - DOM mods in any mouseenter/mouseleave handler changes the common ancestor |
| 87 // match Firefox and IE behavior. | 88 // of exited & entered nodes, etc. |
| 89 // We think the spec specifies a "frozen" state to avoid such corner cases |
| 90 // (check the discussion on "candidate event listeners" at |
| 91 // http://www.w3.org/TR/uievents), but our code below preserves one such |
| 92 // behavior from past only to match Firefox and IE behavior. |
| 88 // | 93 // |
| 89 // TODO(mustaq): Confirm spec conformance, double-check with other browsers. | 94 // TODO(mustaq): Confirm spec conformance, double-check with other browsers. |
| 90 | 95 |
| 91 buildAncestorChainsAndFindCommonAncestors( | 96 buildAncestorChainsAndFindCommonAncestors( |
| 92 exitedTarget, enteredTarget, &exitedAncestors, &enteredAncestors, | 97 exitedTarget, enteredTarget, &exitedAncestors, &enteredAncestors, |
| 93 &exitedAncestorsCommonParentIndex, &enteredAncestorsCommonParentIndex); | 98 &exitedAncestorsCommonParentIndex, &enteredAncestorsCommonParentIndex); |
| 94 | 99 |
| 95 bool exitedNodeHasCapturingAncestor = false; | 100 bool exitedNodeHasCapturingAncestor = false; |
| 96 const AtomicString& leaveEvent = getLeaveEvent(); | 101 const AtomicString& leaveEvent = getLeaveEvent(); |
| 97 for (size_t j = 0; j < exitedAncestors.size(); j++) { | 102 for (size_t j = 0; j < exitedAncestors.size(); j++) { |
| 98 if (exitedAncestors[j]->hasCapturingEventListeners(leaveEvent)) { | 103 if (exitedAncestors[j]->hasCapturingEventListeners(leaveEvent)) { |
| 99 exitedNodeHasCapturingAncestor = true; | 104 exitedNodeHasCapturingAncestor = true; |
| 100 break; | 105 break; |
| 101 } | 106 } |
| 102 } | 107 } |
| 103 | 108 |
| 104 // Dispatch leave events, in child-to-parent order. | 109 // Dispatch leave events, in child-to-parent order. |
| 105 for (size_t j = 0; j < exitedAncestorsCommonParentIndex; j++) | 110 for (size_t j = 0; j < exitedAncestorsCommonParentIndex; j++) |
| 106 dispatchLeave(exitedAncestors[j], enteredTarget, | 111 dispatchLeave(exitedAncestors[j], enteredTarget, |
| 107 !exitedNodeHasCapturingAncestor); | 112 !exitedNodeHasCapturingAncestor); |
| 108 | 113 |
| 109 // Dispatch over event | 114 // Dispatch over event |
| 110 if (isInDocument(enteredTarget)) | 115 if (isInDocument(enteredTarget)) |
| 111 dispatchOver(enteredTarget, exitedTarget); | 116 dispatchOver(enteredTarget, exitedTarget); |
| 112 | 117 |
| 113 // Defer locating capturing enter listener until /after/ dispatching the leave
events because | 118 // Defer locating capturing enter listener until /after/ dispatching the leave |
| 114 // the leave handlers might set a capturing enter handler. | 119 // events because the leave handlers might set a capturing enter handler. |
| 115 bool enteredNodeHasCapturingAncestor = false; | 120 bool enteredNodeHasCapturingAncestor = false; |
| 116 const AtomicString& enterEvent = getEnterEvent(); | 121 const AtomicString& enterEvent = getEnterEvent(); |
| 117 for (size_t i = 0; i < enteredAncestors.size(); i++) { | 122 for (size_t i = 0; i < enteredAncestors.size(); i++) { |
| 118 if (enteredAncestors[i]->hasCapturingEventListeners(enterEvent)) { | 123 if (enteredAncestors[i]->hasCapturingEventListeners(enterEvent)) { |
| 119 enteredNodeHasCapturingAncestor = true; | 124 enteredNodeHasCapturingAncestor = true; |
| 120 break; | 125 break; |
| 121 } | 126 } |
| 122 } | 127 } |
| 123 | 128 |
| 124 // Dispatch enter events, in parent-to-child order. | 129 // Dispatch enter events, in parent-to-child order. |
| 125 for (size_t i = enteredAncestorsCommonParentIndex; i > 0; i--) | 130 for (size_t i = enteredAncestorsCommonParentIndex; i > 0; i--) |
| 126 dispatchEnter(enteredAncestors[i - 1], exitedTarget, | 131 dispatchEnter(enteredAncestors[i - 1], exitedTarget, |
| 127 !enteredNodeHasCapturingAncestor); | 132 !enteredNodeHasCapturingAncestor); |
| 128 } | 133 } |
| 129 | 134 |
| 130 } // namespace blink | 135 } // namespace blink |
| OLD | NEW |