1 | /* |
2 | * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
3 | * (C) 1999 Antti Koivisto (koivisto@kde.org) |
4 | * (C) 2001 Dirk Mueller (mueller@kde.org) |
5 | * Copyright (C) 2004-2017 Apple Inc. All rights reserved. |
6 | * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) |
7 | * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/) |
8 | * Copyright (C) 2010, 2011, 2012, 2013 Google Inc. All rights reserved. |
9 | * |
10 | * This library is free software; you can redistribute it and/or |
11 | * modify it under the terms of the GNU Library General Public |
12 | * License as published by the Free Software Foundation; either |
13 | * version 2 of the License, or (at your option) any later version. |
14 | * |
15 | * This library is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 | * Library General Public License for more details. |
19 | * |
20 | * You should have received a copy of the GNU Library General Public License |
21 | * along with this library; see the file COPYING.LIB. If not, write to |
22 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 | * Boston, MA 02110-1301, USA. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "EventDispatcher.h" |
28 | |
29 | #include "CompositionEvent.h" |
30 | #include "EventContext.h" |
31 | #include "EventPath.h" |
32 | #include "Frame.h" |
33 | #include "FrameLoader.h" |
34 | #include "FrameView.h" |
35 | #include "HTMLInputElement.h" |
36 | #include "InputEvent.h" |
37 | #include "KeyboardEvent.h" |
38 | #include "Logging.h" |
39 | #include "MouseEvent.h" |
40 | #include "ScopedEventQueue.h" |
41 | #include "ScriptDisallowedScope.h" |
42 | #include "ShadowRoot.h" |
43 | #include "TextEvent.h" |
44 | #include "TouchEvent.h" |
45 | |
46 | namespace WebCore { |
47 | |
48 | void EventDispatcher::dispatchScopedEvent(Node& node, Event& event) |
49 | { |
50 | // Need to set the target here so the scoped event queue knows which node to dispatch to. |
51 | event.setTarget(EventPath::eventTargetRespectingTargetRules(node)); |
52 | ScopedEventQueue::singleton().enqueueEvent(event); |
53 | } |
54 | |
55 | static void callDefaultEventHandlersInBubblingOrder(Event& event, const EventPath& path) |
56 | { |
57 | if (path.isEmpty()) |
58 | return; |
59 | |
60 | // Non-bubbling events call only one default event handler, the one for the target. |
61 | path.contextAt(0).node()->defaultEventHandler(event); |
62 | ASSERT(!event.defaultPrevented()); |
63 | |
64 | if (event.defaultHandled() || !event.bubbles()) |
65 | return; |
66 | |
67 | size_t size = path.size(); |
68 | for (size_t i = 1; i < size; ++i) { |
69 | path.contextAt(i).node()->defaultEventHandler(event); |
70 | ASSERT(!event.defaultPrevented()); |
71 | if (event.defaultHandled()) |
72 | return; |
73 | } |
74 | } |
75 | |
76 | static void dispatchEventInDOM(Event& event, const EventPath& path) |
77 | { |
78 | // Invoke capturing event listeners in the reverse order. |
79 | for (size_t i = path.size(); i > 0; --i) { |
80 | const EventContext& eventContext = path.contextAt(i - 1); |
81 | if (eventContext.currentTarget() == eventContext.target()) |
82 | event.setEventPhase(Event::AT_TARGET); |
83 | else |
84 | event.setEventPhase(Event::CAPTURING_PHASE); |
85 | eventContext.handleLocalEvents(event, EventTarget::EventInvokePhase::Capturing); |
86 | if (event.propagationStopped()) |
87 | return; |
88 | } |
89 | |
90 | // Invoke bubbling event listeners. |
91 | size_t size = path.size(); |
92 | for (size_t i = 0; i < size; ++i) { |
93 | const EventContext& eventContext = path.contextAt(i); |
94 | if (eventContext.currentTarget() == eventContext.target()) |
95 | event.setEventPhase(Event::AT_TARGET); |
96 | else if (event.bubbles()) |
97 | event.setEventPhase(Event::BUBBLING_PHASE); |
98 | else |
99 | continue; |
100 | eventContext.handleLocalEvents(event, EventTarget::EventInvokePhase::Bubbling); |
101 | if (event.propagationStopped()) |
102 | return; |
103 | } |
104 | } |
105 | |
106 | static bool shouldSuppressEventDispatchInDOM(Node& node, Event& event) |
107 | { |
108 | if (!event.isTrusted()) |
109 | return false; |
110 | |
111 | auto frame = node.document().frame(); |
112 | if (!frame) |
113 | return false; |
114 | |
115 | if (!frame->mainFrame().loader().shouldSuppressTextInputFromEditing()) |
116 | return false; |
117 | |
118 | if (is<TextEvent>(event)) { |
119 | auto& textEvent = downcast<TextEvent>(event); |
120 | return textEvent.isKeyboard() || textEvent.isComposition(); |
121 | } |
122 | |
123 | return is<CompositionEvent>(event) || is<InputEvent>(event) || is<KeyboardEvent>(event); |
124 | } |
125 | |
126 | void EventDispatcher::dispatchEvent(Node& node, Event& event) |
127 | { |
128 | ASSERT_WITH_SECURITY_IMPLICATION(ScriptDisallowedScope::InMainThread::isEventDispatchAllowedInSubtree(node)); |
129 | |
130 | LOG(Events, "EventDispatcher::dispatchEvent %s on node %s" , event.type().string().utf8().data(), node.nodeName().utf8().data()); |
131 | |
132 | auto protectedNode = makeRef(node); |
133 | auto protectedView = makeRefPtr(node.document().view()); |
134 | |
135 | EventPath eventPath { node, event }; |
136 | |
137 | ChildNodesLazySnapshot::takeChildNodesLazySnapshot(); |
138 | |
139 | event.resetBeforeDispatch(); |
140 | |
141 | event.setTarget(EventPath::eventTargetRespectingTargetRules(node)); |
142 | if (!event.target()) |
143 | return; |
144 | |
145 | InputElementClickState clickHandlingState; |
146 | if (is<HTMLInputElement>(node)) |
147 | downcast<HTMLInputElement>(node).willDispatchEvent(event, clickHandlingState); |
148 | |
149 | if (shouldSuppressEventDispatchInDOM(node, event)) |
150 | event.stopPropagation(); |
151 | |
152 | if (!event.propagationStopped() && !eventPath.isEmpty()) { |
153 | event.setEventPath(eventPath); |
154 | dispatchEventInDOM(event, eventPath); |
155 | } |
156 | |
157 | event.resetAfterDispatch(); |
158 | |
159 | if (clickHandlingState.stateful) |
160 | downcast<HTMLInputElement>(node).didDispatchClickEvent(event, clickHandlingState); |
161 | |
162 | // Call default event handlers. While the DOM does have a concept of preventing |
163 | // default handling, the detail of which handlers are called is an internal |
164 | // implementation detail and not part of the DOM. |
165 | if (!event.defaultPrevented() && !event.defaultHandled() && !event.isDefaultEventHandlerIgnored()) { |
166 | // FIXME: Not clear why we need to reset the target for the default event handlers. |
167 | // We should research this, and remove this code if possible. |
168 | auto* finalTarget = event.target(); |
169 | event.setTarget(EventPath::eventTargetRespectingTargetRules(node)); |
170 | callDefaultEventHandlersInBubblingOrder(event, eventPath); |
171 | event.setTarget(finalTarget); |
172 | } |
173 | } |
174 | |
175 | template<typename T> |
176 | static void dispatchEventWithType(const Vector<T*>& targets, Event& event) |
177 | { |
178 | ASSERT(targets.size() >= 1); |
179 | ASSERT(*targets.begin()); |
180 | |
181 | EventPath eventPath { targets }; |
182 | event.setTarget(*targets.begin()); |
183 | event.setEventPath(eventPath); |
184 | event.resetBeforeDispatch(); |
185 | dispatchEventInDOM(event, eventPath); |
186 | event.resetAfterDispatch(); |
187 | } |
188 | |
189 | void EventDispatcher::dispatchEvent(const Vector<EventTarget*>& targets, Event& event) |
190 | { |
191 | dispatchEventWithType<EventTarget>(targets, event); |
192 | } |
193 | |
194 | void EventDispatcher::dispatchEvent(const Vector<Element*>& targets, Event& event) |
195 | { |
196 | dispatchEventWithType<Element>(targets, event); |
197 | } |
198 | |
199 | } |
200 | |