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 "HTMLCollection.h"
34 #include "V8Binding.h"
35 #include "V8CustomBinding.h"
36 #include "V8NamedNodesCollection.h"
41 static v8::Handle<v8::Value> getNamedItems(HTMLCollection* collection, String name)
43 Vector<RefPtr<Node> > namedItems;
44 collection->namedItems(name, namedItems);
46 if (!namedItems.size())
47 return v8::Handle<v8::Value>();
49 if (namedItems.size() == 1)
50 return V8Proxy::NodeToV8Object(namedItems.at(0).get());
52 NodeList* list = new V8NamedNodesCollection(namedItems);
53 return V8Proxy::ToV8Object(V8ClassIndex::NODELIST, list);
56 static v8::Handle<v8::Value> getItem(HTMLCollection* collection, v8::Handle<v8::Value> argument)
58 v8::Local<v8::Uint32> index = argument->ToArrayIndex();
59 if (index.IsEmpty()) {
60 v8::Handle<v8::Value> result = getNamedItems(collection, toWebCoreString(argument->ToString()));
63 return v8::Undefined();
68 RefPtr<Node> result = collection->item(index->Uint32Value());
69 return V8Proxy::NodeToV8Object(result.get());
72 NAMED_PROPERTY_GETTER(HTMLCollection)
74 INC_STATS("DOM.HTMLCollection.NamedPropertyGetter");
75 // Search the prototype chain first.
76 v8::Handle<v8::Value> value = info.Holder()->GetRealNamedPropertyInPrototypeChain(name);
81 // Search local callback properties next to find IDL defined
83 if (info.Holder()->HasRealNamedCallbackProperty(name))
84 return v8::Handle<v8::Value>();
86 // Finally, search the DOM structure.
87 HTMLCollection* imp = V8Proxy::ToNativeObject<HTMLCollection>(V8ClassIndex::HTMLCOLLECTION, info.Holder());
88 return getNamedItems(imp, toWebCoreString(name));
91 CALLBACK_FUNC_DECL(HTMLCollectionItem)
93 INC_STATS("DOM.HTMLCollection.item()");
94 HTMLCollection* imp = V8Proxy::ToNativeObject<HTMLCollection>(V8ClassIndex::HTMLCOLLECTION, args.Holder());
95 return getItem(imp, args[0]);
98 CALLBACK_FUNC_DECL(HTMLCollectionNamedItem)
100 INC_STATS("DOM.HTMLCollection.namedItem()");
101 HTMLCollection* imp = V8Proxy::ToNativeObject<HTMLCollection>(V8ClassIndex::HTMLCOLLECTION, args.Holder());
102 v8::Handle<v8::Value> result = getNamedItems(imp, toWebCoreString(args[0]));
104 if (result.IsEmpty())
105 return v8::Undefined();
110 CALLBACK_FUNC_DECL(HTMLCollectionCallAsFunction)
112 INC_STATS("DOM.HTMLCollection.callAsFunction()");
113 if (args.Length() < 1)
114 return v8::Undefined();
116 HTMLCollection* imp = V8Proxy::ToNativeObject<HTMLCollection>(V8ClassIndex::HTMLCOLLECTION, args.Holder());
118 if (args.Length() == 1)
119 return getItem(imp, args[0]);
121 // If there is a second argument it is the index of the item we want.
122 String name = toWebCoreString(args[0]);
123 v8::Local<v8::Uint32> index = args[1]->ToArrayIndex();
125 return v8::Undefined();
127 unsigned current = index->Uint32Value();
128 Node* node = imp->namedItem(name);
131 return V8Proxy::NodeToV8Object(node);
133 node = imp->nextNamedItem(name);
137 return v8::Undefined();
140 } // namespace WebCore