2008-11-04 Anders Carlsson <andersca@apple.com>
[webkit/qt.git] / WebCore / bindings / objc / DOMUtility.mm
blobe346c9eeaa0f749ad90414086355796be18c4bad
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2006 James G. Speth (speth@end.com)
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
25  */
27 #import "config.h"
29 #import "DOMImplementationFront.h"
30 #import "DOMInternal.h"
31 #import "JSCSSRule.h"
32 #import "JSCSSRuleList.h"
33 #import "JSCSSStyleDeclaration.h"
34 #import "JSCSSValue.h"
35 #import "JSCounter.h"
36 #import "JSDOMImplementation.h"
37 #import "JSDOMWindow.h"
38 #import "JSDOMWindowShell.h"
39 #import "JSEvent.h"
40 #import "JSHTMLCollection.h"
41 #import "JSHTMLOptionsCollection.h"
42 #import "JSMediaList.h"
43 #import "JSNamedNodeMap.h"
44 #import "JSNode.h"
45 #import "JSNodeIterator.h"
46 #import "JSNodeList.h"
47 #import "JSRGBColor.h"
48 #import "JSRange.h"
49 #import "JSRect.h"
50 #import "JSStyleSheet.h"
51 #import "JSStyleSheetList.h"
52 #import "JSTreeWalker.h"
53 #import "JSXPathExpression.h"
54 #import "JSXPathResult.h"
55 #import "Node.h"
56 #import "WebScriptObjectPrivate.h"
57 #import "runtime_root.h"
58 #import <objc/objc-runtime.h>
60 // This file makes use of both the ObjC DOM API and the C++ DOM API, so we need to be careful about what
61 // headers are included and what namespaces we use to avoid naming conflicts.
63 // FIXME: This has to be in the KJS namespace to avoid an Objective-C++ ambiguity with C++ and
64 // Objective-C classes of the same name (even when not in the same namespace). That's also the
65 // reason for the use of objc_getClass in the WRAP_OLD macro below.
67 // Some day if the compiler is fixed, or if all the JS wrappers are named with a "JS" prefix,
68 // we could move the function into the WebCore namespace where it belongs.
70 namespace JSC {
72 static inline id createDOMWrapper(JSC::JSObject* object)
74     #define WRAP(className) \
75         if (object->inherits(&WebCore::JS##className::s_info)) \
76             return [DOM##className _wrap##className:static_cast<WebCore::JS##className*>(object)->impl()];
78     WRAP(CSSRule)
79     WRAP(CSSRuleList)
80     WRAP(CSSStyleDeclaration)
81     WRAP(CSSValue)
82     WRAP(Counter)
83     WRAP(Event)
84     WRAP(HTMLOptionsCollection)
85     WRAP(MediaList)
86     WRAP(NamedNodeMap)
87     WRAP(Node)
88     WRAP(NodeIterator)
89     WRAP(NodeList)
90     WRAP(RGBColor)
91     WRAP(Range)
92     WRAP(Rect)
93     WRAP(StyleSheet)
94     WRAP(StyleSheetList)
95     WRAP(TreeWalker)
96 #if ENABLE(XPATH)
97     WRAP(XPathExpression)
98     WRAP(XPathResult)
99 #endif
101     // This must be after the HTMLOptionsCollection check, because it's a subclass in the JavaScript
102     // binding, but not a subclass in the ObjC binding.
103     WRAP(HTMLCollection)
105     #undef WRAP
107     if (object->inherits(&WebCore::JSDOMWindowShell::s_info))
108         return [DOMAbstractView _wrapAbstractView:static_cast<WebCore::JSDOMWindowShell*>(object)->impl()];
110     if (object->inherits(&WebCore::JSDOMImplementation::s_info))
111         return [DOMImplementation _wrapDOMImplementation:implementationFront(static_cast<WebCore::JSDOMImplementation*>(object))];
113     return nil;
118 namespace WebCore {
120 id createDOMWrapper(JSC::JSObject* object, PassRefPtr<JSC::Bindings::RootObject> origin, PassRefPtr<JSC::Bindings::RootObject> current)
122     id wrapper = JSC::createDOMWrapper(object);
123     if (![wrapper _hasImp]) // new wrapper, not from cache
124         [wrapper _setImp:object originRootObject:origin rootObject:current];
125     return wrapper;