Home | History | Annotate | Download | only in events
      1 /*
      2  * Copyright (C) 2001 Peter Kelly (pmk (at) post.com)
      3  * Copyright (C) 2001 Tobias Anton (anton (at) stud.fbi.fh-darmstadt.de)
      4  * Copyright (C) 2006 Samuel Weinig (sam.weinig (at) gmail.com)
      5  * Copyright (C) 2003, 2005, 2006, 2008 Apple Inc. All rights reserved.
      6  *
      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.
     11  *
     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.
     16  *
     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.
     21  */
     22 
     23 #include "config.h"
     24 #include "core/events/Event.h"
     25 
     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"
     31 
     32 namespace WebCore {
     33 
     34 EventInit::EventInit()
     35     : bubbles(false)
     36     , cancelable(false)
     37 {
     38 }
     39 
     40 
     41 Event::Event()
     42     : m_canBubble(false)
     43     , m_cancelable(false)
     44     , m_propagationStopped(false)
     45     , m_immediatePropagationStopped(false)
     46     , m_defaultPrevented(false)
     47     , m_defaultHandled(false)
     48     , m_cancelBubble(false)
     49     , m_eventPhase(0)
     50     , m_currentTarget(nullptr)
     51     , m_createTime(convertSecondsToDOMTimeStamp(currentTime()))
     52 {
     53     ScriptWrappable::init(this);
     54 }
     55 
     56 Event::Event(const AtomicString& eventType, bool canBubbleArg, bool cancelableArg)
     57     : m_type(eventType)
     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)
     65     , m_eventPhase(0)
     66     , m_currentTarget(nullptr)
     67     , m_createTime(convertSecondsToDOMTimeStamp(currentTime()))
     68 {
     69     ScriptWrappable::init(this);
     70 }
     71 
     72 Event::Event(const AtomicString& eventType, const EventInit& initializer)
     73     : m_type(eventType)
     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)
     81     , m_eventPhase(0)
     82     , m_currentTarget(nullptr)
     83     , m_createTime(convertSecondsToDOMTimeStamp(currentTime()))
     84 {
     85     ScriptWrappable::init(this);
     86 }
     87 
     88 Event::~Event()
     89 {
     90 }
     91 
     92 void Event::initEvent(const AtomicString& eventTypeArg, bool canBubbleArg, bool cancelableArg)
     93 {
     94     if (dispatched())
     95         return;
     96 
     97     m_propagationStopped = false;
     98     m_immediatePropagationStopped = false;
     99     m_defaultPrevented = false;
    100 
    101     m_type = eventTypeArg;
    102     m_canBubble = canBubbleArg;
    103     m_cancelable = cancelableArg;
    104 }
    105 
    106 bool Event::legacyReturnValue(ExecutionContext* executionContext) const
    107 {
    108     bool returnValue = !defaultPrevented();
    109     if (returnValue)
    110         UseCounter::count(executionContext, UseCounter::EventGetReturnValueTrue);
    111     else
    112         UseCounter::count(executionContext, UseCounter::EventGetReturnValueFalse);
    113     return returnValue;
    114 }
    115 
    116 void Event::setLegacyReturnValue(ExecutionContext* executionContext, bool returnValue)
    117 {
    118     if (returnValue)
    119         UseCounter::count(executionContext, UseCounter::EventSetReturnValueTrue);
    120     else
    121         UseCounter::count(executionContext, UseCounter::EventSetReturnValueFalse);
    122     setDefaultPrevented(!returnValue);
    123 }
    124 
    125 const AtomicString& Event::interfaceName() const
    126 {
    127     return EventNames::Event;
    128 }
    129 
    130 bool Event::hasInterface(const AtomicString& name) const
    131 {
    132     return interfaceName() == name;
    133 }
    134 
    135 bool Event::isUIEvent() const
    136 {
    137     return false;
    138 }
    139 
    140 bool Event::isMouseEvent() const
    141 {
    142     return false;
    143 }
    144 
    145 bool Event::isFocusEvent() const
    146 {
    147     return false;
    148 }
    149 
    150 bool Event::isKeyboardEvent() const
    151 {
    152     return false;
    153 }
    154 
    155 bool Event::isTouchEvent() const
    156 {
    157     return false;
    158 }
    159 
    160 bool Event::isGestureEvent() const
    161 {
    162     return false;
    163 }
    164 
    165 bool Event::isWheelEvent() const
    166 {
    167     return false;
    168 }
    169 
    170 bool Event::isDragEvent() const
    171 {
    172     return false;
    173 }
    174 
    175 bool Event::isClipboardEvent() const
    176 {
    177     return false;
    178 }
    179 
    180 bool Event::isBeforeTextInsertedEvent() const
    181 {
    182     return false;
    183 }
    184 
    185 bool Event::isBeforeUnloadEvent() const
    186 {
    187     return false;
    188 }
    189 
    190 void Event::setTarget(PassRefPtrWillBeRawPtr<EventTarget> target)
    191 {
    192     if (m_target == target)
    193         return;
    194 
    195     m_target = target;
    196     if (m_target)
    197         receivedTarget();
    198 }
    199 
    200 void Event::receivedTarget()
    201 {
    202 }
    203 
    204 void Event::setUnderlyingEvent(PassRefPtrWillBeRawPtr<Event> ue)
    205 {
    206     // Prohibit creation of a cycle -- just do nothing in that case.
    207     for (Event* e = ue.get(); e; e = e->underlyingEvent())
    208         if (e == this)
    209             return;
    210     m_underlyingEvent = ue;
    211 }
    212 
    213 EventPath& Event::ensureEventPath()
    214 {
    215     if (!m_eventPath)
    216         m_eventPath = adoptPtrWillBeNoop(new EventPath(this));
    217     return *m_eventPath;
    218 }
    219 
    220 PassRefPtrWillBeRawPtr<StaticNodeList> Event::path() const
    221 {
    222     if (!m_currentTarget || !m_currentTarget->toNode())
    223         return StaticNodeList::createEmpty();
    224     Node* node = m_currentTarget->toNode();
    225     // FIXME: Support SVG Elements.
    226     if (node->isSVGElement())
    227         return StaticNodeList::createEmpty();
    228     size_t eventPathSize = m_eventPath->size();
    229     for (size_t i = 0; i < eventPathSize; ++i) {
    230         if (node == (*m_eventPath)[i].node()) {
    231             return (*m_eventPath)[i].treeScopeEventContext().ensureEventPath(*m_eventPath);
    232         }
    233     }
    234     return StaticNodeList::createEmpty();
    235 }
    236 
    237 EventTarget* Event::currentTarget() const
    238 {
    239     if (!m_currentTarget)
    240         return 0;
    241     Node* node = m_currentTarget->toNode();
    242     if (node && node->isSVGElement()) {
    243         if (SVGElement* svgElement = toSVGElement(node)->correspondingElement())
    244             return svgElement;
    245     }
    246     return m_currentTarget;
    247 }
    248 
    249 void Event::trace(Visitor* visitor)
    250 {
    251     visitor->trace(m_currentTarget);
    252     visitor->trace(m_target);
    253     visitor->trace(m_underlyingEvent);
    254     visitor->trace(m_eventPath);
    255 }
    256 
    257 } // namespace WebCore
    258