blob: 23603c01772dd0124e004de9875d6e12b29ef5e5 [file] [log] [blame]
darinb9481ed2006-03-20 02:57:59 +00001/*
darinb9481ed2006-03-20 02:57:59 +00002 * 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.com760090a2016-08-22 15:51:58 +00005 * Copyright (C) 2004-2016 Apple Inc. All rights reserved.
darinb9481ed2006-03-20 02:57:59 +00006 *
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
ddkilzerc8eccec2007-09-26 02:29:57 +000019 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
darinb9481ed2006-03-20 02:57:59 +000021 *
22 */
23
achristensen@apple.com760090a2016-08-22 15:51:58 +000024#pragma once
darinb9481ed2006-03-20 02:57:59 +000025
cdumez@apple.com4f3090f2015-08-16 18:58:48 +000026#include "CachedHTMLCollection.h"
darin@apple.com01d74682016-04-12 04:15:16 +000027#include "HTMLOptionElement.h"
akling@apple.comf954a072013-11-04 06:54:12 +000028#include "HTMLSelectElement.h"
darinb9481ed2006-03-20 02:57:59 +000029
30namespace WebCore {
31
cdumez@apple.com4f3090f2015-08-16 18:58:48 +000032class HTMLOptionsCollection final : public CachedHTMLCollection<HTMLOptionsCollection, CollectionTypeTraits<SelectOptions>::traversalType> {
commit-queue@webkit.org105372d2022-03-23 04:25:54 +000033 WTF_MAKE_ISO_ALLOCATED_EXPORT(HTMLOptionsCollection, WEBCORE_EXPORT);
darinb9481ed2006-03-20 02:57:59 +000034public:
weinig@apple.com6b3f5c72017-06-04 18:00:01 +000035 using Base = CachedHTMLCollection<HTMLOptionsCollection, CollectionTypeTraits<SelectOptions>::traversalType>;
36
akling@apple.com689f7612014-12-14 08:21:05 +000037 static Ref<HTMLOptionsCollection> create(HTMLSelectElement&, CollectionType);
akling@apple.comf954a072013-11-04 06:54:12 +000038
cdumez@apple.com72754ba2014-09-23 22:03:15 +000039 HTMLSelectElement& selectElement() { return downcast<HTMLSelectElement>(ownerNode()); }
40 const HTMLSelectElement& selectElement() const { return downcast<HTMLSelectElement>(ownerNode()); }
darinb9481ed2006-03-20 02:57:59 +000041
darin@apple.com71c0ce72016-04-12 05:49:45 +000042 HTMLOptionElement* item(unsigned offset) const final;
darin@apple.com0ce67df2019-06-17 01:48:13 +000043 HTMLOptionElement* namedItem(const AtomString& name) const final;
weinig@apple.com6b3f5c72017-06-04 18:00:01 +000044
45 ExceptionOr<void> setItem(unsigned index, HTMLOptionElement*);
46
commit-queue@webkit.org79445f22021-10-13 01:40:07 +000047 using OptionOrOptGroupElement = std::variant<RefPtr<HTMLOptionElement>, RefPtr<HTMLOptGroupElement>>;
48 using HTMLElementOrInt = std::variant<RefPtr<HTMLElement>, int>;
darin@apple.coma4ddc782021-05-30 16:11:40 +000049 WEBCORE_EXPORT ExceptionOr<void> add(const OptionOrOptGroupElement&, const std::optional<HTMLElementOrInt>& before);
achristensen@apple.com760090a2016-08-22 15:51:58 +000050 WEBCORE_EXPORT void remove(int index);
ddkilzer20e0d1b2006-07-11 03:16:41 +000051
achristensen@apple.com760090a2016-08-22 15:51:58 +000052 WEBCORE_EXPORT int selectedIndex() const;
53 WEBCORE_EXPORT void setSelectedIndex(int);
ddkilzer20e0d1b2006-07-11 03:16:41 +000054
darin@apple.com66d41182016-10-29 02:32:20 +000055 WEBCORE_EXPORT ExceptionOr<void> setLength(unsigned);
darin@apple.com642f5002008-06-07 22:51:37 +000056
cdumez@apple.com4f3090f2015-08-16 18:58:48 +000057 // For CachedHTMLCollection.
58 bool elementMatches(Element&) const;
59
darin@apple.com642f5002008-06-07 22:51:37 +000060private:
akling@apple.comf954a072013-11-04 06:54:12 +000061 explicit HTMLOptionsCollection(HTMLSelectElement&);
darinb9481ed2006-03-20 02:57:59 +000062};
63
cdumez@apple.come1118d72015-10-20 18:48:26 +000064inline HTMLOptionElement* HTMLOptionsCollection::item(unsigned offset) const
65{
weinig@apple.com6b3f5c72017-06-04 18:00:01 +000066 return downcast<HTMLOptionElement>(Base::item(offset));
cdumez@apple.come1118d72015-10-20 18:48:26 +000067}
68
darin@apple.com0ce67df2019-06-17 01:48:13 +000069inline HTMLOptionElement* HTMLOptionsCollection::namedItem(const AtomString& name) const
cdumez@apple.come1118d72015-10-20 18:48:26 +000070{
weinig@apple.com6b3f5c72017-06-04 18:00:01 +000071 return downcast<HTMLOptionElement>(Base::namedItem(name));
72}
73
74inline ExceptionOr<void> HTMLOptionsCollection::setItem(unsigned index, HTMLOptionElement* optionElement)
75{
76 return selectElement().setItem(index, optionElement);
cdumez@apple.come1118d72015-10-20 18:48:26 +000077}
78
cdumez@apple.com4f3090f2015-08-16 18:58:48 +000079inline bool HTMLOptionsCollection::elementMatches(Element& element) const
80{
cdumez@apple.com93255582016-10-12 00:25:55 +000081 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.com4f3090f2015-08-16 18:58:48 +000089}
90
cdumez@apple.com3f8bde12014-11-09 17:21:51 +000091} // namespace WebCore
darinb9481ed2006-03-20 02:57:59 +000092
cdumez@apple.com3f8bde12014-11-09 17:21:51 +000093SPECIALIZE_TYPE_TRAITS_HTMLCOLLECTION(HTMLOptionsCollection, SelectOptions)