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()))
53 ScriptWrappable::init(this);
56 Event::Event(const AtomicString& eventType, bool canBubbleArg, bool cancelableArg)
58 , m_canBubble(canBubbleArg)
59 , m_cancelable(cancelableArg)
60 , m_propagationStopped(false)
61 , m_immediatePropagationStopped(false)
62 , m_defaultPrevented(false)
63 , m_defaultHandled(false)
64 , m_cancelBubble(false)
66 , m_currentTarget(nullptr)
67 , m_createTime(convertSecondsToDOMTimeStamp(currentTime()))
69 ScriptWrappable::init(this);
72 Event::Event(const AtomicString& eventType, const EventInit& initializer)
74 , m_canBubble(initializer.bubbles)
75 , m_cancelable(initializer.cancelable)
76 , m_propagationStopped(false)
77 , m_immediatePropagationStopped(false)
78 , m_defaultPrevented(false)
79 , m_defaultHandled(false)
80 , m_cancelBubble(false)
82 , m_currentTarget(nullptr)
83 , m_createTime(convertSecondsToDOMTimeStamp(currentTime()))
85 ScriptWrappable::init(this);
92 void Event::initEvent(const AtomicString& eventTypeArg, bool canBubbleArg, bool cancelableArg)
97 m_propagationStopped = false;
98 m_immediatePropagationStopped = false;
99 m_defaultPrevented = false;
101 m_type = eventTypeArg;
102 m_canBubble = canBubbleArg;
103 m_cancelable = cancelableArg;
106 bool Event::legacyReturnValue(ExecutionContext* executionContext) const
108 bool returnValue = !defaultPrevented();
110 UseCounter::count(executionContext, UseCounter::EventGetReturnValueTrue);
112 UseCounter::count(executionContext, UseCounter::EventGetReturnValueFalse);
116 void Event::setLegacyReturnValue(ExecutionContext* executionContext, bool returnValue)
119 UseCounter::count(executionContext, UseCounter::EventSetReturnValueTrue);
121 UseCounter::count(executionContext, UseCounter::EventSetReturnValueFalse);
122 setDefaultPrevented(!returnValue);
125 const AtomicString& Event::interfaceName() const
127 return EventNames::Event;
130 bool Event::hasInterface(const AtomicString& name) const
132 return interfaceName() == name;
135 bool Event::isUIEvent() const
140 bool Event::isMouseEvent() const
145 bool Event::isFocusEvent() const
150 bool Event::isKeyboardEvent() const
155 bool Event::isTouchEvent() const
160 bool Event::isGestureEvent() const
165 bool Event::isWheelEvent() const
170 bool Event::isRelatedEvent() const
175 bool Event::isDragEvent() const
180 bool Event::isClipboardEvent() const
185 bool Event::isBeforeTextInsertedEvent() const
190 bool Event::isBeforeUnloadEvent() const
195 void Event::setTarget(PassRefPtrWillBeRawPtr<EventTarget> target)
197 if (m_target == target)
205 void Event::receivedTarget()
209 void Event::setUnderlyingEvent(PassRefPtrWillBeRawPtr<Event> ue)
211 // Prohibit creation of a cycle -- just do nothing in that case.
212 for (Event* e = ue.get(); e; e = e->underlyingEvent())
215 m_underlyingEvent = ue;
218 EventPath& Event::ensureEventPath()
221 m_eventPath = adoptPtrWillBeNoop(new EventPath(this));
225 PassRefPtrWillBeRawPtr<StaticNodeList> Event::path() const
227 if (!m_currentTarget) {
228 ASSERT(m_eventPhase == PhaseType::NONE);
230 // Before dispatching the event
231 return StaticNodeList::createEmpty();
233 ASSERT(!m_eventPath->isEmpty());
234 // After dispatching the event
235 return m_eventPath->last().treeScopeEventContext().ensureEventPath(*m_eventPath);
237 if (!m_currentTarget->toNode())
238 return StaticNodeList::createEmpty();
239 Node* node = m_currentTarget->toNode();
240 size_t eventPathSize = m_eventPath->size();
241 for (size_t i = 0; i < eventPathSize; ++i) {
242 if (node == (*m_eventPath)[i].node()) {
243 return (*m_eventPath)[i].treeScopeEventContext().ensureEventPath(*m_eventPath);
246 return StaticNodeList::createEmpty();
249 EventTarget* Event::currentTarget() const
251 if (!m_currentTarget)
253 Node* node = m_currentTarget->toNode();
254 if (node && node->isSVGElement()) {
255 if (SVGElement* svgElement = toSVGElement(node)->correspondingElement())
258 return m_currentTarget;
261 void Event::trace(Visitor* visitor)
263 visitor->trace(m_currentTarget);
264 visitor->trace(m_target);
265 visitor->trace(m_underlyingEvent);
266 visitor->trace(m_eventPath);