f801303e596dae10eda3f5a1432b8980c561853e
[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/Document.h>
26 #include <WebCore/ExceptionCode.h>
27 #include <WebCore/ExceptionCodeDescription.h>
28 #include "GObjectEventListener.h"
29 #include <WebCore/JSMainThreadExecState.h>
30 #include "WebKitDOMDocumentFragmentPrivate.h"
31 #include "WebKitDOMElementPrivate.h"
32 #include "WebKitDOMEventPrivate.h"
33 #include "WebKitDOMEventTarget.h"
34 #include "WebKitDOMHTMLCollectionPrivate.h"
35 #include "WebKitDOMNodeListPrivate.h"
36 #include "WebKitDOMNodePrivate.h"
37 #include "WebKitDOMPrivate.h"
38 #include "ConvertToUTF8String.h"
39 #include "WebKitDOMDocumentFragmentUnstable.h"
40 #include <wtf/GetPtr.h>
41 #include <wtf/RefPtr.h>
42
43 namespace WebKit {
44
45 WebKitDOMDocumentFragment* kit(WebCore::DocumentFragment* obj)
46 {
47     return WEBKIT_DOM_DOCUMENT_FRAGMENT(kit(static_cast<WebCore::Node*>(obj)));
48 }
49
50 WebCore::DocumentFragment* core(WebKitDOMDocumentFragment* request)
51 {
52     return request ? static_cast<WebCore::DocumentFragment*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
53 }
54
55 WebKitDOMDocumentFragment* wrapDocumentFragment(WebCore::DocumentFragment* coreObject)
56 {
57     ASSERT(coreObject);
58     return WEBKIT_DOM_DOCUMENT_FRAGMENT(g_object_new(WEBKIT_DOM_TYPE_DOCUMENT_FRAGMENT, "core-object", coreObject, nullptr));
59 }
60
61 } // namespace WebKit
62
63 static gboolean webkit_dom_document_fragment_dispatch_event(WebKitDOMEventTarget* target, WebKitDOMEvent* event, GError** error)
64 {
65     WebCore::Event* coreEvent = WebKit::core(event);
66     if (!coreEvent)
67         return false;
68     WebCore::DocumentFragment* coreTarget = static_cast<WebCore::DocumentFragment*>(WEBKIT_DOM_OBJECT(target)->coreObject);
69
70     auto result = coreTarget->dispatchEventForBindings(*coreEvent);
71     if (result.hasException()) {
72         WebCore::ExceptionCodeDescription description(result.releaseException().code());
73         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.code, description.name);
74         return false;
75     }
76     return result.releaseReturnValue();
77 }
78
79 static gboolean webkit_dom_document_fragment_add_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
80 {
81     WebCore::DocumentFragment* coreTarget = static_cast<WebCore::DocumentFragment*>(WEBKIT_DOM_OBJECT(target)->coreObject);
82     return WebKit::GObjectEventListener::addEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
83 }
84
85 static gboolean webkit_dom_document_fragment_remove_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
86 {
87     WebCore::DocumentFragment* coreTarget = static_cast<WebCore::DocumentFragment*>(WEBKIT_DOM_OBJECT(target)->coreObject);
88     return WebKit::GObjectEventListener::removeEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
89 }
90
91 static void webkit_dom_event_target_init(WebKitDOMEventTargetIface* iface)
92 {
93     iface->dispatch_event = webkit_dom_document_fragment_dispatch_event;
94     iface->add_event_listener = webkit_dom_document_fragment_add_event_listener;
95     iface->remove_event_listener = webkit_dom_document_fragment_remove_event_listener;
96 }
97
98 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))
99
100 enum {
101     PROP_0,
102     PROP_CHILDREN,
103     PROP_FIRST_ELEMENT_CHILD,
104     PROP_LAST_ELEMENT_CHILD,
105     PROP_CHILD_ELEMENT_COUNT,
106 };
107
108 static void webkit_dom_document_fragment_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
109 {
110     WebKitDOMDocumentFragment* self = WEBKIT_DOM_DOCUMENT_FRAGMENT(object);
111
112     switch (propertyId) {
113     case PROP_CHILDREN:
114         g_value_set_object(value, webkit_dom_document_fragment_get_children(self));
115         break;
116     case PROP_FIRST_ELEMENT_CHILD:
117         g_value_set_object(value, webkit_dom_document_fragment_get_first_element_child(self));
118         break;
119     case PROP_LAST_ELEMENT_CHILD:
120         g_value_set_object(value, webkit_dom_document_fragment_get_last_element_child(self));
121         break;
122     case PROP_CHILD_ELEMENT_COUNT:
123         g_value_set_ulong(value, webkit_dom_document_fragment_get_child_element_count(self));
124         break;
125     default:
126         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
127         break;
128     }
129 }
130
131 static void webkit_dom_document_fragment_class_init(WebKitDOMDocumentFragmentClass* requestClass)
132 {
133     GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
134     gobjectClass->get_property = webkit_dom_document_fragment_get_property;
135
136     g_object_class_install_property(
137         gobjectClass,
138         PROP_CHILDREN,
139         g_param_spec_object(
140             "children",
141             "DocumentFragment:children",
142             "read-only WebKitDOMHTMLCollection* DocumentFragment:children",
143             WEBKIT_DOM_TYPE_HTML_COLLECTION,
144             WEBKIT_PARAM_READABLE));
145
146     g_object_class_install_property(
147         gobjectClass,
148         PROP_FIRST_ELEMENT_CHILD,
149         g_param_spec_object(
150             "first-element-child",
151             "DocumentFragment:first-element-child",
152             "read-only WebKitDOMElement* DocumentFragment:first-element-child",
153             WEBKIT_DOM_TYPE_ELEMENT,
154             WEBKIT_PARAM_READABLE));
155
156     g_object_class_install_property(
157         gobjectClass,
158         PROP_LAST_ELEMENT_CHILD,
159         g_param_spec_object(
160             "last-element-child",
161             "DocumentFragment:last-element-child",
162             "read-only WebKitDOMElement* DocumentFragment:last-element-child",
163             WEBKIT_DOM_TYPE_ELEMENT,
164             WEBKIT_PARAM_READABLE));
165
166     g_object_class_install_property(
167         gobjectClass,
168         PROP_CHILD_ELEMENT_COUNT,
169         g_param_spec_ulong(
170             "child-element-count",
171             "DocumentFragment:child-element-count",
172             "read-only gulong DocumentFragment:child-element-count",
173             0, G_MAXULONG, 0,
174             WEBKIT_PARAM_READABLE));
175
176 }
177
178 static void webkit_dom_document_fragment_init(WebKitDOMDocumentFragment* request)
179 {
180     UNUSED_PARAM(request);
181 }
182
183 WebKitDOMElement* webkit_dom_document_fragment_get_element_by_id(WebKitDOMDocumentFragment* self, const gchar* elementId)
184 {
185     WebCore::JSMainThreadNullState state;
186     g_return_val_if_fail(WEBKIT_DOM_IS_DOCUMENT_FRAGMENT(self), 0);
187     g_return_val_if_fail(elementId, 0);
188     WebCore::DocumentFragment* item = WebKit::core(self);
189     WTF::String convertedElementId = WTF::String::fromUTF8(elementId);
190     RefPtr<WebCore::Element> gobjectResult = WTF::getPtr(item->getElementById(convertedElementId));
191     return WebKit::kit(gobjectResult.get());
192 }
193
194 WebKitDOMElement* webkit_dom_document_fragment_query_selector(WebKitDOMDocumentFragment* self, const gchar* selectors, GError** error)
195 {
196     WebCore::JSMainThreadNullState state;
197     g_return_val_if_fail(WEBKIT_DOM_IS_DOCUMENT_FRAGMENT(self), 0);
198     g_return_val_if_fail(selectors, 0);
199     g_return_val_if_fail(!error || !*error, 0);
200     WebCore::DocumentFragment* item = WebKit::core(self);
201     WTF::String convertedSelectors = WTF::String::fromUTF8(selectors);
202     auto result = item->querySelector(convertedSelectors);
203     if (result.hasException()) {
204         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
205         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
206         return nullptr;
207     }
208     return WebKit::kit(result.releaseReturnValue());
209 }
210
211 WebKitDOMNodeList* webkit_dom_document_fragment_query_selector_all(WebKitDOMDocumentFragment* self, const gchar* selectors, GError** error)
212 {
213     WebCore::JSMainThreadNullState state;
214     g_return_val_if_fail(WEBKIT_DOM_IS_DOCUMENT_FRAGMENT(self), 0);
215     g_return_val_if_fail(selectors, 0);
216     g_return_val_if_fail(!error || !*error, 0);
217     WebCore::DocumentFragment* item = WebKit::core(self);
218     WTF::String convertedSelectors = WTF::String::fromUTF8(selectors);
219     auto result = item->querySelectorAll(convertedSelectors);
220     if (result.hasException()) {
221         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
222         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
223         return nullptr;
224     }
225     return WebKit::kit(result.releaseReturnValue().ptr());
226 }
227
228 WebKitDOMHTMLCollection* webkit_dom_document_fragment_get_children(WebKitDOMDocumentFragment* self)
229 {
230     WebCore::JSMainThreadNullState state;
231     g_return_val_if_fail(WEBKIT_DOM_IS_DOCUMENT_FRAGMENT(self), 0);
232     WebCore::DocumentFragment* item = WebKit::core(self);
233     RefPtr<WebCore::HTMLCollection> gobjectResult = WTF::getPtr(item->children());
234     return WebKit::kit(gobjectResult.get());
235 }
236
237 WebKitDOMElement* webkit_dom_document_fragment_get_first_element_child(WebKitDOMDocumentFragment* self)
238 {
239     WebCore::JSMainThreadNullState state;
240     g_return_val_if_fail(WEBKIT_DOM_IS_DOCUMENT_FRAGMENT(self), 0);
241     WebCore::DocumentFragment* item = WebKit::core(self);
242     RefPtr<WebCore::Element> gobjectResult = WTF::getPtr(item->firstElementChild());
243     return WebKit::kit(gobjectResult.get());
244 }
245
246 WebKitDOMElement* webkit_dom_document_fragment_get_last_element_child(WebKitDOMDocumentFragment* self)
247 {
248     WebCore::JSMainThreadNullState state;
249     g_return_val_if_fail(WEBKIT_DOM_IS_DOCUMENT_FRAGMENT(self), 0);
250     WebCore::DocumentFragment* item = WebKit::core(self);
251     RefPtr<WebCore::Element> gobjectResult = WTF::getPtr(item->lastElementChild());
252     return WebKit::kit(gobjectResult.get());
253 }
254
255 gulong webkit_dom_document_fragment_get_child_element_count(WebKitDOMDocumentFragment* self)
256 {
257     WebCore::JSMainThreadNullState state;
258     g_return_val_if_fail(WEBKIT_DOM_IS_DOCUMENT_FRAGMENT(self), 0);
259     WebCore::DocumentFragment* item = WebKit::core(self);
260     gulong result = item->childElementCount();
261     return result;
262 }
263