b09bb7de7df80e709e25bcb7cde78c31a83da1fc
[WebKit-https.git] / Source / WebKit / WebProcess / InjectedBundle / API / gtk / DOM / WebKitDOMCharacterData.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 "WebKitDOMCharacterData.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 "WebKitDOMCharacterDataPrivate.h"
31 #include "WebKitDOMElementPrivate.h"
32 #include "WebKitDOMEventPrivate.h"
33 #include "WebKitDOMEventTarget.h"
34 #include "WebKitDOMNodePrivate.h"
35 #include "WebKitDOMPrivate.h"
36 #include "ConvertToUTF8String.h"
37 #include <wtf/GetPtr.h>
38 #include <wtf/RefPtr.h>
39
40 namespace WebKit {
41
42 WebKitDOMCharacterData* kit(WebCore::CharacterData* obj)
43 {
44     return WEBKIT_DOM_CHARACTER_DATA(kit(static_cast<WebCore::Node*>(obj)));
45 }
46
47 WebCore::CharacterData* core(WebKitDOMCharacterData* request)
48 {
49     return request ? static_cast<WebCore::CharacterData*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
50 }
51
52 WebKitDOMCharacterData* wrapCharacterData(WebCore::CharacterData* coreObject)
53 {
54     ASSERT(coreObject);
55     return WEBKIT_DOM_CHARACTER_DATA(g_object_new(WEBKIT_DOM_TYPE_CHARACTER_DATA, "core-object", coreObject, nullptr));
56 }
57
58 } // namespace WebKit
59
60 static gboolean webkit_dom_character_data_dispatch_event(WebKitDOMEventTarget* target, WebKitDOMEvent* event, GError** error)
61 {
62     WebCore::Event* coreEvent = WebKit::core(event);
63     if (!coreEvent)
64         return false;
65     WebCore::CharacterData* coreTarget = static_cast<WebCore::CharacterData*>(WEBKIT_DOM_OBJECT(target)->coreObject);
66
67     auto result = coreTarget->dispatchEventForBindings(*coreEvent);
68     if (result.hasException()) {
69         WebCore::ExceptionCodeDescription description(result.releaseException().code());
70         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.code, description.name);
71         return false;
72     }
73     return result.releaseReturnValue();
74 }
75
76 static gboolean webkit_dom_character_data_add_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
77 {
78     WebCore::CharacterData* coreTarget = static_cast<WebCore::CharacterData*>(WEBKIT_DOM_OBJECT(target)->coreObject);
79     return WebKit::GObjectEventListener::addEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
80 }
81
82 static gboolean webkit_dom_character_data_remove_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
83 {
84     WebCore::CharacterData* coreTarget = static_cast<WebCore::CharacterData*>(WEBKIT_DOM_OBJECT(target)->coreObject);
85     return WebKit::GObjectEventListener::removeEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
86 }
87
88 static void webkit_dom_event_target_init(WebKitDOMEventTargetIface* iface)
89 {
90     iface->dispatch_event = webkit_dom_character_data_dispatch_event;
91     iface->add_event_listener = webkit_dom_character_data_add_event_listener;
92     iface->remove_event_listener = webkit_dom_character_data_remove_event_listener;
93 }
94
95 G_DEFINE_TYPE_WITH_CODE(WebKitDOMCharacterData, webkit_dom_character_data, WEBKIT_DOM_TYPE_NODE, G_IMPLEMENT_INTERFACE(WEBKIT_DOM_TYPE_EVENT_TARGET, webkit_dom_event_target_init))
96
97 enum {
98     PROP_0,
99     PROP_DATA,
100     PROP_LENGTH,
101 };
102
103 static void webkit_dom_character_data_set_property(GObject* object, guint propertyId, const GValue* value, GParamSpec* pspec)
104 {
105     WebKitDOMCharacterData* self = WEBKIT_DOM_CHARACTER_DATA(object);
106
107     switch (propertyId) {
108     case PROP_DATA:
109         webkit_dom_character_data_set_data(self, g_value_get_string(value), nullptr);
110         break;
111     default:
112         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
113         break;
114     }
115 }
116
117 static void webkit_dom_character_data_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
118 {
119     WebKitDOMCharacterData* self = WEBKIT_DOM_CHARACTER_DATA(object);
120
121     switch (propertyId) {
122     case PROP_DATA:
123         g_value_take_string(value, webkit_dom_character_data_get_data(self));
124         break;
125     case PROP_LENGTH:
126         g_value_set_ulong(value, webkit_dom_character_data_get_length(self));
127         break;
128     default:
129         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
130         break;
131     }
132 }
133
134 static void webkit_dom_character_data_class_init(WebKitDOMCharacterDataClass* requestClass)
135 {
136     GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
137     gobjectClass->set_property = webkit_dom_character_data_set_property;
138     gobjectClass->get_property = webkit_dom_character_data_get_property;
139
140     g_object_class_install_property(
141         gobjectClass,
142         PROP_DATA,
143         g_param_spec_string(
144             "data",
145             "CharacterData:data",
146             "read-write gchar* CharacterData:data",
147             "",
148             WEBKIT_PARAM_READWRITE));
149
150     g_object_class_install_property(
151         gobjectClass,
152         PROP_LENGTH,
153         g_param_spec_ulong(
154             "length",
155             "CharacterData:length",
156             "read-only gulong CharacterData:length",
157             0, G_MAXULONG, 0,
158             WEBKIT_PARAM_READABLE));
159 }
160
161 static void webkit_dom_character_data_init(WebKitDOMCharacterData* request)
162 {
163     UNUSED_PARAM(request);
164 }
165
166 gchar* webkit_dom_character_data_substring_data(WebKitDOMCharacterData* self, gulong offset, gulong length, GError** error)
167 {
168     WebCore::JSMainThreadNullState state;
169     g_return_val_if_fail(WEBKIT_DOM_IS_CHARACTER_DATA(self), 0);
170     g_return_val_if_fail(!error || !*error, 0);
171     WebCore::CharacterData* item = WebKit::core(self);
172     auto result = item->substringData(offset, length);
173     if (result.hasException())
174         return nullptr;
175     return convertToUTF8String(result.releaseReturnValue());
176 }
177
178 void webkit_dom_character_data_append_data(WebKitDOMCharacterData* self, const gchar* data, GError** error)
179 {
180     WebCore::JSMainThreadNullState state;
181     g_return_if_fail(WEBKIT_DOM_IS_CHARACTER_DATA(self));
182     g_return_if_fail(data);
183     UNUSED_PARAM(error);
184     WebCore::CharacterData* item = WebKit::core(self);
185     WTF::String convertedData = WTF::String::fromUTF8(data);
186     item->appendData(convertedData);
187 }
188
189 void webkit_dom_character_data_insert_data(WebKitDOMCharacterData* self, gulong offset, const gchar* data, GError** error)
190 {
191     WebCore::JSMainThreadNullState state;
192     g_return_if_fail(WEBKIT_DOM_IS_CHARACTER_DATA(self));
193     g_return_if_fail(data);
194     g_return_if_fail(!error || !*error);
195     WebCore::CharacterData* item = WebKit::core(self);
196     WTF::String convertedData = WTF::String::fromUTF8(data);
197     auto result = item->insertData(offset, convertedData);
198     if (result.hasException()) {
199         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
200         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
201     }
202 }
203
204 void webkit_dom_character_data_delete_data(WebKitDOMCharacterData* self, gulong offset, gulong length, GError** error)
205 {
206     WebCore::JSMainThreadNullState state;
207     g_return_if_fail(WEBKIT_DOM_IS_CHARACTER_DATA(self));
208     g_return_if_fail(!error || !*error);
209     WebCore::CharacterData* item = WebKit::core(self);
210     auto result = item->deleteData(offset, length);
211     if (result.hasException()) {
212         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
213         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
214     }
215 }
216
217 void webkit_dom_character_data_replace_data(WebKitDOMCharacterData* self, gulong offset, gulong length, const gchar* data, GError** error)
218 {
219     WebCore::JSMainThreadNullState state;
220     g_return_if_fail(WEBKIT_DOM_IS_CHARACTER_DATA(self));
221     g_return_if_fail(data);
222     g_return_if_fail(!error || !*error);
223     WebCore::CharacterData* item = WebKit::core(self);
224     WTF::String convertedData = WTF::String::fromUTF8(data);
225     auto result = item->replaceData(offset, length, convertedData);
226     if (result.hasException()) {
227         WebCore::ExceptionCodeDescription ecdesc(result.releaseException().code());
228         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
229     }
230 }
231
232 gchar* webkit_dom_character_data_get_data(WebKitDOMCharacterData* self)
233 {
234     WebCore::JSMainThreadNullState state;
235     g_return_val_if_fail(WEBKIT_DOM_IS_CHARACTER_DATA(self), 0);
236     WebCore::CharacterData* item = WebKit::core(self);
237     gchar* result = convertToUTF8String(item->data());
238     return result;
239 }
240
241 void webkit_dom_character_data_set_data(WebKitDOMCharacterData* self, const gchar* value, GError** error)
242 {
243     WebCore::JSMainThreadNullState state;
244     g_return_if_fail(WEBKIT_DOM_IS_CHARACTER_DATA(self));
245     g_return_if_fail(value);
246     UNUSED_PARAM(error);
247     WebCore::CharacterData* item = WebKit::core(self);
248     WTF::String convertedValue = WTF::String::fromUTF8(value);
249     item->setData(convertedValue);
250 }
251
252 gulong webkit_dom_character_data_get_length(WebKitDOMCharacterData* self)
253 {
254     WebCore::JSMainThreadNullState state;
255     g_return_val_if_fail(WEBKIT_DOM_IS_CHARACTER_DATA(self), 0);
256     WebCore::CharacterData* item = WebKit::core(self);
257     gulong result = item->length();
258     return result;
259 }