1/*
2 * Copyright (C) 2006 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 "HTMLOptionsCollection.h"
23
24#include "ExceptionCode.h"
25#include "HTMLOptionElement.h"
26#include "HTMLSelectElement.h"
27
28namespace WebCore {
29
30HTMLOptionsCollection::HTMLOptionsCollection(PassRefPtr<HTMLSelectElement> select)
31    : HTMLCollection(select.get(), SelectOptions, select->collectionInfo())
32{
33}
34
35PassRefPtr<HTMLOptionsCollection> HTMLOptionsCollection::create(PassRefPtr<HTMLSelectElement> select)
36{
37    return adoptRef(new HTMLOptionsCollection(select));
38}
39
40void HTMLOptionsCollection::add(PassRefPtr<HTMLOptionElement> element, ExceptionCode &ec)
41{
42    add(element, length(), ec);
43}
44
45void HTMLOptionsCollection::add(PassRefPtr<HTMLOptionElement> element, int index, ExceptionCode &ec)
46{
47    HTMLOptionElement* newOption = element.get();
48
49    if (!newOption) {
50        ec = TYPE_MISMATCH_ERR;
51        return;
52    }
53
54    if (index < -1) {
55        ec = INDEX_SIZE_ERR;
56        return;
57    }
58
59    ec = 0;
60    HTMLSelectElement* select = static_cast<HTMLSelectElement*>(base());
61
62    if (index == -1 || unsigned(index) >= length())
63        select->add(newOption, 0, ec);
64    else
65        select->add(newOption, static_cast<HTMLOptionElement*>(item(index)), ec);
66
67    ASSERT(ec == 0);
68}
69
70void HTMLOptionsCollection::remove(int index)
71{
72    static_cast<HTMLSelectElement*>(base())->remove(index);
73}
74
75int HTMLOptionsCollection::selectedIndex() const
76{
77    return static_cast<HTMLSelectElement*>(base())->selectedIndex();
78}
79
80void HTMLOptionsCollection::setSelectedIndex(int index)
81{
82    static_cast<HTMLSelectElement*>(base())->setSelectedIndex(index);
83}
84
85void HTMLOptionsCollection::setLength(unsigned length, ExceptionCode& ec)
86{
87    static_cast<HTMLSelectElement*>(base())->setLength(length, ec);
88}
89
90} //namespace
91