2 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
3 * Copyright (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de)
4 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
5 * Copyright (C) 2003, 2005, 2006, 2008 Apple Inc. All rights reserved.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 #include "core/events/Event.h"
26 #include "core/dom/StaticNodeList.h"
27 #include "core/events/EventTarget.h"
28 #include "core/frame/UseCounter.h"
29 #include "core/svg/SVGElement.h"
30 #include "wtf/CurrentTime.h"
34 EventInit::EventInit()
44 , m_propagationStopped(false)
45 , m_immediatePropagationStopped(false)
46 , m_defaultPrevented(false)
47 , m_defaultHandled(false)
48 , m_cancelBubble(false)
50 , m_currentTarget(nullptr)
51 , m_createTime(convertSecondsToDOMTimeStamp(currentTime()))
55 Event::Event(const AtomicString& eventType, bool canBubbleArg, bool cancelableArg)
57 , m_canBubble(canBubbleArg)
58 , m_cancelable(cancelableArg)
59 , m_propagationStopped(false)
60 , m_immediatePropagationStopped(false)
61 , m_defaultPrevented(false)
62 , m_defaultHandled(false)
63 , m_cancelBubble(false)
65 , m_currentTarget(nullptr)
66 , m_createTime(convertSecondsToDOMTimeStamp(currentTime()))
70 Event::Event(const AtomicString& eventType, const EventInit& initializer)
72 , m_canBubble(initializer.bubbles)
73 , m_cancelable(initializer.cancelable)
74 , m_propagationStopped(false)
75 , m_immediatePropagationStopped(false)
76 , m_defaultPrevented(false)
77 , m_defaultHandled(false)
78 , m_cancelBubble(false)
80 , m_currentTarget(nullptr)
81 , m_createTime(convertSecondsToDOMTimeStamp(currentTime()))
89 void Event::initEvent(const AtomicString& eventTypeArg, bool canBubbleArg, bool cancelableArg)
94 m_propagationStopped = false;
95 m_immediatePropagationStopped = false;
96 m_defaultPrevented = false;
98 m_type = eventTypeArg;
99 m_canBubble = canBubbleArg;
100 m_cancelable = cancelableArg;
103 bool Event::legacyReturnValue(ExecutionContext* executionContext) const
105 bool returnValue = !defaultPrevented();
107 UseCounter::count(executionContext, UseCounter::EventGetReturnValueTrue);
109 UseCounter::count(executionContext, UseCounter::EventGetReturnValueFalse);
113 void Event::setLegacyReturnValue(ExecutionContext* executionContext, bool returnValue)
116 UseCounter::count(executionContext, UseCounter::EventSetReturnValueTrue);
118 UseCounter::count(executionContext, UseCounter::EventSetReturnValueFalse);
119 setDefaultPrevented(!returnValue);
122 const AtomicString& Event::interfaceName() const
124 return EventNames::Event;
127 bool Event::hasInterface(const AtomicString& name) const
129 return interfaceName() == name;
132 bool Event::isUIEvent() const
137 bool Event::isMouseEvent() const
142 bool Event::isFocusEvent() const
147 bool Event::isKeyboardEvent() const
152 bool Event::isTouchEvent() const
157 bool Event::isGestureEvent() const
162 bool Event::isWheelEvent() const
167 bool Event::isRelatedEvent() const
172 bool Event::isDragEvent() const
177 bool Event::isClipboardEvent() const
182 bool Event::isBeforeTextInsertedEvent() const
187 bool Event::isBeforeUnloadEvent() const
192 void Event::setTarget(PassRefPtrWillBeRawPtr<EventTarget> target)
194 if (m_target == target)
202 void Event::receivedTarget()
206 void Event::setUnderlyingEvent(PassRefPtrWillBeRawPtr<Event> ue)
208 // Prohibit creation of a cycle -- just do nothing in that case.
209 for (Event* e = ue.get(); e; e = e->underlyingEvent())
212 m_underlyingEvent = ue;
215 EventPath& Event::ensureEventPath()
218 m_eventPath = adoptPtrWillBeNoop(new EventPath(this));
222 PassRefPtrWillBeRawPtr<StaticNodeList> Event::path() const
224 if (!m_currentTarget) {
225 ASSERT(m_eventPhase == Event::NONE);
227 // Before dispatching the event
228 return StaticNodeList::createEmpty();
230 ASSERT(!m_eventPath->isEmpty());
231 // After dispatching the event
232 return m_eventPath->last().treeScopeEventContext().ensureEventPath(*m_eventPath);
234 if (!m_currentTarget->toNode())
235 return StaticNodeList::createEmpty();
236 Node* node = m_currentTarget->toNode();
237 size_t eventPathSize = m_eventPath->size();
238 for (size_t i = 0; i < eventPathSize; ++i) {
239 if (node == (*m_eventPath)[i].node()) {
240 return (*m_eventPath)[i].treeScopeEventContext().ensureEventPath(*m_eventPath);
243 return StaticNodeList::createEmpty();
246 EventTarget* Event::currentTarget() const
248 if (!m_currentTarget)
250 Node* node = m_currentTarget->toNode();
251 if (node && node->isSVGElement()) {
252 if (SVGElement* svgElement = toSVGElement(node)->correspondingElement())
255 return m_currentTarget.get();
258 void Event::trace(Visitor* visitor)
260 visitor->trace(m_currentTarget);
261 visitor->trace(m_target);
262 visitor->trace(m_underlyingEvent);
263 visitor->trace(m_eventPath);