2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "V8HTMLCollection.h"
34 #include "HTMLCollection.h"
35 #include "V8Binding.h"
36 #include "V8HTMLAllCollection.h"
37 #include "V8NamedNodesCollection.h"
39 #include "V8NodeList.h"
44 static v8::Handle<v8::Value> getNamedItems(HTMLCollection* collection, AtomicString name)
46 Vector<RefPtr<Node> > namedItems;
47 collection->namedItems(name, namedItems);
49 if (!namedItems.size())
50 return v8::Handle<v8::Value>();
52 if (namedItems.size() == 1)
53 return toV8(namedItems.at(0).release());
55 return toV8(V8NamedNodesCollection::create(namedItems));
58 static v8::Handle<v8::Value> getItem(HTMLCollection* collection, v8::Handle<v8::Value> argument)
60 v8::Local<v8::Uint32> index = argument->ToArrayIndex();
61 if (index.IsEmpty()) {
62 v8::Local<v8::String> asString = argument->ToString();
63 if (asString.IsEmpty())
64 return v8::Handle<v8::Value>();
65 v8::Handle<v8::Value> result = getNamedItems(collection, toWebCoreString(asString));
68 return v8::Undefined();
73 RefPtr<Node> result = collection->item(index->Uint32Value());
74 return toV8(result.release());
77 v8::Handle<v8::Value> V8HTMLCollection::namedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
79 INC_STATS("DOM.HTMLCollection.NamedPropertyGetter");
80 // Search the prototype chain first.
81 v8::Handle<v8::Value> value = info.Holder()->GetRealNamedPropertyInPrototypeChain(name);
86 // Search local callback properties next to find IDL defined
88 if (info.Holder()->HasRealNamedCallbackProperty(name))
89 return v8::Handle<v8::Value>();
91 // Finally, search the DOM structure.
92 HTMLCollection* imp = V8HTMLCollection::toNative(info.Holder());
93 return getNamedItems(imp, v8StringToAtomicWebCoreString(name));
96 v8::Handle<v8::Value> V8HTMLCollection::itemCallback(const v8::Arguments& args)
98 INC_STATS("DOM.HTMLCollection.item()");
99 HTMLCollection* imp = V8HTMLCollection::toNative(args.Holder());
100 return getItem(imp, args[0]);
103 v8::Handle<v8::Value> V8HTMLCollection::namedItemCallback(const v8::Arguments& args)
105 INC_STATS("DOM.HTMLCollection.namedItem()");
106 HTMLCollection* imp = V8HTMLCollection::toNative(args.Holder());
107 v8::Handle<v8::Value> result = getNamedItems(imp, toWebCoreString(args[0]));
109 if (result.IsEmpty())
110 return v8::Undefined();
115 v8::Handle<v8::Value> V8HTMLCollection::callAsFunctionCallback(const v8::Arguments& args)
117 INC_STATS("DOM.HTMLCollection.callAsFunction()");
118 if (args.Length() < 1)
119 return v8::Undefined();
121 HTMLCollection* imp = V8HTMLCollection::toNative(args.Holder());
123 if (args.Length() == 1)
124 return getItem(imp, args[0]);
126 // If there is a second argument it is the index of the item we want.
127 String name = toWebCoreString(args[0]);
128 v8::Local<v8::Uint32> index = args[1]->ToArrayIndex();
130 return v8::Undefined();
132 unsigned current = index->Uint32Value();
133 Node* node = imp->namedItem(name);
138 node = imp->nextNamedItem(name);
142 return v8::Undefined();
145 v8::Handle<v8::Value> toV8(HTMLCollection* impl)
147 if (impl->type() == DocAll)
148 return toV8(static_cast<HTMLAllCollection*>(impl));
149 return V8HTMLCollection::wrap(impl);
152 } // namespace WebCore