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