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