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