Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / html / HTMLOptionsCollection.h
blobe4300c876d5328cd55c6d2c078cd1150a7ee4332
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"
14 #include "nsCOMPtr.h"
15 #include "nsError.h"
16 #include "nsGenericHTMLElement.h"
17 #include "nsTArray.h"
19 namespace mozilla {
20 class ErrorResult;
22 namespace dom {
24 class DocGroup;
25 class HTMLElementOrLong;
26 class HTMLOptionElementOrHTMLOptGroupElement;
27 class HTMLSelectElement;
29 /**
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;
37 public:
38 explicit HTMLOptionsCollection(HTMLSelectElement* aSelect);
40 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
42 // nsWrapperCache
43 using nsWrapperCache::GetWrapper;
44 using nsWrapperCache::GetWrapperPreserveColor;
45 using nsWrapperCache::PreserveWrapper;
46 virtual JSObject* WrapObject(JSContext* aCx,
47 JS::Handle<JSObject*> aGivenProto) override;
49 protected:
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);
60 public:
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,
67 nsIHTMLCollection)
69 // Helpers for HTMLSelectElement
70 /**
71 * Insert an option
72 * @param aOption the option to insert
73 * @param aIndex the index to insert at
75 void InsertOptionAt(mozilla::dom::HTMLOptionElement* aOption,
76 uint32_t aIndex) {
77 mElements.InsertElementAt(aIndex, aOption);
80 /**
81 * Remove an option
82 * @param aIndex the index of the option to remove
84 void RemoveOptionAt(uint32_t aIndex) { mElements.RemoveElementAt(aIndex); }
86 /**
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);
95 /**
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,
118 int32_t* aIndex);
120 HTMLOptionElement* GetNamedItem(const nsAString& aName) {
121 bool dummy;
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);
139 private:
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;
147 } // namespace dom
148 } // namespace mozilla
150 #endif // mozilla_dom_HTMLOptionsCollection_h