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 "WebKitDOMNamedNodeMap.h"
22
23#include "ConvertToUTF8String.h"
24#include "DOMObjectCache.h"
25#include "WebKitDOMNamedNodeMapPrivate.h"
26#include "WebKitDOMNodePrivate.h"
27#include "WebKitDOMPrivate.h"
28#include <WebCore/Attr.h>
29#include <WebCore/CSSImportRule.h>
30#include <WebCore/DOMException.h>
31#include <WebCore/Document.h>
32#include <WebCore/JSExecState.h>
33#include <wtf/GetPtr.h>
34#include <wtf/RefPtr.h>
35
36#define WEBKIT_DOM_NAMED_NODE_MAP_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, WEBKIT_DOM_TYPE_NAMED_NODE_MAP, WebKitDOMNamedNodeMapPrivate)
37
38typedef struct _WebKitDOMNamedNodeMapPrivate {
39 RefPtr<WebCore::NamedNodeMap> coreObject;
40} WebKitDOMNamedNodeMapPrivate;
41
42G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
43
44namespace WebKit {
45
46WebKitDOMNamedNodeMap* kit(WebCore::NamedNodeMap* obj)
47{
48 if (!obj)
49 return 0;
50
51 if (gpointer ret = DOMObjectCache::get(obj))
52 return WEBKIT_DOM_NAMED_NODE_MAP(ret);
53
54 return wrapNamedNodeMap(obj);
55}
56
57WebCore::NamedNodeMap* core(WebKitDOMNamedNodeMap* request)
58{
59 return request ? static_cast<WebCore::NamedNodeMap*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
60}
61
62WebKitDOMNamedNodeMap* wrapNamedNodeMap(WebCore::NamedNodeMap* coreObject)
63{
64 ASSERT(coreObject);
65 return WEBKIT_DOM_NAMED_NODE_MAP(g_object_new(WEBKIT_DOM_TYPE_NAMED_NODE_MAP, "core-object", coreObject, nullptr));
66}
67
68} // namespace WebKit
69
70G_DEFINE_TYPE(WebKitDOMNamedNodeMap, webkit_dom_named_node_map, WEBKIT_DOM_TYPE_OBJECT)
71
72enum {
73 DOM_NAMED_NODE_MAP_PROP_0,
74 DOM_NAMED_NODE_MAP_PROP_LENGTH,
75};
76
77static void webkit_dom_named_node_map_finalize(GObject* object)
78{
79 WebKitDOMNamedNodeMapPrivate* priv = WEBKIT_DOM_NAMED_NODE_MAP_GET_PRIVATE(object);
80
81 WebKit::DOMObjectCache::forget(priv->coreObject.get());
82
83 priv->~WebKitDOMNamedNodeMapPrivate();
84 G_OBJECT_CLASS(webkit_dom_named_node_map_parent_class)->finalize(object);
85}
86
87static void webkit_dom_named_node_map_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
88{
89 WebKitDOMNamedNodeMap* self = WEBKIT_DOM_NAMED_NODE_MAP(object);
90
91 switch (propertyId) {
92 case DOM_NAMED_NODE_MAP_PROP_LENGTH:
93 g_value_set_ulong(value, webkit_dom_named_node_map_get_length(self));
94 break;
95 default:
96 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
97 break;
98 }
99}
100
101static GObject* webkit_dom_named_node_map_constructor(GType type, guint constructPropertiesCount, GObjectConstructParam* constructProperties)
102{
103 GObject* object = G_OBJECT_CLASS(webkit_dom_named_node_map_parent_class)->constructor(type, constructPropertiesCount, constructProperties);
104
105 WebKitDOMNamedNodeMapPrivate* priv = WEBKIT_DOM_NAMED_NODE_MAP_GET_PRIVATE(object);
106 priv->coreObject = static_cast<WebCore::NamedNodeMap*>(WEBKIT_DOM_OBJECT(object)->coreObject);
107 WebKit::DOMObjectCache::put(priv->coreObject.get(), object);
108
109 return object;
110}
111
112static void webkit_dom_named_node_map_class_init(WebKitDOMNamedNodeMapClass* requestClass)
113{
114 GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
115 g_type_class_add_private(gobjectClass, sizeof(WebKitDOMNamedNodeMapPrivate));
116 gobjectClass->constructor = webkit_dom_named_node_map_constructor;
117 gobjectClass->finalize = webkit_dom_named_node_map_finalize;
118 gobjectClass->get_property = webkit_dom_named_node_map_get_property;
119
120 g_object_class_install_property(
121 gobjectClass,
122 DOM_NAMED_NODE_MAP_PROP_LENGTH,
123 g_param_spec_ulong(
124 "length",
125 "NamedNodeMap:length",
126 "read-only gulong NamedNodeMap:length",
127 0, G_MAXULONG, 0,
128 WEBKIT_PARAM_READABLE));
129
130}
131
132static void webkit_dom_named_node_map_init(WebKitDOMNamedNodeMap* request)
133{
134 WebKitDOMNamedNodeMapPrivate* priv = WEBKIT_DOM_NAMED_NODE_MAP_GET_PRIVATE(request);
135 new (priv) WebKitDOMNamedNodeMapPrivate();
136}
137
138WebKitDOMNode* webkit_dom_named_node_map_get_named_item(WebKitDOMNamedNodeMap* self, const gchar* name)
139{
140 WebCore::JSMainThreadNullState state;
141 g_return_val_if_fail(WEBKIT_DOM_IS_NAMED_NODE_MAP(self), 0);
142 g_return_val_if_fail(name, 0);
143 WebCore::NamedNodeMap* item = WebKit::core(self);
144 WTF::String convertedName = WTF::String::fromUTF8(name);
145 RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(item->getNamedItem(convertedName));
146 return WebKit::kit(gobjectResult.get());
147}
148
149WebKitDOMNode* webkit_dom_named_node_map_set_named_item(WebKitDOMNamedNodeMap* self, WebKitDOMNode* node, GError** error)
150{
151 WebCore::JSMainThreadNullState state;
152 g_return_val_if_fail(WEBKIT_DOM_IS_NAMED_NODE_MAP(self), 0);
153 g_return_val_if_fail(WEBKIT_DOM_IS_NODE(node), 0);
154 g_return_val_if_fail(!error || !*error, 0);
155 WebCore::NamedNodeMap* item = WebKit::core(self);
156 WebCore::Node* convertedNode = WebKit::core(node);
157 if (!is<WebCore::Attr>(*convertedNode)) {
158 auto description = WebCore::DOMException::description(WebCore::TypeError);
159 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
160 return nullptr;
161 }
162 auto result = item->setNamedItem(downcast<WebCore::Attr>(*convertedNode));
163 if (result.hasException()) {
164 auto description = WebCore::DOMException::description(result.releaseException().code());
165 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
166 return nullptr;
167 }
168 return WebKit::kit(result.releaseReturnValue().get());
169}
170
171WebKitDOMNode* webkit_dom_named_node_map_remove_named_item(WebKitDOMNamedNodeMap* self, const gchar* name, GError** error)
172{
173 WebCore::JSMainThreadNullState state;
174 g_return_val_if_fail(WEBKIT_DOM_IS_NAMED_NODE_MAP(self), 0);
175 g_return_val_if_fail(name, 0);
176 g_return_val_if_fail(!error || !*error, 0);
177 WebCore::NamedNodeMap* item = WebKit::core(self);
178 WTF::String convertedName = WTF::String::fromUTF8(name);
179 auto result = item->removeNamedItem(convertedName);
180 if (result.hasException()) {
181 auto description = WebCore::DOMException::description(result.releaseException().code());
182 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
183 return nullptr;
184 }
185 return WebKit::kit(result.releaseReturnValue().ptr());
186}
187
188WebKitDOMNode* webkit_dom_named_node_map_item(WebKitDOMNamedNodeMap* self, gulong index)
189{
190 WebCore::JSMainThreadNullState state;
191 g_return_val_if_fail(WEBKIT_DOM_IS_NAMED_NODE_MAP(self), 0);
192 WebCore::NamedNodeMap* item = WebKit::core(self);
193 RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(item->item(index));
194 return WebKit::kit(gobjectResult.get());
195}
196
197WebKitDOMNode* webkit_dom_named_node_map_get_named_item_ns(WebKitDOMNamedNodeMap* self, const gchar* namespaceURI, const gchar* localName)
198{
199 WebCore::JSMainThreadNullState state;
200 g_return_val_if_fail(WEBKIT_DOM_IS_NAMED_NODE_MAP(self), 0);
201 g_return_val_if_fail(namespaceURI, 0);
202 g_return_val_if_fail(localName, 0);
203 WebCore::NamedNodeMap* item = WebKit::core(self);
204 WTF::String convertedNamespaceURI = WTF::String::fromUTF8(namespaceURI);
205 WTF::String convertedLocalName = WTF::String::fromUTF8(localName);
206 RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(item->getNamedItemNS(convertedNamespaceURI, convertedLocalName));
207 return WebKit::kit(gobjectResult.get());
208}
209
210WebKitDOMNode* webkit_dom_named_node_map_set_named_item_ns(WebKitDOMNamedNodeMap* self, WebKitDOMNode* node, GError** error)
211{
212 return webkit_dom_named_node_map_set_named_item(self, node, error);
213}
214
215WebKitDOMNode* webkit_dom_named_node_map_remove_named_item_ns(WebKitDOMNamedNodeMap* self, const gchar* namespaceURI, const gchar* localName, GError** error)
216{
217 WebCore::JSMainThreadNullState state;
218 g_return_val_if_fail(WEBKIT_DOM_IS_NAMED_NODE_MAP(self), 0);
219 g_return_val_if_fail(namespaceURI, 0);
220 g_return_val_if_fail(localName, 0);
221 g_return_val_if_fail(!error || !*error, 0);
222 WebCore::NamedNodeMap* item = WebKit::core(self);
223 WTF::String convertedNamespaceURI = WTF::String::fromUTF8(namespaceURI);
224 WTF::String convertedLocalName = WTF::String::fromUTF8(localName);
225 auto result = item->removeNamedItemNS(convertedNamespaceURI, convertedLocalName);
226 if (result.hasException()) {
227 auto description = WebCore::DOMException::description(result.releaseException().code());
228 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
229 return nullptr;
230 }
231 return WebKit::kit(result.releaseReturnValue().ptr());
232}
233
234gulong webkit_dom_named_node_map_get_length(WebKitDOMNamedNodeMap* self)
235{
236 WebCore::JSMainThreadNullState state;
237 g_return_val_if_fail(WEBKIT_DOM_IS_NAMED_NODE_MAP(self), 0);
238 WebCore::NamedNodeMap* item = WebKit::core(self);
239 gulong result = item->length();
240 return result;
241}
242
243G_GNUC_END_IGNORE_DEPRECATIONS;
244