1/* 2 * Copyright (C) 2006, 2011, 2012 Apple Computer, Inc. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 * 19 */ 20 21#include "config.h" 22#include "core/html/HTMLOptionsCollection.h" 23 24#include "bindings/v8/ExceptionState.h" 25#include "core/dom/ExceptionCode.h" 26#include "core/dom/NamedNodesCollection.h" 27#include "core/html/HTMLOptionElement.h" 28#include "core/html/HTMLSelectElement.h" 29 30namespace WebCore { 31 32HTMLOptionsCollection::HTMLOptionsCollection(Node* select) 33 : HTMLCollection(select, SelectOptions, DoesNotOverrideItemAfter) 34{ 35 ASSERT(select->hasTagName(HTMLNames::selectTag)); 36 ScriptWrappable::init(this); 37} 38 39PassRefPtr<HTMLOptionsCollection> HTMLOptionsCollection::create(Node* select, CollectionType) 40{ 41 return adoptRef(new HTMLOptionsCollection(select)); 42} 43 44void HTMLOptionsCollection::add(PassRefPtr<HTMLOptionElement> element, ExceptionState& es) 45{ 46 add(element, length(), es); 47} 48 49void HTMLOptionsCollection::add(PassRefPtr<HTMLOptionElement> element, int index, ExceptionState& es) 50{ 51 HTMLOptionElement* newOption = element.get(); 52 53 if (!newOption) { 54 es.throwDOMException(TypeMismatchError); 55 return; 56 } 57 58 if (index < -1) { 59 es.throwDOMException(IndexSizeError); 60 return; 61 } 62 63 HTMLSelectElement* select = toHTMLSelectElement(ownerNode()); 64 65 if (index == -1 || unsigned(index) >= length()) 66 select->add(newOption, 0, es); 67 else 68 select->add(newOption, toHTMLOptionElement(item(index)), es); 69 70 ASSERT(!es.hadException()); 71} 72 73void HTMLOptionsCollection::remove(int index) 74{ 75 toHTMLSelectElement(ownerNode())->remove(index); 76} 77 78int HTMLOptionsCollection::selectedIndex() const 79{ 80 return toHTMLSelectElement(ownerNode())->selectedIndex(); 81} 82 83void HTMLOptionsCollection::setSelectedIndex(int index) 84{ 85 toHTMLSelectElement(ownerNode())->setSelectedIndex(index); 86} 87 88void HTMLOptionsCollection::setLength(unsigned length, ExceptionState& es) 89{ 90 toHTMLSelectElement(ownerNode())->setLength(length, es); 91} 92 93void HTMLOptionsCollection::anonymousNamedGetter(const AtomicString& name, bool& returnValue0Enabled, RefPtr<NodeList>& returnValue0, bool& returnValue1Enabled, RefPtr<Node>& returnValue1) 94{ 95 Vector<RefPtr<Node> > namedItems; 96 this->namedItems(name, namedItems); 97 98 if (!namedItems.size()) 99 return; 100 101 if (namedItems.size() == 1) { 102 returnValue1Enabled = true; 103 returnValue1 = namedItems.at(0); 104 return; 105 } 106 107 returnValue0Enabled = true; 108 returnValue0 = NamedNodesCollection::create(namedItems); 109} 110 111bool HTMLOptionsCollection::anonymousIndexedSetterRemove(unsigned index, ExceptionState& es) 112{ 113 HTMLSelectElement* base = toHTMLSelectElement(ownerNode()); 114 base->remove(index); 115 return true; 116} 117 118bool HTMLOptionsCollection::anonymousIndexedSetter(unsigned index, PassRefPtr<HTMLOptionElement> value, ExceptionState& es) 119{ 120 HTMLSelectElement* base = toHTMLSelectElement(ownerNode()); 121 if (!value) { 122 es.throwDOMException(TypeMismatchError); 123 return true; 124 } 125 base->setOption(index, value.get(), es); 126 return true; 127} 128 129} //namespace 130 131