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 "core/dom/NamedNodesCollection.h"
35#include "core/html/HTMLAllCollection.h"
36
37#include "V8Node.h"
38#include "V8NodeList.h"
39#include "bindings/v8/V8Binding.h"
40
41namespace WebCore {
42
43template<class CallbackInfo>
44static v8::Handle<v8::Value> getNamedItems(HTMLAllCollection* collection, AtomicString name, const CallbackInfo& callbackInfo)
45{
46    Vector<RefPtr<Node> > namedItems;
47    collection->namedItems(name, namedItems);
48
49    if (!namedItems.size())
50        return v8Undefined();
51
52    if (namedItems.size() == 1)
53        return toV8Fast(namedItems.at(0).release(), callbackInfo, collection);
54
55    // FIXME: HTML5 specification says this should be a HTMLCollection.
56    // http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interfaces.html#htmlallcollection
57    return toV8Fast(NamedNodesCollection::create(namedItems), callbackInfo, collection);
58}
59
60template<class CallbackInfo>
61static v8::Handle<v8::Value> getItem(HTMLAllCollection* collection, v8::Handle<v8::Value> argument, const CallbackInfo& callbackInfo)
62{
63    v8::Local<v8::Uint32> index = argument->ToArrayIndex();
64    if (index.IsEmpty()) {
65        v8::Handle<v8::Value> result = getNamedItems(collection, toWebCoreString(argument->ToString()), callbackInfo);
66
67        if (result.IsEmpty())
68            return v8::Undefined();
69
70        return result;
71    }
72
73    RefPtr<Node> result = collection->item(index->Uint32Value());
74    return toV8Fast(result.release(), callbackInfo, collection);
75}
76
77void V8HTMLAllCollection::itemMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args)
78{
79    HTMLAllCollection* imp = V8HTMLAllCollection::toNative(args.Holder());
80    v8SetReturnValue(args, getItem(imp, args[0], args));
81}
82
83void V8HTMLAllCollection::namedItemMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args)
84{
85    HTMLAllCollection* imp = V8HTMLAllCollection::toNative(args.Holder());
86    v8::Handle<v8::Value> result = getNamedItems(imp, toWebCoreString(args[0]), args);
87
88    if (result.IsEmpty()) {
89        v8SetReturnValueNull(args);
90        return;
91    }
92
93    v8SetReturnValue(args, result);
94}
95
96void V8HTMLAllCollection::legacyCallCustom(const v8::FunctionCallbackInfo<v8::Value>& args)
97{
98    if (args.Length() < 1)
99        return;
100
101    HTMLAllCollection* imp = V8HTMLAllCollection::toNative(args.Holder());
102
103    if (args.Length() == 1) {
104        v8SetReturnValue(args, getItem(imp, args[0], args));
105        return;
106    }
107
108    // If there is a second argument it is the index of the item we want.
109    String name = toWebCoreString(args[0]);
110    v8::Local<v8::Uint32> index = args[1]->ToArrayIndex();
111    if (index.IsEmpty())
112        return;
113
114    if (Node* node = imp->namedItemWithIndex(name, index->Uint32Value())) {
115        v8SetReturnValue(args, toV8Fast(node, args, imp));
116        return;
117    }
118}
119
120} // namespace WebCore
121