1 | /* |
2 | * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
3 | * (C) 1999 Antti Koivisto (koivisto@kde.org) |
4 | * (C) 2000 Dirk Mueller (mueller@kde.org) |
5 | * Copyright (C) 2004-2016 Apple Inc. All rights reserved. |
6 | * |
7 | * This library is free software; you can redistribute it and/or |
8 | * modify it under the terms of the GNU Library General Public |
9 | * License as published by the Free Software Foundation; either |
10 | * version 2 of the License, or (at your option) any later version. |
11 | * |
12 | * This library is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | * Library General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU Library General Public License |
18 | * along with this library; see the file COPYING.LIB. If not, write to |
19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | * Boston, MA 02110-1301, USA. |
21 | * |
22 | */ |
23 | |
24 | #pragma once |
25 | |
26 | #include "CachedHTMLCollection.h" |
27 | #include "HTMLOptionElement.h" |
28 | #include "HTMLSelectElement.h" |
29 | |
30 | namespace WebCore { |
31 | |
32 | class HTMLOptionsCollection final : public CachedHTMLCollection<HTMLOptionsCollection, CollectionTypeTraits<SelectOptions>::traversalType> { |
33 | WTF_MAKE_ISO_ALLOCATED(HTMLOptionsCollection); |
34 | public: |
35 | using Base = CachedHTMLCollection<HTMLOptionsCollection, CollectionTypeTraits<SelectOptions>::traversalType>; |
36 | |
37 | static Ref<HTMLOptionsCollection> create(HTMLSelectElement&, CollectionType); |
38 | |
39 | HTMLSelectElement& selectElement() { return downcast<HTMLSelectElement>(ownerNode()); } |
40 | const HTMLSelectElement& selectElement() const { return downcast<HTMLSelectElement>(ownerNode()); } |
41 | |
42 | HTMLOptionElement* item(unsigned offset) const final; |
43 | HTMLOptionElement* namedItem(const AtomicString& name) const final; |
44 | |
45 | ExceptionOr<void> setItem(unsigned index, HTMLOptionElement*); |
46 | |
47 | using OptionOrOptGroupElement = Variant<RefPtr<HTMLOptionElement>, RefPtr<HTMLOptGroupElement>>; |
48 | using HTMLElementOrInt = Variant<RefPtr<HTMLElement>, int>; |
49 | WEBCORE_EXPORT ExceptionOr<void> add(const OptionOrOptGroupElement&, const Optional<HTMLElementOrInt>& before); |
50 | WEBCORE_EXPORT void remove(int index); |
51 | |
52 | WEBCORE_EXPORT int selectedIndex() const; |
53 | WEBCORE_EXPORT void setSelectedIndex(int); |
54 | |
55 | WEBCORE_EXPORT ExceptionOr<void> setLength(unsigned); |
56 | |
57 | // For CachedHTMLCollection. |
58 | bool elementMatches(Element&) const; |
59 | |
60 | private: |
61 | explicit HTMLOptionsCollection(HTMLSelectElement&); |
62 | }; |
63 | |
64 | inline HTMLOptionElement* HTMLOptionsCollection::item(unsigned offset) const |
65 | { |
66 | return downcast<HTMLOptionElement>(Base::item(offset)); |
67 | } |
68 | |
69 | inline HTMLOptionElement* HTMLOptionsCollection::namedItem(const AtomicString& name) const |
70 | { |
71 | return downcast<HTMLOptionElement>(Base::namedItem(name)); |
72 | } |
73 | |
74 | inline ExceptionOr<void> HTMLOptionsCollection::setItem(unsigned index, HTMLOptionElement* optionElement) |
75 | { |
76 | return selectElement().setItem(index, optionElement); |
77 | } |
78 | |
79 | inline bool HTMLOptionsCollection::elementMatches(Element& element) const |
80 | { |
81 | if (!element.hasTagName(HTMLNames::optionTag)) |
82 | return false; |
83 | |
84 | if (element.parentNode() == &selectElement()) |
85 | return true; |
86 | |
87 | ASSERT(element.parentNode()); |
88 | return element.parentNode()->hasTagName(HTMLNames::optgroupTag) && element.parentNode()->parentNode() == &selectElement(); |
89 | } |
90 | |
91 | } // namespace WebCore |
92 | |
93 | SPECIALIZE_TYPE_TRAITS_HTMLCOLLECTION(HTMLOptionsCollection, SelectOptions) |
94 | |