Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLOptionsCollection.cpp
1 /*
2  * Copyright (C) 2006, 2011, 2012 Apple Computer, Inc.
3  * Copyright (C) 2014 Samsung Electronics. All rights reserved.
4  *
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.
9  *
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.
14  *
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.
19  *
20  */
21
22 #include "config.h"
23 #include "core/html/HTMLOptionsCollection.h"
24
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"
32
33 namespace blink {
34
35 HTMLOptionsCollection::HTMLOptionsCollection(ContainerNode& select)
36     : HTMLCollection(select, SelectOptions, DoesNotOverrideItemAfter)
37 {
38     ASSERT(isHTMLSelectElement(select));
39 }
40
41 void HTMLOptionsCollection::supportedPropertyNames(Vector<String>& names)
42 {
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);
51         ASSERT(element);
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);
57         }
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);
63         }
64     }
65 }
66
67 PassRefPtrWillBeRawPtr<HTMLOptionsCollection> HTMLOptionsCollection::create(ContainerNode& select, CollectionType)
68 {
69     return adoptRefWillBeNoop(new HTMLOptionsCollection(select));
70 }
71
72 void HTMLOptionsCollection::add(PassRefPtrWillBeRawPtr<HTMLOptionElement> element, ExceptionState& exceptionState)
73 {
74     add(element, length(), exceptionState);
75 }
76
77 void HTMLOptionsCollection::add(PassRefPtrWillBeRawPtr<HTMLOptionElement> element, int index, ExceptionState& exceptionState)
78 {
79     HTMLOptionElement* newOption = element.get();
80
81     if (!newOption) {
82         exceptionState.throwTypeError("The element provided was not an HTMLOptionElement.");
83         return;
84     }
85
86     if (index < -1) {
87         exceptionState.throwDOMException(IndexSizeError, "The index provided (" + String::number(index) + ") is less than -1.");
88         return;
89     }
90
91     HTMLSelectElement& select = toHTMLSelectElement(ownerNode());
92
93     if (index == -1 || unsigned(index) >= length())
94         select.add(newOption, 0, exceptionState);
95     else
96         select.addBeforeOptionAtIndex(newOption, index, exceptionState);
97
98     ASSERT(!exceptionState.hadException());
99 }
100
101 void HTMLOptionsCollection::remove(int index)
102 {
103     toHTMLSelectElement(ownerNode()).remove(index);
104 }
105
106 int HTMLOptionsCollection::selectedIndex() const
107 {
108     return toHTMLSelectElement(ownerNode()).selectedIndex();
109 }
110
111 void HTMLOptionsCollection::setSelectedIndex(int index)
112 {
113     toHTMLSelectElement(ownerNode()).setSelectedIndex(index);
114 }
115
116 void HTMLOptionsCollection::setLength(unsigned length, ExceptionState& exceptionState)
117 {
118     toHTMLSelectElement(ownerNode()).setLength(length, exceptionState);
119 }
120
121 void HTMLOptionsCollection::namedGetter(const AtomicString& name, NodeListOrElement& returnValue)
122 {
123     WillBeHeapVector<RefPtrWillBeMember<Element>> namedItems;
124     this->namedItems(name, namedItems);
125
126     if (!namedItems.size())
127         return;
128
129     if (namedItems.size() == 1) {
130         returnValue.setElement(namedItems.at(0));
131         return;
132     }
133
134     // FIXME: The spec and Firefox do not return a NodeList. They always return the first matching Element.
135     returnValue.setNodeList(StaticElementList::adopt(namedItems));
136 }
137
138 bool HTMLOptionsCollection::anonymousIndexedSetter(unsigned index, PassRefPtrWillBeRawPtr<HTMLOptionElement> value, ExceptionState& exceptionState)
139 {
140     HTMLSelectElement& base = toHTMLSelectElement(ownerNode());
141     if (!value) { // undefined or null
142         base.remove(index);
143         return true;
144     }
145     base.setOption(index, value.get(), exceptionState);
146     return true;
147 }
148
149 } //namespace
150