• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "HTMLCollection.h"
33 
34 #include "V8Binding.h"
35 #include "V8CustomBinding.h"
36 #include "V8NamedNodesCollection.h"
37 #include "V8Proxy.h"
38 
39 namespace WebCore {
40 
getNamedItems(HTMLCollection * collection,AtomicString name)41 static v8::Handle<v8::Value> getNamedItems(HTMLCollection* collection, AtomicString name)
42 {
43     Vector<RefPtr<Node> > namedItems;
44     collection->namedItems(name, namedItems);
45 
46     if (!namedItems.size())
47         return v8::Handle<v8::Value>();
48 
49     if (namedItems.size() == 1)
50         return V8DOMWrapper::convertNodeToV8Object(namedItems.at(0).release());
51 
52     NodeList* list = new V8NamedNodesCollection(namedItems);
53     return V8DOMWrapper::convertToV8Object(V8ClassIndex::NODELIST, list);
54 }
55 
getItem(HTMLCollection * collection,v8::Handle<v8::Value> argument)56 static v8::Handle<v8::Value> getItem(HTMLCollection* collection, v8::Handle<v8::Value> argument)
57 {
58     v8::Local<v8::Uint32> index = argument->ToArrayIndex();
59     if (index.IsEmpty()) {
60         v8::Handle<v8::Value> result = getNamedItems(collection, toWebCoreString(argument->ToString()));
61 
62         if (result.IsEmpty())
63             return v8::Undefined();
64 
65         return result;
66     }
67 
68     RefPtr<Node> result = collection->item(index->Uint32Value());
69     return V8DOMWrapper::convertNodeToV8Object(result.release());
70 }
71 
NAMED_PROPERTY_GETTER(HTMLCollection)72 NAMED_PROPERTY_GETTER(HTMLCollection)
73 {
74     INC_STATS("DOM.HTMLCollection.NamedPropertyGetter");
75     // Search the prototype chain first.
76     v8::Handle<v8::Value> value = info.Holder()->GetRealNamedPropertyInPrototypeChain(name);
77 
78     if (!value.IsEmpty())
79         return value;
80 
81     // Search local callback properties next to find IDL defined
82     // properties.
83     if (info.Holder()->HasRealNamedCallbackProperty(name))
84         return v8::Handle<v8::Value>();
85 
86     // Finally, search the DOM structure.
87     HTMLCollection* imp = V8DOMWrapper::convertToNativeObject<HTMLCollection>(V8ClassIndex::HTMLCOLLECTION, info.Holder());
88     return getNamedItems(imp, v8StringToAtomicWebCoreString(name));
89 }
90 
CALLBACK_FUNC_DECL(HTMLCollectionItem)91 CALLBACK_FUNC_DECL(HTMLCollectionItem)
92 {
93     INC_STATS("DOM.HTMLCollection.item()");
94     HTMLCollection* imp = V8DOMWrapper::convertToNativeObject<HTMLCollection>(V8ClassIndex::HTMLCOLLECTION, args.Holder());
95     return getItem(imp, args[0]);
96 }
97 
CALLBACK_FUNC_DECL(HTMLCollectionNamedItem)98 CALLBACK_FUNC_DECL(HTMLCollectionNamedItem)
99 {
100     INC_STATS("DOM.HTMLCollection.namedItem()");
101     HTMLCollection* imp = V8DOMWrapper::convertToNativeObject<HTMLCollection>(V8ClassIndex::HTMLCOLLECTION, args.Holder());
102     v8::Handle<v8::Value> result = getNamedItems(imp, toWebCoreString(args[0]));
103 
104     if (result.IsEmpty())
105         return v8::Undefined();
106 
107     return result;
108 }
109 
CALLBACK_FUNC_DECL(HTMLCollectionCallAsFunction)110 CALLBACK_FUNC_DECL(HTMLCollectionCallAsFunction)
111 {
112     INC_STATS("DOM.HTMLCollection.callAsFunction()");
113     if (args.Length() < 1)
114         return v8::Undefined();
115 
116     HTMLCollection* imp = V8DOMWrapper::convertToNativeObject<HTMLCollection>(V8ClassIndex::HTMLCOLLECTION, args.Holder());
117 
118     if (args.Length() == 1)
119         return getItem(imp, args[0]);
120 
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();
124     if (index.IsEmpty())
125         return v8::Undefined();
126 
127     unsigned current = index->Uint32Value();
128     Node* node = imp->namedItem(name);
129     while (node) {
130         if (!current)
131             return V8DOMWrapper::convertNodeToV8Object(node);
132 
133         node = imp->nextNamedItem(name);
134         current--;
135     }
136 
137     return v8::Undefined();
138 }
139 
140 } // namespace WebCore
141