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 "WebKitDOMHTMLScriptElement.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 "WebKitDOMHTMLScriptElementPrivate.h"
33#include "WebKitDOMNodePrivate.h"
34#include "WebKitDOMPrivate.h"
35#include "ConvertToUTF8String.h"
36#include <wtf/GetPtr.h>
37#include <wtf/RefPtr.h>
38
39G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
40
41namespace WebKit {
42
43WebKitDOMHTMLScriptElement* kit(WebCore::HTMLScriptElement* obj)
44{
45 return WEBKIT_DOM_HTML_SCRIPT_ELEMENT(kit(static_cast<WebCore::Node*>(obj)));
46}
47
48WebCore::HTMLScriptElement* core(WebKitDOMHTMLScriptElement* request)
49{
50 return request ? static_cast<WebCore::HTMLScriptElement*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
51}
52
53WebKitDOMHTMLScriptElement* wrapHTMLScriptElement(WebCore::HTMLScriptElement* coreObject)
54{
55 ASSERT(coreObject);
56 return WEBKIT_DOM_HTML_SCRIPT_ELEMENT(g_object_new(WEBKIT_DOM_TYPE_HTML_SCRIPT_ELEMENT, "core-object", coreObject, nullptr));
57}
58
59} // namespace WebKit
60
61static gboolean webkit_dom_html_script_element_dispatch_event(WebKitDOMEventTarget* target, WebKitDOMEvent* event, GError** error)
62{
63 WebCore::Event* coreEvent = WebKit::core(event);
64 if (!coreEvent)
65 return false;
66 WebCore::HTMLScriptElement* coreTarget = static_cast<WebCore::HTMLScriptElement*>(WEBKIT_DOM_OBJECT(target)->coreObject);
67
68 auto result = coreTarget->dispatchEventForBindings(*coreEvent);
69 if (result.hasException()) {
70 auto description = WebCore::DOMException::description(result.releaseException().code());
71 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
72 return false;
73 }
74 return result.releaseReturnValue();
75}
76
77static gboolean webkit_dom_html_script_element_add_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
78{
79 WebCore::HTMLScriptElement* coreTarget = static_cast<WebCore::HTMLScriptElement*>(WEBKIT_DOM_OBJECT(target)->coreObject);
80 return WebKit::GObjectEventListener::addEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
81}
82
83static gboolean webkit_dom_html_script_element_remove_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
84{
85 WebCore::HTMLScriptElement* coreTarget = static_cast<WebCore::HTMLScriptElement*>(WEBKIT_DOM_OBJECT(target)->coreObject);
86 return WebKit::GObjectEventListener::removeEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
87}
88
89static void webkit_dom_html_script_element_dom_event_target_init(WebKitDOMEventTargetIface* iface)
90{
91 iface->dispatch_event = webkit_dom_html_script_element_dispatch_event;
92 iface->add_event_listener = webkit_dom_html_script_element_add_event_listener;
93 iface->remove_event_listener = webkit_dom_html_script_element_remove_event_listener;
94}
95
96G_DEFINE_TYPE_WITH_CODE(WebKitDOMHTMLScriptElement, webkit_dom_html_script_element, WEBKIT_DOM_TYPE_HTML_ELEMENT, G_IMPLEMENT_INTERFACE(WEBKIT_DOM_TYPE_EVENT_TARGET, webkit_dom_html_script_element_dom_event_target_init))
97
98enum {
99 DOM_HTML_SCRIPT_ELEMENT_PROP_0,
100 DOM_HTML_SCRIPT_ELEMENT_PROP_TEXT,
101 DOM_HTML_SCRIPT_ELEMENT_PROP_HTML_FOR,
102 DOM_HTML_SCRIPT_ELEMENT_PROP_EVENT,
103 DOM_HTML_SCRIPT_ELEMENT_PROP_CHARSET,
104 DOM_HTML_SCRIPT_ELEMENT_PROP_DEFER,
105 DOM_HTML_SCRIPT_ELEMENT_PROP_SRC,
106 DOM_HTML_SCRIPT_ELEMENT_PROP_TYPE,
107};
108
109static void webkit_dom_html_script_element_set_property(GObject* object, guint propertyId, const GValue* value, GParamSpec* pspec)
110{
111 WebKitDOMHTMLScriptElement* self = WEBKIT_DOM_HTML_SCRIPT_ELEMENT(object);
112
113 switch (propertyId) {
114 case DOM_HTML_SCRIPT_ELEMENT_PROP_TEXT:
115 webkit_dom_html_script_element_set_text(self, g_value_get_string(value));
116 break;
117 case DOM_HTML_SCRIPT_ELEMENT_PROP_HTML_FOR:
118 webkit_dom_html_script_element_set_html_for(self, g_value_get_string(value));
119 break;
120 case DOM_HTML_SCRIPT_ELEMENT_PROP_EVENT:
121 webkit_dom_html_script_element_set_event(self, g_value_get_string(value));
122 break;
123 case DOM_HTML_SCRIPT_ELEMENT_PROP_CHARSET:
124 webkit_dom_html_script_element_set_charset(self, g_value_get_string(value));
125 break;
126 case DOM_HTML_SCRIPT_ELEMENT_PROP_DEFER:
127 webkit_dom_html_script_element_set_defer(self, g_value_get_boolean(value));
128 break;
129 case DOM_HTML_SCRIPT_ELEMENT_PROP_SRC:
130 webkit_dom_html_script_element_set_src(self, g_value_get_string(value));
131 break;
132 case DOM_HTML_SCRIPT_ELEMENT_PROP_TYPE:
133 webkit_dom_html_script_element_set_type_attr(self, g_value_get_string(value));
134 break;
135 default:
136 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
137 break;
138 }
139}
140
141static void webkit_dom_html_script_element_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
142{
143 WebKitDOMHTMLScriptElement* self = WEBKIT_DOM_HTML_SCRIPT_ELEMENT(object);
144
145 switch (propertyId) {
146 case DOM_HTML_SCRIPT_ELEMENT_PROP_TEXT:
147 g_value_take_string(value, webkit_dom_html_script_element_get_text(self));
148 break;
149 case DOM_HTML_SCRIPT_ELEMENT_PROP_HTML_FOR:
150 g_value_take_string(value, webkit_dom_html_script_element_get_html_for(self));
151 break;
152 case DOM_HTML_SCRIPT_ELEMENT_PROP_EVENT:
153 g_value_take_string(value, webkit_dom_html_script_element_get_event(self));
154 break;
155 case DOM_HTML_SCRIPT_ELEMENT_PROP_CHARSET:
156 g_value_take_string(value, webkit_dom_html_script_element_get_charset(self));
157 break;
158 case DOM_HTML_SCRIPT_ELEMENT_PROP_DEFER:
159 g_value_set_boolean(value, webkit_dom_html_script_element_get_defer(self));
160 break;
161 case DOM_HTML_SCRIPT_ELEMENT_PROP_SRC:
162 g_value_take_string(value, webkit_dom_html_script_element_get_src(self));
163 break;
164 case DOM_HTML_SCRIPT_ELEMENT_PROP_TYPE:
165 g_value_take_string(value, webkit_dom_html_script_element_get_type_attr(self));
166 break;
167 default:
168 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
169 break;
170 }
171}
172
173static void webkit_dom_html_script_element_class_init(WebKitDOMHTMLScriptElementClass* requestClass)
174{
175 GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
176 gobjectClass->set_property = webkit_dom_html_script_element_set_property;
177 gobjectClass->get_property = webkit_dom_html_script_element_get_property;
178
179 g_object_class_install_property(
180 gobjectClass,
181 DOM_HTML_SCRIPT_ELEMENT_PROP_TEXT,
182 g_param_spec_string(
183 "text",
184 "HTMLScriptElement:text",
185 "read-write gchar* HTMLScriptElement:text",
186 "",
187 WEBKIT_PARAM_READWRITE));
188
189 g_object_class_install_property(
190 gobjectClass,
191 DOM_HTML_SCRIPT_ELEMENT_PROP_HTML_FOR,
192 g_param_spec_string(
193 "html-for",
194 "HTMLScriptElement:html-for",
195 "read-write gchar* HTMLScriptElement:html-for",
196 "",
197 WEBKIT_PARAM_READWRITE));
198
199 g_object_class_install_property(
200 gobjectClass,
201 DOM_HTML_SCRIPT_ELEMENT_PROP_EVENT,
202 g_param_spec_string(
203 "event",
204 "HTMLScriptElement:event",
205 "read-write gchar* HTMLScriptElement:event",
206 "",
207 WEBKIT_PARAM_READWRITE));
208
209 g_object_class_install_property(
210 gobjectClass,
211 DOM_HTML_SCRIPT_ELEMENT_PROP_CHARSET,
212 g_param_spec_string(
213 "charset",
214 "HTMLScriptElement:charset",
215 "read-write gchar* HTMLScriptElement:charset",
216 "",
217 WEBKIT_PARAM_READWRITE));
218
219 g_object_class_install_property(
220 gobjectClass,
221 DOM_HTML_SCRIPT_ELEMENT_PROP_DEFER,
222 g_param_spec_boolean(
223 "defer",
224 "HTMLScriptElement:defer",
225 "read-write gboolean HTMLScriptElement:defer",
226 FALSE,
227 WEBKIT_PARAM_READWRITE));
228
229 g_object_class_install_property(
230 gobjectClass,
231 DOM_HTML_SCRIPT_ELEMENT_PROP_SRC,
232 g_param_spec_string(
233 "src",
234 "HTMLScriptElement:src",
235 "read-write gchar* HTMLScriptElement:src",
236 "",
237 WEBKIT_PARAM_READWRITE));
238
239 g_object_class_install_property(
240 gobjectClass,
241 DOM_HTML_SCRIPT_ELEMENT_PROP_TYPE,
242 g_param_spec_string(
243 "type",
244 "HTMLScriptElement:type",
245 "read-write gchar* HTMLScriptElement:type",
246 "",
247 WEBKIT_PARAM_READWRITE));
248}
249
250static void webkit_dom_html_script_element_init(WebKitDOMHTMLScriptElement* request)
251{
252 UNUSED_PARAM(request);
253}
254
255gchar* webkit_dom_html_script_element_get_text(WebKitDOMHTMLScriptElement* self)
256{
257 WebCore::JSMainThreadNullState state;
258 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_SCRIPT_ELEMENT(self), 0);
259 WebCore::HTMLScriptElement* item = WebKit::core(self);
260 gchar* result = convertToUTF8String(item->text());
261 return result;
262}
263
264void webkit_dom_html_script_element_set_text(WebKitDOMHTMLScriptElement* self, const gchar* value)
265{
266 WebCore::JSMainThreadNullState state;
267 g_return_if_fail(WEBKIT_DOM_IS_HTML_SCRIPT_ELEMENT(self));
268 g_return_if_fail(value);
269 WebCore::HTMLScriptElement* item = WebKit::core(self);
270 WTF::String convertedValue = WTF::String::fromUTF8(value);
271 item->setText(convertedValue);
272}
273
274gchar* webkit_dom_html_script_element_get_html_for(WebKitDOMHTMLScriptElement* self)
275{
276 WebCore::JSMainThreadNullState state;
277 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_SCRIPT_ELEMENT(self), 0);
278 WebCore::HTMLScriptElement* item = WebKit::core(self);
279 gchar* result = convertToUTF8String(item->attributeWithoutSynchronization(WebCore::HTMLNames::forAttr));
280 return result;
281}
282
283void webkit_dom_html_script_element_set_html_for(WebKitDOMHTMLScriptElement* self, const gchar* value)
284{
285 WebCore::JSMainThreadNullState state;
286 g_return_if_fail(WEBKIT_DOM_IS_HTML_SCRIPT_ELEMENT(self));
287 g_return_if_fail(value);
288 WebCore::HTMLScriptElement* item = WebKit::core(self);
289 WTF::String convertedValue = WTF::String::fromUTF8(value);
290 item->setAttributeWithoutSynchronization(WebCore::HTMLNames::forAttr, convertedValue);
291}
292
293gchar* webkit_dom_html_script_element_get_event(WebKitDOMHTMLScriptElement* self)
294{
295 WebCore::JSMainThreadNullState state;
296 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_SCRIPT_ELEMENT(self), 0);
297 WebCore::HTMLScriptElement* item = WebKit::core(self);
298 gchar* result = convertToUTF8String(item->attributeWithoutSynchronization(WebCore::HTMLNames::eventAttr));
299 return result;
300}
301
302void webkit_dom_html_script_element_set_event(WebKitDOMHTMLScriptElement* self, const gchar* value)
303{
304 WebCore::JSMainThreadNullState state;
305 g_return_if_fail(WEBKIT_DOM_IS_HTML_SCRIPT_ELEMENT(self));
306 g_return_if_fail(value);
307 WebCore::HTMLScriptElement* item = WebKit::core(self);
308 WTF::String convertedValue = WTF::String::fromUTF8(value);
309 item->setAttributeWithoutSynchronization(WebCore::HTMLNames::eventAttr, convertedValue);
310}
311
312gchar* webkit_dom_html_script_element_get_charset(WebKitDOMHTMLScriptElement* self)
313{
314 WebCore::JSMainThreadNullState state;
315 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_SCRIPT_ELEMENT(self), 0);
316 WebCore::HTMLScriptElement* item = WebKit::core(self);
317 gchar* result = convertToUTF8String(item->attributeWithoutSynchronization(WebCore::HTMLNames::charsetAttr));
318 return result;
319}
320
321void webkit_dom_html_script_element_set_charset(WebKitDOMHTMLScriptElement* self, const gchar* value)
322{
323 WebCore::JSMainThreadNullState state;
324 g_return_if_fail(WEBKIT_DOM_IS_HTML_SCRIPT_ELEMENT(self));
325 g_return_if_fail(value);
326 WebCore::HTMLScriptElement* item = WebKit::core(self);
327 WTF::String convertedValue = WTF::String::fromUTF8(value);
328 item->setAttributeWithoutSynchronization(WebCore::HTMLNames::charsetAttr, convertedValue);
329}
330
331gboolean webkit_dom_html_script_element_get_defer(WebKitDOMHTMLScriptElement* self)
332{
333 WebCore::JSMainThreadNullState state;
334 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_SCRIPT_ELEMENT(self), FALSE);
335 WebCore::HTMLScriptElement* item = WebKit::core(self);
336 gboolean result = item->hasAttributeWithoutSynchronization(WebCore::HTMLNames::deferAttr);
337 return result;
338}
339
340void webkit_dom_html_script_element_set_defer(WebKitDOMHTMLScriptElement* self, gboolean value)
341{
342 WebCore::JSMainThreadNullState state;
343 g_return_if_fail(WEBKIT_DOM_IS_HTML_SCRIPT_ELEMENT(self));
344 WebCore::HTMLScriptElement* item = WebKit::core(self);
345 item->setBooleanAttribute(WebCore::HTMLNames::deferAttr, value);
346}
347
348gchar* webkit_dom_html_script_element_get_src(WebKitDOMHTMLScriptElement* self)
349{
350 WebCore::JSMainThreadNullState state;
351 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_SCRIPT_ELEMENT(self), 0);
352 WebCore::HTMLScriptElement* item = WebKit::core(self);
353 gchar* result = convertToUTF8String(item->getURLAttribute(WebCore::HTMLNames::srcAttr));
354 return result;
355}
356
357void webkit_dom_html_script_element_set_src(WebKitDOMHTMLScriptElement* self, const gchar* value)
358{
359 WebCore::JSMainThreadNullState state;
360 g_return_if_fail(WEBKIT_DOM_IS_HTML_SCRIPT_ELEMENT(self));
361 g_return_if_fail(value);
362 WebCore::HTMLScriptElement* item = WebKit::core(self);
363 WTF::String convertedValue = WTF::String::fromUTF8(value);
364 item->setAttributeWithoutSynchronization(WebCore::HTMLNames::srcAttr, convertedValue);
365}
366
367gchar* webkit_dom_html_script_element_get_type_attr(WebKitDOMHTMLScriptElement* self)
368{
369 WebCore::JSMainThreadNullState state;
370 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_SCRIPT_ELEMENT(self), 0);
371 WebCore::HTMLScriptElement* item = WebKit::core(self);
372 gchar* result = convertToUTF8String(item->attributeWithoutSynchronization(WebCore::HTMLNames::typeAttr));
373 return result;
374}
375
376void webkit_dom_html_script_element_set_type_attr(WebKitDOMHTMLScriptElement* self, const gchar* value)
377{
378 WebCore::JSMainThreadNullState state;
379 g_return_if_fail(WEBKIT_DOM_IS_HTML_SCRIPT_ELEMENT(self));
380 g_return_if_fail(value);
381 WebCore::HTMLScriptElement* item = WebKit::core(self);
382 WTF::String convertedValue = WTF::String::fromUTF8(value);
383 item->setAttributeWithoutSynchronization(WebCore::HTMLNames::typeAttr, convertedValue);
384}
385G_GNUC_END_IGNORE_DEPRECATIONS;
386