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