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