Drop ExceptionCodeDescription class
[WebKit-https.git] / Source / WebKit / WebProcess / InjectedBundle / API / gtk / DOM / WebKitDOMDocumentFragment.cpp
1 /*
2  *  This file is part of the WebKit open source project.
3  *
4  *  This library is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU Library General Public
6  *  License as published by the Free Software Foundation; either
7  *  version 2 of the License, or (at your option) any later version.
8  *
9  *  This library is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  Library General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Library General Public License
15  *  along with this library; see the file COPYING.LIB.  If not, write to
16  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  *  Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21 #include "WebKitDOMDocumentFragment.h"
22
23 #include <WebCore/CSSImportRule.h>
24 #include "DOMObjectCache.h"
25 #include <WebCore/DOMException.h>
26 #include <WebCore/Document.h>
27 #include "GObjectEventListener.h"
28 #include <WebCore/JSMainThreadExecState.h>
29 #include "WebKitDOMDocumentFragmentPrivate.h"
30 #include "WebKitDOMElementPrivate.h"
31 #include "WebKitDOMEventPrivate.h"
32 #include "WebKitDOMEventTarget.h"
33 #include "WebKitDOMHTMLCollectionPrivate.h"
34 #include "WebKitDOMNodeListPrivate.h"
35 #include "WebKitDOMNodePrivate.h"
36 #include "WebKitDOMPrivate.h"
37 #include "ConvertToUTF8String.h"
38 #include "WebKitDOMDocumentFragmentUnstable.h"
39 #include <wtf/GetPtr.h>
40 #include <wtf/RefPtr.h>
41
42 namespace WebKit {
43
44 WebKitDOMDocumentFragment* kit(WebCore::DocumentFragment* obj)
45 {
46     return WEBKIT_DOM_DOCUMENT_FRAGMENT(kit(static_cast<WebCore::Node*>(obj)));
47 }
48
49 WebCore::DocumentFragment* core(WebKitDOMDocumentFragment* request)
50 {
51     return request ? static_cast<WebCore::DocumentFragment*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
52 }
53
54 WebKitDOMDocumentFragment* wrapDocumentFragment(WebCore::DocumentFragment* coreObject)
55 {
56     ASSERT(coreObject);
57     return WEBKIT_DOM_DOCUMENT_FRAGMENT(g_object_new(WEBKIT_DOM_TYPE_DOCUMENT_FRAGMENT, "core-object", coreObject, nullptr));
58 }
59
60 } // namespace WebKit
61
62 static gboolean webkit_dom_document_fragment_dispatch_event(WebKitDOMEventTarget* target, WebKitDOMEvent* event, GError** error)
63 {
64     WebCore::Event* coreEvent = WebKit::core(event);
65     if (!coreEvent)
66         return false;
67     WebCore::DocumentFragment* coreTarget = static_cast<WebCore::DocumentFragment*>(WEBKIT_DOM_OBJECT(target)->coreObject);
68
69     auto result = coreTarget->dispatchEventForBindings(*coreEvent);
70     if (result.hasException()) {
71         auto description = WebCore::DOMException::description(result.releaseException().code());
72         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
73         return false;
74     }
75     return result.releaseReturnValue();
76 }
77
78 static gboolean webkit_dom_document_fragment_add_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
79 {
80     WebCore::DocumentFragment* coreTarget = static_cast<WebCore::DocumentFragment*>(WEBKIT_DOM_OBJECT(target)->coreObject);
81     return WebKit::GObjectEventListener::addEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
82 }
83
84 static gboolean webkit_dom_document_fragment_remove_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
85 {
86     WebCore::DocumentFragment* coreTarget = static_cast<WebCore::DocumentFragment*>(WEBKIT_DOM_OBJECT(target)->coreObject);
87     return WebKit::GObjectEventListener::removeEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
88 }
89
90 static void webkit_dom_event_target_init(WebKitDOMEventTargetIface* iface)
91 {
92     iface->dispatch_event = webkit_dom_document_fragment_dispatch_event;
93     iface->add_event_listener = webkit_dom_document_fragment_add_event_listener;
94     iface->remove_event_listener = webkit_dom_document_fragment_remove_event_listener;
95 }
96
97 G_DEFINE_TYPE_WITH_CODE(WebKitDOMDocumentFragment, webkit_dom_document_fragment, WEBKIT_DOM_TYPE_NODE, G_IMPLEMENT_INTERFACE(WEBKIT_DOM_TYPE_EVENT_TARGET, webkit_dom_event_target_init))
98
99 enum {
100     PROP_0,
101     PROP_CHILDREN,
102     PROP_FIRST_ELEMENT_CHILD,
103     PROP_LAST_ELEMENT_CHILD,
104     PROP_CHILD_ELEMENT_COUNT,
105 };
106
107 static void webkit_dom_document_fragment_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
108 {
109     WebKitDOMDocumentFragment* self = WEBKIT_DOM_DOCUMENT_FRAGMENT(object);
110
111     switch (propertyId) {
112     case PROP_CHILDREN:
113         g_value_set_object(value, webkit_dom_document_fragment_get_children(self));
114         break;
115     case PROP_FIRST_ELEMENT_CHILD:
116         g_value_set_object(value, webkit_dom_document_fragment_get_first_element_child(self));
117         break;
118     case PROP_LAST_ELEMENT_CHILD:
119         g_value_set_object(value, webkit_dom_document_fragment_get_last_element_child(self));
120         break;
121     case PROP_CHILD_ELEMENT_COUNT:
122         g_value_set_ulong(value, webkit_dom_document_fragment_get_child_element_count(self));
123         break;
124     default:
125         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
126         break;
127     }
128 }
129
130 static void webkit_dom_document_fragment_class_init(WebKitDOMDocumentFragmentClass* requestClass)
131 {
132     GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
133     gobjectClass->get_property = webkit_dom_document_fragment_get_property;
134
135     g_object_class_install_property(
136         gobjectClass,
137         PROP_CHILDREN,
138         g_param_spec_object(
139             "children",
140             "DocumentFragment:children",
141             "read-only WebKitDOMHTMLCollection* DocumentFragment:children",
142             WEBKIT_DOM_TYPE_HTML_COLLECTION,
143             WEBKIT_PARAM_READABLE));
144
145     g_object_class_install_property(
146         gobjectClass,
147         PROP_FIRST_ELEMENT_CHILD,
148         g_param_spec_object(
149             "first-element-child",
150             "DocumentFragment:first-element-child",
151             "read-only WebKitDOMElement* DocumentFragment:first-element-child",
152             WEBKIT_DOM_TYPE_ELEMENT,
153             WEBKIT_PARAM_READABLE));
154
155     g_object_class_install_property(
156         gobjectClass,
157         PROP_LAST_ELEMENT_CHILD,
158         g_param_spec_object(
159             "last-element-child",
160             "DocumentFragment:last-element-child",
161             "read-only WebKitDOMElement* DocumentFragment:last-element-child",
162             WEBKIT_DOM_TYPE_ELEMENT,
163             WEBKIT_PARAM_READABLE));
164
165     g_object_class_install_property(
166         gobjectClass,
167         PROP_CHILD_ELEMENT_COUNT,
168         g_param_spec_ulong(
169             "child-element-count",
170             "DocumentFragment:child-element-count",
171             "read-only gulong DocumentFragment:child-element-count",
172             0, G_MAXULONG, 0,
173             WEBKIT_PARAM_READABLE));
174
175 }
176
177 static void webkit_dom_document_fragment_init(WebKitDOMDocumentFragment* request)
178 {
179     UNUSED_PARAM(request);
180 }
181
182 WebKitDOMElement* webkit_dom_document_fragment_get_element_by_id(WebKitDOMDocumentFragment* self, const gchar* elementId)
183 {
184     WebCore::JSMainThreadNullState state;
185     g_return_val_if_fail(WEBKIT_DOM_IS_DOCUMENT_FRAGMENT(self), 0);
186     g_return_val_if_fail(elementId, 0);
187     WebCore::DocumentFragment* item = WebKit::core(self);
188     WTF::String convertedElementId = WTF::String::fromUTF8(elementId);
189     RefPtr<WebCore::Element> gobjectResult = WTF::getPtr(item->getElementById(convertedElementId));
190     return WebKit::kit(gobjectResult.get());
191 }
192
193 WebKitDOMElement* webkit_dom_document_fragment_query_selector(WebKitDOMDocumentFragment* self, const gchar* selectors, GError** error)
194 {
195     WebCore::JSMainThreadNullState state;
196     g_return_val_if_fail(WEBKIT_DOM_IS_DOCUMENT_FRAGMENT(self), 0);
197     g_return_val_if_fail(selectors, 0);
198     g_return_val_if_fail(!error || !*error, 0);
199     WebCore::DocumentFragment* item = WebKit::core(self);
200     WTF::String convertedSelectors = WTF::String::fromUTF8(selectors);
201     auto result = item->querySelector(convertedSelectors);
202     if (result.hasException()) {
203         auto description = WebCore::DOMException::description(result.releaseException().code());
204         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
205         return nullptr;
206     }
207     return WebKit::kit(result.releaseReturnValue());
208 }
209
210 WebKitDOMNodeList* webkit_dom_document_fragment_query_selector_all(WebKitDOMDocumentFragment* self, const gchar* selectors, GError** error)
211 {
212     WebCore::JSMainThreadNullState state;
213     g_return_val_if_fail(WEBKIT_DOM_IS_DOCUMENT_FRAGMENT(self), 0);
214     g_return_val_if_fail(selectors, 0);
215     g_return_val_if_fail(!error || !*error, 0);
216     WebCore::DocumentFragment* item = WebKit::core(self);
217     WTF::String convertedSelectors = WTF::String::fromUTF8(selectors);
218     auto result = item->querySelectorAll(convertedSelectors);
219     if (result.hasException()) {
220         auto description = WebCore::DOMException::description(result.releaseException().code());
221         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
222         return nullptr;
223     }
224     return WebKit::kit(result.releaseReturnValue().ptr());
225 }
226
227 WebKitDOMHTMLCollection* webkit_dom_document_fragment_get_children(WebKitDOMDocumentFragment* self)
228 {
229     WebCore::JSMainThreadNullState state;
230     g_return_val_if_fail(WEBKIT_DOM_IS_DOCUMENT_FRAGMENT(self), 0);
231     WebCore::DocumentFragment* item = WebKit::core(self);
232     RefPtr<WebCore::HTMLCollection> gobjectResult = WTF::getPtr(item->children());
233     return WebKit::kit(gobjectResult.get());
234 }
235
236 WebKitDOMElement* webkit_dom_document_fragment_get_first_element_child(WebKitDOMDocumentFragment* self)
237 {
238     WebCore::JSMainThreadNullState state;
239     g_return_val_if_fail(WEBKIT_DOM_IS_DOCUMENT_FRAGMENT(self), 0);
240     WebCore::DocumentFragment* item = WebKit::core(self);
241     RefPtr<WebCore::Element> gobjectResult = WTF::getPtr(item->firstElementChild());
242     return WebKit::kit(gobjectResult.get());
243 }
244
245 WebKitDOMElement* webkit_dom_document_fragment_get_last_element_child(WebKitDOMDocumentFragment* self)
246 {
247     WebCore::JSMainThreadNullState state;
248     g_return_val_if_fail(WEBKIT_DOM_IS_DOCUMENT_FRAGMENT(self), 0);
249     WebCore::DocumentFragment* item = WebKit::core(self);
250     RefPtr<WebCore::Element> gobjectResult = WTF::getPtr(item->lastElementChild());
251     return WebKit::kit(gobjectResult.get());
252 }
253
254 gulong webkit_dom_document_fragment_get_child_element_count(WebKitDOMDocumentFragment* self)
255 {
256     WebCore::JSMainThreadNullState state;
257     g_return_val_if_fail(WEBKIT_DOM_IS_DOCUMENT_FRAGMENT(self), 0);
258     WebCore::DocumentFragment* item = WebKit::core(self);
259     gulong result = item->childElementCount();
260     return result;
261 }
262