blob: c113fd185f075aecc0d25763882916d4a7e9ff96 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "core/dom/EventRetargeter.h"
22
Torne (Richard Coles)e5249552013-05-15 11:35:13 +010023#include "RuntimeEnabledFeatures.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010024#include "core/dom/ContainerNode.h"
25#include "core/dom/EventContext.h"
26#include "core/dom/EventPathWalker.h"
27#include "core/dom/FocusEvent.h"
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +010028#include "core/dom/FullscreenElementStack.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010029#include "core/dom/MouseEvent.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010030#include "core/dom/Touch.h"
31#include "core/dom/TouchEvent.h"
32#include "core/dom/TouchList.h"
33#include "core/dom/TreeScope.h"
Torne (Richard Coles)e5249552013-05-15 11:35:13 +010034#include "core/dom/shadow/ShadowRoot.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010035#include "wtf/PassRefPtr.h"
36#include "wtf/RefPtr.h"
37#include "wtf/Vector.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010038
39namespace WebCore {
40
41static inline bool inTheSameScope(ShadowRoot* shadowRoot, EventTarget* target)
42{
43 return target->toNode() && target->toNode()->treeScope()->rootNode() == shadowRoot;
44}
45
46static inline EventDispatchBehavior determineDispatchBehavior(Event* event, ShadowRoot* shadowRoot, EventTarget* target)
47{
48 // Video-only full screen is a mode where we use the shadow DOM as an implementation
49 // detail that should not be detectable by the web content.
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +010050 if (Element* element = FullscreenElementStack::currentFullScreenElementFrom(target->toNode()->document())) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010051 // FIXME: We assume that if the full screen element is a media element that it's
52 // the video-only full screen. Both here and elsewhere. But that is probably wrong.
53 if (element->isMediaElement() && shadowRoot && shadowRoot->host() == element)
54 return StayInsideShadowDOM;
55 }
56
57 // WebKit never allowed selectstart event to cross the the shadow DOM boundary.
58 // Changing this breaks existing sites.
59 // See https://bugs.webkit.org/show_bug.cgi?id=52195 for details.
60 const AtomicString eventType = event->type();
61 if (inTheSameScope(shadowRoot, target)
62 && (eventType == eventNames().abortEvent
63 || eventType == eventNames().changeEvent
64 || eventType == eventNames().errorEvent
65 || eventType == eventNames().loadEvent
66 || eventType == eventNames().resetEvent
67 || eventType == eventNames().resizeEvent
68 || eventType == eventNames().scrollEvent
69 || eventType == eventNames().selectEvent
70 || eventType == eventNames().selectstartEvent))
71 return StayInsideShadowDOM;
72
73 return RetargetEvent;
74}
75
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010076void EventRetargeter::ensureEventPath(Node* node, Event* event)
77{
78 calculateEventPath(node, event);
79 calculateAdjustedEventPathForEachNode(event->eventPath());
80}
81
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010082void EventRetargeter::calculateEventPath(Node* node, Event* event)
83{
84 EventPath& eventPath = event->eventPath();
85 eventPath.clear();
86 bool inDocument = node->inDocument();
87 bool isSVGElement = node->isSVGElement();
88 bool isMouseOrFocusEvent = event->isMouseEvent() || event->isFocusEvent();
89 bool isTouchEvent = event->isTouchEvent();
90 Vector<EventTarget*, 32> targetStack;
91 for (EventPathWalker walker(node); walker.node(); walker.moveToParent()) {
92 Node* node = walker.node();
93 if (targetStack.isEmpty())
94 targetStack.append(eventTargetRespectingTargetRules(node));
95 else if (walker.isVisitingInsertionPointInReprojection())
96 targetStack.append(targetStack.last());
97 if (isMouseOrFocusEvent)
98 eventPath.append(adoptPtr(new MouseOrFocusEventContext(node, eventTargetRespectingTargetRules(node), targetStack.last())));
99 else if (isTouchEvent)
100 eventPath.append(adoptPtr(new TouchEventContext(node, eventTargetRespectingTargetRules(node), targetStack.last())));
101 else
102 eventPath.append(adoptPtr(new EventContext(node, eventTargetRespectingTargetRules(node), targetStack.last())));
103 if (!inDocument)
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100104 break;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100105 if (!node->isShadowRoot())
106 continue;
107 if (determineDispatchBehavior(event, toShadowRoot(node), targetStack.last()) == StayInsideShadowDOM)
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100108 break;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100109 if (!isSVGElement) {
110 ASSERT(!targetStack.isEmpty());
111 targetStack.removeLast();
112 }
113 }
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100114}
Torne (Richard Coles)e5249552013-05-15 11:35:13 +0100115
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100116void EventRetargeter::calculateAdjustedEventPathForEachNode(EventPath& eventPath)
117{
Ben Murdoch02772c62013-07-26 10:21:05 +0100118 if (!RuntimeEnabledFeatures::shadowDOMEnabled())
Torne (Richard Coles)e5249552013-05-15 11:35:13 +0100119 return;
120 TreeScope* lastScope = 0;
121 size_t eventPathSize = eventPath.size();
122 for (size_t i = 0; i < eventPathSize; ++i) {
123 TreeScope* currentScope = eventPath[i]->node()->treeScope();
124 if (currentScope == lastScope) {
125 // Fast path.
126 eventPath[i]->setEventPath(eventPath[i - 1]->eventPath());
127 continue;
128 }
129 lastScope = currentScope;
130 Vector<RefPtr<Node> > nodes;
131 for (size_t j = 0; j < eventPathSize; ++j) {
132 Node* node = eventPath[j]->node();
133 if (node->treeScope()->isInclusiveAncestorOf(currentScope))
134 nodes.append(node);
135 }
136 eventPath[i]->adoptEventPath(nodes);
137 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100138}
139
140void EventRetargeter::adjustForMouseEvent(Node* node, MouseEvent& mouseEvent)
141{
142 adjustForRelatedTarget(node, mouseEvent.relatedTarget(), mouseEvent.eventPath());
143}
144
145void EventRetargeter::adjustForFocusEvent(Node* node, FocusEvent& focusEvent)
146{
147 adjustForRelatedTarget(node, focusEvent.relatedTarget(), focusEvent.eventPath());
148}
149
150void EventRetargeter::adjustForTouchEvent(Node* node, TouchEvent& touchEvent)
151{
152 EventPath& eventPath = touchEvent.eventPath();
153 size_t eventPathSize = eventPath.size();
154
155 EventPathTouchLists eventPathTouches(eventPathSize);
156 EventPathTouchLists eventPathTargetTouches(eventPathSize);
157 EventPathTouchLists eventPathChangedTouches(eventPathSize);
158
159 for (size_t i = 0; i < eventPathSize; ++i) {
160 ASSERT(eventPath[i]->isTouchEventContext());
161 TouchEventContext* touchEventContext = toTouchEventContext(eventPath[i].get());
162 eventPathTouches[i] = touchEventContext->touches();
163 eventPathTargetTouches[i] = touchEventContext->targetTouches();
164 eventPathChangedTouches[i] = touchEventContext->changedTouches();
165 }
166
167 adjustTouchList(node, touchEvent.touches(), eventPath, eventPathTouches);
168 adjustTouchList(node, touchEvent.targetTouches(), eventPath, eventPathTargetTouches);
169 adjustTouchList(node, touchEvent.changedTouches(), eventPath, eventPathChangedTouches);
170}
171
172void EventRetargeter::adjustTouchList(const Node* node, const TouchList* touchList, const EventPath& eventPath, EventPathTouchLists& eventPathTouchLists)
173{
174 if (!touchList)
175 return;
176 size_t eventPathSize = eventPath.size();
177 ASSERT(eventPathTouchLists.size() == eventPathSize);
178 for (size_t i = 0; i < touchList->length(); ++i) {
179 const Touch& touch = *touchList->item(i);
180 AdjustedNodes adjustedNodes;
181 calculateAdjustedNodes(node, touch.target()->toNode(), DoesNotStopAtBoundary, const_cast<EventPath&>(eventPath), adjustedNodes);
182 ASSERT(adjustedNodes.size() == eventPathSize);
183 for (size_t j = 0; j < eventPathSize; ++j)
184 eventPathTouchLists[j]->append(touch.cloneWithNewTarget(adjustedNodes[j].get()));
185 }
186}
187
188void EventRetargeter::adjustForRelatedTarget(const Node* node, EventTarget* relatedTarget, EventPath& eventPath)
189{
190 if (!node)
191 return;
192 if (!relatedTarget)
193 return;
194 Node* relatedNode = relatedTarget->toNode();
195 if (!relatedNode)
196 return;
197 AdjustedNodes adjustedNodes;
198 calculateAdjustedNodes(node, relatedNode, StopAtBoundaryIfNeeded, eventPath, adjustedNodes);
199 ASSERT(adjustedNodes.size() <= eventPath.size());
200 for (size_t i = 0; i < adjustedNodes.size(); ++i) {
201 ASSERT(eventPath[i]->isMouseOrFocusEventContext());
202 MouseOrFocusEventContext* mouseOrFocusEventContext = static_cast<MouseOrFocusEventContext*>(eventPath[i].get());
203 mouseOrFocusEventContext->setRelatedTarget(adjustedNodes[i]);
204 }
205}
206
207void EventRetargeter::calculateAdjustedNodes(const Node* node, const Node* relatedNode, EventWithRelatedTargetDispatchBehavior eventWithRelatedTargetDispatchBehavior, EventPath& eventPath, AdjustedNodes& adjustedNodes)
208{
209 RelatedNodeMap relatedNodeMap;
210 buildRelatedNodeMap(relatedNode, relatedNodeMap);
211
212 // Synthetic mouse events can have a relatedTarget which is identical to the target.
213 bool targetIsIdenticalToToRelatedTarget = (node == relatedNode);
214
215 TreeScope* lastTreeScope = 0;
216 Node* adjustedNode = 0;
217 for (EventPath::const_iterator iter = eventPath.begin(); iter < eventPath.end(); ++iter) {
218 TreeScope* scope = (*iter)->node()->treeScope();
219 if (scope == lastTreeScope) {
220 // Re-use the previous adjustedRelatedTarget if treeScope does not change. Just for the performance optimization.
221 adjustedNodes.append(adjustedNode);
222 } else {
223 adjustedNode = findRelatedNode(scope, relatedNodeMap);
224 adjustedNodes.append(adjustedNode);
225 }
226 lastTreeScope = scope;
227 if (eventWithRelatedTargetDispatchBehavior == DoesNotStopAtBoundary)
228 continue;
229 if (targetIsIdenticalToToRelatedTarget) {
230 if (node->treeScope()->rootNode() == (*iter)->node()) {
231 eventPath.shrink(iter + 1 - eventPath.begin());
232 break;
233 }
234 } else if ((*iter)->target() == adjustedNode) {
235 // Event dispatching should be stopped here.
236 eventPath.shrink(iter - eventPath.begin());
237 adjustedNodes.shrink(adjustedNodes.size() - 1);
238 break;
239 }
240 }
241}
242
243void EventRetargeter::buildRelatedNodeMap(const Node* relatedNode, RelatedNodeMap& relatedNodeMap)
244{
245 Vector<Node*, 32> relatedNodeStack;
246 TreeScope* lastTreeScope = 0;
247 for (EventPathWalker walker(relatedNode); walker.node(); walker.moveToParent()) {
248 Node* node = walker.node();
249 if (relatedNodeStack.isEmpty())
250 relatedNodeStack.append(node);
251 else if (walker.isVisitingInsertionPointInReprojection())
252 relatedNodeStack.append(relatedNodeStack.last());
253 TreeScope* scope = node->treeScope();
254 // Skips adding a node to the map if treeScope does not change. Just for the performance optimization.
255 if (scope != lastTreeScope)
256 relatedNodeMap.add(scope, relatedNodeStack.last());
257 lastTreeScope = scope;
258 if (node->isShadowRoot()) {
259 ASSERT(!relatedNodeStack.isEmpty());
260 relatedNodeStack.removeLast();
261 }
262 }
263}
264
265Node* EventRetargeter::findRelatedNode(TreeScope* scope, RelatedNodeMap& relatedNodeMap)
266{
267 Vector<TreeScope*, 32> parentTreeScopes;
268 Node* relatedNode = 0;
269 while (scope) {
270 parentTreeScopes.append(scope);
271 RelatedNodeMap::const_iterator found = relatedNodeMap.find(scope);
272 if (found != relatedNodeMap.end()) {
273 relatedNode = found->value;
274 break;
275 }
276 scope = scope->parentTreeScope();
277 }
278 for (Vector<TreeScope*, 32>::iterator iter = parentTreeScopes.begin(); iter < parentTreeScopes.end(); ++iter)
279 relatedNodeMap.add(*iter, relatedNode);
280 return relatedNode;
281}
282
283}