Home | History | Annotate | Download | only in gobject
      1 /*
      2  *  Copyright (C) 1999-2001 Harri Porten (porten (at) kde.org)
      3  *  Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
      4  *  Copyright (C) 2007 Samuel Weinig <sam (at) webkit.org>
      5  *  Copyright (C) 2008 Luke Kenneth Casson Leighton <lkcl (at) lkcl.net>
      6  *  Copyright (C) 2008 Martin Soto <soto (at) freedesktop.org>
      7  *  Copyright (C) 2009, 2010 Igalia S.L.
      8  *
      9  *  This library is free software; you can redistribute it and/or
     10  *  modify it under the terms of the GNU Lesser General Public
     11  *  License as published by the Free Software Foundation; either
     12  *  version 2 of the License, or (at your option) any later version.
     13  *
     14  *  This library is distributed in the hope that it will be useful,
     15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     17  *  Lesser General Public License for more details.
     18  *
     19  *  You should have received a copy of the GNU Lesser General Public
     20  *  License along with this library; if not, write to the Free Software
     21  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     22  */
     23 
     24 #include "config.h"
     25 #include "WebKitDOMBinding.h"
     26 
     27 #include "DOMObjectCache.h"
     28 #include "Element.h"
     29 #include "Event.h"
     30 #include "EventException.h"
     31 #include "HTMLNames.h"
     32 #include "MouseEvent.h"
     33 #include "UIEvent.h"
     34 #include "WebKitDOMDOMWindowPrivate.h"
     35 #include "WebKitDOMElementPrivate.h"
     36 #include "WebKitDOMEventPrivate.h"
     37 #include "WebKitDOMNode.h"
     38 #include "WebKitDOMNodePrivate.h"
     39 #include "WebKitHTMLElementWrapperFactory.h"
     40 #include "webkit/WebKitDOMMouseEventPrivate.h"
     41 #include "webkit/WebKitDOMUIEventPrivate.h"
     42 
     43 namespace WebKit {
     44 
     45 using namespace WebCore;
     46 using namespace WebCore::HTMLNames;
     47 
     48 // kit methods
     49 
     50 static gpointer createWrapper(Node* node)
     51 {
     52     ASSERT(node);
     53     ASSERT(node->nodeType());
     54 
     55     gpointer wrappedNode = 0;
     56 
     57     switch (node->nodeType()) {
     58     case Node::ELEMENT_NODE:
     59         if (node->isHTMLElement())
     60             wrappedNode = createHTMLElementWrapper(toHTMLElement(node));
     61         else
     62             wrappedNode = wrapElement(static_cast<Element*>(node));
     63         break;
     64     default:
     65         wrappedNode = wrapNode(node);
     66         break;
     67     }
     68 
     69     return DOMObjectCache::put(node, wrappedNode);
     70 }
     71 
     72 WebKitDOMNode* kit(Node* node)
     73 {
     74     if (!node)
     75         return 0;
     76 
     77     gpointer kitNode = DOMObjectCache::get(node);
     78     if (kitNode)
     79         return static_cast<WebKitDOMNode*>(kitNode);
     80 
     81     return static_cast<WebKitDOMNode*>(createWrapper(node));
     82 }
     83 
     84 WebKitDOMElement* kit(Element* element)
     85 {
     86     if (!element)
     87         return 0;
     88 
     89     gpointer kitElement = DOMObjectCache::get(element);
     90     if (kitElement)
     91         return static_cast<WebKitDOMElement*>(kitElement);
     92 
     93     gpointer wrappedElement;
     94 
     95     if (element->isHTMLElement())
     96         wrappedElement = createHTMLElementWrapper(toHTMLElement(element));
     97     else
     98         wrappedElement = wrapElement(element);
     99 
    100     return static_cast<WebKitDOMElement*>(DOMObjectCache::put(element, wrappedElement));
    101 }
    102 
    103 WebKitDOMEvent* kit(Event* event)
    104 {
    105     if (!event)
    106         return 0;
    107 
    108     gpointer kitEvent = DOMObjectCache::get(event);
    109     if (kitEvent)
    110         return static_cast<WebKitDOMEvent*>(kitEvent);
    111 
    112     gpointer wrappedEvent;
    113 
    114     if (event->isMouseEvent())
    115         wrappedEvent = wrapMouseEvent(static_cast<MouseEvent*>(event));
    116     else if (event->isUIEvent())
    117         wrappedEvent = wrapUIEvent(static_cast<UIEvent*>(event));
    118     else
    119         wrappedEvent = wrapEvent(event);
    120 
    121     return static_cast<WebKitDOMEvent*>(DOMObjectCache::put(event, wrappedEvent));
    122 }
    123 
    124 static gpointer wrapEventTarget(EventTarget* target)
    125 {
    126     ASSERT(target);
    127 
    128     gpointer wrappedTarget = 0;
    129 
    130     if (target->toNode()) {
    131         Node* node = target->toNode();
    132         wrappedTarget = wrapNode(node);
    133     } else if (target->toDOMWindow()) {
    134         DOMWindow* window = target->toDOMWindow();
    135         wrappedTarget = wrapDOMWindow(window);
    136     }
    137 
    138     return DOMObjectCache::put(target, wrappedTarget);
    139 }
    140 
    141 WebKitDOMEventTarget* kit(WebCore::EventTarget* obj)
    142 {
    143     g_return_val_if_fail(obj, 0);
    144 
    145     if (gpointer ret = DOMObjectCache::get(obj))
    146         return static_cast<WebKitDOMEventTarget*>(ret);
    147 
    148     return static_cast<WebKitDOMEventTarget*>(DOMObjectCache::put(obj, WebKit::wrapEventTarget(obj)));
    149 }
    150 
    151 } // namespace WebKit
    152