2 * Copyright (C) 2006, 2011, 2012 Apple Computer, Inc.
3 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
23 #include "core/html/HTMLOptionsCollection.h"
25 #include "bindings/core/v8/ExceptionMessages.h"
26 #include "bindings/core/v8/ExceptionState.h"
27 #include "bindings/core/v8/UnionTypesCore.h"
28 #include "core/dom/ExceptionCode.h"
29 #include "core/dom/StaticNodeList.h"
30 #include "core/html/HTMLOptionElement.h"
31 #include "core/html/HTMLSelectElement.h"
35 HTMLOptionsCollection::HTMLOptionsCollection(ContainerNode& select)
36 : HTMLCollection(select, SelectOptions, DoesNotOverrideItemAfter)
38 ASSERT(isHTMLSelectElement(select));
41 void HTMLOptionsCollection::supportedPropertyNames(Vector<String>& names)
43 // As per http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interfaces.html#htmloptionscollection:
44 // The supported property names consist of the non-empty values of all the id and name attributes of all the elements
45 // represented by the collection, in tree order, ignoring later duplicates, with the id of an element preceding its
46 // name if it contributes both, they differ from each other, and neither is the duplicate of an earlier entry.
47 HashSet<AtomicString> existingNames;
48 unsigned length = this->length();
49 for (unsigned i = 0; i < length; ++i) {
50 Element* element = item(i);
52 const AtomicString& idAttribute = element->getIdAttribute();
53 if (!idAttribute.isEmpty()) {
54 HashSet<AtomicString>::AddResult addResult = existingNames.add(idAttribute);
55 if (addResult.isNewEntry)
56 names.append(idAttribute);
58 const AtomicString& nameAttribute = element->getNameAttribute();
59 if (!nameAttribute.isEmpty()) {
60 HashSet<AtomicString>::AddResult addResult = existingNames.add(nameAttribute);
61 if (addResult.isNewEntry)
62 names.append(nameAttribute);
67 PassRefPtrWillBeRawPtr<HTMLOptionsCollection> HTMLOptionsCollection::create(ContainerNode& select, CollectionType)
69 return adoptRefWillBeNoop(new HTMLOptionsCollection(select));
72 void HTMLOptionsCollection::add(PassRefPtrWillBeRawPtr<HTMLOptionElement> element, ExceptionState& exceptionState)
74 add(element, length(), exceptionState);
77 void HTMLOptionsCollection::add(PassRefPtrWillBeRawPtr<HTMLOptionElement> element, int index, ExceptionState& exceptionState)
79 HTMLOptionElement* newOption = element.get();
82 exceptionState.throwTypeError("The element provided was not an HTMLOptionElement.");
87 exceptionState.throwDOMException(IndexSizeError, "The index provided (" + String::number(index) + ") is less than -1.");
91 HTMLSelectElement& select = toHTMLSelectElement(ownerNode());
93 if (index == -1 || unsigned(index) >= length())
94 select.add(newOption, 0, exceptionState);
96 select.addBeforeOptionAtIndex(newOption, index, exceptionState);
98 ASSERT(!exceptionState.hadException());
101 void HTMLOptionsCollection::remove(int index)
103 toHTMLSelectElement(ownerNode()).remove(index);
106 int HTMLOptionsCollection::selectedIndex() const
108 return toHTMLSelectElement(ownerNode()).selectedIndex();
111 void HTMLOptionsCollection::setSelectedIndex(int index)
113 toHTMLSelectElement(ownerNode()).setSelectedIndex(index);
116 void HTMLOptionsCollection::setLength(unsigned length, ExceptionState& exceptionState)
118 toHTMLSelectElement(ownerNode()).setLength(length, exceptionState);
121 void HTMLOptionsCollection::namedGetter(const AtomicString& name, NodeListOrElement& returnValue)
123 WillBeHeapVector<RefPtrWillBeMember<Element>> namedItems;
124 this->namedItems(name, namedItems);
126 if (!namedItems.size())
129 if (namedItems.size() == 1) {
130 returnValue.setElement(namedItems.at(0));
134 // FIXME: The spec and Firefox do not return a NodeList. They always return the first matching Element.
135 returnValue.setNodeList(StaticElementList::adopt(namedItems));
138 bool HTMLOptionsCollection::anonymousIndexedSetter(unsigned index, PassRefPtrWillBeRawPtr<HTMLOptionElement> value, ExceptionState& exceptionState)
140 HTMLSelectElement& base = toHTMLSelectElement(ownerNode());
141 if (!value) { // undefined or null
145 base.setOption(index, value.get(), exceptionState);