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 "WebKitDOMHTMLMapElement.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 "WebKitDOMHTMLCollectionPrivate.h"
33#include "WebKitDOMHTMLMapElementPrivate.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
44WebKitDOMHTMLMapElement* kit(WebCore::HTMLMapElement* obj)
45{
46 return WEBKIT_DOM_HTML_MAP_ELEMENT(kit(static_cast<WebCore::Node*>(obj)));
47}
48
49WebCore::HTMLMapElement* core(WebKitDOMHTMLMapElement* request)
50{
51 return request ? static_cast<WebCore::HTMLMapElement*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
52}
53
54WebKitDOMHTMLMapElement* wrapHTMLMapElement(WebCore::HTMLMapElement* coreObject)
55{
56 ASSERT(coreObject);
57 return WEBKIT_DOM_HTML_MAP_ELEMENT(g_object_new(WEBKIT_DOM_TYPE_HTML_MAP_ELEMENT, "core-object", coreObject, nullptr));
58}
59
60} // namespace WebKit
61
62static gboolean webkit_dom_html_map_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::HTMLMapElement* coreTarget = static_cast<WebCore::HTMLMapElement*>(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_map_element_add_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
79{
80 WebCore::HTMLMapElement* coreTarget = static_cast<WebCore::HTMLMapElement*>(WEBKIT_DOM_OBJECT(target)->coreObject);
81 return WebKit::GObjectEventListener::addEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
82}
83
84static gboolean webkit_dom_html_map_element_remove_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)
85{
86 WebCore::HTMLMapElement* coreTarget = static_cast<WebCore::HTMLMapElement*>(WEBKIT_DOM_OBJECT(target)->coreObject);
87 return WebKit::GObjectEventListener::removeEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);
88}
89
90static void webkit_dom_html_map_element_dom_event_target_init(WebKitDOMEventTargetIface* iface)
91{
92 iface->dispatch_event = webkit_dom_html_map_element_dispatch_event;
93 iface->add_event_listener = webkit_dom_html_map_element_add_event_listener;
94 iface->remove_event_listener = webkit_dom_html_map_element_remove_event_listener;
95}
96
97G_DEFINE_TYPE_WITH_CODE(WebKitDOMHTMLMapElement, webkit_dom_html_map_element, WEBKIT_DOM_TYPE_HTML_ELEMENT, G_IMPLEMENT_INTERFACE(WEBKIT_DOM_TYPE_EVENT_TARGET, webkit_dom_html_map_element_dom_event_target_init))
98
99enum {
100 DOM_HTML_MAP_ELEMENT_PROP_0,
101 DOM_HTML_MAP_ELEMENT_PROP_AREAS,
102 DOM_HTML_MAP_ELEMENT_PROP_NAME,
103};
104
105static void webkit_dom_html_map_element_set_property(GObject* object, guint propertyId, const GValue* value, GParamSpec* pspec)
106{
107 WebKitDOMHTMLMapElement* self = WEBKIT_DOM_HTML_MAP_ELEMENT(object);
108
109 switch (propertyId) {
110 case DOM_HTML_MAP_ELEMENT_PROP_NAME:
111 webkit_dom_html_map_element_set_name(self, g_value_get_string(value));
112 break;
113 default:
114 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
115 break;
116 }
117}
118
119static void webkit_dom_html_map_element_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
120{
121 WebKitDOMHTMLMapElement* self = WEBKIT_DOM_HTML_MAP_ELEMENT(object);
122
123 switch (propertyId) {
124 case DOM_HTML_MAP_ELEMENT_PROP_AREAS:
125 g_value_set_object(value, webkit_dom_html_map_element_get_areas(self));
126 break;
127 case DOM_HTML_MAP_ELEMENT_PROP_NAME:
128 g_value_take_string(value, webkit_dom_html_map_element_get_name(self));
129 break;
130 default:
131 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
132 break;
133 }
134}
135
136static void webkit_dom_html_map_element_class_init(WebKitDOMHTMLMapElementClass* requestClass)
137{
138 GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
139 gobjectClass->set_property = webkit_dom_html_map_element_set_property;
140 gobjectClass->get_property = webkit_dom_html_map_element_get_property;
141
142 g_object_class_install_property(
143 gobjectClass,
144 DOM_HTML_MAP_ELEMENT_PROP_AREAS,
145 g_param_spec_object(
146 "areas",
147 "HTMLMapElement:areas",
148 "read-only WebKitDOMHTMLCollection* HTMLMapElement:areas",
149 WEBKIT_DOM_TYPE_HTML_COLLECTION,
150 WEBKIT_PARAM_READABLE));
151
152 g_object_class_install_property(
153 gobjectClass,
154 DOM_HTML_MAP_ELEMENT_PROP_NAME,
155 g_param_spec_string(
156 "name",
157 "HTMLMapElement:name",
158 "read-write gchar* HTMLMapElement:name",
159 "",
160 WEBKIT_PARAM_READWRITE));
161
162}
163
164static void webkit_dom_html_map_element_init(WebKitDOMHTMLMapElement* request)
165{
166 UNUSED_PARAM(request);
167}
168
169WebKitDOMHTMLCollection* webkit_dom_html_map_element_get_areas(WebKitDOMHTMLMapElement* self)
170{
171 WebCore::JSMainThreadNullState state;
172 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_MAP_ELEMENT(self), 0);
173 WebCore::HTMLMapElement* item = WebKit::core(self);
174 RefPtr<WebCore::HTMLCollection> gobjectResult = WTF::getPtr(item->areas());
175 return WebKit::kit(gobjectResult.get());
176}
177
178gchar* webkit_dom_html_map_element_get_name(WebKitDOMHTMLMapElement* self)
179{
180 WebCore::JSMainThreadNullState state;
181 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_MAP_ELEMENT(self), 0);
182 WebCore::HTMLMapElement* item = WebKit::core(self);
183 gchar* result = convertToUTF8String(item->getNameAttribute());
184 return result;
185}
186
187void webkit_dom_html_map_element_set_name(WebKitDOMHTMLMapElement* self, const gchar* value)
188{
189 WebCore::JSMainThreadNullState state;
190 g_return_if_fail(WEBKIT_DOM_IS_HTML_MAP_ELEMENT(self));
191 g_return_if_fail(value);
192 WebCore::HTMLMapElement* item = WebKit::core(self);
193 WTF::String convertedValue = WTF::String::fromUTF8(value);
194 item->setAttributeWithoutSynchronization(WebCore::HTMLNames::nameAttr, convertedValue);
195}
196
197G_GNUC_END_IGNORE_DEPRECATIONS;
198