Home | History | Annotate | Download | only in custom
      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 "V8HTMLCollection.h"
     33 
     34 #include "HTMLCollection.h"
     35 #include "V8Binding.h"
     36 #include "V8HTMLAllCollection.h"
     37 #include "V8NamedNodesCollection.h"
     38 #include "V8Node.h"
     39 #include "V8NodeList.h"
     40 #include "V8Proxy.h"
     41 
     42 namespace WebCore {
     43 
     44 static v8::Handle<v8::Value> getNamedItems(HTMLCollection* collection, AtomicString name)
     45 {
     46     Vector<RefPtr<Node> > namedItems;
     47     collection->namedItems(name, namedItems);
     48 
     49     if (!namedItems.size())
     50         return v8::Handle<v8::Value>();
     51 
     52     if (namedItems.size() == 1)
     53         return toV8(namedItems.at(0).release());
     54 
     55     return toV8(V8NamedNodesCollection::create(namedItems));
     56 }
     57 
     58 static v8::Handle<v8::Value> getItem(HTMLCollection* collection, v8::Handle<v8::Value> argument)
     59 {
     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));
     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 toV8(result.release());
     75 }
     76 
     77 v8::Handle<v8::Value> V8HTMLCollection::namedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
     78 {
     79     INC_STATS("DOM.HTMLCollection.NamedPropertyGetter");
     80     // Search the prototype chain first.
     81     v8::Handle<v8::Value> value = info.Holder()->GetRealNamedPropertyInPrototypeChain(name);
     82 
     83     if (!value.IsEmpty())
     84         return value;
     85 
     86     // Search local callback properties next to find IDL defined
     87     // properties.
     88     if (info.Holder()->HasRealNamedCallbackProperty(name))
     89         return v8::Handle<v8::Value>();
     90 
     91     // Finally, search the DOM structure.
     92     HTMLCollection* imp = V8HTMLCollection::toNative(info.Holder());
     93     return getNamedItems(imp, v8StringToAtomicWebCoreString(name));
     94 }
     95 
     96 v8::Handle<v8::Value> V8HTMLCollection::itemCallback(const v8::Arguments& args)
     97 {
     98     INC_STATS("DOM.HTMLCollection.item()");
     99     HTMLCollection* imp = V8HTMLCollection::toNative(args.Holder());
    100     return getItem(imp, args[0]);
    101 }
    102 
    103 v8::Handle<v8::Value> V8HTMLCollection::namedItemCallback(const v8::Arguments& args)
    104 {
    105     INC_STATS("DOM.HTMLCollection.namedItem()");
    106     HTMLCollection* imp = V8HTMLCollection::toNative(args.Holder());
    107     v8::Handle<v8::Value> result = getNamedItems(imp, toWebCoreString(args[0]));
    108 
    109     if (result.IsEmpty())
    110         return v8::Undefined();
    111 
    112     return result;
    113 }
    114 
    115 v8::Handle<v8::Value> V8HTMLCollection::callAsFunctionCallback(const v8::Arguments& args)
    116 {
    117     INC_STATS("DOM.HTMLCollection.callAsFunction()");
    118     if (args.Length() < 1)
    119         return v8::Undefined();
    120 
    121     HTMLCollection* imp = V8HTMLCollection::toNative(args.Holder());
    122 
    123     if (args.Length() == 1)
    124         return getItem(imp, args[0]);
    125 
    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();
    129     if (index.IsEmpty())
    130         return v8::Undefined();
    131 
    132     unsigned current = index->Uint32Value();
    133     Node* node = imp->namedItem(name);
    134     while (node) {
    135         if (!current)
    136             return toV8(node);
    137 
    138         node = imp->nextNamedItem(name);
    139         current--;
    140     }
    141 
    142     return v8::Undefined();
    143 }
    144 
    145 v8::Handle<v8::Value> toV8(HTMLCollection* impl)
    146 {
    147     if (impl->type() == DocAll)
    148         return toV8(static_cast<HTMLAllCollection*>(impl));
    149     return V8HTMLCollection::wrap(impl);
    150 }
    151 
    152 } // namespace WebCore
    153