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