1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_dom_HTMLOptionsCollection_h
7 #define mozilla_dom_HTMLOptionsCollection_h
9 #include "mozilla/Attributes.h"
10 #include "nsIHTMLCollection.h"
11 #include "nsWrapperCache.h"
13 #include "mozilla/dom/HTMLOptionElement.h"
16 #include "nsGenericHTMLElement.h"
25 class HTMLElementOrLong
;
26 class HTMLOptionElementOrHTMLOptGroupElement
;
27 class HTMLSelectElement
;
30 * The collection of options in the select (what you get back when you do
31 * select.options in DOM)
33 class HTMLOptionsCollection final
: public nsIHTMLCollection
,
34 public nsWrapperCache
{
35 typedef HTMLOptionElementOrHTMLOptGroupElement HTMLOptionOrOptGroupElement
;
38 explicit HTMLOptionsCollection(HTMLSelectElement
* aSelect
);
40 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
43 using nsWrapperCache::GetWrapper
;
44 using nsWrapperCache::GetWrapperPreserveColor
;
45 using nsWrapperCache::PreserveWrapper
;
46 virtual JSObject
* WrapObject(JSContext
* aCx
,
47 JS::Handle
<JSObject
*> aGivenProto
) override
;
50 virtual ~HTMLOptionsCollection() = default;
52 virtual JSObject
* GetWrapperPreserveColorInternal() override
{
53 return nsWrapperCache::GetWrapperPreserveColor();
55 virtual void PreserveWrapperInternal(
56 nsISupports
* aScriptObjectHolder
) override
{
57 nsWrapperCache::PreserveWrapper(aScriptObjectHolder
);
61 virtual uint32_t Length() override
;
62 virtual Element
* GetElementAt(uint32_t aIndex
) override
;
63 virtual nsINode
* GetParentObject() override
;
64 DocGroup
* GetDocGroup() const;
66 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS_AMBIGUOUS(HTMLOptionsCollection
,
69 // Helpers for HTMLSelectElement
72 * @param aOption the option to insert
73 * @param aIndex the index to insert at
75 void InsertOptionAt(mozilla::dom::HTMLOptionElement
* aOption
,
77 mElements
.InsertElementAt(aIndex
, aOption
);
82 * @param aIndex the index of the option to remove
84 void RemoveOptionAt(uint32_t aIndex
) { mElements
.RemoveElementAt(aIndex
); }
87 * Get the option at the index
88 * @param aIndex the index
89 * @param aReturn the option returned [OUT]
91 mozilla::dom::HTMLOptionElement
* ItemAsOption(uint32_t aIndex
) {
92 return mElements
.SafeElementAt(aIndex
, nullptr);
96 * Clears out all options
98 void Clear() { mElements
.Clear(); }
101 * Append an option to end of array
103 void AppendOption(mozilla::dom::HTMLOptionElement
* aOption
) {
104 mElements
.AppendElement(aOption
);
108 * Finds the index of a given option element.
109 * If the option isn't part of the collection, return NS_ERROR_FAILURE
110 * without setting aIndex.
112 * @param aOption the option to get the index of
113 * @param aStartIndex the index to start looking at
114 * @param aForward TRUE to look forward, FALSE to look backward
115 * @return the option index
117 nsresult
GetOptionIndex(Element
* aOption
, int32_t aStartIndex
, bool aForward
,
120 HTMLOptionElement
* GetNamedItem(const nsAString
& aName
) {
122 return NamedGetter(aName
, dummy
);
124 HTMLOptionElement
* NamedGetter(const nsAString
& aName
, bool& aFound
);
125 virtual Element
* GetFirstNamedElement(const nsAString
& aName
,
126 bool& aFound
) override
{
127 return NamedGetter(aName
, aFound
);
129 void Add(const HTMLOptionOrOptGroupElement
& aElement
,
130 const Nullable
<HTMLElementOrLong
>& aBefore
, ErrorResult
& aError
);
131 void Remove(int32_t aIndex
);
132 int32_t SelectedIndex();
133 void SetSelectedIndex(int32_t aSelectedIndex
);
134 void IndexedSetter(uint32_t aIndex
, HTMLOptionElement
* aOption
,
135 ErrorResult
& aError
);
136 virtual void GetSupportedNames(nsTArray
<nsString
>& aNames
) override
;
137 void SetLength(uint32_t aLength
, ErrorResult
& aError
);
140 /** The list of options (holds strong references). This is infallible, so
141 * various members such as InsertOptionAt are also infallible. */
142 nsTArray
<RefPtr
<mozilla::dom::HTMLOptionElement
> > mElements
;
143 /** The select element that contains this array */
144 RefPtr
<HTMLSelectElement
> mSelect
;
148 } // namespace mozilla
150 #endif // mozilla_dom_HTMLOptionsCollection_h