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