1/*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "V8HTMLAllCollection.h"
33
34#include "HTMLAllCollection.h"
35
36#include "V8Binding.h"
37#include "V8NamedNodesCollection.h"
38#include "V8Node.h"
39#include "V8NodeList.h"
40#include "V8Proxy.h"
41
42namespace WebCore {
43
44static v8::Handle<v8::Value> getNamedItems(HTMLAllCollection* collection, AtomicString name)
45{
46    Vector<RefPtr<Node> > namedItems;
47    collection->namedItems(name, namedItems);
48
49    if (!namedItems.size())
50        return v8::Handle<v8::Value>();
51
52    if (namedItems.size() == 1)
53        return toV8(namedItems.at(0).release());
54
55    return toV8(V8NamedNodesCollection::create(namedItems));
56}
57
58static v8::Handle<v8::Value> getItem(HTMLAllCollection* collection, v8::Handle<v8::Value> argument)
59{
60    v8::Local<v8::Uint32> index = argument->ToArrayIndex();
61    if (index.IsEmpty()) {
62        v8::Handle<v8::Value> result = getNamedItems(collection, toWebCoreString(argument->ToString()));
63
64        if (result.IsEmpty())
65            return v8::Undefined();
66
67        return result;
68    }
69
70    RefPtr<Node> result = collection->item(index->Uint32Value());
71    return toV8(result.release());
72}
73
74v8::Handle<v8::Value> V8HTMLAllCollection::namedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
75{
76    INC_STATS("DOM.HTMLAllCollection.NamedPropertyGetter");
77    // Search the prototype chain first.
78    v8::Handle<v8::Value> value = info.Holder()->GetRealNamedPropertyInPrototypeChain(name);
79
80    if (!value.IsEmpty())
81        return value;
82
83    // Search local callback properties next to find IDL defined
84    // properties.
85    if (info.Holder()->HasRealNamedCallbackProperty(name))
86        return v8::Handle<v8::Value>();
87
88    // Finally, search the DOM structure.
89    HTMLAllCollection* imp = V8HTMLAllCollection::toNative(info.Holder());
90    return getNamedItems(imp, v8StringToAtomicWebCoreString(name));
91}
92
93v8::Handle<v8::Value> V8HTMLAllCollection::itemCallback(const v8::Arguments& args)
94{
95    INC_STATS("DOM.HTMLAllCollection.item()");
96    HTMLAllCollection* imp = V8HTMLAllCollection::toNative(args.Holder());
97    return getItem(imp, args[0]);
98}
99
100v8::Handle<v8::Value> V8HTMLAllCollection::namedItemCallback(const v8::Arguments& args)
101{
102    INC_STATS("DOM.HTMLAllCollection.namedItem()");
103    HTMLAllCollection* imp = V8HTMLAllCollection::toNative(args.Holder());
104    v8::Handle<v8::Value> result = getNamedItems(imp, toWebCoreString(args[0]));
105
106    if (result.IsEmpty())
107        return v8::Undefined();
108
109    return result;
110}
111
112v8::Handle<v8::Value> V8HTMLAllCollection::callAsFunctionCallback(const v8::Arguments& args)
113{
114    INC_STATS("DOM.HTMLAllCollection.callAsFunction()");
115    if (args.Length() < 1)
116        return v8::Undefined();
117
118    HTMLAllCollection* imp = V8HTMLAllCollection::toNative(args.Holder());
119
120    if (args.Length() == 1)
121        return getItem(imp, args[0]);
122
123    // If there is a second argument it is the index of the item we want.
124    String name = toWebCoreString(args[0]);
125    v8::Local<v8::Uint32> index = args[1]->ToArrayIndex();
126    if (index.IsEmpty())
127        return v8::Undefined();
128
129    unsigned current = index->Uint32Value();
130    Node* node = imp->namedItem(name);
131    while (node) {
132        if (!current)
133            return toV8(node);
134
135        node = imp->nextNamedItem(name);
136        current--;
137    }
138
139    return v8::Undefined();
140}
141
142} // namespace WebCore
143