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 "WebKitDOMText.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 "WebKitDOMEventPrivate.h"
30#include "WebKitDOMEventTarget.h"
31#include "WebKitDOMNodePrivate.h"
32#include "WebKitDOMPrivate.h"
33#include "WebKitDOMTextPrivate.h"
34#include "ConvertToUTF8String.h"
35#include <wtf/GetPtr.h>
36#include <wtf/RefPtr.h>
37
38G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
39
40namespace WebKit {
41
42WebKitDOMText* kit(WebCore::Text* obj)
43{
44 return WEBKIT_DOM_TEXT(kit(static_cast<WebCore::Node*>(obj)));
45}
46
47WebCore::Text* core(WebKitDOMText* request)
48{
49 return request ? static_cast<WebCore::Text*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
50}
51
52WebKitDOMText* wrapText(WebCore::Text* coreObject)
53{
54 ASSERT(coreObject);
55 return WEBKIT_DOM_TEXT(g_object_new(WEBKIT_DOM_TYPE_TEXT, "core-object", coreObject, nullptr));
56}
57
58} // namespace WebKit
59
60static gboolean webkit_dom_text_dispatch_event(WebKitDOMEventTarget* target, WebKitDOMEvent* event, GError** error)
61{
62 WebCore::Event* coreEvent = WebKit::core(event);
63 if (!coreEvent)
64 return false;
65 WebCore::Text* coreTarget = static_cast<WebCore::Text*>(WEBKIT_DOM_OBJECT(target)->coreObject);
66
67 auto result = coreTarget->dispatchEventForBindings(*coreEvent);
68 if (result.hasException()) {
69 auto description = WebCore::DOMException::description(result.releaseException().code());
70 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
71 return false;
72 }
73 return result.releaseReturnValue();
74}
75
76static gboolean webkit_dom_text_add_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
77{
78 WebCore::Text* coreTarget = static_cast<WebCore::Text*>(WEBKIT_DOM_OBJECT(target)->coreObject);
79 return WebKit::GObjectEventListener::addEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
80}
81
82static gboolean webkit_dom_text_remove_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
83{
84 WebCore::Text* coreTarget = static_cast<WebCore::Text*>(WEBKIT_DOM_OBJECT(target)->coreObject);
85 return WebKit::GObjectEventListener::removeEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
86}
87
88static void webkit_dom_text_dom_event_target_init(WebKitDOMEventTargetIface* iface)
89{
90 iface->dispatch_event = webkit_dom_text_dispatch_event;
91 iface->add_event_listener = webkit_dom_text_add_event_listener;
92 iface->remove_event_listener = webkit_dom_text_remove_event_listener;
93}
94
95G_DEFINE_TYPE_WITH_CODE(WebKitDOMText, webkit_dom_text, WEBKIT_DOM_TYPE_CHARACTER_DATA, G_IMPLEMENT_INTERFACE(WEBKIT_DOM_TYPE_EVENT_TARGET, webkit_dom_text_dom_event_target_init))
96
97enum {
98 DOM_TEXT_PROP_0,
99 DOM_TEXT_PROP_WHOLE_TEXT,
100};
101
102static void webkit_dom_text_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
103{
104 WebKitDOMText* self = WEBKIT_DOM_TEXT(object);
105
106 switch (propertyId) {
107 case DOM_TEXT_PROP_WHOLE_TEXT:
108 g_value_take_string(value, webkit_dom_text_get_whole_text(self));
109 break;
110 default:
111 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
112 break;
113 }
114}
115
116static void webkit_dom_text_class_init(WebKitDOMTextClass* requestClass)
117{
118 GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
119 gobjectClass->get_property = webkit_dom_text_get_property;
120
121 g_object_class_install_property(
122 gobjectClass,
123 DOM_TEXT_PROP_WHOLE_TEXT,
124 g_param_spec_string(
125 "whole-text",
126 "Text:whole-text",
127 "read-only gchar* Text:whole-text",
128 "",
129 WEBKIT_PARAM_READABLE));
130
131}
132
133static void webkit_dom_text_init(WebKitDOMText* request)
134{
135 UNUSED_PARAM(request);
136}
137
138WebKitDOMText* webkit_dom_text_split_text(WebKitDOMText* self, gulong offset, GError** error)
139{
140 WebCore::JSMainThreadNullState state;
141 g_return_val_if_fail(WEBKIT_DOM_IS_TEXT(self), 0);
142 g_return_val_if_fail(!error || !*error, 0);
143 WebCore::Text* item = WebKit::core(self);
144 auto result = item->splitText(offset);
145 if (result.hasException()) {
146 auto description = WebCore::DOMException::description(result.releaseException().code());
147 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
148 return nullptr;
149 }
150 return WebKit::kit(result.releaseReturnValue().ptr());
151}
152
153gchar* webkit_dom_text_get_whole_text(WebKitDOMText* self)
154{
155 WebCore::JSMainThreadNullState state;
156 g_return_val_if_fail(WEBKIT_DOM_IS_TEXT(self), 0);
157 WebCore::Text* item = WebKit::core(self);
158 gchar* result = convertToUTF8String(item->wholeText());
159 return result;
160}
161
162G_GNUC_END_IGNORE_DEPRECATIONS;
163