b08ab0eb5c6cd55b62e82b23157ec98b566c0de9
[WebKit-https.git] / Source / WebKit / WebProcess / InjectedBundle / API / gtk / DOM / WebKitDOMHTMLFormElement.cpp
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 "WebKitDOMHTMLFormElement.h"
22
23 #include <WebCore/CSSImportRule.h>
24 #include "DOMObjectCache.h"
25 #include <WebCore/Document.h>
26 #include <WebCore/ExceptionCode.h>
27 #include <WebCore/ExceptionCodeDescription.h>
28 #include "GObjectEventListener.h"
29 #include <WebCore/HTMLNames.h>
30 #include <WebCore/JSMainThreadExecState.h>
31 #include "WebKitDOMEventPrivate.h"
32 #include "WebKitDOMEventTarget.h"
33 #include "WebKitDOMHTMLCollectionPrivate.h"
34 #include "WebKitDOMHTMLFormElementPrivate.h"
35 #include "WebKitDOMNodePrivate.h"
36 #include "WebKitDOMPrivate.h"
37 #include "ConvertToUTF8String.h"
38 #include <wtf/GetPtr.h>
39 #include <wtf/RefPtr.h>
40
41 namespace WebKit {
42
43 WebKitDOMHTMLFormElement* kit(WebCore::HTMLFormElement* obj)
44 {
45     return WEBKIT_DOM_HTML_FORM_ELEMENT(kit(static_cast<WebCore::Node*>(obj)));
46 }
47
48 WebCore::HTMLFormElement* core(WebKitDOMHTMLFormElement* request)
49 {
50     return request ? static_cast<WebCore::HTMLFormElement*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
51 }
52
53 WebKitDOMHTMLFormElement* wrapHTMLFormElement(WebCore::HTMLFormElement* coreObject)
54 {
55     ASSERT(coreObject);
56     return WEBKIT_DOM_HTML_FORM_ELEMENT(g_object_new(WEBKIT_DOM_TYPE_HTML_FORM_ELEMENT, "core-object", coreObject, nullptr));
57 }
58
59 } // namespace WebKit
60
61 static gboolean webkit_dom_html_form_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::HTMLFormElement* coreTarget = static_cast<WebCore::HTMLFormElement*>(WEBKIT_DOM_OBJECT(target)->coreObject);
67
68     auto result = coreTarget->dispatchEventForBindings(*coreEvent);
69     if (result.hasException()) {
70         WebCore::ExceptionCodeDescription description(result.releaseException().code());
71         g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.code, description.name);
72         return false;
73     }
74     return result.releaseReturnValue();
75 }
76
77 static gboolean webkit_dom_html_form_element_add_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
78 {
79     WebCore::HTMLFormElement* coreTarget = static_cast<WebCore::HTMLFormElement*>(WEBKIT_DOM_OBJECT(target)->coreObject);
80     return WebKit::GObjectEventListener::addEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
81 }
82
83 static gboolean webkit_dom_html_form_element_remove_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
84 {
85     WebCore::HTMLFormElement* coreTarget = static_cast<WebCore::HTMLFormElement*>(WEBKIT_DOM_OBJECT(target)->coreObject);
86     return WebKit::GObjectEventListener::removeEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
87 }
88
89 static void webkit_dom_event_target_init(WebKitDOMEventTargetIface* iface)
90 {
91     iface->dispatch_event = webkit_dom_html_form_element_dispatch_event;
92     iface->add_event_listener = webkit_dom_html_form_element_add_event_listener;
93     iface->remove_event_listener = webkit_dom_html_form_element_remove_event_listener;
94 }
95
96 G_DEFINE_TYPE_WITH_CODE(WebKitDOMHTMLFormElement, webkit_dom_html_form_element, WEBKIT_DOM_TYPE_HTML_ELEMENT, G_IMPLEMENT_INTERFACE(WEBKIT_DOM_TYPE_EVENT_TARGET, webkit_dom_event_target_init))
97
98 enum {
99     PROP_0,
100     PROP_ACCEPT_CHARSET,
101     PROP_ACTION,
102     PROP_ENCTYPE,
103     PROP_ENCODING,
104     PROP_METHOD,
105     PROP_NAME,
106     PROP_TARGET,
107     PROP_ELEMENTS,
108     PROP_LENGTH,
109 };
110
111 static void webkit_dom_html_form_element_set_property(GObject* object, guint propertyId, const GValue* value, GParamSpec* pspec)
112 {
113     WebKitDOMHTMLFormElement* self = WEBKIT_DOM_HTML_FORM_ELEMENT(object);
114
115     switch (propertyId) {
116     case PROP_ACCEPT_CHARSET:
117         webkit_dom_html_form_element_set_accept_charset(self, g_value_get_string(value));
118         break;
119     case PROP_ACTION:
120         webkit_dom_html_form_element_set_action(self, g_value_get_string(value));
121         break;
122     case PROP_ENCTYPE:
123         webkit_dom_html_form_element_set_enctype(self, g_value_get_string(value));
124         break;
125     case PROP_ENCODING:
126         webkit_dom_html_form_element_set_encoding(self, g_value_get_string(value));
127         break;
128     case PROP_METHOD:
129         webkit_dom_html_form_element_set_method(self, g_value_get_string(value));
130         break;
131     case PROP_NAME:
132         webkit_dom_html_form_element_set_name(self, g_value_get_string(value));
133         break;
134     case PROP_TARGET:
135         webkit_dom_html_form_element_set_target(self, g_value_get_string(value));
136         break;
137     default:
138         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
139         break;
140     }
141 }
142
143 static void webkit_dom_html_form_element_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
144 {
145     WebKitDOMHTMLFormElement* self = WEBKIT_DOM_HTML_FORM_ELEMENT(object);
146
147     switch (propertyId) {
148     case PROP_ACCEPT_CHARSET:
149         g_value_take_string(value, webkit_dom_html_form_element_get_accept_charset(self));
150         break;
151     case PROP_ACTION:
152         g_value_take_string(value, webkit_dom_html_form_element_get_action(self));
153         break;
154     case PROP_ENCTYPE:
155         g_value_take_string(value, webkit_dom_html_form_element_get_enctype(self));
156         break;
157     case PROP_ENCODING:
158         g_value_take_string(value, webkit_dom_html_form_element_get_encoding(self));
159         break;
160     case PROP_METHOD:
161         g_value_take_string(value, webkit_dom_html_form_element_get_method(self));
162         break;
163     case PROP_NAME:
164         g_value_take_string(value, webkit_dom_html_form_element_get_name(self));
165         break;
166     case PROP_TARGET:
167         g_value_take_string(value, webkit_dom_html_form_element_get_target(self));
168         break;
169     case PROP_ELEMENTS:
170         g_value_set_object(value, webkit_dom_html_form_element_get_elements(self));
171         break;
172     case PROP_LENGTH:
173         g_value_set_long(value, webkit_dom_html_form_element_get_length(self));
174         break;
175     default:
176         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
177         break;
178     }
179 }
180
181 static void webkit_dom_html_form_element_class_init(WebKitDOMHTMLFormElementClass* requestClass)
182 {
183     GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
184     gobjectClass->set_property = webkit_dom_html_form_element_set_property;
185     gobjectClass->get_property = webkit_dom_html_form_element_get_property;
186
187     g_object_class_install_property(
188         gobjectClass,
189         PROP_ACCEPT_CHARSET,
190         g_param_spec_string(
191             "accept-charset",
192             "HTMLFormElement:accept-charset",
193             "read-write gchar* HTMLFormElement:accept-charset",
194             "",
195             WEBKIT_PARAM_READWRITE));
196
197     g_object_class_install_property(
198         gobjectClass,
199         PROP_ACTION,
200         g_param_spec_string(
201             "action",
202             "HTMLFormElement:action",
203             "read-write gchar* HTMLFormElement:action",
204             "",
205             WEBKIT_PARAM_READWRITE));
206
207     g_object_class_install_property(
208         gobjectClass,
209         PROP_ENCTYPE,
210         g_param_spec_string(
211             "enctype",
212             "HTMLFormElement:enctype",
213             "read-write gchar* HTMLFormElement:enctype",
214             "",
215             WEBKIT_PARAM_READWRITE));
216
217     g_object_class_install_property(
218         gobjectClass,
219         PROP_ENCODING,
220         g_param_spec_string(
221             "encoding",
222             "HTMLFormElement:encoding",
223             "read-write gchar* HTMLFormElement:encoding",
224             "",
225             WEBKIT_PARAM_READWRITE));
226
227     g_object_class_install_property(
228         gobjectClass,
229         PROP_METHOD,
230         g_param_spec_string(
231             "method",
232             "HTMLFormElement:method",
233             "read-write gchar* HTMLFormElement:method",
234             "",
235             WEBKIT_PARAM_READWRITE));
236
237     g_object_class_install_property(
238         gobjectClass,
239         PROP_NAME,
240         g_param_spec_string(
241             "name",
242             "HTMLFormElement:name",
243             "read-write gchar* HTMLFormElement:name",
244             "",
245             WEBKIT_PARAM_READWRITE));
246
247     g_object_class_install_property(
248         gobjectClass,
249         PROP_TARGET,
250         g_param_spec_string(
251             "target",
252             "HTMLFormElement:target",
253             "read-write gchar* HTMLFormElement:target",
254             "",
255             WEBKIT_PARAM_READWRITE));
256
257     g_object_class_install_property(
258         gobjectClass,
259         PROP_ELEMENTS,
260         g_param_spec_object(
261             "elements",
262             "HTMLFormElement:elements",
263             "read-only WebKitDOMHTMLCollection* HTMLFormElement:elements",
264             WEBKIT_DOM_TYPE_HTML_COLLECTION,
265             WEBKIT_PARAM_READABLE));
266
267     g_object_class_install_property(
268         gobjectClass,
269         PROP_LENGTH,
270         g_param_spec_long(
271             "length",
272             "HTMLFormElement:length",
273             "read-only glong HTMLFormElement:length",
274             G_MINLONG, G_MAXLONG, 0,
275             WEBKIT_PARAM_READABLE));
276 }
277
278 static void webkit_dom_html_form_element_init(WebKitDOMHTMLFormElement* request)
279 {
280     UNUSED_PARAM(request);
281 }
282
283 void webkit_dom_html_form_element_submit(WebKitDOMHTMLFormElement* self)
284 {
285     WebCore::JSMainThreadNullState state;
286     g_return_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self));
287     WebCore::HTMLFormElement* item = WebKit::core(self);
288     item->submit();
289 }
290
291 void webkit_dom_html_form_element_reset(WebKitDOMHTMLFormElement* self)
292 {
293     WebCore::JSMainThreadNullState state;
294     g_return_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self));
295     WebCore::HTMLFormElement* item = WebKit::core(self);
296     item->reset();
297 }
298
299 gchar* webkit_dom_html_form_element_get_accept_charset(WebKitDOMHTMLFormElement* self)
300 {
301     WebCore::JSMainThreadNullState state;
302     g_return_val_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self), 0);
303     WebCore::HTMLFormElement* item = WebKit::core(self);
304     gchar* result = convertToUTF8String(item->attributeWithoutSynchronization(WebCore::HTMLNames::accept_charsetAttr));
305     return result;
306 }
307
308 void webkit_dom_html_form_element_set_accept_charset(WebKitDOMHTMLFormElement* self, const gchar* value)
309 {
310     WebCore::JSMainThreadNullState state;
311     g_return_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self));
312     g_return_if_fail(value);
313     WebCore::HTMLFormElement* item = WebKit::core(self);
314     WTF::String convertedValue = WTF::String::fromUTF8(value);
315     item->setAttributeWithoutSynchronization(WebCore::HTMLNames::accept_charsetAttr, convertedValue);
316 }
317
318 gchar* webkit_dom_html_form_element_get_action(WebKitDOMHTMLFormElement* self)
319 {
320     WebCore::JSMainThreadNullState state;
321     g_return_val_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self), 0);
322     WebCore::HTMLFormElement* item = WebKit::core(self);
323     gchar* result = convertToUTF8String(item->getURLAttribute(WebCore::HTMLNames::actionAttr));
324     return result;
325 }
326
327 void webkit_dom_html_form_element_set_action(WebKitDOMHTMLFormElement* self, const gchar* value)
328 {
329     WebCore::JSMainThreadNullState state;
330     g_return_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self));
331     g_return_if_fail(value);
332     WebCore::HTMLFormElement* item = WebKit::core(self);
333     WTF::String convertedValue = WTF::String::fromUTF8(value);
334     item->setAttributeWithoutSynchronization(WebCore::HTMLNames::actionAttr, convertedValue);
335 }
336
337 gchar* webkit_dom_html_form_element_get_enctype(WebKitDOMHTMLFormElement* self)
338 {
339     WebCore::JSMainThreadNullState state;
340     g_return_val_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self), 0);
341     WebCore::HTMLFormElement* item = WebKit::core(self);
342     gchar* result = convertToUTF8String(item->enctype());
343     return result;
344 }
345
346 void webkit_dom_html_form_element_set_enctype(WebKitDOMHTMLFormElement* self, const gchar* value)
347 {
348     WebCore::JSMainThreadNullState state;
349     g_return_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self));
350     g_return_if_fail(value);
351     WebCore::HTMLFormElement* item = WebKit::core(self);
352     WTF::String convertedValue = WTF::String::fromUTF8(value);
353     item->setEnctype(convertedValue);
354 }
355
356 gchar* webkit_dom_html_form_element_get_encoding(WebKitDOMHTMLFormElement* self)
357 {
358     WebCore::JSMainThreadNullState state;
359     g_return_val_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self), 0);
360     WebCore::HTMLFormElement* item = WebKit::core(self);
361     gchar* result = convertToUTF8String(item->enctype());
362     return result;
363 }
364
365 void webkit_dom_html_form_element_set_encoding(WebKitDOMHTMLFormElement* self, const gchar* value)
366 {
367     WebCore::JSMainThreadNullState state;
368     g_return_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self));
369     g_return_if_fail(value);
370     WebCore::HTMLFormElement* item = WebKit::core(self);
371     WTF::String convertedValue = WTF::String::fromUTF8(value);
372     item->setEnctype(convertedValue);
373 }
374
375 gchar* webkit_dom_html_form_element_get_method(WebKitDOMHTMLFormElement* self)
376 {
377     WebCore::JSMainThreadNullState state;
378     g_return_val_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self), 0);
379     WebCore::HTMLFormElement* item = WebKit::core(self);
380     gchar* result = convertToUTF8String(item->method());
381     return result;
382 }
383
384 void webkit_dom_html_form_element_set_method(WebKitDOMHTMLFormElement* self, const gchar* value)
385 {
386     WebCore::JSMainThreadNullState state;
387     g_return_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self));
388     g_return_if_fail(value);
389     WebCore::HTMLFormElement* item = WebKit::core(self);
390     WTF::String convertedValue = WTF::String::fromUTF8(value);
391     item->setMethod(convertedValue);
392 }
393
394 gchar* webkit_dom_html_form_element_get_name(WebKitDOMHTMLFormElement* self)
395 {
396     WebCore::JSMainThreadNullState state;
397     g_return_val_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self), 0);
398     WebCore::HTMLFormElement* item = WebKit::core(self);
399     gchar* result = convertToUTF8String(item->getNameAttribute());
400     return result;
401 }
402
403 void webkit_dom_html_form_element_set_name(WebKitDOMHTMLFormElement* self, const gchar* value)
404 {
405     WebCore::JSMainThreadNullState state;
406     g_return_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self));
407     g_return_if_fail(value);
408     WebCore::HTMLFormElement* item = WebKit::core(self);
409     WTF::String convertedValue = WTF::String::fromUTF8(value);
410     item->setAttributeWithoutSynchronization(WebCore::HTMLNames::nameAttr, convertedValue);
411 }
412
413 gchar* webkit_dom_html_form_element_get_target(WebKitDOMHTMLFormElement* self)
414 {
415     WebCore::JSMainThreadNullState state;
416     g_return_val_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self), 0);
417     WebCore::HTMLFormElement* item = WebKit::core(self);
418     gchar* result = convertToUTF8String(item->attributeWithoutSynchronization(WebCore::HTMLNames::targetAttr));
419     return result;
420 }
421
422 void webkit_dom_html_form_element_set_target(WebKitDOMHTMLFormElement* self, const gchar* value)
423 {
424     WebCore::JSMainThreadNullState state;
425     g_return_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self));
426     g_return_if_fail(value);
427     WebCore::HTMLFormElement* item = WebKit::core(self);
428     WTF::String convertedValue = WTF::String::fromUTF8(value);
429     item->setAttributeWithoutSynchronization(WebCore::HTMLNames::targetAttr, convertedValue);
430 }
431
432 WebKitDOMHTMLCollection* webkit_dom_html_form_element_get_elements(WebKitDOMHTMLFormElement* self)
433 {
434     WebCore::JSMainThreadNullState state;
435     g_return_val_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self), 0);
436     WebCore::HTMLFormElement* item = WebKit::core(self);
437     RefPtr<WebCore::HTMLCollection> gobjectResult = WTF::getPtr(item->elementsForNativeBindings());
438     return WebKit::kit(gobjectResult.get());
439 }
440
441 glong webkit_dom_html_form_element_get_length(WebKitDOMHTMLFormElement* self)
442 {
443     WebCore::JSMainThreadNullState state;
444     g_return_val_if_fail(WEBKIT_DOM_IS_HTML_FORM_ELEMENT(self), 0);
445     WebCore::HTMLFormElement* item = WebKit::core(self);
446     glong result = item->length();
447     return result;
448 }