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     NodeList* list = new V8NamedNodesCollection(namedItems);
     56     return toV8(list);
     57 }
     58 
     59 static v8::Handle<v8::Value> getItem(HTMLCollection* collection, v8::Handle<v8::Value> argument)
     60 {
     61     v8::Local<v8::Uint32> index = argument->ToArrayIndex();
     62     if (index.IsEmpty()) {
     63         v8::Handle<v8::Value> result = getNamedItems(collection, toWebCoreString(argument->ToString()));
     64 
     65         if (result.IsEmpty())
     66             return v8::Undefined();
     67 
     68         return result;
     69     }
     70 
     71     RefPtr<Node> result = collection->item(index->Uint32Value());
     72     return toV8(result.release());
     73 }
     74 
     75 v8::Handle<v8::Value> V8HTMLCollection::namedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
     76 {
     77     INC_STATS("DOM.HTMLCollection.NamedPropertyGetter");
     78     // Search the prototype chain first.
     79     v8::Handle<v8::Value> value = info.Holder()->GetRealNamedPropertyInPrototypeChain(name);
     80 
     81     if (!value.IsEmpty())
     82         return value;
     83 
     84     // Search local callback properties next to find IDL defined
     85     // properties.
     86     if (info.Holder()->HasRealNamedCallbackProperty(name))
     87         return v8::Handle<v8::Value>();
     88 
     89     // Finally, search the DOM structure.
     90     HTMLCollection* imp = V8HTMLCollection::toNative(info.Holder());
     91     return getNamedItems(imp, v8StringToAtomicWebCoreString(name));
     92 }
     93 
     94 v8::Handle<v8::Value> V8HTMLCollection::itemCallback(const v8::Arguments& args)
     95 {
     96     INC_STATS("DOM.HTMLCollection.item()");
     97     HTMLCollection* imp = V8HTMLCollection::toNative(args.Holder());
     98     return getItem(imp, args[0]);
     99 }
    100 
    101 v8::Handle<v8::Value> V8HTMLCollection::namedItemCallback(const v8::Arguments& args)
    102 {
    103     INC_STATS("DOM.HTMLCollection.namedItem()");
    104     HTMLCollection* imp = V8HTMLCollection::toNative(args.Holder());
    105     v8::Handle<v8::Value> result = getNamedItems(imp, toWebCoreString(args[0]));
    106 
    107     if (result.IsEmpty())
    108         return v8::Undefined();
    109 
    110     return result;
    111 }
    112 
    113 v8::Handle<v8::Value> V8HTMLCollection::callAsFunctionCallback(const v8::Arguments& args)
    114 {
    115     INC_STATS("DOM.HTMLCollection.callAsFunction()");
    116     if (args.Length() < 1)
    117         return v8::Undefined();
    118 
    119     HTMLCollection* imp = V8HTMLCollection::toNative(args.Holder());
    120 
    121     if (args.Length() == 1)
    122         return getItem(imp, args[0]);
    123 
    124     // If there is a second argument it is the index of the item we want.
    125     String name = toWebCoreString(args[0]);
    126     v8::Local<v8::Uint32> index = args[1]->ToArrayIndex();
    127     if (index.IsEmpty())
    128         return v8::Undefined();
    129 
    130     unsigned current = index->Uint32Value();
    131     Node* node = imp->namedItem(name);
    132     while (node) {
    133         if (!current)
    134             return toV8(node);
    135 
    136         node = imp->nextNamedItem(name);
    137         current--;
    138     }
    139 
    140     return v8::Undefined();
    141 }
    142 
    143 v8::Handle<v8::Value> toV8(HTMLCollection* impl)
    144 {
    145     if (impl->type() == DocAll)
    146         return toV8(static_cast<HTMLAllCollection*>(impl));
    147     return V8HTMLCollection::wrap(impl);
    148 }
    149 
    150 } // namespace WebCore
    151