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/EventDispatchMediator.h"
28 #include "core/events/EventTarget.h"
29 #include "core/frame/OriginsUsingFeatures.h"
30 #include "core/frame/UseCounter.h"
31 #include "core/svg/SVGElement.h"
32 #include "wtf/CurrentTime.h"
37 : Event("", false, false)
41 Event::Event(const AtomicString
& eventType
, bool canBubbleArg
, bool cancelableArg
)
43 , m_canBubble(canBubbleArg
)
44 , m_cancelable(cancelableArg
)
45 , m_propagationStopped(false)
46 , m_immediatePropagationStopped(false)
47 , m_defaultPrevented(false)
48 , m_defaultHandled(false)
49 , m_cancelBubble(false)
52 , m_currentTarget(nullptr)
53 , m_createTime(convertSecondsToDOMTimeStamp(currentTime()))
58 Event::Event(const AtomicString
& eventType
, const EventInit
& initializer
)
59 : Event(eventType
, initializer
.bubbles(), initializer
.cancelable())
67 void Event::initEvent(const AtomicString
& eventTypeArg
, bool canBubbleArg
, bool cancelableArg
)
72 m_propagationStopped
= false;
73 m_immediatePropagationStopped
= false;
74 m_defaultPrevented
= false;
77 m_type
= eventTypeArg
;
78 m_canBubble
= canBubbleArg
;
79 m_cancelable
= cancelableArg
;
82 bool Event::legacyReturnValue(ExecutionContext
* executionContext
) const
84 bool returnValue
= !defaultPrevented();
86 UseCounter::count(executionContext
, UseCounter::EventGetReturnValueTrue
);
88 UseCounter::count(executionContext
, UseCounter::EventGetReturnValueFalse
);
92 void Event::setLegacyReturnValue(ExecutionContext
* executionContext
, bool returnValue
)
95 UseCounter::count(executionContext
, UseCounter::EventSetReturnValueTrue
);
97 UseCounter::count(executionContext
, UseCounter::EventSetReturnValueFalse
);
98 setDefaultPrevented(!returnValue
);
101 const AtomicString
& Event::interfaceName() const
103 return EventNames::Event
;
106 bool Event::hasInterface(const AtomicString
& name
) const
108 return interfaceName() == name
;
111 bool Event::isUIEvent() const
116 bool Event::isMouseEvent() const
121 bool Event::isFocusEvent() const
126 bool Event::isKeyboardEvent() const
131 bool Event::isTouchEvent() const
136 bool Event::isGestureEvent() const
141 bool Event::isWheelEvent() const
146 bool Event::isRelatedEvent() const
151 bool Event::isPointerEvent() const
156 bool Event::isDragEvent() const
161 bool Event::isClipboardEvent() const
166 bool Event::isBeforeTextInsertedEvent() const
171 bool Event::isBeforeUnloadEvent() const
176 void Event::setTarget(PassRefPtrWillBeRawPtr
<EventTarget
> target
)
178 if (m_target
== target
)
186 void Event::receivedTarget()
190 void Event::setUnderlyingEvent(PassRefPtrWillBeRawPtr
<Event
> ue
)
192 // Prohibit creation of a cycle -- just do nothing in that case.
193 for (Event
* e
= ue
.get(); e
; e
= e
->underlyingEvent())
196 m_underlyingEvent
= ue
;
199 void Event::initEventPath(Node
& node
)
202 m_eventPath
= adoptPtrWillBeNoop(new EventPath(node
, this));
204 m_eventPath
->initializeWith(node
, this);
208 WillBeHeapVector
<RefPtrWillBeMember
<EventTarget
>> Event::path(ScriptState
* scriptState
) const
211 OriginsUsingFeatures::countOriginOrIsolatedWorldHumanReadableName(scriptState
, *m_target
, OriginsUsingFeatures::Feature::EventPath
);
213 if (!m_currentTarget
) {
214 ASSERT(m_eventPhase
== Event::NONE
);
216 // Before dispatching the event
217 return WillBeHeapVector
<RefPtrWillBeMember
<EventTarget
>>();
219 ASSERT(!m_eventPath
->isEmpty());
220 // After dispatching the event
221 return m_eventPath
->last().treeScopeEventContext().ensureEventPath(*m_eventPath
);
224 if (Node
* node
= m_currentTarget
->toNode()) {
226 size_t eventPathSize
= m_eventPath
->size();
227 for (size_t i
= 0; i
< eventPathSize
; ++i
) {
228 if (node
== (*m_eventPath
)[i
].node()) {
229 return (*m_eventPath
)[i
].treeScopeEventContext().ensureEventPath(*m_eventPath
);
232 ASSERT_NOT_REACHED();
235 // Returns [window] for events that are directly dispatched to the window object;
236 // e.g., window.load, pageshow, etc.
237 if (LocalDOMWindow
* window
= m_currentTarget
->toDOMWindow())
238 return WillBeHeapVector
<RefPtrWillBeMember
<EventTarget
>>(1, window
);
240 return WillBeHeapVector
<RefPtrWillBeMember
<EventTarget
>>();
243 PassRefPtrWillBeRawPtr
<EventDispatchMediator
> Event::createMediator()
245 return EventDispatchMediator::create(this);
248 EventTarget
* Event::currentTarget() const
250 if (!m_currentTarget
)
252 Node
* node
= m_currentTarget
->toNode();
253 if (node
&& node
->isSVGElement()) {
254 if (SVGElement
* svgElement
= toSVGElement(node
)->correspondingElement())
257 return m_currentTarget
.get();
262 visitor
->trace(m_currentTarget
);
263 visitor
->trace(m_target
);
264 visitor
->trace(m_underlyingEvent
);
265 visitor
->trace(m_eventPath
);