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 "WebKitDOMHTMLButtonElement.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 "WebKitDOMHTMLButtonElementPrivate.h"
33#include "WebKitDOMHTMLFormElementPrivate.h"
34#include "WebKitDOMNodeListPrivate.h"
35#include "WebKitDOMNodePrivate.h"
36#include "WebKitDOMPrivate.h"
37#include "ConvertToUTF8String.h"
38#include <wtf/GetPtr.h>
39#include <wtf/RefPtr.h>
40
41G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
42
43namespace WebKit {
44
45WebKitDOMHTMLButtonElement* kit(WebCore::HTMLButtonElement* obj)
46{
47 return WEBKIT_DOM_HTML_BUTTON_ELEMENT(kit(static_cast<WebCore::Node*>(obj)));
48}
49
50WebCore::HTMLButtonElement* core(WebKitDOMHTMLButtonElement* request)
51{
52 return request ? static_cast<WebCore::HTMLButtonElement*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
53}
54
55WebKitDOMHTMLButtonElement* wrapHTMLButtonElement(WebCore::HTMLButtonElement* coreObject)
56{
57 ASSERT(coreObject);
58 return WEBKIT_DOM_HTML_BUTTON_ELEMENT(g_object_new(WEBKIT_DOM_TYPE_HTML_BUTTON_ELEMENT, "core-object", coreObject, nullptr));
59}
60
61} // namespace WebKit
62
63static gboolean webkit_dom_html_button_element_dispatch_event(WebKitDOMEventTarget* target, WebKitDOMEvent* event, GError** error)
64{
65 WebCore::Event* coreEvent = WebKit::core(event);
66 if (!coreEvent)
67 return false;
68 WebCore::HTMLButtonElement* coreTarget = static_cast<WebCore::HTMLButtonElement*>(WEBKIT_DOM_OBJECT(target)->coreObject);
69
70 auto result = coreTarget->dispatchEventForBindings(*coreEvent);
71 if (result.hasException()) {
72 auto description = WebCore::DOMException::description(result.releaseException().code());
73 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
74 return false;
75 }
76 return result.releaseReturnValue();
77}
78
79static gboolean webkit_dom_html_button_element_add_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
80{
81 WebCore::HTMLButtonElement* coreTarget = static_cast<WebCore::HTMLButtonElement*>(WEBKIT_DOM_OBJECT(target)->coreObject);
82 return WebKit::GObjectEventListener::addEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
83}
84
85static gboolean webkit_dom_html_button_element_remove_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
86{
87 WebCore::HTMLButtonElement* coreTarget = static_cast<WebCore::HTMLButtonElement*>(WEBKIT_DOM_OBJECT(target)->coreObject);
88 return WebKit::GObjectEventListener::removeEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
89}
90
91static void webkit_dom_html_button_element_dom_event_target_init(WebKitDOMEventTargetIface* iface)
92{
93 iface->dispatch_event = webkit_dom_html_button_element_dispatch_event;
94 iface->add_event_listener = webkit_dom_html_button_element_add_event_listener;
95 iface->remove_event_listener = webkit_dom_html_button_element_remove_event_listener;
96}
97
98G_DEFINE_TYPE_WITH_CODE(WebKitDOMHTMLButtonElement, webkit_dom_html_button_element, WEBKIT_DOM_TYPE_HTML_ELEMENT, G_IMPLEMENT_INTERFACE(WEBKIT_DOM_TYPE_EVENT_TARGET, webkit_dom_html_button_element_dom_event_target_init))
99
100enum {
101 DOM_HTML_BUTTON_ELEMENT_PROP_0,
102 DOM_HTML_BUTTON_ELEMENT_PROP_AUTOFOCUS,
103 DOM_HTML_BUTTON_ELEMENT_PROP_DISABLED,
104 DOM_HTML_BUTTON_ELEMENT_PROP_FORM,
105 DOM_HTML_BUTTON_ELEMENT_PROP_TYPE,
106 DOM_HTML_BUTTON_ELEMENT_PROP_NAME,
107 DOM_HTML_BUTTON_ELEMENT_PROP_VALUE,
108 DOM_HTML_BUTTON_ELEMENT_PROP_WILL_VALIDATE,
109};
110
111static void webkit_dom_html_button_element_set_property(GObject* object, guint propertyId, const GValue* value, GParamSpec* pspec)
112{
113 WebKitDOMHTMLButtonElement* self = WEBKIT_DOM_HTML_BUTTON_ELEMENT(object);
114
115 switch (propertyId) {
116 case DOM_HTML_BUTTON_ELEMENT_PROP_AUTOFOCUS:
117 webkit_dom_html_button_element_set_autofocus(self, g_value_get_boolean(value));
118 break;
119 case DOM_HTML_BUTTON_ELEMENT_PROP_DISABLED:
120 webkit_dom_html_button_element_set_disabled(self, g_value_get_boolean(value));
121 break;
122 case DOM_HTML_BUTTON_ELEMENT_PROP_TYPE:
123 webkit_dom_html_button_element_set_button_type(self, g_value_get_string(value));
124 break;
125 case DOM_HTML_BUTTON_ELEMENT_PROP_NAME:
126 webkit_dom_html_button_element_set_name(self, g_value_get_string(value));
127 break;
128 case DOM_HTML_BUTTON_ELEMENT_PROP_VALUE:
129 webkit_dom_html_button_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_button_element_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
138{
139 WebKitDOMHTMLButtonElement* self = WEBKIT_DOM_HTML_BUTTON_ELEMENT(object);
140
141 switch (propertyId) {
142 case DOM_HTML_BUTTON_ELEMENT_PROP_AUTOFOCUS:
143 g_value_set_boolean(value, webkit_dom_html_button_element_get_autofocus(self));
144 break;
145 case DOM_HTML_BUTTON_ELEMENT_PROP_DISABLED:
146 g_value_set_boolean(value, webkit_dom_html_button_element_get_disabled(self));
147 break;
148 case DOM_HTML_BUTTON_ELEMENT_PROP_FORM:
149 g_value_set_object(value, webkit_dom_html_button_element_get_form(self));
150 break;
151 case DOM_HTML_BUTTON_ELEMENT_PROP_TYPE:
152 g_value_take_string(value, webkit_dom_html_button_element_get_button_type(self));
153 break;
154 case DOM_HTML_BUTTON_ELEMENT_PROP_NAME:
155 g_value_take_string(value, webkit_dom_html_button_element_get_name(self));
156 break;
157 case DOM_HTML_BUTTON_ELEMENT_PROP_VALUE:
158 g_value_take_string(value, webkit_dom_html_button_element_get_value(self));
159 break;
160 case DOM_HTML_BUTTON_ELEMENT_PROP_WILL_VALIDATE:
161 g_value_set_boolean(value, webkit_dom_html_button_element_get_will_validate(self));
162 break;
163 default:
164 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
165 break;
166 }
167}
168
169static void webkit_dom_html_button_element_class_init(WebKitDOMHTMLButtonElementClass* requestClass)
170{
171 GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
172 gobjectClass->set_property = webkit_dom_html_button_element_set_property;
173 gobjectClass->get_property = webkit_dom_html_button_element_get_property;
174
175 g_object_class_install_property(
176 gobjectClass,
177 DOM_HTML_BUTTON_ELEMENT_PROP_AUTOFOCUS,
178 g_param_spec_boolean(
179 "autofocus",
180 "HTMLButtonElement:autofocus",
181 "read-write gboolean HTMLButtonElement:autofocus",
182 FALSE,
183 WEBKIT_PARAM_READWRITE));
184
185 g_object_class_install_property(
186 gobjectClass,
187 DOM_HTML_BUTTON_ELEMENT_PROP_DISABLED,
188 g_param_spec_boolean(
189 "disabled",
190 "HTMLButtonElement:disabled",
191 "read-write gboolean HTMLButtonElement:disabled",
192 FALSE,
193 WEBKIT_PARAM_READWRITE));
194
195 g_object_class_install_property(
196 gobjectClass,
197 DOM_HTML_BUTTON_ELEMENT_PROP_FORM,
198 g_param_spec_object(
199 "form",
200 "HTMLButtonElement:form",
201 "read-only WebKitDOMHTMLFormElement* HTMLButtonElement:form",
202 WEBKIT_DOM_TYPE_HTML_FORM_ELEMENT,
203 WEBKIT_PARAM_READABLE));
204
205 g_object_class_install_property(
206 gobjectClass,
207 DOM_HTML_BUTTON_ELEMENT_PROP_TYPE,
208 g_param_spec_string(
209 "type",
210 "HTMLButtonElement:type",
211 "read-write gchar* HTMLButtonElement:type",
212 "",
213 WEBKIT_PARAM_READWRITE));
214
215 g_object_class_install_property(
216 gobjectClass,
217 DOM_HTML_BUTTON_ELEMENT_PROP_NAME,
218 g_param_spec_string(
219 "name",
220 "HTMLButtonElement:name",
221 "read-write gchar* HTMLButtonElement:name",
222 "",
223 WEBKIT_PARAM_READWRITE));
224
225 g_object_class_install_property(
226 gobjectClass,
227 DOM_HTML_BUTTON_ELEMENT_PROP_VALUE,
228 g_param_spec_string(
229 "value",
230 "HTMLButtonElement:value",
231 "read-write gchar* HTMLButtonElement:value",
232 "",
233 WEBKIT_PARAM_READWRITE));
234
235 g_object_class_install_property(
236 gobjectClass,
237 DOM_HTML_BUTTON_ELEMENT_PROP_WILL_VALIDATE,
238 g_param_spec_boolean(
239 "will-validate",
240 "HTMLButtonElement:will-validate",
241 "read-only gboolean HTMLButtonElement:will-validate",
242 FALSE,
243 WEBKIT_PARAM_READABLE));
244}
245
246static void webkit_dom_html_button_element_init(WebKitDOMHTMLButtonElement* request)
247{
248 UNUSED_PARAM(request);
249}
250
251gboolean webkit_dom_html_button_element_get_autofocus(WebKitDOMHTMLButtonElement* self)
252{
253 WebCore::JSMainThreadNullState state;
254 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_BUTTON_ELEMENT(self), FALSE);
255 WebCore::HTMLButtonElement* item = WebKit::core(self);
256 gboolean result = item->hasAttributeWithoutSynchronization(WebCore::HTMLNames::autofocusAttr);
257 return result;
258}
259
260void webkit_dom_html_button_element_set_autofocus(WebKitDOMHTMLButtonElement* self, gboolean value)
261{
262 WebCore::JSMainThreadNullState state;
263 g_return_if_fail(WEBKIT_DOM_IS_HTML_BUTTON_ELEMENT(self));
264 WebCore::HTMLButtonElement* item = WebKit::core(self);
265 item->setBooleanAttribute(WebCore::HTMLNames::autofocusAttr, value);
266}
267
268gboolean webkit_dom_html_button_element_get_disabled(WebKitDOMHTMLButtonElement* self)
269{
270 WebCore::JSMainThreadNullState state;
271 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_BUTTON_ELEMENT(self), FALSE);
272 WebCore::HTMLButtonElement* item = WebKit::core(self);
273 gboolean result = item->hasAttributeWithoutSynchronization(WebCore::HTMLNames::disabledAttr);
274 return result;
275}
276
277void webkit_dom_html_button_element_set_disabled(WebKitDOMHTMLButtonElement* self, gboolean value)
278{
279 WebCore::JSMainThreadNullState state;
280 g_return_if_fail(WEBKIT_DOM_IS_HTML_BUTTON_ELEMENT(self));
281 WebCore::HTMLButtonElement* item = WebKit::core(self);
282 item->setBooleanAttribute(WebCore::HTMLNames::disabledAttr, value);
283}
284
285WebKitDOMHTMLFormElement* webkit_dom_html_button_element_get_form(WebKitDOMHTMLButtonElement* self)
286{
287 WebCore::JSMainThreadNullState state;
288 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_BUTTON_ELEMENT(self), 0);
289 WebCore::HTMLButtonElement* item = WebKit::core(self);
290 RefPtr<WebCore::HTMLFormElement> gobjectResult = WTF::getPtr(item->form());
291 return WebKit::kit(gobjectResult.get());
292}
293
294gchar* webkit_dom_html_button_element_get_button_type(WebKitDOMHTMLButtonElement* self)
295{
296 WebCore::JSMainThreadNullState state;
297 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_BUTTON_ELEMENT(self), 0);
298 WebCore::HTMLButtonElement* item = WebKit::core(self);
299 gchar* result = convertToUTF8String(item->type());
300 return result;
301}
302
303void webkit_dom_html_button_element_set_button_type(WebKitDOMHTMLButtonElement* self, const gchar* value)
304{
305 WebCore::JSMainThreadNullState state;
306 g_return_if_fail(WEBKIT_DOM_IS_HTML_BUTTON_ELEMENT(self));
307 g_return_if_fail(value);
308 WebCore::HTMLButtonElement* item = WebKit::core(self);
309 WTF::String convertedValue = WTF::String::fromUTF8(value);
310 item->setType(convertedValue);
311}
312
313gchar* webkit_dom_html_button_element_get_name(WebKitDOMHTMLButtonElement* self)
314{
315 WebCore::JSMainThreadNullState state;
316 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_BUTTON_ELEMENT(self), 0);
317 WebCore::HTMLButtonElement* item = WebKit::core(self);
318 gchar* result = convertToUTF8String(item->getNameAttribute());
319 return result;
320}
321
322void webkit_dom_html_button_element_set_name(WebKitDOMHTMLButtonElement* self, const gchar* value)
323{
324 WebCore::JSMainThreadNullState state;
325 g_return_if_fail(WEBKIT_DOM_IS_HTML_BUTTON_ELEMENT(self));
326 g_return_if_fail(value);
327 WebCore::HTMLButtonElement* item = WebKit::core(self);
328 WTF::String convertedValue = WTF::String::fromUTF8(value);
329 item->setAttributeWithoutSynchronization(WebCore::HTMLNames::nameAttr, convertedValue);
330}
331
332gchar* webkit_dom_html_button_element_get_value(WebKitDOMHTMLButtonElement* self)
333{
334 WebCore::JSMainThreadNullState state;
335 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_BUTTON_ELEMENT(self), 0);
336 WebCore::HTMLButtonElement* item = WebKit::core(self);
337 gchar* result = convertToUTF8String(item->attributeWithoutSynchronization(WebCore::HTMLNames::valueAttr));
338 return result;
339}
340
341void webkit_dom_html_button_element_set_value(WebKitDOMHTMLButtonElement* self, const gchar* value)
342{
343 WebCore::JSMainThreadNullState state;
344 g_return_if_fail(WEBKIT_DOM_IS_HTML_BUTTON_ELEMENT(self));
345 g_return_if_fail(value);
346 WebCore::HTMLButtonElement* item = WebKit::core(self);
347 WTF::String convertedValue = WTF::String::fromUTF8(value);
348 item->setAttributeWithoutSynchronization(WebCore::HTMLNames::valueAttr, convertedValue);
349}
350
351gboolean webkit_dom_html_button_element_get_will_validate(WebKitDOMHTMLButtonElement* self)
352{
353 WebCore::JSMainThreadNullState state;
354 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_BUTTON_ELEMENT(self), FALSE);
355 WebCore::HTMLButtonElement* item = WebKit::core(self);
356 gboolean result = item->willValidate();
357 return result;
358}
359G_GNUC_END_IGNORE_DEPRECATIONS;
360