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 "WebKitDOMCSSStyleSheet.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 "WebKitDOMCSSRuleListPrivate.h"
29#include "WebKitDOMCSSRulePrivate.h"
30#include "WebKitDOMCSSStyleSheetPrivate.h"
31#include "WebKitDOMPrivate.h"
32#include "WebKitDOMStyleSheetPrivate.h"
33#include "ConvertToUTF8String.h"
34#include <wtf/GetPtr.h>
35#include <wtf/RefPtr.h>
36
37G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
38
39namespace WebKit {
40
41WebKitDOMCSSStyleSheet* kit(WebCore::CSSStyleSheet* obj)
42{
43 return WEBKIT_DOM_CSS_STYLE_SHEET(kit(static_cast<WebCore::StyleSheet*>(obj)));
44}
45
46WebCore::CSSStyleSheet* core(WebKitDOMCSSStyleSheet* request)
47{
48 return request ? static_cast<WebCore::CSSStyleSheet*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
49}
50
51WebKitDOMCSSStyleSheet* wrapCSSStyleSheet(WebCore::CSSStyleSheet* coreObject)
52{
53 ASSERT(coreObject);
54 return WEBKIT_DOM_CSS_STYLE_SHEET(g_object_new(WEBKIT_DOM_TYPE_CSS_STYLE_SHEET, "core-object", coreObject, nullptr));
55}
56
57} // namespace WebKit
58
59G_DEFINE_TYPE(WebKitDOMCSSStyleSheet, webkit_dom_css_style_sheet, WEBKIT_DOM_TYPE_STYLE_SHEET)
60
61enum {
62 DOM_CSS_STYLE_SHEET_PROP_0,
63 DOM_CSS_STYLE_SHEET_PROP_OWNER_RULE,
64 DOM_CSS_STYLE_SHEET_PROP_CSS_RULES,
65 DOM_CSS_STYLE_SHEET_PROP_RULES,
66};
67
68static void webkit_dom_css_style_sheet_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
69{
70 WebKitDOMCSSStyleSheet* self = WEBKIT_DOM_CSS_STYLE_SHEET(object);
71
72 switch (propertyId) {
73 case DOM_CSS_STYLE_SHEET_PROP_OWNER_RULE:
74 g_value_set_object(value, webkit_dom_css_style_sheet_get_owner_rule(self));
75 break;
76 case DOM_CSS_STYLE_SHEET_PROP_CSS_RULES:
77 g_value_set_object(value, webkit_dom_css_style_sheet_get_css_rules(self));
78 break;
79 case DOM_CSS_STYLE_SHEET_PROP_RULES:
80 g_value_set_object(value, webkit_dom_css_style_sheet_get_rules(self));
81 break;
82 default:
83 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
84 break;
85 }
86}
87
88static void webkit_dom_css_style_sheet_class_init(WebKitDOMCSSStyleSheetClass* requestClass)
89{
90 GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
91 gobjectClass->get_property = webkit_dom_css_style_sheet_get_property;
92
93 g_object_class_install_property(
94 gobjectClass,
95 DOM_CSS_STYLE_SHEET_PROP_OWNER_RULE,
96 g_param_spec_object(
97 "owner-rule",
98 "CSSStyleSheet:owner-rule",
99 "read-only WebKitDOMCSSRule* CSSStyleSheet:owner-rule",
100 WEBKIT_DOM_TYPE_CSS_RULE,
101 WEBKIT_PARAM_READABLE));
102
103 g_object_class_install_property(
104 gobjectClass,
105 DOM_CSS_STYLE_SHEET_PROP_CSS_RULES,
106 g_param_spec_object(
107 "css-rules",
108 "CSSStyleSheet:css-rules",
109 "read-only WebKitDOMCSSRuleList* CSSStyleSheet:css-rules",
110 WEBKIT_DOM_TYPE_CSS_RULE_LIST,
111 WEBKIT_PARAM_READABLE));
112
113 g_object_class_install_property(
114 gobjectClass,
115 DOM_CSS_STYLE_SHEET_PROP_RULES,
116 g_param_spec_object(
117 "rules",
118 "CSSStyleSheet:rules",
119 "read-only WebKitDOMCSSRuleList* CSSStyleSheet:rules",
120 WEBKIT_DOM_TYPE_CSS_RULE_LIST,
121 WEBKIT_PARAM_READABLE));
122
123}
124
125static void webkit_dom_css_style_sheet_init(WebKitDOMCSSStyleSheet* request)
126{
127 UNUSED_PARAM(request);
128}
129
130gulong webkit_dom_css_style_sheet_insert_rule(WebKitDOMCSSStyleSheet* self, const gchar* rule, gulong index, GError** error)
131{
132 WebCore::JSMainThreadNullState state;
133 g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_SHEET(self), 0);
134 g_return_val_if_fail(rule, 0);
135 g_return_val_if_fail(!error || !*error, 0);
136 WebCore::CSSStyleSheet* item = WebKit::core(self);
137 WTF::String convertedRule = WTF::String::fromUTF8(rule);
138 auto result = item->insertRule(convertedRule, index);
139 if (result.hasException()) {
140 auto description = WebCore::DOMException::description(result.releaseException().code());
141 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
142 return 0;
143 }
144 return result.releaseReturnValue();
145}
146
147void webkit_dom_css_style_sheet_delete_rule(WebKitDOMCSSStyleSheet* self, gulong index, GError** error)
148{
149 WebCore::JSMainThreadNullState state;
150 g_return_if_fail(WEBKIT_DOM_IS_CSS_STYLE_SHEET(self));
151 g_return_if_fail(!error || !*error);
152 WebCore::CSSStyleSheet* item = WebKit::core(self);
153 auto result = item->deleteRule(index);
154 if (result.hasException()) {
155 auto description = WebCore::DOMException::description(result.releaseException().code());
156 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
157 }
158}
159
160glong webkit_dom_css_style_sheet_add_rule(WebKitDOMCSSStyleSheet* self, const gchar* selector, const gchar* style, gulong index, GError** error)
161{
162 WebCore::JSMainThreadNullState state;
163 g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_SHEET(self), 0);
164 g_return_val_if_fail(selector, 0);
165 g_return_val_if_fail(style, 0);
166 g_return_val_if_fail(!error || !*error, 0);
167 WebCore::CSSStyleSheet* item = WebKit::core(self);
168 WTF::String convertedSelector = WTF::String::fromUTF8(selector);
169 WTF::String convertedStyle = WTF::String::fromUTF8(style);
170 auto result = item->addRule(convertedSelector, convertedStyle, index);
171 if (result.hasException()) {
172 auto description = WebCore::DOMException::description(result.releaseException().code());
173 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
174 return -1;
175 }
176 return result.releaseReturnValue();
177}
178
179void webkit_dom_css_style_sheet_remove_rule(WebKitDOMCSSStyleSheet* self, gulong index, GError** error)
180{
181 WebCore::JSMainThreadNullState state;
182 g_return_if_fail(WEBKIT_DOM_IS_CSS_STYLE_SHEET(self));
183 g_return_if_fail(!error || !*error);
184 WebCore::CSSStyleSheet* item = WebKit::core(self);
185 auto result = item->removeRule(index);
186 if (result.hasException()) {
187 auto description = WebCore::DOMException::description(result.releaseException().code());
188 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
189 }
190}
191
192WebKitDOMCSSRule* webkit_dom_css_style_sheet_get_owner_rule(WebKitDOMCSSStyleSheet* self)
193{
194 WebCore::JSMainThreadNullState state;
195 g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_SHEET(self), 0);
196 WebCore::CSSStyleSheet* item = WebKit::core(self);
197 RefPtr<WebCore::CSSRule> gobjectResult = WTF::getPtr(item->ownerRule());
198 return WebKit::kit(gobjectResult.get());
199}
200
201WebKitDOMCSSRuleList* webkit_dom_css_style_sheet_get_css_rules(WebKitDOMCSSStyleSheet* self)
202{
203 WebCore::JSMainThreadNullState state;
204 g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_SHEET(self), 0);
205 WebCore::CSSStyleSheet* item = WebKit::core(self);
206 RefPtr<WebCore::CSSRuleList> gobjectResult = WTF::getPtr(item->cssRules());
207 return WebKit::kit(gobjectResult.get());
208}
209
210WebKitDOMCSSRuleList* webkit_dom_css_style_sheet_get_rules(WebKitDOMCSSStyleSheet* self)
211{
212 WebCore::JSMainThreadNullState state;
213 g_return_val_if_fail(WEBKIT_DOM_IS_CSS_STYLE_SHEET(self), 0);
214 WebCore::CSSStyleSheet* item = WebKit::core(self);
215 RefPtr<WebCore::CSSRuleList> gobjectResult = WTF::getPtr(item->rules());
216 return WebKit::kit(gobjectResult.get());
217}
218
219G_GNUC_END_IGNORE_DEPRECATIONS;
220