1 /*
2 * This file is part of the DOM implementation for KDE.
3 *
4 * Copyright (C) 2006 Apple Computer, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "config.h"
24 #include "HTMLOptionsCollection.h"
25
26 #include "ExceptionCode.h"
27 #include "HTMLOptionElement.h"
28 #include "HTMLSelectElement.h"
29
30 namespace WebCore {
31
HTMLOptionsCollection(PassRefPtr<HTMLSelectElement> select)32 HTMLOptionsCollection::HTMLOptionsCollection(PassRefPtr<HTMLSelectElement> select)
33 : HTMLCollection(select.get(), SelectOptions, select->collectionInfo())
34 {
35 }
36
create(PassRefPtr<HTMLSelectElement> select)37 PassRefPtr<HTMLOptionsCollection> HTMLOptionsCollection::create(PassRefPtr<HTMLSelectElement> select)
38 {
39 return adoptRef(new HTMLOptionsCollection(select));
40 }
41
add(PassRefPtr<HTMLOptionElement> element,ExceptionCode & ec)42 void HTMLOptionsCollection::add(PassRefPtr<HTMLOptionElement> element, ExceptionCode &ec)
43 {
44 add(element, length(), ec);
45 }
46
add(PassRefPtr<HTMLOptionElement> element,int index,ExceptionCode & ec)47 void HTMLOptionsCollection::add(PassRefPtr<HTMLOptionElement> element, int index, ExceptionCode &ec)
48 {
49 HTMLOptionElement* newOption = element.get();
50
51 if (!newOption) {
52 ec = TYPE_MISMATCH_ERR;
53 return;
54 }
55
56 if (index < -1) {
57 ec = INDEX_SIZE_ERR;
58 return;
59 }
60
61 ec = 0;
62 HTMLSelectElement* select = static_cast<HTMLSelectElement*>(base());
63
64 if (index == -1 || unsigned(index) >= length())
65 select->add(newOption, 0, ec);
66 else
67 select->add(newOption, static_cast<HTMLOptionElement*>(item(index)), ec);
68
69 ASSERT(ec == 0);
70 }
71
remove(int index)72 void HTMLOptionsCollection::remove(int index)
73 {
74 static_cast<HTMLSelectElement*>(base())->remove(index);
75 }
76
selectedIndex() const77 int HTMLOptionsCollection::selectedIndex() const
78 {
79 return static_cast<HTMLSelectElement*>(base())->selectedIndex();
80 }
81
setSelectedIndex(int index)82 void HTMLOptionsCollection::setSelectedIndex(int index)
83 {
84 static_cast<HTMLSelectElement*>(base())->setSelectedIndex(index);
85 }
86
setLength(unsigned length,ExceptionCode & ec)87 void HTMLOptionsCollection::setLength(unsigned length, ExceptionCode& ec)
88 {
89 static_cast<HTMLSelectElement*>(base())->setLength(length, ec);
90 }
91
92 } //namespace
93