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 "V8HTMLSelectElementCustom.h" 33 34 #include "HTMLSelectElement.h" 35 #include "HTMLOptionElement.h" 36 #include "HTMLOptionsCollection.h" 37 38 #include "V8Binding.h" 39 #include "V8Collection.h" 40 #include "V8HTMLOptionElement.h" 41 #include "V8HTMLSelectElement.h" 42 #include "V8NamedNodesCollection.h" 43 #include "V8Node.h" 44 #include "V8NodeList.h" 45 #include "V8Proxy.h" 46 47 namespace WebCore { 48 49 v8::Handle<v8::Value> V8HTMLSelectElement::namedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info) 50 { 51 INC_STATS("DOM.HTMLSelectElement.NamedPropertyGetter"); 52 HTMLSelectElement* select = V8HTMLSelectElement::toNative(info.Holder()); 53 v8::Handle<v8::Value> value = info.Holder()->GetRealNamedPropertyInPrototypeChain(name); 54 55 if (!value.IsEmpty()) 56 return value; 57 58 // Search local callback properties next to find IDL defined properties. 59 if (info.Holder()->HasRealNamedCallbackProperty(name)) 60 return notHandledByInterceptor(); 61 62 PassRefPtr<HTMLOptionsCollection> collection = select->options(); 63 64 Vector<RefPtr<Node> > items; 65 collection->namedItems(v8StringToAtomicWebCoreString(name), items); 66 67 if (!items.size()) 68 return notHandledByInterceptor(); 69 70 if (items.size() == 1) 71 return toV8(items.at(0).release()); 72 73 NodeList* list = new V8NamedNodesCollection(items); 74 return toV8(list); 75 } 76 77 v8::Handle<v8::Value> V8HTMLSelectElement::indexedPropertyGetter(uint32_t index, const v8::AccessorInfo& info) 78 { 79 ASSERT(V8DOMWrapper::maybeDOMWrapper(info.Holder())); 80 RefPtr<Node> result = V8HTMLSelectElement::toNative(info.Holder())->item(index); 81 if (!result) 82 return notHandledByInterceptor(); 83 84 return toV8(result.release()); 85 } 86 87 v8::Handle<v8::Value> V8HTMLSelectElement::indexedPropertySetter(uint32_t index, v8::Local<v8::Value> value, const v8::AccessorInfo& info) 88 { 89 INC_STATS("DOM.HTMLSelectElement.IndexedPropertySetter"); 90 HTMLSelectElement* select = V8HTMLSelectElement::toNative(info.Holder()); 91 return toOptionsCollectionSetter(index, value, select); 92 } 93 94 v8::Handle<v8::Value> V8HTMLSelectElement::removeCallback(const v8::Arguments& args) 95 { 96 INC_STATS("DOM.HTMLSelectElement.remove"); 97 HTMLSelectElement* imp = V8HTMLSelectElement::toNative(args.Holder()); 98 return removeElement(imp, args); 99 } 100 101 v8::Handle<v8::Value> removeElement(HTMLSelectElement* imp, const v8::Arguments& args) 102 { 103 if (V8HTMLOptionElement::HasInstance(args[0])) { 104 HTMLOptionElement* element = V8HTMLOptionElement::toNative(v8::Handle<v8::Object>::Cast(args[0])); 105 imp->remove(element->index()); 106 return v8::Undefined(); 107 } 108 109 imp->remove(toInt32(args[0])); 110 return v8::Undefined(); 111 } 112 113 } // namespace WebCore 114