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 "HTMLAllCollection.h"
33
34 #include "V8Binding.h"
35 #include "V8Collection.h"
36 #include "V8CustomBinding.h"
37 #include "V8Proxy.h"
38
39 namespace WebCore {
40
41 NAMED_PROPERTY_GETTER(HTMLAllCollection)
42 {
43 INC_STATS("DOM.HTMLAllCollection.NamedPropertyGetter");
44 // Search the prototype chain first.
45 v8::Handle<v8::Value> value = info.Holder()->GetRealNamedPropertyInPrototypeChain(name);
46
47 if (!value.IsEmpty())
48 return value;
49
50 // Search local callback properties next to find IDL defined
51 // properties.
52 if (info.Holder()->HasRealNamedCallbackProperty(name))
53 return v8::Handle<v8::Value>();
54
55 // Finally, search the DOM structure.
56 HTMLAllCollection* imp = V8DOMWrapper::convertToNativeObject<HTMLAllCollection>(V8ClassIndex::HTMLALLCOLLECTION, info.Holder());
57 return getNamedItemsFromCollection(imp, v8StringToAtomicWebCoreString(name));
58 }
59
60 CALLBACK_FUNC_DECL(HTMLAllCollectionItem)
61 {
62 INC_STATS("DOM.HTMLAllCollection.item()");
63 HTMLAllCollection* imp = V8DOMWrapper::convertToNativeObject<HTMLAllCollection>(V8ClassIndex::HTMLALLCOLLECTION, args.Holder());
64 return getItemFromCollection(imp, args[0]);
65 }
66
67 CALLBACK_FUNC_DECL(HTMLAllCollectionNamedItem)
68 {
69 INC_STATS("DOM.HTMLAllCollection.namedItem()");
70 HTMLAllCollection* imp = V8DOMWrapper::convertToNativeObject<HTMLAllCollection>(V8ClassIndex::HTMLALLCOLLECTION, args.Holder());
71 v8::Handle<v8::Value> result = getNamedItemsFromCollection(imp, toWebCoreString(args[0]));
72
73 if (result.IsEmpty())
74 return v8::Undefined();
75
76 return result;
77 }
78
79 CALLBACK_FUNC_DECL(HTMLAllCollectionCallAsFunction)
80 {
81 INC_STATS("DOM.HTMLAllCollection.callAsFunction()");
82 if (args.Length() < 1)
83 return v8::Undefined();
84
85 HTMLAllCollection* imp = V8DOMWrapper::convertToNativeObject<HTMLAllCollection>(V8ClassIndex::HTMLALLCOLLECTION, args.Holder());
86
87 if (args.Length() == 1)
88 return getItemFromCollection(imp, args[0]);
89
90 // If there is a second argument it is the index of the item we want.
91 String name = toWebCoreString(args[0]);
92 v8::Local<v8::Uint32> index = args[1]->ToArrayIndex();
93 if (index.IsEmpty())
94 return v8::Undefined();
95
96 unsigned current = index->Uint32Value();
97 Node* node = imp->namedItem(name);
98 while (node) {
99 if (!current)
100 return V8DOMWrapper::convertNodeToV8Object(node);
101
102 node = imp->nextNamedItem(name);
103 current--;
104 }
105
106 return v8::Undefined();
107 }
108
109 } // namespace WebCore