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 "WebKitDOMHTMLOptionElement.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 "WebKitDOMHTMLFormElementPrivate.h"
33#include "WebKitDOMHTMLOptionElementPrivate.h"
34#include "WebKitDOMNodePrivate.h"
35#include "WebKitDOMPrivate.h"
36#include "ConvertToUTF8String.h"
37#include <wtf/GetPtr.h>
38#include <wtf/RefPtr.h>
39
40G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
41
42namespace WebKit {
43
44WebKitDOMHTMLOptionElement* kit(WebCore::HTMLOptionElement* obj)
45{
46 return WEBKIT_DOM_HTML_OPTION_ELEMENT(kit(static_cast<WebCore::Node*>(obj)));
47}
48
49WebCore::HTMLOptionElement* core(WebKitDOMHTMLOptionElement* request)
50{
51 return request ? static_cast<WebCore::HTMLOptionElement*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
52}
53
54WebKitDOMHTMLOptionElement* wrapHTMLOptionElement(WebCore::HTMLOptionElement* coreObject)
55{
56 ASSERT(coreObject);
57 return WEBKIT_DOM_HTML_OPTION_ELEMENT(g_object_new(WEBKIT_DOM_TYPE_HTML_OPTION_ELEMENT, "core-object", coreObject, nullptr));
58}
59
60} // namespace WebKit
61
62static gboolean webkit_dom_html_option_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::HTMLOptionElement* coreTarget = static_cast<WebCore::HTMLOptionElement*>(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
78static gboolean webkit_dom_html_option_element_add_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
79{
80 WebCore::HTMLOptionElement* coreTarget = static_cast<WebCore::HTMLOptionElement*>(WEBKIT_DOM_OBJECT(target)->coreObject);
81 return WebKit::GObjectEventListener::addEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
82}
83
84static gboolean webkit_dom_html_option_element_remove_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
85{
86 WebCore::HTMLOptionElement* coreTarget = static_cast<WebCore::HTMLOptionElement*>(WEBKIT_DOM_OBJECT(target)->coreObject);
87 return WebKit::GObjectEventListener::removeEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
88}
89
90static void webkit_dom_html_option_element_dom_event_target_init(WebKitDOMEventTargetIface* iface)
91{
92 iface->dispatch_event = webkit_dom_html_option_element_dispatch_event;
93 iface->add_event_listener = webkit_dom_html_option_element_add_event_listener;
94 iface->remove_event_listener = webkit_dom_html_option_element_remove_event_listener;
95}
96
97G_DEFINE_TYPE_WITH_CODE(WebKitDOMHTMLOptionElement, webkit_dom_html_option_element, WEBKIT_DOM_TYPE_HTML_ELEMENT, G_IMPLEMENT_INTERFACE(WEBKIT_DOM_TYPE_EVENT_TARGET, webkit_dom_html_option_element_dom_event_target_init))
98
99enum {
100 DOM_HTML_OPTION_ELEMENT_PROP_0,
101 DOM_HTML_OPTION_ELEMENT_PROP_DISABLED,
102 DOM_HTML_OPTION_ELEMENT_PROP_FORM,
103 DOM_HTML_OPTION_ELEMENT_PROP_LABEL,
104 DOM_HTML_OPTION_ELEMENT_PROP_DEFAULT_SELECTED,
105 DOM_HTML_OPTION_ELEMENT_PROP_SELECTED,
106 DOM_HTML_OPTION_ELEMENT_PROP_VALUE,
107 DOM_HTML_OPTION_ELEMENT_PROP_TEXT,
108 DOM_HTML_OPTION_ELEMENT_PROP_INDEX,
109};
110
111static void webkit_dom_html_option_element_set_property(GObject* object, guint propertyId, const GValue* value, GParamSpec* pspec)
112{
113 WebKitDOMHTMLOptionElement* self = WEBKIT_DOM_HTML_OPTION_ELEMENT(object);
114
115 switch (propertyId) {
116 case DOM_HTML_OPTION_ELEMENT_PROP_DISABLED:
117 webkit_dom_html_option_element_set_disabled(self, g_value_get_boolean(value));
118 break;
119 case DOM_HTML_OPTION_ELEMENT_PROP_LABEL:
120 webkit_dom_html_option_element_set_label(self, g_value_get_string(value));
121 break;
122 case DOM_HTML_OPTION_ELEMENT_PROP_DEFAULT_SELECTED:
123 webkit_dom_html_option_element_set_default_selected(self, g_value_get_boolean(value));
124 break;
125 case DOM_HTML_OPTION_ELEMENT_PROP_SELECTED:
126 webkit_dom_html_option_element_set_selected(self, g_value_get_boolean(value));
127 break;
128 case DOM_HTML_OPTION_ELEMENT_PROP_VALUE:
129 webkit_dom_html_option_element_set_value(self, g_value_get_string(value));
130 break;
131 default:
132 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
133 break;
134 }
135}
136
137static void webkit_dom_html_option_element_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
138{
139 WebKitDOMHTMLOptionElement* self = WEBKIT_DOM_HTML_OPTION_ELEMENT(object);
140
141 switch (propertyId) {
142 case DOM_HTML_OPTION_ELEMENT_PROP_DISABLED:
143 g_value_set_boolean(value, webkit_dom_html_option_element_get_disabled(self));
144 break;
145 case DOM_HTML_OPTION_ELEMENT_PROP_FORM:
146 g_value_set_object(value, webkit_dom_html_option_element_get_form(self));
147 break;
148 case DOM_HTML_OPTION_ELEMENT_PROP_LABEL:
149 g_value_take_string(value, webkit_dom_html_option_element_get_label(self));
150 break;
151 case DOM_HTML_OPTION_ELEMENT_PROP_DEFAULT_SELECTED:
152 g_value_set_boolean(value, webkit_dom_html_option_element_get_default_selected(self));
153 break;
154 case DOM_HTML_OPTION_ELEMENT_PROP_SELECTED:
155 g_value_set_boolean(value, webkit_dom_html_option_element_get_selected(self));
156 break;
157 case DOM_HTML_OPTION_ELEMENT_PROP_VALUE:
158 g_value_take_string(value, webkit_dom_html_option_element_get_value(self));
159 break;
160 case DOM_HTML_OPTION_ELEMENT_PROP_TEXT:
161 g_value_take_string(value, webkit_dom_html_option_element_get_text(self));
162 break;
163 case DOM_HTML_OPTION_ELEMENT_PROP_INDEX:
164 g_value_set_long(value, webkit_dom_html_option_element_get_index(self));
165 break;
166 default:
167 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
168 break;
169 }
170}
171
172static void webkit_dom_html_option_element_class_init(WebKitDOMHTMLOptionElementClass* requestClass)
173{
174 GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
175 gobjectClass->set_property = webkit_dom_html_option_element_set_property;
176 gobjectClass->get_property = webkit_dom_html_option_element_get_property;
177
178 g_object_class_install_property(
179 gobjectClass,
180 DOM_HTML_OPTION_ELEMENT_PROP_DISABLED,
181 g_param_spec_boolean(
182 "disabled",
183 "HTMLOptionElement:disabled",
184 "read-write gboolean HTMLOptionElement:disabled",
185 FALSE,
186 WEBKIT_PARAM_READWRITE));
187
188 g_object_class_install_property(
189 gobjectClass,
190 DOM_HTML_OPTION_ELEMENT_PROP_FORM,
191 g_param_spec_object(
192 "form",
193 "HTMLOptionElement:form",
194 "read-only WebKitDOMHTMLFormElement* HTMLOptionElement:form",
195 WEBKIT_DOM_TYPE_HTML_FORM_ELEMENT,
196 WEBKIT_PARAM_READABLE));
197
198 g_object_class_install_property(
199 gobjectClass,
200 DOM_HTML_OPTION_ELEMENT_PROP_LABEL,
201 g_param_spec_string(
202 "label",
203 "HTMLOptionElement:label",
204 "read-write gchar* HTMLOptionElement:label",
205 "",
206 WEBKIT_PARAM_READWRITE));
207
208 g_object_class_install_property(
209 gobjectClass,
210 DOM_HTML_OPTION_ELEMENT_PROP_DEFAULT_SELECTED,
211 g_param_spec_boolean(
212 "default-selected",
213 "HTMLOptionElement:default-selected",
214 "read-write gboolean HTMLOptionElement:default-selected",
215 FALSE,
216 WEBKIT_PARAM_READWRITE));
217
218 g_object_class_install_property(
219 gobjectClass,
220 DOM_HTML_OPTION_ELEMENT_PROP_SELECTED,
221 g_param_spec_boolean(
222 "selected",
223 "HTMLOptionElement:selected",
224 "read-write gboolean HTMLOptionElement:selected",
225 FALSE,
226 WEBKIT_PARAM_READWRITE));
227
228 g_object_class_install_property(
229 gobjectClass,
230 DOM_HTML_OPTION_ELEMENT_PROP_VALUE,
231 g_param_spec_string(
232 "value",
233 "HTMLOptionElement:value",
234 "read-write gchar* HTMLOptionElement:value",
235 "",
236 WEBKIT_PARAM_READWRITE));
237
238 g_object_class_install_property(
239 gobjectClass,
240 DOM_HTML_OPTION_ELEMENT_PROP_TEXT,
241 g_param_spec_string(
242 "text",
243 "HTMLOptionElement:text",
244 "read-only gchar* HTMLOptionElement:text",
245 "",
246 WEBKIT_PARAM_READABLE));
247
248 g_object_class_install_property(
249 gobjectClass,
250 DOM_HTML_OPTION_ELEMENT_PROP_INDEX,
251 g_param_spec_long(
252 "index",
253 "HTMLOptionElement:index",
254 "read-only glong HTMLOptionElement:index",
255 G_MINLONG, G_MAXLONG, 0,
256 WEBKIT_PARAM_READABLE));
257
258}
259
260static void webkit_dom_html_option_element_init(WebKitDOMHTMLOptionElement* request)
261{
262 UNUSED_PARAM(request);
263}
264
265gboolean webkit_dom_html_option_element_get_disabled(WebKitDOMHTMLOptionElement* self)
266{
267 WebCore::JSMainThreadNullState state;
268 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_OPTION_ELEMENT(self), FALSE);
269 WebCore::HTMLOptionElement* item = WebKit::core(self);
270 gboolean result = item->hasAttributeWithoutSynchronization(WebCore::HTMLNames::disabledAttr);
271 return result;
272}
273
274void webkit_dom_html_option_element_set_disabled(WebKitDOMHTMLOptionElement* self, gboolean value)
275{
276 WebCore::JSMainThreadNullState state;
277 g_return_if_fail(WEBKIT_DOM_IS_HTML_OPTION_ELEMENT(self));
278 WebCore::HTMLOptionElement* item = WebKit::core(self);
279 item->setBooleanAttribute(WebCore::HTMLNames::disabledAttr, value);
280}
281
282WebKitDOMHTMLFormElement* webkit_dom_html_option_element_get_form(WebKitDOMHTMLOptionElement* self)
283{
284 WebCore::JSMainThreadNullState state;
285 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_OPTION_ELEMENT(self), 0);
286 WebCore::HTMLOptionElement* item = WebKit::core(self);
287 RefPtr<WebCore::HTMLFormElement> gobjectResult = WTF::getPtr(item->form());
288 return WebKit::kit(gobjectResult.get());
289}
290
291gchar* webkit_dom_html_option_element_get_label(WebKitDOMHTMLOptionElement* self)
292{
293 WebCore::JSMainThreadNullState state;
294 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_OPTION_ELEMENT(self), 0);
295 WebCore::HTMLOptionElement* item = WebKit::core(self);
296 gchar* result = convertToUTF8String(item->label());
297 return result;
298}
299
300void webkit_dom_html_option_element_set_label(WebKitDOMHTMLOptionElement* self, const gchar* value)
301{
302 WebCore::JSMainThreadNullState state;
303 g_return_if_fail(WEBKIT_DOM_IS_HTML_OPTION_ELEMENT(self));
304 g_return_if_fail(value);
305 WebCore::HTMLOptionElement* item = WebKit::core(self);
306 WTF::String convertedValue = WTF::String::fromUTF8(value);
307 item->setLabel(convertedValue);
308}
309
310gboolean webkit_dom_html_option_element_get_default_selected(WebKitDOMHTMLOptionElement* self)
311{
312 WebCore::JSMainThreadNullState state;
313 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_OPTION_ELEMENT(self), FALSE);
314 WebCore::HTMLOptionElement* item = WebKit::core(self);
315 gboolean result = item->hasAttributeWithoutSynchronization(WebCore::HTMLNames::selectedAttr);
316 return result;
317}
318
319void webkit_dom_html_option_element_set_default_selected(WebKitDOMHTMLOptionElement* self, gboolean value)
320{
321 WebCore::JSMainThreadNullState state;
322 g_return_if_fail(WEBKIT_DOM_IS_HTML_OPTION_ELEMENT(self));
323 WebCore::HTMLOptionElement* item = WebKit::core(self);
324 item->setBooleanAttribute(WebCore::HTMLNames::selectedAttr, value);
325}
326
327gboolean webkit_dom_html_option_element_get_selected(WebKitDOMHTMLOptionElement* self)
328{
329 WebCore::JSMainThreadNullState state;
330 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_OPTION_ELEMENT(self), FALSE);
331 WebCore::HTMLOptionElement* item = WebKit::core(self);
332 gboolean result = item->selected();
333 return result;
334}
335
336void webkit_dom_html_option_element_set_selected(WebKitDOMHTMLOptionElement* self, gboolean value)
337{
338 WebCore::JSMainThreadNullState state;
339 g_return_if_fail(WEBKIT_DOM_IS_HTML_OPTION_ELEMENT(self));
340 WebCore::HTMLOptionElement* item = WebKit::core(self);
341 item->setSelected(value);
342}
343
344gchar* webkit_dom_html_option_element_get_value(WebKitDOMHTMLOptionElement* self)
345{
346 WebCore::JSMainThreadNullState state;
347 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_OPTION_ELEMENT(self), 0);
348 WebCore::HTMLOptionElement* item = WebKit::core(self);
349 gchar* result = convertToUTF8String(item->value());
350 return result;
351}
352
353void webkit_dom_html_option_element_set_value(WebKitDOMHTMLOptionElement* self, const gchar* value)
354{
355 WebCore::JSMainThreadNullState state;
356 g_return_if_fail(WEBKIT_DOM_IS_HTML_OPTION_ELEMENT(self));
357 g_return_if_fail(value);
358 WebCore::HTMLOptionElement* item = WebKit::core(self);
359 WTF::String convertedValue = WTF::String::fromUTF8(value);
360 item->setValue(convertedValue);
361}
362
363gchar* webkit_dom_html_option_element_get_text(WebKitDOMHTMLOptionElement* self)
364{
365 WebCore::JSMainThreadNullState state;
366 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_OPTION_ELEMENT(self), 0);
367 WebCore::HTMLOptionElement* item = WebKit::core(self);
368 gchar* result = convertToUTF8String(item->text());
369 return result;
370}
371
372glong webkit_dom_html_option_element_get_index(WebKitDOMHTMLOptionElement* self)
373{
374 WebCore::JSMainThreadNullState state;
375 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_OPTION_ELEMENT(self), 0);
376 WebCore::HTMLOptionElement* item = WebKit::core(self);
377 glong result = item->index();
378 return result;
379}
380
381G_GNUC_END_IGNORE_DEPRECATIONS;
382