• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ExceptionMessages.h"
25 #include "bindings/v8/ExceptionState.h"
26 #include "core/dom/ExceptionCode.h"
27 #include "core/dom/NamedNodesCollection.h"
28 #include "core/html/HTMLOptionElement.h"
29 #include "core/html/HTMLSelectElement.h"
30 
31 namespace WebCore {
32 
HTMLOptionsCollection(Node * select)33 HTMLOptionsCollection::HTMLOptionsCollection(Node* select)
34     : HTMLCollection(select, SelectOptions, DoesNotOverrideItemAfter)
35 {
36     ASSERT(select->hasTagName(HTMLNames::selectTag));
37     ScriptWrappable::init(this);
38 }
39 
create(Node * select,CollectionType)40 PassRefPtr<HTMLOptionsCollection> HTMLOptionsCollection::create(Node* select, CollectionType)
41 {
42     return adoptRef(new HTMLOptionsCollection(select));
43 }
44 
add(PassRefPtr<HTMLOptionElement> element,ExceptionState & exceptionState)45 void HTMLOptionsCollection::add(PassRefPtr<HTMLOptionElement> element, ExceptionState& exceptionState)
46 {
47     add(element, length(), exceptionState);
48 }
49 
add(PassRefPtr<HTMLOptionElement> element,int index,ExceptionState & exceptionState)50 void HTMLOptionsCollection::add(PassRefPtr<HTMLOptionElement> element, int index, ExceptionState& exceptionState)
51 {
52     HTMLOptionElement* newOption = element.get();
53 
54     if (!newOption) {
55         exceptionState.throwTypeError("The element provided was not an HTMLOptionElement.");
56         return;
57     }
58 
59     if (index < -1) {
60         exceptionState.throwDOMException(IndexSizeError, "The index provided (" + String::number(index) + ") is less than -1.");
61         return;
62     }
63 
64     HTMLSelectElement* select = toHTMLSelectElement(ownerNode());
65 
66     if (index == -1 || unsigned(index) >= length())
67         select->add(newOption, 0, exceptionState);
68     else
69         select->add(newOption, toHTMLOptionElement(item(index)), exceptionState);
70 
71     ASSERT(!exceptionState.hadException());
72 }
73 
remove(int index)74 void HTMLOptionsCollection::remove(int index)
75 {
76     toHTMLSelectElement(ownerNode())->remove(index);
77 }
78 
remove(HTMLOptionElement * option)79 void HTMLOptionsCollection::remove(HTMLOptionElement* option)
80 {
81     return remove(option->index());
82 }
83 
selectedIndex() const84 int HTMLOptionsCollection::selectedIndex() const
85 {
86     return toHTMLSelectElement(ownerNode())->selectedIndex();
87 }
88 
setSelectedIndex(int index)89 void HTMLOptionsCollection::setSelectedIndex(int index)
90 {
91     toHTMLSelectElement(ownerNode())->setSelectedIndex(index);
92 }
93 
setLength(unsigned length,ExceptionState & exceptionState)94 void HTMLOptionsCollection::setLength(unsigned length, ExceptionState& exceptionState)
95 {
96     toHTMLSelectElement(ownerNode())->setLength(length, exceptionState);
97 }
98 
anonymousNamedGetter(const AtomicString & name,bool & returnValue0Enabled,RefPtr<NodeList> & returnValue0,bool & returnValue1Enabled,RefPtr<Node> & returnValue1)99 void HTMLOptionsCollection::anonymousNamedGetter(const AtomicString& name, bool& returnValue0Enabled, RefPtr<NodeList>& returnValue0, bool& returnValue1Enabled, RefPtr<Node>& returnValue1)
100 {
101     Vector<RefPtr<Node> > namedItems;
102     this->namedItems(name, namedItems);
103 
104     if (!namedItems.size())
105         return;
106 
107     if (namedItems.size() == 1) {
108         returnValue1Enabled = true;
109         returnValue1 = namedItems.at(0);
110         return;
111     }
112 
113     returnValue0Enabled = true;
114     returnValue0 = NamedNodesCollection::create(namedItems);
115 }
116 
anonymousIndexedSetterRemove(unsigned index,ExceptionState & exceptionState)117 bool HTMLOptionsCollection::anonymousIndexedSetterRemove(unsigned index, ExceptionState& exceptionState)
118 {
119     HTMLSelectElement* base = toHTMLSelectElement(ownerNode());
120     base->remove(index);
121     return true;
122 }
123 
anonymousIndexedSetter(unsigned index,PassRefPtr<HTMLOptionElement> value,ExceptionState & exceptionState)124 bool HTMLOptionsCollection::anonymousIndexedSetter(unsigned index, PassRefPtr<HTMLOptionElement> value, ExceptionState& exceptionState)
125 {
126     HTMLSelectElement* base = toHTMLSelectElement(ownerNode());
127     if (!value) {
128         exceptionState.throwTypeError(ExceptionMessages::failedToSet(String::number(index), "HTMLOptionsCollection", "The element provided was not an HTMLOptionElement."));
129         return true;
130     }
131     base->setOption(index, value.get(), exceptionState);
132     return true;
133 }
134 
135 } //namespace
136 
137