Drop ExceptionCodeDescription class
[WebKit-https.git] / Source / WebKit / WebProcess / InjectedBundle / API / gtk / DOM / WebKitDOMCSSStyleDeclaration.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 "WebKitDOMCSSStyleDeclaration.h"
22
23 #include <WebCore/CSSImportRule.h>
24 #include "DOMObjectCache.h"
25 #include <WebCore/DOMException.h>
26 #include <WebCore/Document.h>
27 #include <WebCore/JSMainThreadExecState.h>
28 #include "WebKitDOMCSSRulePrivate.h"
29 #include "WebKitDOMCSSStyleDeclarationPrivate.h"
30 #include "WebKitDOMPrivate.h"
31 #include "ConvertToUTF8String.h"
32 #include <wtf/GetPtr.h>
33 #include <wtf/RefPtr.h>
34
35 #define WEBKIT_DOM_CSS_STYLE_DECLARATION_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, WEBKIT_DOM_TYPE_CSS_STYLE_DECLARATION, WebKitDOMCSSStyleDeclarationPrivate)
36
37 typedef struct _WebKitDOMCSSStyleDeclarationPrivate {
38     RefPtr<WebCore::CSSStyleDeclaration> coreObject;
39 } WebKitDOMCSSStyleDeclarationPrivate;
40
41 namespace WebKit {
42
43 WebKitDOMCSSStyleDeclaration* kit(WebCore::CSSStyleDeclaration* obj)
44 {
45     if (!obj)
46         return 0;
47
48     if (gpointer ret = DOMObjectCache::get(obj))
49         return WEBKIT_DOM_CSS_STYLE_DECLARATION(ret);
50
51     return wrapCSSStyleDeclaration(obj);
52 }
53
54 WebCore::CSSStyleDeclaration* core(WebKitDOMCSSStyleDeclaration* request)
55 {
56     return request ? static_cast<WebCore::CSSStyleDeclaration*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
57 }
58
59 WebKitDOMCSSStyleDeclaration* wrapCSSStyleDeclaration(WebCore::CSSStyleDeclaration* coreObject)
60 {
61     ASSERT(coreObject);
62     return WEBKIT_DOM_CSS_STYLE_DECLARATION(g_object_new(WEBKIT_DOM_TYPE_CSS_STYLE_DECLARATION, "core-object", coreObject, nullptr));
63 }
64
65 } // namespace WebKit
66
67 G_DEFINE_TYPE(WebKitDOMCSSStyleDeclaration, webkit_dom_css_style_declaration, WEBKIT_DOM_TYPE_OBJECT)
68
69 enum {
70     PROP_0,
71     PROP_CSS_TEXT,
72     PROP_LENGTH,
73     PROP_PARENT_RULE,
74 };
75
76 static void webkit_dom_css_style_declaration_finalize(GObject* object)
77 {
78     WebKitDOMCSSStyleDeclarationPrivate* priv = WEBKIT_DOM_CSS_STYLE_DECLARATION_GET_PRIVATE(object);
79
80     WebKit::DOMObjectCache::forget(priv->coreObject.get());
81
82     priv->~WebKitDOMCSSStyleDeclarationPrivate();
83     G_OBJECT_CLASS(webkit_dom_css_style_declaration_parent_class)->finalize(object);
84 }
85
86 static void webkit_dom_css_style_declaration_set_property(GObject* object, guint propertyId, const GValue* value, GParamSpec* pspec)
87 {
88     WebKitDOMCSSStyleDeclaration* self = WEBKIT_DOM_CSS_STYLE_DECLARATION(object);
89
90     switch (propertyId) {
91     case PROP_CSS_TEXT:
92         webkit_dom_css_style_declaration_set_css_text(self, g_value_get_string(value), nullptr);
93         break;
94     default:
95         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
96         break;
97     }
98 }
99
100 static void webkit_dom_css_style_declaration_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
101 {
102     WebKitDOMCSSStyleDeclaration* self = WEBKIT_DOM_CSS_STYLE_DECLARATION(object);
103
104     switch (propertyId) {
105     case PROP_CSS_TEXT:
106         g_value_take_string(value, webkit_dom_css_style_declaration_get_css_text(self));
107         break;
108     case PROP_LENGTH:
109         g_value_set_ulong(value, webkit_dom_css_style_declaration_get_length(self));
110         break;
111     case PROP_PARENT_RULE:
112         g_value_set_object(value, webkit_dom_css_style_declaration_get_parent_rule(self));
113         break;
114     default:
115         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
116         break;
117     }
118 }
119
120 static GObject* webkit_dom_css_style_declaration_constructor(GType type, guint constructPropertiesCount, GObjectConstructParam* constructProperties)
121 {
122     GObject* object = G_OBJECT_CLASS(webkit_dom_css_style_declaration_parent_class)->constructor(type, constructPropertiesCount, constructProperties);
123
124     WebKitDOMCSSStyleDeclarationPrivate* priv = WEBKIT_DOM_CSS_STYLE_DECLARATION_GET_PRIVATE(object);
125     priv->coreObject = static_cast<WebCore::CSSStyleDeclaration*>(WEBKIT_DOM_OBJECT(object)->coreObject);
126     WebKit::DOMObjectCache::put(priv->coreObject.get(), object);
127
128     return object;
129 }
130
131 static void webkit_dom_css_style_declaration_class_init(WebKitDOMCSSStyleDeclarationClass* requestClass)
132 {
133     GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
134     g_type_class_add_private(gobjectClass, sizeof(WebKitDOMCSSStyleDeclarationPrivate));
135     gobjectClass->constructor = webkit_dom_css_style_declaration_constructor;
136     gobjectClass->finalize = webkit_dom_css_style_declaration_finalize;
137     gobjectClass->set_property = webkit_dom_css_style_declaration_set_property;
138     gobjectClass->get_property = webkit_dom_css_style_declaration_get_property;
139
140     g_object_class_install_property(
141         gobjectClass,
142         PROP_CSS_TEXT,
143         g_param_spec_string(
144             "css-text",
145             "CSSStyleDeclaration:css-text",
146             "read-write gchar* CSSStyleDeclaration:css-text",
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             "CSSStyleDeclaration:length",
156             "read-only gulong CSSStyleDeclaration:length",
157             0, G_MAXULONG, 0,
158             WEBKIT_PARAM_READABLE));
159
160     g_object_class_install_property(
161         gobjectClass,
162         PROP_PARENT_RULE,
163         g_param_spec_object(
164             "parent-rule",
165             "CSSStyleDeclaration:parent-rule",
166             "read-only WebKitDOMCSSRule* CSSStyleDeclaration:parent-rule",
167             WEBKIT_DOM_TYPE_CSS_RULE,
168             WEBKIT_PARAM_READABLE));
169
170 }
171
172 static void webkit_dom_css_style_declaration_init(WebKitDOMCSSStyleDeclaration* request)
173 {
174     WebKitDOMCSSStyleDeclarationPrivate* priv = WEBKIT_DOM_CSS_STYLE_DECLARATION_GET_PRIVATE(request);
175     new (priv) WebKitDOMCSSStyleDeclarationPrivate();
176 }
177
178 gchar* webkit_dom_css_style_declaration_get_property_value(WebKitDOMCSSStyleDeclaration* self, const gchar* propertyName)
179 {
180     WebCore::JSMainThreadNullState state;
181     g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_DECLARATION(self), 0);
182     g_return_val_if_fail(propertyName, 0);
183     WebCore::CSSStyleDeclaration* item = WebKit::core(self);
184     WTF::String convertedPropertyName = WTF::String::fromUTF8(propertyName);
185     gchar* result = convertToUTF8String(item->getPropertyValue(convertedPropertyName));
186     return result;
187 }
188
189 gchar* webkit_dom_css_style_declaration_remove_property(WebKitDOMCSSStyleDeclaration* self, const gchar* propertyName, GError** error)
190 {
191     WebCore::JSMainThreadNullState state;
192     g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_DECLARATION(self), 0);
193     g_return_val_if_fail(propertyName, 0);
194     g_return_val_if_fail(!error || !*error, 0);
195     WebCore::CSSStyleDeclaration* item = WebKit::core(self);
196     WTF::String convertedPropertyName = WTF::String::fromUTF8(propertyName);
197     auto result = item->removeProperty(convertedPropertyName);
198     if (result.hasException())
199         return nullptr;
200     return convertToUTF8String(result.releaseReturnValue());
201 }
202
203 gchar* webkit_dom_css_style_declaration_get_property_priority(WebKitDOMCSSStyleDeclaration* self, const gchar* propertyName)
204 {
205     WebCore::JSMainThreadNullState state;
206     g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_DECLARATION(self), 0);
207     g_return_val_if_fail(propertyName, 0);
208     WebCore::CSSStyleDeclaration* item = WebKit::core(self);
209     WTF::String convertedPropertyName = WTF::String::fromUTF8(propertyName);
210     gchar* result = convertToUTF8String(item->getPropertyPriority(convertedPropertyName));
211     return result;
212 }
213
214 void webkit_dom_css_style_declaration_set_property(WebKitDOMCSSStyleDeclaration* self, const gchar* propertyName, const gchar* value, const gchar* priority, GError** error)
215 {
216     WebCore::JSMainThreadNullState state;
217     g_return_if_fail(WEBKIT_DOM_IS_CSS_STYLE_DECLARATION(self));
218     g_return_if_fail(propertyName);
219     g_return_if_fail(value);
220     g_return_if_fail(priority);
221     g_return_if_fail(!error || !*error);
222     WebCore::CSSStyleDeclaration* item = WebKit::core(self);
223     WTF::String convertedPropertyName = WTF::String::fromUTF8(propertyName);
224     WTF::String convertedValue = WTF::String::fromUTF8(value);
225     WTF::String convertedPriority = WTF::String::fromUTF8(priority);
226     auto result = item->setProperty(convertedPropertyName, convertedValue, convertedPriority);
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
233 gchar* webkit_dom_css_style_declaration_item(WebKitDOMCSSStyleDeclaration* self, gulong index)
234 {
235     WebCore::JSMainThreadNullState state;
236     g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_DECLARATION(self), 0);
237     WebCore::CSSStyleDeclaration* item = WebKit::core(self);
238     gchar* result = convertToUTF8String(item->item(index));
239     return result;
240 }
241
242 gchar* webkit_dom_css_style_declaration_get_property_shorthand(WebKitDOMCSSStyleDeclaration* self, const gchar* propertyName)
243 {
244     WebCore::JSMainThreadNullState state;
245     g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_DECLARATION(self), 0);
246     g_return_val_if_fail(propertyName, 0);
247     WebCore::CSSStyleDeclaration* item = WebKit::core(self);
248     WTF::String convertedPropertyName = WTF::String::fromUTF8(propertyName);
249     gchar* result = convertToUTF8String(item->getPropertyShorthand(convertedPropertyName));
250     return result;
251 }
252
253 gboolean webkit_dom_css_style_declaration_is_property_implicit(WebKitDOMCSSStyleDeclaration* self, const gchar* propertyName)
254 {
255     WebCore::JSMainThreadNullState state;
256     g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_DECLARATION(self), FALSE);
257     g_return_val_if_fail(propertyName, FALSE);
258     WebCore::CSSStyleDeclaration* item = WebKit::core(self);
259     WTF::String convertedPropertyName = WTF::String::fromUTF8(propertyName);
260     gboolean result = item->isPropertyImplicit(convertedPropertyName);
261     return result;
262 }
263
264 gchar* webkit_dom_css_style_declaration_get_css_text(WebKitDOMCSSStyleDeclaration* self)
265 {
266     WebCore::JSMainThreadNullState state;
267     g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_DECLARATION(self), 0);
268     WebCore::CSSStyleDeclaration* item = WebKit::core(self);
269     gchar* result = convertToUTF8String(item->cssText());
270     return result;
271 }
272
273 void webkit_dom_css_style_declaration_set_css_text(WebKitDOMCSSStyleDeclaration* self, const gchar* value, GError** error)
274 {
275     WebCore::JSMainThreadNullState state;
276     g_return_if_fail(WEBKIT_DOM_IS_CSS_STYLE_DECLARATION(self));
277     g_return_if_fail(value);
278     g_return_if_fail(!error || !*error);
279     WebCore::CSSStyleDeclaration* item = WebKit::core(self);
280     WTF::String convertedValue = WTF::String::fromUTF8(value);
281     auto result = item->setCssText(convertedValue);
282     if (result.hasException()) {
283         auto description = WebCore::DOMException::description(result.releaseException().code());
284         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
285     }
286 }
287
288 gulong webkit_dom_css_style_declaration_get_length(WebKitDOMCSSStyleDeclaration* self)
289 {
290     WebCore::JSMainThreadNullState state;
291     g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_DECLARATION(self), 0);
292     WebCore::CSSStyleDeclaration* item = WebKit::core(self);
293     gulong result = item->length();
294     return result;
295 }
296
297 WebKitDOMCSSRule* webkit_dom_css_style_declaration_get_parent_rule(WebKitDOMCSSStyleDeclaration* self)
298 {
299     WebCore::JSMainThreadNullState state;
300     g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_DECLARATION(self), 0);
301     WebCore::CSSStyleDeclaration* item = WebKit::core(self);
302     RefPtr<WebCore::CSSRule> gobjectResult = WTF::getPtr(item->parentRule());
303     return WebKit::kit(gobjectResult.get());
304 }
305