darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 1 | /* |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 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) |
achristensen@apple.com | 760090a | 2016-08-22 15:51:58 +0000 | [diff] [blame] | 5 | * Copyright (C) 2004-2016 Apple Inc. All rights reserved. |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 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 |
ddkilzer | c8eccec | 2007-09-26 02:29:57 +0000 | [diff] [blame] | 19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 20 | * Boston, MA 02110-1301, USA. |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
achristensen@apple.com | 760090a | 2016-08-22 15:51:58 +0000 | [diff] [blame] | 24 | #pragma once |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 25 | |
cdumez@apple.com | 4f3090f | 2015-08-16 18:58:48 +0000 | [diff] [blame] | 26 | #include "CachedHTMLCollection.h" |
darin@apple.com | 01d7468 | 2016-04-12 04:15:16 +0000 | [diff] [blame] | 27 | #include "HTMLOptionElement.h" |
akling@apple.com | f954a07 | 2013-11-04 06:54:12 +0000 | [diff] [blame] | 28 | #include "HTMLSelectElement.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 29 | |
| 30 | namespace WebCore { |
| 31 | |
cdumez@apple.com | 4f3090f | 2015-08-16 18:58:48 +0000 | [diff] [blame] | 32 | class HTMLOptionsCollection final : public CachedHTMLCollection<HTMLOptionsCollection, CollectionTypeTraits<SelectOptions>::traversalType> { |
commit-queue@webkit.org | 105372d | 2022-03-23 04:25:54 +0000 | [diff] [blame] | 33 | WTF_MAKE_ISO_ALLOCATED_EXPORT(HTMLOptionsCollection, WEBCORE_EXPORT); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 34 | public: |
weinig@apple.com | 6b3f5c7 | 2017-06-04 18:00:01 +0000 | [diff] [blame] | 35 | using Base = CachedHTMLCollection<HTMLOptionsCollection, CollectionTypeTraits<SelectOptions>::traversalType>; |
| 36 | |
akling@apple.com | 689f761 | 2014-12-14 08:21:05 +0000 | [diff] [blame] | 37 | static Ref<HTMLOptionsCollection> create(HTMLSelectElement&, CollectionType); |
akling@apple.com | f954a07 | 2013-11-04 06:54:12 +0000 | [diff] [blame] | 38 | |
cdumez@apple.com | 72754ba | 2014-09-23 22:03:15 +0000 | [diff] [blame] | 39 | HTMLSelectElement& selectElement() { return downcast<HTMLSelectElement>(ownerNode()); } |
| 40 | const HTMLSelectElement& selectElement() const { return downcast<HTMLSelectElement>(ownerNode()); } |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 41 | |
darin@apple.com | 71c0ce7 | 2016-04-12 05:49:45 +0000 | [diff] [blame] | 42 | HTMLOptionElement* item(unsigned offset) const final; |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 43 | HTMLOptionElement* namedItem(const AtomString& name) const final; |
weinig@apple.com | 6b3f5c7 | 2017-06-04 18:00:01 +0000 | [diff] [blame] | 44 | |
| 45 | ExceptionOr<void> setItem(unsigned index, HTMLOptionElement*); |
| 46 | |
commit-queue@webkit.org | 79445f2 | 2021-10-13 01:40:07 +0000 | [diff] [blame] | 47 | using OptionOrOptGroupElement = std::variant<RefPtr<HTMLOptionElement>, RefPtr<HTMLOptGroupElement>>; |
| 48 | using HTMLElementOrInt = std::variant<RefPtr<HTMLElement>, int>; |
darin@apple.com | a4ddc78 | 2021-05-30 16:11:40 +0000 | [diff] [blame] | 49 | WEBCORE_EXPORT ExceptionOr<void> add(const OptionOrOptGroupElement&, const std::optional<HTMLElementOrInt>& before); |
achristensen@apple.com | 760090a | 2016-08-22 15:51:58 +0000 | [diff] [blame] | 50 | WEBCORE_EXPORT void remove(int index); |
ddkilzer | 20e0d1b | 2006-07-11 03:16:41 +0000 | [diff] [blame] | 51 | |
achristensen@apple.com | 760090a | 2016-08-22 15:51:58 +0000 | [diff] [blame] | 52 | WEBCORE_EXPORT int selectedIndex() const; |
| 53 | WEBCORE_EXPORT void setSelectedIndex(int); |
ddkilzer | 20e0d1b | 2006-07-11 03:16:41 +0000 | [diff] [blame] | 54 | |
darin@apple.com | 66d4118 | 2016-10-29 02:32:20 +0000 | [diff] [blame] | 55 | WEBCORE_EXPORT ExceptionOr<void> setLength(unsigned); |
darin@apple.com | 642f500 | 2008-06-07 22:51:37 +0000 | [diff] [blame] | 56 | |
cdumez@apple.com | 4f3090f | 2015-08-16 18:58:48 +0000 | [diff] [blame] | 57 | // For CachedHTMLCollection. |
| 58 | bool elementMatches(Element&) const; |
| 59 | |
darin@apple.com | 642f500 | 2008-06-07 22:51:37 +0000 | [diff] [blame] | 60 | private: |
akling@apple.com | f954a07 | 2013-11-04 06:54:12 +0000 | [diff] [blame] | 61 | explicit HTMLOptionsCollection(HTMLSelectElement&); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
cdumez@apple.com | e1118d7 | 2015-10-20 18:48:26 +0000 | [diff] [blame] | 64 | inline HTMLOptionElement* HTMLOptionsCollection::item(unsigned offset) const |
| 65 | { |
weinig@apple.com | 6b3f5c7 | 2017-06-04 18:00:01 +0000 | [diff] [blame] | 66 | return downcast<HTMLOptionElement>(Base::item(offset)); |
cdumez@apple.com | e1118d7 | 2015-10-20 18:48:26 +0000 | [diff] [blame] | 67 | } |
| 68 | |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 69 | inline HTMLOptionElement* HTMLOptionsCollection::namedItem(const AtomString& name) const |
cdumez@apple.com | e1118d7 | 2015-10-20 18:48:26 +0000 | [diff] [blame] | 70 | { |
weinig@apple.com | 6b3f5c7 | 2017-06-04 18:00:01 +0000 | [diff] [blame] | 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); |
cdumez@apple.com | e1118d7 | 2015-10-20 18:48:26 +0000 | [diff] [blame] | 77 | } |
| 78 | |
cdumez@apple.com | 4f3090f | 2015-08-16 18:58:48 +0000 | [diff] [blame] | 79 | inline bool HTMLOptionsCollection::elementMatches(Element& element) const |
| 80 | { |
cdumez@apple.com | 9325558 | 2016-10-12 00:25:55 +0000 | [diff] [blame] | 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(); |
cdumez@apple.com | 4f3090f | 2015-08-16 18:58:48 +0000 | [diff] [blame] | 89 | } |
| 90 | |
cdumez@apple.com | 3f8bde1 | 2014-11-09 17:21:51 +0000 | [diff] [blame] | 91 | } // namespace WebCore |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 92 | |
cdumez@apple.com | 3f8bde1 | 2014-11-09 17:21:51 +0000 | [diff] [blame] | 93 | SPECIALIZE_TYPE_TRAITS_HTMLCOLLECTION(HTMLOptionsCollection, SelectOptions) |