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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2001 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013, 2015 Apple Inc. All rights reserved.
* Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
* Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
* Copyright (C) 2010, 2011, 2012, 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 "EventDispatcher.h"
#include "EventContext.h"
#include "FocusEvent.h"
#include "FrameView.h"
#include "HTMLInputElement.h"
#include "HTMLMediaElement.h"
#include "HTMLSlotElement.h"
#include "MouseEvent.h"
#include "PseudoElement.h"
#include "ScopedEventQueue.h"
#include "ShadowRoot.h"
#include "SVGNames.h"
#include "SVGUseElement.h"
#include "TouchEvent.h"
#include <wtf/NeverDestroyed.h>
namespace WebCore {
class WindowEventContext {
public:
WindowEventContext(PassRefPtr<Node>, const EventContext*);
DOMWindow* window() const { return m_window.get(); }
EventTarget* target() const { return m_target.get(); }
bool handleLocalEvents(Event&);
private:
RefPtr<DOMWindow> m_window;
RefPtr<EventTarget> m_target;
};
WindowEventContext::WindowEventContext(PassRefPtr<Node> node, const EventContext* topEventContext)
{
Node* topLevelContainer = topEventContext ? topEventContext->node() : node.get();
if (!is<Document>(*topLevelContainer))
return;
m_window = downcast<Document>(*topLevelContainer).domWindow();
m_target = topEventContext ? topEventContext->target() : node.get();
}
bool WindowEventContext::handleLocalEvents(Event& event)
{
if (!m_window)
return false;
event.setTarget(m_target.copyRef());
event.setCurrentTarget(m_window.get());
m_window->fireEventListeners(event);
return true;
}
class EventPath {
public:
EventPath(Node& origin, Event&);
bool isEmpty() const { return m_path.isEmpty(); }
size_t size() const { return m_path.size(); }
const EventContext& contextAt(size_t i) const { return *m_path[i]; }
EventContext& contextAt(size_t i) { return *m_path[i]; }
#if ENABLE(TOUCH_EVENTS)
void retargetTouchLists(const TouchEvent&);
#endif
void setRelatedTarget(Node& origin, EventTarget&);
bool hasEventListeners(const AtomicString& eventType) const;
EventContext* lastContextIfExists() { return m_path.isEmpty() ? nullptr : m_path.last().get(); }
private:
#if ENABLE(TOUCH_EVENTS)
void retargetTouch(TouchEventContext::TouchListType, const Touch&);
#endif
Event& m_event;
Vector<std::unique_ptr<EventContext>, 32> m_path;
};
inline EventTarget* eventTargetRespectingTargetRules(Node& referenceNode)
{
if (is<PseudoElement>(referenceNode))
return downcast<PseudoElement>(referenceNode).hostElement();
// Events sent to elements inside an SVG use element's shadow tree go to the use element.
if (is<SVGElement>(referenceNode)) {
if (auto* useElement = downcast<SVGElement>(referenceNode).correspondingUseElement())
return useElement;
}
return &referenceNode;
}
void EventDispatcher::dispatchScopedEvent(Node& node, Event& event)
{
// We need to set the target here because it can go away by the time we actually fire the event.
event.setTarget(eventTargetRespectingTargetRules(node));
ScopedEventQueue::singleton().enqueueEvent(event);
}
static void callDefaultEventHandlersInTheBubblingOrder(Event& event, const EventPath& path)
{
if (path.isEmpty())
return;
// Non-bubbling events call only one default event handler, the one for the target.
path.contextAt(0).node()->defaultEventHandler(&event);
ASSERT(!event.defaultPrevented());
if (event.defaultHandled() || !event.bubbles())
return;
size_t size = path.size();
for (size_t i = 1; i < size; ++i) {
path.contextAt(i).node()->defaultEventHandler(&event);
ASSERT(!event.defaultPrevented());
if (event.defaultHandled())
return;
}
}
static void dispatchEventInDOM(Event& event, const EventPath& path, WindowEventContext& windowEventContext)
{
// Trigger capturing event handlers, starting at the top and working our way down.
event.setEventPhase(Event::CAPTURING_PHASE);
// We don't dispatch load events to the window. This quirk was originally
// added because Mozilla doesn't propagate load events to the window object.
bool shouldFireEventAtWindow = event.type() != eventNames().loadEvent;
if (shouldFireEventAtWindow && windowEventContext.handleLocalEvents(event) && event.propagationStopped())
return;
for (size_t i = path.size() - 1; i > 0; --i) {
const EventContext& eventContext = path.contextAt(i);
if (eventContext.currentTargetSameAsTarget())
continue;
eventContext.handleLocalEvents(event);
if (event.propagationStopped())
return;
}
event.setEventPhase(Event::AT_TARGET);
path.contextAt(0).handleLocalEvents(event);
if (event.propagationStopped())
return;
// Trigger bubbling event handlers, starting at the bottom and working our way up.
size_t size = path.size();
for (size_t i = 1; i < size; ++i) {
const EventContext& eventContext = path.contextAt(i);
if (eventContext.currentTargetSameAsTarget())
event.setEventPhase(Event::AT_TARGET);
else if (event.bubbles() && !event.cancelBubble())
event.setEventPhase(Event::BUBBLING_PHASE);
else
continue;
eventContext.handleLocalEvents(event);
if (event.propagationStopped())
return;
}
if (event.bubbles() && !event.cancelBubble()) {
event.setEventPhase(Event::BUBBLING_PHASE);
if (shouldFireEventAtWindow)
windowEventContext.handleLocalEvents(event);
}
}
bool EventDispatcher::dispatchEvent(Node* origin, Event& event)
{
ASSERT_WITH_SECURITY_IMPLICATION(!NoEventDispatchAssertion::isEventDispatchForbidden());
ASSERT(origin);
RefPtr<Node> node(origin);
RefPtr<FrameView> view = node->document().view();
EventPath eventPath(*node, event);
if (EventTarget* relatedTarget = event.relatedTarget())
eventPath.setRelatedTarget(*node, *relatedTarget);
#if ENABLE(TOUCH_EVENTS)
if (is<TouchEvent>(event))
eventPath.retargetTouchLists(downcast<TouchEvent>(event));
#endif
ChildNodesLazySnapshot::takeChildNodesLazySnapshot();
EventTarget* target = eventTargetRespectingTargetRules(*node);
event.setTarget(target);
if (!event.target())
return true;
ASSERT_WITH_SECURITY_IMPLICATION(!NoEventDispatchAssertion::isEventDispatchForbidden());
WindowEventContext windowEventContext(node.get(), eventPath.lastContextIfExists());
InputElementClickState clickHandlingState;
if (is<HTMLInputElement>(*node))
downcast<HTMLInputElement>(*node).willDispatchEvent(event, clickHandlingState);
if (!event.propagationStopped() && !eventPath.isEmpty())
dispatchEventInDOM(event, eventPath, windowEventContext);
event.setTarget(eventTargetRespectingTargetRules(*node));
event.setCurrentTarget(nullptr);
event.setEventPhase(0);
if (clickHandlingState.stateful)
downcast<HTMLInputElement>(*node).didDispatchClickEvent(event, clickHandlingState);
// Call default event handlers. While the DOM does have a concept of preventing
// default handling, the detail of which handlers are called is an internal
// implementation detail and not part of the DOM.
if (!event.defaultPrevented() && !event.defaultHandled())
callDefaultEventHandlersInTheBubblingOrder(event, eventPath);
// Ensure that after event dispatch, the event's target object is the
// outermost shadow DOM boundary.
event.setTarget(windowEventContext.target());
event.setCurrentTarget(nullptr);
return !event.defaultPrevented();
}
static inline bool shouldEventCrossShadowBoundary(Event& event, ShadowRoot& shadowRoot, EventTarget& target)
{
Node* targetNode = target.toNode();
#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 (targetNode) {
if (Element* element = targetNode->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.host() == element)
return false;
}
}
#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();
bool targetIsInShadowRoot = targetNode && &targetNode->treeScope().rootNode() == &shadowRoot;
return !targetIsInShadowRoot
|| !(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);
}
static Node* nodeOrHostIfPseudoElement(Node* node)
{
return is<PseudoElement>(*node) ? downcast<PseudoElement>(*node).hostElement() : node;
}
EventPath::EventPath(Node& originalTarget, Event& event)
: m_event(event)
{
#if ENABLE(SHADOW_DOM) || ENABLE(DETAILS_ELEMENT)
Vector<EventTarget*, 16> targetStack;
#endif
bool isMouseOrFocusEvent = event.isMouseEvent() || event.isFocusEvent();
#if ENABLE(TOUCH_EVENTS)
bool isTouchEvent = event.isTouchEvent();
#endif
EventTarget* target = nullptr;
Node* node = nodeOrHostIfPseudoElement(&originalTarget);
while (node) {
if (!target)
target = eventTargetRespectingTargetRules(*node);
ContainerNode* parent;
for (; node; node = parent) {
EventTarget* currentTarget = eventTargetRespectingTargetRules(*node);
if (isMouseOrFocusEvent)
m_path.append(std::make_unique<MouseOrFocusEventContext>(node, currentTarget, target));
#if ENABLE(TOUCH_EVENTS)
else if (isTouchEvent)
m_path.append(std::make_unique<TouchEventContext>(node, currentTarget, target));
#endif
else
m_path.append(std::make_unique<EventContext>(node, currentTarget, target));
if (is<ShadowRoot>(*node))
break;
parent = node->parentNode();
if (!parent)
return;
#if ENABLE(SHADOW_DOM) || ENABLE(DETAILS_ELEMENT)
if (ShadowRoot* shadowRootOfParent = parent->shadowRoot()) {
if (auto* assignedSlot = shadowRootOfParent->findAssignedSlot(*node)) {
// node is assigned to a slot. Continue dispatching the event at this slot.
targetStack.append(target);
parent = assignedSlot;
target = assignedSlot;
}
}
#endif
node = parent;
}
ShadowRoot& shadowRoot = downcast<ShadowRoot>(*node);
// At a shadow root. Continue dispatching the event at the shadow host.
#if ENABLE(SHADOW_DOM) || ENABLE(DETAILS_ELEMENT)
if (!targetStack.isEmpty()) {
// Move target back to a descendant of the shadow host if the event did not originate in this shadow tree or its inner shadow trees.
target = targetStack.last();
targetStack.removeLast();
ASSERT(shadowRoot.host()->contains(target->toNode()));
} else
#endif
target = nullptr;
if (!shouldEventCrossShadowBoundary(event, shadowRoot, originalTarget))
return;
node = shadowRoot.host();
}
}
class RelatedNodeRetargeter {
public:
RelatedNodeRetargeter(Node& relatedNode, TreeScope& targetTreeScope)
: m_relatedNode(relatedNode)
, m_retargetedRelatedNode(&relatedNode)
{
TreeScope* currentTreeScope = &m_relatedNode.treeScope();
if (LIKELY(currentTreeScope == &targetTreeScope))
return;
if (¤tTreeScope->documentScope() != &targetTreeScope.documentScope()) {
m_hasDifferentTreeRoot = true;
m_retargetedRelatedNode = nullptr;
return;
}
if (relatedNode.inDocument() != targetTreeScope.rootNode().inDocument()) {
m_hasDifferentTreeRoot = true;
while (m_retargetedRelatedNode->isInShadowTree())
m_retargetedRelatedNode = downcast<ShadowRoot>(m_retargetedRelatedNode->treeScope().rootNode()).host();
return;
}
collectTreeScopes();
// FIXME: We should collect this while constructing the event path.
Vector<TreeScope*, 8> targetTreeScopeAncestors;
for (TreeScope* currentTreeScope = &targetTreeScope; currentTreeScope; currentTreeScope = currentTreeScope->parentTreeScope())
targetTreeScopeAncestors.append(currentTreeScope);
ASSERT_WITH_SECURITY_IMPLICATION(!targetTreeScopeAncestors.isEmpty());
unsigned i = m_ancestorTreeScopes.size();
unsigned j = targetTreeScopeAncestors.size();
ASSERT_WITH_SECURITY_IMPLICATION(m_ancestorTreeScopes.last() == targetTreeScopeAncestors.last());
while (m_ancestorTreeScopes[i - 1] == targetTreeScopeAncestors[j - 1]) {
i--;
j--;
if (!i || !j)
break;
}
m_lowestCommonAncestorIndex = i;
m_retargetedRelatedNode = nodeInLowestCommonAncestor();
}
Node* currentNode(TreeScope& currentTreeScope)
{
checkConsistency(currentTreeScope);
return m_retargetedRelatedNode;
}
void moveToNewTreeScope(TreeScope* previousTreeScope, TreeScope& newTreeScope)
{
if (m_hasDifferentTreeRoot)
return;
auto& currentRelatedNodeScope = m_retargetedRelatedNode->treeScope();
if (previousTreeScope != ¤tRelatedNodeScope) {
// currentRelatedNode is still outside our shadow tree. New tree scope may contain currentRelatedNode
// but there is no need to re-target it. Moving into a slot (thereby a deeper shadow tree) doesn't matter.
return;
}
bool enteredSlot = newTreeScope.parentTreeScope() == previousTreeScope;
if (enteredSlot) {
if (m_lowestCommonAncestorIndex) {
if (m_ancestorTreeScopes.isEmpty())
collectTreeScopes();
bool relatedNodeIsInSlot = m_ancestorTreeScopes[m_lowestCommonAncestorIndex - 1] == &newTreeScope;
if (relatedNodeIsInSlot) {
m_lowestCommonAncestorIndex--;
m_retargetedRelatedNode = nodeInLowestCommonAncestor();
ASSERT(&newTreeScope == &m_retargetedRelatedNode->treeScope());
}
} else
ASSERT(m_retargetedRelatedNode == &m_relatedNode);
} else {
ASSERT(previousTreeScope->parentTreeScope() == &newTreeScope);
m_lowestCommonAncestorIndex++;
ASSERT_WITH_SECURITY_IMPLICATION(m_ancestorTreeScopes.isEmpty() || m_lowestCommonAncestorIndex < m_ancestorTreeScopes.size());
m_retargetedRelatedNode = downcast<ShadowRoot>(currentRelatedNodeScope.rootNode()).host();
ASSERT(&newTreeScope == &m_retargetedRelatedNode->treeScope());
}
}
void checkConsistency(TreeScope& currentTreeScope)
{
#if !ASSERT_DISABLED
for (auto* relatedNodeScope = &m_relatedNode.treeScope(); relatedNodeScope; relatedNodeScope = relatedNodeScope->parentTreeScope()) {
for (auto* targetScope = ¤tTreeScope; targetScope; targetScope = targetScope->parentTreeScope()) {
if (targetScope == relatedNodeScope) {
ASSERT(&m_retargetedRelatedNode->treeScope() == relatedNodeScope);
return;
}
}
}
ASSERT(!m_retargetedRelatedNode);
#else
UNUSED_PARAM(currentTreeScope);
#endif
}
private:
Node* nodeInLowestCommonAncestor()
{
if (!m_lowestCommonAncestorIndex)
return &m_relatedNode;
auto& rootNode = m_ancestorTreeScopes[m_lowestCommonAncestorIndex - 1]->rootNode();
return downcast<ShadowRoot>(rootNode).host();
}
void collectTreeScopes()
{
ASSERT(m_ancestorTreeScopes.isEmpty());
for (TreeScope* currentTreeScope = &m_relatedNode.treeScope(); currentTreeScope; currentTreeScope = currentTreeScope->parentTreeScope())
m_ancestorTreeScopes.append(currentTreeScope);
ASSERT_WITH_SECURITY_IMPLICATION(!m_ancestorTreeScopes.isEmpty());
}
Node& m_relatedNode;
Node* m_retargetedRelatedNode;
Vector<TreeScope*, 8> m_ancestorTreeScopes;
unsigned m_lowestCommonAncestorIndex { 0 };
bool m_hasDifferentTreeRoot { false };
};
void EventPath::setRelatedTarget(Node& origin, EventTarget& relatedTarget)
{
Node* relatedNode = relatedTarget.toNode();
if (!relatedNode || m_path.isEmpty())
return;
RelatedNodeRetargeter retargeter(*relatedNode, downcast<MouseOrFocusEventContext>(*m_path[0]).node()->treeScope());
bool originIsRelatedTarget = &origin == relatedNode;
// FIXME: We should add a new flag on Event instead.
bool shouldTrimEventPath = m_event.type() == eventNames().mouseoverEvent
|| m_event.type() == eventNames().mousemoveEvent
|| m_event.type() == eventNames().mouseoutEvent;
Node& rootNodeInOriginTreeScope = origin.treeScope().rootNode();
TreeScope* previousTreeScope = nullptr;
size_t originalEventPathSize = m_path.size();
for (unsigned contextIndex = 0; contextIndex < originalEventPathSize; contextIndex++) {
auto& context = downcast<MouseOrFocusEventContext>(*m_path[contextIndex]);
TreeScope& currentTreeScope = context.node()->treeScope();
if (UNLIKELY(previousTreeScope && ¤tTreeScope != previousTreeScope))
retargeter.moveToNewTreeScope(previousTreeScope, currentTreeScope);
Node* currentRelatedNode = retargeter.currentNode(currentTreeScope);
if (UNLIKELY(shouldTrimEventPath && !originIsRelatedTarget && context.target() == currentRelatedNode)) {
m_path.shrink(contextIndex);
break;
}
context.setRelatedTarget(currentRelatedNode);
if (UNLIKELY(shouldTrimEventPath && originIsRelatedTarget && context.node() == &rootNodeInOriginTreeScope)) {
m_path.shrink(contextIndex + 1);
break;
}
previousTreeScope = ¤tTreeScope;
}
}
#if ENABLE(TOUCH_EVENTS)
void EventPath::retargetTouch(TouchEventContext::TouchListType touchListType, const Touch& touch)
{
EventTarget* eventTarget = touch.target();
if (!eventTarget)
return;
Node* targetNode = eventTarget->toNode();
if (!targetNode)
return;
RelatedNodeRetargeter retargeter(*targetNode, m_path[0]->node()->treeScope());
TreeScope* previousTreeScope = nullptr;
for (auto& context : m_path) {
TreeScope& currentTreeScope = context->node()->treeScope();
if (UNLIKELY(previousTreeScope && ¤tTreeScope != previousTreeScope))
retargeter.moveToNewTreeScope(previousTreeScope, currentTreeScope);
Node* currentRelatedNode = retargeter.currentNode(currentTreeScope);
downcast<TouchEventContext>(*context).touchList(touchListType)->append(touch.cloneWithNewTarget(currentRelatedNode));
previousTreeScope = ¤tTreeScope;
}
}
void EventPath::retargetTouchLists(const TouchEvent& touchEvent)
{
if (touchEvent.touches()) {
for (size_t i = 0; i < touchEvent.touches()->length(); ++i)
retargetTouch(TouchEventContext::Touches, *touchEvent.touches()->item(i));
}
if (touchEvent.targetTouches()) {
for (size_t i = 0; i < touchEvent.targetTouches()->length(); ++i)
retargetTouch(TouchEventContext::TargetTouches, *touchEvent.targetTouches()->item(i));
}
if (touchEvent.changedTouches()) {
for (size_t i = 0; i < touchEvent.changedTouches()->length(); ++i)
retargetTouch(TouchEventContext::ChangedTouches, *touchEvent.changedTouches()->item(i));
}
}
#endif
bool EventPath::hasEventListeners(const AtomicString& eventType) const
{
for (auto& eventPath : m_path) {
if (eventPath->node()->hasEventListeners(eventType))
return true;
}
return false;
}
}
|