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 "WebKitDOMHTMLCollection.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 "WebKitDOMHTMLCollectionPrivate.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_HTML_COLLECTION_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, WEBKIT_DOM_TYPE_HTML_COLLECTION, WebKitDOMHTMLCollectionPrivate)
36
37typedef struct _WebKitDOMHTMLCollectionPrivate {
38 RefPtr<WebCore::HTMLCollection> coreObject;
39} WebKitDOMHTMLCollectionPrivate;
40
41G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
42
43namespace WebKit {
44
45WebKitDOMHTMLCollection* kit(WebCore::HTMLCollection* obj)
46{
47 if (!obj)
48 return 0;
49
50 if (gpointer ret = DOMObjectCache::get(obj))
51 return WEBKIT_DOM_HTML_COLLECTION(ret);
52
53 return wrap(obj);
54}
55
56WebCore::HTMLCollection* core(WebKitDOMHTMLCollection* request)
57{
58 return request ? static_cast<WebCore::HTMLCollection*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
59}
60
61WebKitDOMHTMLCollection* wrapHTMLCollection(WebCore::HTMLCollection* coreObject)
62{
63 ASSERT(coreObject);
64 return WEBKIT_DOM_HTML_COLLECTION(g_object_new(WEBKIT_DOM_TYPE_HTML_COLLECTION, "core-object", coreObject, nullptr));
65}
66
67} // namespace WebKit
68
69G_DEFINE_TYPE(WebKitDOMHTMLCollection, webkit_dom_html_collection, WEBKIT_DOM_TYPE_OBJECT)
70
71enum {
72 DOM_HTML_COLLECTION_PROP_0,
73 DOM_HTML_COLLECTION_PROP_LENGTH,
74};
75
76static void webkit_dom_html_collection_finalize(GObject* object)
77{
78 WebKitDOMHTMLCollectionPrivate* priv = WEBKIT_DOM_HTML_COLLECTION_GET_PRIVATE(object);
79
80 WebKit::DOMObjectCache::forget(priv->coreObject.get());
81
82 priv->~WebKitDOMHTMLCollectionPrivate();
83 G_OBJECT_CLASS(webkit_dom_html_collection_parent_class)->finalize(object);
84}
85
86static void webkit_dom_html_collection_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
87{
88 WebKitDOMHTMLCollection* self = WEBKIT_DOM_HTML_COLLECTION(object);
89
90 switch (propertyId) {
91 case DOM_HTML_COLLECTION_PROP_LENGTH:
92 g_value_set_ulong(value, webkit_dom_html_collection_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_html_collection_constructor(GType type, guint constructPropertiesCount, GObjectConstructParam* constructProperties)
101{
102 GObject* object = G_OBJECT_CLASS(webkit_dom_html_collection_parent_class)->constructor(type, constructPropertiesCount, constructProperties);
103
104 WebKitDOMHTMLCollectionPrivate* priv = WEBKIT_DOM_HTML_COLLECTION_GET_PRIVATE(object);
105 priv->coreObject = static_cast<WebCore::HTMLCollection*>(WEBKIT_DOM_OBJECT(object)->coreObject);
106 WebKit::DOMObjectCache::put(priv->coreObject.get(), object);
107
108 return object;
109}
110
111static void webkit_dom_html_collection_class_init(WebKitDOMHTMLCollectionClass* requestClass)
112{
113 GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
114 g_type_class_add_private(gobjectClass, sizeof(WebKitDOMHTMLCollectionPrivate));
115 gobjectClass->constructor = webkit_dom_html_collection_constructor;
116 gobjectClass->finalize = webkit_dom_html_collection_finalize;
117 gobjectClass->get_property = webkit_dom_html_collection_get_property;
118
119 g_object_class_install_property(
120 gobjectClass,
121 DOM_HTML_COLLECTION_PROP_LENGTH,
122 g_param_spec_ulong(
123 "length",
124 "HTMLCollection:length",
125 "read-only gulong HTMLCollection:length",
126 0, G_MAXULONG, 0,
127 WEBKIT_PARAM_READABLE));
128
129}
130
131static void webkit_dom_html_collection_init(WebKitDOMHTMLCollection* request)
132{
133 WebKitDOMHTMLCollectionPrivate* priv = WEBKIT_DOM_HTML_COLLECTION_GET_PRIVATE(request);
134 new (priv) WebKitDOMHTMLCollectionPrivate();
135}
136
137WebKitDOMNode* webkit_dom_html_collection_item(WebKitDOMHTMLCollection* self, gulong index)
138{
139 WebCore::JSMainThreadNullState state;
140 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_COLLECTION(self), 0);
141 WebCore::HTMLCollection* item = WebKit::core(self);
142 RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(item->item(index));
143 return WebKit::kit(gobjectResult.get());
144}
145
146WebKitDOMNode* webkit_dom_html_collection_named_item(WebKitDOMHTMLCollection* self, const gchar* name)
147{
148 WebCore::JSMainThreadNullState state;
149 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_COLLECTION(self), 0);
150 g_return_val_if_fail(name, 0);
151 WebCore::HTMLCollection* item = WebKit::core(self);
152 WTF::String convertedName = WTF::String::fromUTF8(name);
153 RefPtr<WebCore::Node> gobjectResult = WTF::getPtr(item->namedItem(convertedName));
154 return WebKit::kit(gobjectResult.get());
155}
156
157gulong webkit_dom_html_collection_get_length(WebKitDOMHTMLCollection* self)
158{
159 WebCore::JSMainThreadNullState state;
160 g_return_val_if_fail(WEBKIT_DOM_IS_HTML_COLLECTION(self), 0);
161 WebCore::HTMLCollection* item = WebKit::core(self);
162 gulong result = item->length();
163 return result;
164}
165
166G_GNUC_END_IGNORE_DEPRECATIONS;
167