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 "WebKitDOMNodeList.h"
22
23#include <WebCore/CSSImportRule.h>
24#include "DOMObjectCache.h"
25#include <WebCore/Document.h>
26#include <WebCore/ExceptionCode.h>
27#include <WebCore/JSExecState.h>
28#include "WebKitDOMNodeListPrivate.h"
29#include "WebKitDOMNodePrivate.h"
30#include "WebKitDOMPrivate.h"
31#include "ConvertToUTF8String.h"
32#include <wtf/GetPtr.h>
33#include <wtf/RefPtr.h>
34
35#define WEBKIT_DOM_NODE_LIST_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, WEBKIT_DOM_TYPE_NODE_LIST, WebKitDOMNodeListPrivate)
36
37typedef struct _WebKitDOMNodeListPrivate {
38 RefPtr<WebCore::NodeList> coreObject;
39} WebKitDOMNodeListPrivate;
40
41G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
42
43namespace WebKit {
44
45WebKitDOMNodeList* kit(WebCore::NodeList* obj)
46{
47 if (!obj)
48 return 0;
49
50 if (gpointer ret = DOMObjectCache::get(obj))
51 return WEBKIT_DOM_NODE_LIST(ret);
52
53 return wrapNodeList(obj);
54}
55
56WebCore::NodeList* core(WebKitDOMNodeList* request)
57{
58 return request ? static_cast<WebCore::NodeList*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
59}
60
61WebKitDOMNodeList* wrapNodeList(WebCore::NodeList* coreObject)
62{
63 ASSERT(coreObject);
64 return WEBKIT_DOM_NODE_LIST(g_object_new(WEBKIT_DOM_TYPE_NODE_LIST, "core-object", coreObject, nullptr));
65}
66
67} // namespace WebKit
68
69G_DEFINE_TYPE(WebKitDOMNodeList, webkit_dom_node_list, WEBKIT_DOM_TYPE_OBJECT)
70
71enum {
72 DOM_NODE_LIST_PROP_0,
73 DOM_NODE_LIST_PROP_LENGTH,
74};
75
76static void webkit_dom_node_list_finalize(GObject* object)
77{
78 WebKitDOMNodeListPrivate* priv = WEBKIT_DOM_NODE_LIST_GET_PRIVATE(object);
79
80 WebKit::DOMObjectCache::forget(priv->coreObject.get());
81
82 priv->~WebKitDOMNodeListPrivate();
83 G_OBJECT_CLASS(webkit_dom_node_list_parent_class)->finalize(object);
84}
85
86static void webkit_dom_node_list_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
87{
88 WebKitDOMNodeList* self = WEBKIT_DOM_NODE_LIST(object);
89
90 switch (propertyId) {
91 case DOM_NODE_LIST_PROP_LENGTH:
92 g_value_set_ulong(value, webkit_dom_node_list_get_length(self));
93 break;
94 default:
95 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
96 break;
97 }
98}
99
100static GObject* webkit_dom_node_list_constructor(GType type, guint constructPropertiesCount, GObjectConstructParam* constructProperties)
101{
102 GObject* object = G_OBJECT_CLASS(webkit_dom_node_list_parent_class)->constructor(type, constructPropertiesCount, constructProperties);
103
104 WebKitDOMNodeListPrivate* priv = WEBKIT_DOM_NODE_LIST_GET_PRIVATE(object);
105 priv->coreObject = static_cast<WebCore::NodeList*>(WEBKIT_DOM_OBJECT(object)->coreObject);
106 WebKit::DOMObjectCache::put(priv->coreObject.get(), object);
107
108 return object;
109}
110
111static void webkit_dom_node_list_class_init(WebKitDOMNodeListClass* requestClass)
112{
113 GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
114 g_type_class_add_private(gobjectClass, sizeof(WebKitDOMNodeListPrivate));
115 gobjectClass->constructor = webkit_dom_node_list_constructor;
116 gobjectClass->finalize = webkit_dom_node_list_finalize;
117 gobjectClass->get_property = webkit_dom_node_list_get_property;
118
119 g_object_class_install_property(
120 gobjectClass,
121 DOM_NODE_LIST_PROP_LENGTH,
122 g_param_spec_ulong(
123 "length",
124 "NodeList:length",
125 "read-only gulong NodeList:length",
126 0, G_MAXULONG, 0,
127 WEBKIT_PARAM_READABLE));
128
129}
130
131static void webkit_dom_node_list_init(WebKitDOMNodeList* request)
132{
133 WebKitDOMNodeListPrivate* priv = WEBKIT_DOM_NODE_LIST_GET_PRIVATE(request);
134 new (priv) WebKitDOMNodeListPrivate();
135}
136
137WebKitDOMNode* webkit_dom_node_list_item(WebKitDOMNodeList* self, gulong index)
138{
139 WebCore::JSMainThreadNullState state;
140 g_return_val_if_fail(WEBKIT_DOM_IS_NODE_LIST(self), 0);
141 WebCore::NodeList* item = WebKit::core(self);
142 RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(item->item(index));
143 return WebKit::kit(gobjectResult.get());
144}
145
146gulong webkit_dom_node_list_get_length(WebKitDOMNodeList* self)
147{
148 WebCore::JSMainThreadNullState state;
149 g_return_val_if_fail(WEBKIT_DOM_IS_NODE_LIST(self), 0);
150 WebCore::NodeList* item = WebKit::core(self);
151 gulong result = item->length();
152 return result;
153}
154
155G_GNUC_END_IGNORE_DEPRECATIONS;
156