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 "WebKitDOMHTMLStyleElement.h" |
22 | |
23 | #include <WebCore/CSSImportRule.h> |
24 | #include "DOMObjectCache.h" |
25 | #include <WebCore/DOMException.h> |
26 | #include <WebCore/Document.h> |
27 | #include "GObjectEventListener.h" |
28 | #include <WebCore/HTMLNames.h> |
29 | #include <WebCore/JSExecState.h> |
30 | #include "WebKitDOMEventPrivate.h" |
31 | #include "WebKitDOMEventTarget.h" |
32 | #include "WebKitDOMHTMLStyleElementPrivate.h" |
33 | #include "WebKitDOMNodePrivate.h" |
34 | #include "WebKitDOMPrivate.h" |
35 | #include "WebKitDOMStyleSheetPrivate.h" |
36 | #include "ConvertToUTF8String.h" |
37 | #include <wtf/GetPtr.h> |
38 | #include <wtf/RefPtr.h> |
39 | |
40 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS; |
41 | |
42 | namespace WebKit { |
43 | |
44 | WebKitDOMHTMLStyleElement* kit(WebCore::HTMLStyleElement* obj) |
45 | { |
46 | return WEBKIT_DOM_HTML_STYLE_ELEMENT(kit(static_cast<WebCore::Node*>(obj))); |
47 | } |
48 | |
49 | WebCore::HTMLStyleElement* core(WebKitDOMHTMLStyleElement* request) |
50 | { |
51 | return request ? static_cast<WebCore::HTMLStyleElement*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0; |
52 | } |
53 | |
54 | WebKitDOMHTMLStyleElement* wrapHTMLStyleElement(WebCore::HTMLStyleElement* coreObject) |
55 | { |
56 | ASSERT(coreObject); |
57 | return WEBKIT_DOM_HTML_STYLE_ELEMENT(g_object_new(WEBKIT_DOM_TYPE_HTML_STYLE_ELEMENT, "core-object" , coreObject, nullptr)); |
58 | } |
59 | |
60 | } // namespace WebKit |
61 | |
62 | static gboolean webkit_dom_html_style_element_dispatch_event(WebKitDOMEventTarget* target, WebKitDOMEvent* event, GError** error) |
63 | { |
64 | WebCore::Event* coreEvent = WebKit::core(event); |
65 | if (!coreEvent) |
66 | return false; |
67 | WebCore::HTMLStyleElement* coreTarget = static_cast<WebCore::HTMLStyleElement*>(WEBKIT_DOM_OBJECT(target)->coreObject); |
68 | |
69 | auto result = coreTarget->dispatchEventForBindings(*coreEvent); |
70 | if (result.hasException()) { |
71 | auto description = WebCore::DOMException::description(result.releaseException().code()); |
72 | g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM" ), description.legacyCode, description.name); |
73 | return false; |
74 | } |
75 | return result.releaseReturnValue(); |
76 | } |
77 | |
78 | static gboolean webkit_dom_html_style_element_add_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture) |
79 | { |
80 | WebCore::HTMLStyleElement* coreTarget = static_cast<WebCore::HTMLStyleElement*>(WEBKIT_DOM_OBJECT(target)->coreObject); |
81 | return WebKit::GObjectEventListener::addEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture); |
82 | } |
83 | |
84 | static gboolean webkit_dom_html_style_element_remove_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture) |
85 | { |
86 | WebCore::HTMLStyleElement* coreTarget = static_cast<WebCore::HTMLStyleElement*>(WEBKIT_DOM_OBJECT(target)->coreObject); |
87 | return WebKit::GObjectEventListener::removeEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture); |
88 | } |
89 | |
90 | static void webkit_dom_html_style_element_dom_event_target_init(WebKitDOMEventTargetIface* iface) |
91 | { |
92 | iface->dispatch_event = webkit_dom_html_style_element_dispatch_event; |
93 | iface->add_event_listener = webkit_dom_html_style_element_add_event_listener; |
94 | iface->remove_event_listener = webkit_dom_html_style_element_remove_event_listener; |
95 | } |
96 | |
97 | G_DEFINE_TYPE_WITH_CODE(WebKitDOMHTMLStyleElement, webkit_dom_html_style_element, WEBKIT_DOM_TYPE_HTML_ELEMENT, G_IMPLEMENT_INTERFACE(WEBKIT_DOM_TYPE_EVENT_TARGET, webkit_dom_html_style_element_dom_event_target_init)) |
98 | |
99 | enum { |
100 | DOM_HTML_STYLE_ELEMENT_PROP_0, |
101 | DOM_HTML_STYLE_ELEMENT_PROP_DISABLED, |
102 | DOM_HTML_STYLE_ELEMENT_PROP_MEDIA, |
103 | DOM_HTML_STYLE_ELEMENT_PROP_TYPE, |
104 | DOM_HTML_STYLE_ELEMENT_PROP_SHEET, |
105 | }; |
106 | |
107 | static void webkit_dom_html_style_element_set_property(GObject* object, guint propertyId, const GValue* value, GParamSpec* pspec) |
108 | { |
109 | WebKitDOMHTMLStyleElement* self = WEBKIT_DOM_HTML_STYLE_ELEMENT(object); |
110 | |
111 | switch (propertyId) { |
112 | case DOM_HTML_STYLE_ELEMENT_PROP_DISABLED: |
113 | webkit_dom_html_style_element_set_disabled(self, g_value_get_boolean(value)); |
114 | break; |
115 | case DOM_HTML_STYLE_ELEMENT_PROP_MEDIA: |
116 | webkit_dom_html_style_element_set_media(self, g_value_get_string(value)); |
117 | break; |
118 | case DOM_HTML_STYLE_ELEMENT_PROP_TYPE: |
119 | webkit_dom_html_style_element_set_type_attr(self, g_value_get_string(value)); |
120 | break; |
121 | default: |
122 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec); |
123 | break; |
124 | } |
125 | } |
126 | |
127 | static void webkit_dom_html_style_element_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec) |
128 | { |
129 | WebKitDOMHTMLStyleElement* self = WEBKIT_DOM_HTML_STYLE_ELEMENT(object); |
130 | |
131 | switch (propertyId) { |
132 | case DOM_HTML_STYLE_ELEMENT_PROP_DISABLED: |
133 | g_value_set_boolean(value, webkit_dom_html_style_element_get_disabled(self)); |
134 | break; |
135 | case DOM_HTML_STYLE_ELEMENT_PROP_MEDIA: |
136 | g_value_take_string(value, webkit_dom_html_style_element_get_media(self)); |
137 | break; |
138 | case DOM_HTML_STYLE_ELEMENT_PROP_TYPE: |
139 | g_value_take_string(value, webkit_dom_html_style_element_get_type_attr(self)); |
140 | break; |
141 | case DOM_HTML_STYLE_ELEMENT_PROP_SHEET: |
142 | g_value_set_object(value, webkit_dom_html_style_element_get_sheet(self)); |
143 | break; |
144 | default: |
145 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec); |
146 | break; |
147 | } |
148 | } |
149 | |
150 | static void webkit_dom_html_style_element_class_init(WebKitDOMHTMLStyleElementClass* requestClass) |
151 | { |
152 | GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass); |
153 | gobjectClass->set_property = webkit_dom_html_style_element_set_property; |
154 | gobjectClass->get_property = webkit_dom_html_style_element_get_property; |
155 | |
156 | g_object_class_install_property( |
157 | gobjectClass, |
158 | DOM_HTML_STYLE_ELEMENT_PROP_DISABLED, |
159 | g_param_spec_boolean( |
160 | "disabled" , |
161 | "HTMLStyleElement:disabled" , |
162 | "read-write gboolean HTMLStyleElement:disabled" , |
163 | FALSE, |
164 | WEBKIT_PARAM_READWRITE)); |
165 | |
166 | g_object_class_install_property( |
167 | gobjectClass, |
168 | DOM_HTML_STYLE_ELEMENT_PROP_MEDIA, |
169 | g_param_spec_string( |
170 | "media" , |
171 | "HTMLStyleElement:media" , |
172 | "read-write gchar* HTMLStyleElement:media" , |
173 | "" , |
174 | WEBKIT_PARAM_READWRITE)); |
175 | |
176 | g_object_class_install_property( |
177 | gobjectClass, |
178 | DOM_HTML_STYLE_ELEMENT_PROP_TYPE, |
179 | g_param_spec_string( |
180 | "type" , |
181 | "HTMLStyleElement:type" , |
182 | "read-write gchar* HTMLStyleElement:type" , |
183 | "" , |
184 | WEBKIT_PARAM_READWRITE)); |
185 | |
186 | g_object_class_install_property( |
187 | gobjectClass, |
188 | DOM_HTML_STYLE_ELEMENT_PROP_SHEET, |
189 | g_param_spec_object( |
190 | "sheet" , |
191 | "HTMLStyleElement:sheet" , |
192 | "read-only WebKitDOMStyleSheet* HTMLStyleElement:sheet" , |
193 | WEBKIT_DOM_TYPE_STYLE_SHEET, |
194 | WEBKIT_PARAM_READABLE)); |
195 | } |
196 | |
197 | static void webkit_dom_html_style_element_init(WebKitDOMHTMLStyleElement* request) |
198 | { |
199 | UNUSED_PARAM(request); |
200 | } |
201 | |
202 | gboolean webkit_dom_html_style_element_get_disabled(WebKitDOMHTMLStyleElement* self) |
203 | { |
204 | WebCore::JSMainThreadNullState state; |
205 | g_return_val_if_fail(WEBKIT_DOM_IS_HTML_STYLE_ELEMENT(self), FALSE); |
206 | WebCore::HTMLStyleElement* item = WebKit::core(self); |
207 | gboolean result = item->disabled(); |
208 | return result; |
209 | } |
210 | |
211 | void webkit_dom_html_style_element_set_disabled(WebKitDOMHTMLStyleElement* self, gboolean value) |
212 | { |
213 | WebCore::JSMainThreadNullState state; |
214 | g_return_if_fail(WEBKIT_DOM_IS_HTML_STYLE_ELEMENT(self)); |
215 | WebCore::HTMLStyleElement* item = WebKit::core(self); |
216 | item->setDisabled(value); |
217 | } |
218 | |
219 | gchar* webkit_dom_html_style_element_get_media(WebKitDOMHTMLStyleElement* self) |
220 | { |
221 | WebCore::JSMainThreadNullState state; |
222 | g_return_val_if_fail(WEBKIT_DOM_IS_HTML_STYLE_ELEMENT(self), 0); |
223 | WebCore::HTMLStyleElement* item = WebKit::core(self); |
224 | gchar* result = convertToUTF8String(item->attributeWithoutSynchronization(WebCore::HTMLNames::mediaAttr)); |
225 | return result; |
226 | } |
227 | |
228 | void webkit_dom_html_style_element_set_media(WebKitDOMHTMLStyleElement* self, const gchar* value) |
229 | { |
230 | WebCore::JSMainThreadNullState state; |
231 | g_return_if_fail(WEBKIT_DOM_IS_HTML_STYLE_ELEMENT(self)); |
232 | g_return_if_fail(value); |
233 | WebCore::HTMLStyleElement* item = WebKit::core(self); |
234 | WTF::String convertedValue = WTF::String::fromUTF8(value); |
235 | item->setAttributeWithoutSynchronization(WebCore::HTMLNames::mediaAttr, convertedValue); |
236 | } |
237 | |
238 | gchar* webkit_dom_html_style_element_get_type_attr(WebKitDOMHTMLStyleElement* self) |
239 | { |
240 | WebCore::JSMainThreadNullState state; |
241 | g_return_val_if_fail(WEBKIT_DOM_IS_HTML_STYLE_ELEMENT(self), 0); |
242 | WebCore::HTMLStyleElement* item = WebKit::core(self); |
243 | gchar* result = convertToUTF8String(item->attributeWithoutSynchronization(WebCore::HTMLNames::typeAttr)); |
244 | return result; |
245 | } |
246 | |
247 | void webkit_dom_html_style_element_set_type_attr(WebKitDOMHTMLStyleElement* self, const gchar* value) |
248 | { |
249 | WebCore::JSMainThreadNullState state; |
250 | g_return_if_fail(WEBKIT_DOM_IS_HTML_STYLE_ELEMENT(self)); |
251 | g_return_if_fail(value); |
252 | WebCore::HTMLStyleElement* item = WebKit::core(self); |
253 | WTF::String convertedValue = WTF::String::fromUTF8(value); |
254 | item->setAttributeWithoutSynchronization(WebCore::HTMLNames::typeAttr, convertedValue); |
255 | } |
256 | |
257 | WebKitDOMStyleSheet* webkit_dom_html_style_element_get_sheet(WebKitDOMHTMLStyleElement* self) |
258 | { |
259 | WebCore::JSMainThreadNullState state; |
260 | g_return_val_if_fail(WEBKIT_DOM_IS_HTML_STYLE_ELEMENT(self), 0); |
261 | WebCore::HTMLStyleElement* item = WebKit::core(self); |
262 | RefPtr<WebCore::StyleSheet> gobjectResult = WTF::getPtr(item->sheet()); |
263 | return WebKit::kit(gobjectResult.get()); |
264 | } |
265 | G_GNUC_END_IGNORE_DEPRECATIONS; |
266 | |