cc5291a1154008014aed378d49443a42746fa8a2
[WebKit-https.git] / Source / WebKit / WebProcess / InjectedBundle / API / gtk / DOM / WebKitDOMRange.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 "WebKitDOMRange.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 <WebCore/JSMainThreadExecState.h>
29 #include "WebKitDOMDocumentFragmentPrivate.h"
30 #include "WebKitDOMNodePrivate.h"
31 #include "WebKitDOMPrivate.h"
32 #include "WebKitDOMRangePrivate.h"
33 #include "ConvertToUTF8String.h"
34 #include "WebKitDOMRangeUnstable.h"
35 #include <wtf/GetPtr.h>
36 #include <wtf/RefPtr.h>
37
38 #define WEBKIT_DOM_RANGE_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, WEBKIT_DOM_TYPE_RANGE, WebKitDOMRangePrivate)
39
40 typedef struct _WebKitDOMRangePrivate {
41     RefPtr<WebCore::Range> coreObject;
42 } WebKitDOMRangePrivate;
43
44 namespace WebKit {
45
46 WebKitDOMRange* kit(WebCore::Range* obj)
47 {
48     if (!obj)
49         return 0;
50
51     if (gpointer ret = DOMObjectCache::get(obj))
52         return WEBKIT_DOM_RANGE(ret);
53
54     return wrapRange(obj);
55 }
56
57 WebCore::Range* core(WebKitDOMRange* request)
58 {
59     return request ? static_cast<WebCore::Range*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
60 }
61
62 WebKitDOMRange* wrapRange(WebCore::Range* coreObject)
63 {
64     ASSERT(coreObject);
65     return WEBKIT_DOM_RANGE(g_object_new(WEBKIT_DOM_TYPE_RANGE, "core-object", coreObject, nullptr));
66 }
67
68 } // namespace WebKit
69
70 G_DEFINE_TYPE(WebKitDOMRange, webkit_dom_range, WEBKIT_DOM_TYPE_OBJECT)
71
72 enum {
73     PROP_0,
74     PROP_START_CONTAINER,
75     PROP_START_OFFSET,
76     PROP_END_CONTAINER,
77     PROP_END_OFFSET,
78     PROP_COLLAPSED,
79     PROP_COMMON_ANCESTOR_CONTAINER,
80     PROP_TEXT,
81 };
82
83 static void webkit_dom_range_finalize(GObject* object)
84 {
85     WebKitDOMRangePrivate* priv = WEBKIT_DOM_RANGE_GET_PRIVATE(object);
86
87     WebKit::DOMObjectCache::forget(priv->coreObject.get());
88
89     priv->~WebKitDOMRangePrivate();
90     G_OBJECT_CLASS(webkit_dom_range_parent_class)->finalize(object);
91 }
92
93 static void webkit_dom_range_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
94 {
95     WebKitDOMRange* self = WEBKIT_DOM_RANGE(object);
96
97     switch (propertyId) {
98     case PROP_START_CONTAINER:
99         g_value_set_object(value, webkit_dom_range_get_start_container(self, nullptr));
100         break;
101     case PROP_START_OFFSET:
102         g_value_set_long(value, webkit_dom_range_get_start_offset(self, nullptr));
103         break;
104     case PROP_END_CONTAINER:
105         g_value_set_object(value, webkit_dom_range_get_end_container(self, nullptr));
106         break;
107     case PROP_END_OFFSET:
108         g_value_set_long(value, webkit_dom_range_get_end_offset(self, nullptr));
109         break;
110     case PROP_COLLAPSED:
111         g_value_set_boolean(value, webkit_dom_range_get_collapsed(self, nullptr));
112         break;
113     case PROP_COMMON_ANCESTOR_CONTAINER:
114         g_value_set_object(value, webkit_dom_range_get_common_ancestor_container(self, nullptr));
115         break;
116     case PROP_TEXT:
117         g_value_take_string(value, webkit_dom_range_get_text(self));
118         break;
119     default:
120         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
121         break;
122     }
123 }
124
125 static GObject* webkit_dom_range_constructor(GType type, guint constructPropertiesCount, GObjectConstructParam* constructProperties)
126 {
127     GObject* object = G_OBJECT_CLASS(webkit_dom_range_parent_class)->constructor(type, constructPropertiesCount, constructProperties);
128
129     WebKitDOMRangePrivate* priv = WEBKIT_DOM_RANGE_GET_PRIVATE(object);
130     priv->coreObject = static_cast<WebCore::Range*>(WEBKIT_DOM_OBJECT(object)->coreObject);
131     WebKit::DOMObjectCache::put(priv->coreObject.get(), object);
132
133     return object;
134 }
135
136 static void webkit_dom_range_class_init(WebKitDOMRangeClass* requestClass)
137 {
138     GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
139     g_type_class_add_private(gobjectClass, sizeof(WebKitDOMRangePrivate));
140     gobjectClass->constructor = webkit_dom_range_constructor;
141     gobjectClass->finalize = webkit_dom_range_finalize;
142     gobjectClass->get_property = webkit_dom_range_get_property;
143
144     g_object_class_install_property(
145         gobjectClass,
146         PROP_START_CONTAINER,
147         g_param_spec_object(
148             "start-container",
149             "Range:start-container",
150             "read-only WebKitDOMNode* Range:start-container",
151             WEBKIT_DOM_TYPE_NODE,
152             WEBKIT_PARAM_READABLE));
153
154     g_object_class_install_property(
155         gobjectClass,
156         PROP_START_OFFSET,
157         g_param_spec_long(
158             "start-offset",
159             "Range:start-offset",
160             "read-only glong Range:start-offset",
161             G_MINLONG, G_MAXLONG, 0,
162             WEBKIT_PARAM_READABLE));
163
164     g_object_class_install_property(
165         gobjectClass,
166         PROP_END_CONTAINER,
167         g_param_spec_object(
168             "end-container",
169             "Range:end-container",
170             "read-only WebKitDOMNode* Range:end-container",
171             WEBKIT_DOM_TYPE_NODE,
172             WEBKIT_PARAM_READABLE));
173
174     g_object_class_install_property(
175         gobjectClass,
176         PROP_END_OFFSET,
177         g_param_spec_long(
178             "end-offset",
179             "Range:end-offset",
180             "read-only glong Range:end-offset",
181             G_MINLONG, G_MAXLONG, 0,
182             WEBKIT_PARAM_READABLE));
183
184     g_object_class_install_property(
185         gobjectClass,
186         PROP_COLLAPSED,
187         g_param_spec_boolean(
188             "collapsed",
189             "Range:collapsed",
190             "read-only gboolean Range:collapsed",
191             FALSE,
192             WEBKIT_PARAM_READABLE));
193
194     g_object_class_install_property(
195         gobjectClass,
196         PROP_COMMON_ANCESTOR_CONTAINER,
197         g_param_spec_object(
198             "common-ancestor-container",
199             "Range:common-ancestor-container",
200             "read-only WebKitDOMNode* Range:common-ancestor-container",
201             WEBKIT_DOM_TYPE_NODE,
202             WEBKIT_PARAM_READABLE));
203
204     g_object_class_install_property(
205         gobjectClass,
206         PROP_TEXT,
207         g_param_spec_string(
208             "text",
209             "Range:text",
210             "read-only gchar* Range:text",
211             "",
212             WEBKIT_PARAM_READABLE));
213
214 }
215
216 static void webkit_dom_range_init(WebKitDOMRange* request)
217 {
218     WebKitDOMRangePrivate* priv = WEBKIT_DOM_RANGE_GET_PRIVATE(request);
219     new (priv) WebKitDOMRangePrivate();
220 }
221
222 void webkit_dom_range_set_start(WebKitDOMRange* self, WebKitDOMNode* refNode, glong offset, GError** error)
223 {
224     WebCore::JSMainThreadNullState state;
225     g_return_if_fail(WEBKIT_DOM_IS_RANGE(self));
226     g_return_if_fail(WEBKIT_DOM_IS_NODE(refNode));
227     g_return_if_fail(!error || !*error);
228     WebCore::Range* item = WebKit::core(self);
229     WebCore::Node* convertedRefNode = WebKit::core(refNode);
230     auto result = item->setStart(*convertedRefNode, offset);
231     if (result.hasException()) {
232         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
233         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
234     }
235 }
236
237 void webkit_dom_range_set_end(WebKitDOMRange* self, WebKitDOMNode* refNode, glong offset, GError** error)
238 {
239     WebCore::JSMainThreadNullState state;
240     g_return_if_fail(WEBKIT_DOM_IS_RANGE(self));
241     g_return_if_fail(WEBKIT_DOM_IS_NODE(refNode));
242     g_return_if_fail(!error || !*error);
243     WebCore::Range* item = WebKit::core(self);
244     WebCore::Node* convertedRefNode = WebKit::core(refNode);
245     auto result = item->setEnd(*convertedRefNode, offset);
246     if (result.hasException()) {
247         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
248         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
249     }
250 }
251
252 void webkit_dom_range_set_start_before(WebKitDOMRange* self, WebKitDOMNode* refNode, GError** error)
253 {
254     WebCore::JSMainThreadNullState state;
255     g_return_if_fail(WEBKIT_DOM_IS_RANGE(self));
256     g_return_if_fail(WEBKIT_DOM_IS_NODE(refNode));
257     g_return_if_fail(!error || !*error);
258     WebCore::Range* item = WebKit::core(self);
259     WebCore::Node* convertedRefNode = WebKit::core(refNode);
260     auto result = item->setStartBefore(*convertedRefNode);
261     if (result.hasException()) {
262         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
263         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
264     }
265 }
266
267 void webkit_dom_range_set_start_after(WebKitDOMRange* self, WebKitDOMNode* refNode, GError** error)
268 {
269     WebCore::JSMainThreadNullState state;
270     g_return_if_fail(WEBKIT_DOM_IS_RANGE(self));
271     g_return_if_fail(WEBKIT_DOM_IS_NODE(refNode));
272     g_return_if_fail(!error || !*error);
273     WebCore::Range* item = WebKit::core(self);
274     WebCore::Node* convertedRefNode = WebKit::core(refNode);
275     auto result = item->setStartAfter(*convertedRefNode);
276     if (result.hasException()) {
277         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
278         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
279     }
280 }
281
282 void webkit_dom_range_set_end_before(WebKitDOMRange* self, WebKitDOMNode* refNode, GError** error)
283 {
284     WebCore::JSMainThreadNullState state;
285     g_return_if_fail(WEBKIT_DOM_IS_RANGE(self));
286     g_return_if_fail(WEBKIT_DOM_IS_NODE(refNode));
287     g_return_if_fail(!error || !*error);
288     WebCore::Range* item = WebKit::core(self);
289     WebCore::Node* convertedRefNode = WebKit::core(refNode);
290     auto result = item->setEndBefore(*convertedRefNode);
291     if (result.hasException()) {
292         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
293         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
294     }
295 }
296
297 void webkit_dom_range_set_end_after(WebKitDOMRange* self, WebKitDOMNode* refNode, GError** error)
298 {
299     WebCore::JSMainThreadNullState state;
300     g_return_if_fail(WEBKIT_DOM_IS_RANGE(self));
301     g_return_if_fail(WEBKIT_DOM_IS_NODE(refNode));
302     g_return_if_fail(!error || !*error);
303     WebCore::Range* item = WebKit::core(self);
304     WebCore::Node* convertedRefNode = WebKit::core(refNode);
305     auto result = item->setEndAfter(*convertedRefNode);
306     if (result.hasException()) {
307         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
308         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
309     }
310 }
311
312 void webkit_dom_range_collapse(WebKitDOMRange* self, gboolean toStart, GError** error)
313 {
314     WebCore::JSMainThreadNullState state;
315     g_return_if_fail(WEBKIT_DOM_IS_RANGE(self));
316     UNUSED_PARAM(error);
317     WebCore::Range* item = WebKit::core(self);
318     item->collapse(toStart);
319 }
320
321 void webkit_dom_range_select_node(WebKitDOMRange* self, WebKitDOMNode* refNode, GError** error)
322 {
323     WebCore::JSMainThreadNullState state;
324     g_return_if_fail(WEBKIT_DOM_IS_RANGE(self));
325     g_return_if_fail(WEBKIT_DOM_IS_NODE(refNode));
326     g_return_if_fail(!error || !*error);
327     WebCore::Range* item = WebKit::core(self);
328     WebCore::Node* convertedRefNode = WebKit::core(refNode);
329     auto result = item->selectNode(*convertedRefNode);
330     if (result.hasException()) {
331         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
332         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
333     }
334 }
335
336 void webkit_dom_range_select_node_contents(WebKitDOMRange* self, WebKitDOMNode* refNode, GError** error)
337 {
338     WebCore::JSMainThreadNullState state;
339     g_return_if_fail(WEBKIT_DOM_IS_RANGE(self));
340     g_return_if_fail(WEBKIT_DOM_IS_NODE(refNode));
341     g_return_if_fail(!error || !*error);
342     WebCore::Range* item = WebKit::core(self);
343     WebCore::Node* convertedRefNode = WebKit::core(refNode);
344     auto result = item->selectNodeContents(*convertedRefNode);
345     if (result.hasException()) {
346         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
347         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
348     }
349 }
350
351 gshort webkit_dom_range_compare_boundary_points(WebKitDOMRange* self, gushort how, WebKitDOMRange* sourceRange, GError** error)
352 {
353     WebCore::JSMainThreadNullState state;
354     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), 0);
355     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(sourceRange), 0);
356     g_return_val_if_fail(!error || !*error, 0);
357     WebCore::Range* item = WebKit::core(self);
358     WebCore::Range* convertedSourceRange = WebKit::core(sourceRange);
359     auto result = item->compareBoundaryPointsForBindings(how, *convertedSourceRange);
360     if (result.hasException()) {
361         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
362         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
363         return 0;
364     }
365     return result.releaseReturnValue();
366 }
367
368 void webkit_dom_range_delete_contents(WebKitDOMRange* self, GError** error)
369 {
370     WebCore::JSMainThreadNullState state;
371     g_return_if_fail(WEBKIT_DOM_IS_RANGE(self));
372     g_return_if_fail(!error || !*error);
373     WebCore::Range* item = WebKit::core(self);
374     auto result = item->deleteContents();
375     if (result.hasException()) {
376         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
377         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
378     }
379 }
380
381 WebKitDOMDocumentFragment* webkit_dom_range_extract_contents(WebKitDOMRange* self, GError** error)
382 {
383     WebCore::JSMainThreadNullState state;
384     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), 0);
385     g_return_val_if_fail(!error || !*error, 0);
386     WebCore::Range* item = WebKit::core(self);
387     auto result = item->extractContents();
388     if (result.hasException()) {
389         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
390         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
391         return nullptr;
392     }
393     return WebKit::kit(result.releaseReturnValue().ptr());
394 }
395
396 WebKitDOMDocumentFragment* webkit_dom_range_clone_contents(WebKitDOMRange* self, GError** error)
397 {
398     WebCore::JSMainThreadNullState state;
399     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), 0);
400     g_return_val_if_fail(!error || !*error, 0);
401     WebCore::Range* item = WebKit::core(self);
402     auto result = item->cloneContents();
403     if (result.hasException()) {
404         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
405         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
406         return nullptr;
407     }
408     return WebKit::kit(result.releaseReturnValue().ptr());
409 }
410
411 void webkit_dom_range_insert_node(WebKitDOMRange* self, WebKitDOMNode* newNode, GError** error)
412 {
413     WebCore::JSMainThreadNullState state;
414     g_return_if_fail(WEBKIT_DOM_IS_RANGE(self));
415     g_return_if_fail(WEBKIT_DOM_IS_NODE(newNode));
416     g_return_if_fail(!error || !*error);
417     WebCore::Range* item = WebKit::core(self);
418     WebCore::Node* convertedNewNode = WebKit::core(newNode);
419     auto result = item->insertNode(*convertedNewNode);
420     if (result.hasException()) {
421         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
422         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
423     }
424 }
425
426 void webkit_dom_range_surround_contents(WebKitDOMRange* self, WebKitDOMNode* newParent, GError** error)
427 {
428     WebCore::JSMainThreadNullState state;
429     g_return_if_fail(WEBKIT_DOM_IS_RANGE(self));
430     g_return_if_fail(WEBKIT_DOM_IS_NODE(newParent));
431     g_return_if_fail(!error || !*error);
432     WebCore::Range* item = WebKit::core(self);
433     WebCore::Node* convertedNewParent = WebKit::core(newParent);
434     auto result = item->surroundContents(*convertedNewParent);
435     if (result.hasException()) {
436         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
437         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
438     }
439 }
440
441 WebKitDOMRange* webkit_dom_range_clone_range(WebKitDOMRange* self, GError** error)
442 {
443     WebCore::JSMainThreadNullState state;
444     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), 0);
445     UNUSED_PARAM(error);
446     WebCore::Range* item = WebKit::core(self);
447     RefPtr<WebCore::Range> gobjectResult = WTF::getPtr(item->cloneRange());
448     return WebKit::kit(gobjectResult.get());
449 }
450
451 gchar* webkit_dom_range_to_string(WebKitDOMRange* self, GError** error)
452 {
453     WebCore::JSMainThreadNullState state;
454     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), 0);
455     UNUSED_PARAM(error);
456     WebCore::Range* item = WebKit::core(self);
457     gchar* result = convertToUTF8String(item->toString());
458     return result;
459 }
460
461 void webkit_dom_range_detach(WebKitDOMRange* self, GError** error)
462 {
463     WebCore::JSMainThreadNullState state;
464     g_return_if_fail(WEBKIT_DOM_IS_RANGE(self));
465     UNUSED_PARAM(error);
466     WebCore::Range* item = WebKit::core(self);
467     item->detach();
468 }
469
470 WebKitDOMDocumentFragment* webkit_dom_range_create_contextual_fragment(WebKitDOMRange* self, const gchar* html, GError** error)
471 {
472     WebCore::JSMainThreadNullState state;
473     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), 0);
474     g_return_val_if_fail(html, 0);
475     g_return_val_if_fail(!error || !*error, 0);
476     WebCore::Range* item = WebKit::core(self);
477     WTF::String convertedHtml = WTF::String::fromUTF8(html);
478     auto result = item->createContextualFragment(convertedHtml);
479     if (result.hasException()) {
480         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
481         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
482         return nullptr;
483     }
484     return WebKit::kit(result.releaseReturnValue().ptr());
485 }
486
487 gshort webkit_dom_range_compare_node(WebKitDOMRange* self, WebKitDOMNode* refNode, GError** error)
488 {
489     WebCore::JSMainThreadNullState state;
490     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), 0);
491     g_return_val_if_fail(WEBKIT_DOM_IS_NODE(refNode), 0);
492     g_return_val_if_fail(!error || !*error, 0);
493     WebCore::Range* item = WebKit::core(self);
494     WebCore::Node* convertedRefNode = WebKit::core(refNode);
495     auto result = item->compareNode(*convertedRefNode);
496     if (result.hasException()) {
497         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
498         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
499         return 0;
500     }
501     return result.releaseReturnValue();
502 }
503
504 gboolean webkit_dom_range_intersects_node(WebKitDOMRange* self, WebKitDOMNode* refNode, GError** error)
505 {
506     WebCore::JSMainThreadNullState state;
507     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), FALSE);
508     g_return_val_if_fail(WEBKIT_DOM_IS_NODE(refNode), FALSE);
509     g_return_val_if_fail(!error || !*error, FALSE);
510     WebCore::Range* item = WebKit::core(self);
511     WebCore::Node* convertedRefNode = WebKit::core(refNode);
512     auto result = item->intersectsNode(*convertedRefNode);
513     if (result.hasException()) {
514         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
515         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
516         return false;
517     }
518     return result.releaseReturnValue();
519 }
520
521 gshort webkit_dom_range_compare_point(WebKitDOMRange* self, WebKitDOMNode* refNode, glong offset, GError** error)
522 {
523     WebCore::JSMainThreadNullState state;
524     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), 0);
525     g_return_val_if_fail(WEBKIT_DOM_IS_NODE(refNode), 0);
526     g_return_val_if_fail(!error || !*error, 0);
527     WebCore::Range* item = WebKit::core(self);
528     WebCore::Node* convertedRefNode = WebKit::core(refNode);
529     auto result = item->comparePoint(*convertedRefNode, offset);
530     if (result.hasException()) {
531         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
532         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
533         return 0;
534     }
535     return result.releaseReturnValue();
536 }
537
538 gboolean webkit_dom_range_is_point_in_range(WebKitDOMRange* self, WebKitDOMNode* refNode, glong offset, GError** error)
539 {
540     WebCore::JSMainThreadNullState state;
541     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), FALSE);
542     g_return_val_if_fail(WEBKIT_DOM_IS_NODE(refNode), FALSE);
543     g_return_val_if_fail(!error || !*error, FALSE);
544     WebCore::Range* item = WebKit::core(self);
545     WebCore::Node* convertedRefNode = WebKit::core(refNode);
546     auto result = item->isPointInRange(*convertedRefNode, offset);
547     if (result.hasException()) {
548         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
549         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
550         return false;
551     }
552     return result.releaseReturnValue();
553 }
554
555 void webkit_dom_range_expand(WebKitDOMRange* self, const gchar* unit, GError** error)
556 {
557     WebCore::JSMainThreadNullState state;
558     g_return_if_fail(WEBKIT_DOM_IS_RANGE(self));
559     g_return_if_fail(unit);
560     g_return_if_fail(!error || !*error);
561     WebCore::Range* item = WebKit::core(self);
562     WTF::String convertedUnit = WTF::String::fromUTF8(unit);
563     auto result = item->expand(convertedUnit);
564     if (result.hasException()) {
565         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
566         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
567     }
568 }
569
570 WebKitDOMNode* webkit_dom_range_get_start_container(WebKitDOMRange* self, GError** error)
571 {
572     WebCore::JSMainThreadNullState state;
573     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), 0);
574     UNUSED_PARAM(error);
575     WebCore::Range* item = WebKit::core(self);
576     RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(item->startContainer());
577     return WebKit::kit(gobjectResult.get());
578 }
579
580 glong webkit_dom_range_get_start_offset(WebKitDOMRange* self, GError** error)
581 {
582     WebCore::JSMainThreadNullState state;
583     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), 0);
584     UNUSED_PARAM(error);
585     WebCore::Range* item = WebKit::core(self);
586     glong result = item->startOffset();
587     return result;
588 }
589
590 WebKitDOMNode* webkit_dom_range_get_end_container(WebKitDOMRange* self, GError** error)
591 {
592     WebCore::JSMainThreadNullState state;
593     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), 0);
594     UNUSED_PARAM(error);
595     WebCore::Range* item = WebKit::core(self);
596     RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(item->endContainer());
597     return WebKit::kit(gobjectResult.get());
598 }
599
600 glong webkit_dom_range_get_end_offset(WebKitDOMRange* self, GError** error)
601 {
602     WebCore::JSMainThreadNullState state;
603     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), 0);
604     UNUSED_PARAM(error);
605     WebCore::Range* item = WebKit::core(self);
606     glong result = item->endOffset();
607     return result;
608 }
609
610 gboolean webkit_dom_range_get_collapsed(WebKitDOMRange* self, GError** error)
611 {
612     WebCore::JSMainThreadNullState state;
613     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), FALSE);
614     UNUSED_PARAM(error);
615     WebCore::Range* item = WebKit::core(self);
616     gboolean result = item->collapsed();
617     return result;
618 }
619
620 WebKitDOMNode* webkit_dom_range_get_common_ancestor_container(WebKitDOMRange* self, GError** error)
621 {
622     WebCore::JSMainThreadNullState state;
623     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), 0);
624     UNUSED_PARAM(error);
625     WebCore::Range* item = WebKit::core(self);
626     RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(item->commonAncestorContainer());
627     return WebKit::kit(gobjectResult.get());
628 }
629
630 gchar* webkit_dom_range_get_text(WebKitDOMRange* self)
631 {
632     WebCore::JSMainThreadNullState state;
633     g_return_val_if_fail(WEBKIT_DOM_IS_RANGE(self), 0);
634     WebCore::Range* item = WebKit::core(self);
635     gchar* result = convertToUTF8String(item->text());
636     return result;
637 }
638