Line data Source code
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
8 :
9 : #include "mozilla/Attributes.h"
10 : #include "nsIHTMLCollection.h"
11 : #include "nsIDOMHTMLOptionsCollection.h"
12 : #include "nsWrapperCache.h"
13 :
14 : #include "mozilla/dom/HTMLOptionElement.h"
15 : #include "mozilla/ErrorResult.h"
16 : #include "nsCOMPtr.h"
17 : #include "nsError.h"
18 : #include "nsGenericHTMLElement.h"
19 : #include "nsTArray.h"
20 :
21 : class nsIDOMHTMLOptionElement;
22 :
23 : namespace mozilla {
24 : namespace dom {
25 :
26 : class HTMLElementOrLong;
27 : class HTMLOptionElementOrHTMLOptGroupElement;
28 : class HTMLSelectElement;
29 :
30 : /**
31 : * The collection of options in the select (what you get back when you do
32 : * select.options in DOM)
33 : */
34 : class HTMLOptionsCollection final : public nsIHTMLCollection
35 : , public nsIDOMHTMLOptionsCollection
36 : , public nsWrapperCache
37 : {
38 : typedef HTMLOptionElementOrHTMLOptGroupElement HTMLOptionOrOptGroupElement;
39 : public:
40 : explicit HTMLOptionsCollection(HTMLSelectElement* aSelect);
41 :
42 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
43 :
44 : // nsWrapperCache
45 : using nsWrapperCache::GetWrapperPreserveColor;
46 : using nsWrapperCache::GetWrapper;
47 : using nsWrapperCache::PreserveWrapper;
48 : virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
49 : protected:
50 : virtual ~HTMLOptionsCollection();
51 :
52 0 : virtual JSObject* GetWrapperPreserveColorInternal() override
53 : {
54 0 : return nsWrapperCache::GetWrapperPreserveColor();
55 : }
56 0 : virtual void PreserveWrapperInternal(nsISupports* aScriptObjectHolder) override
57 : {
58 0 : nsWrapperCache::PreserveWrapper(aScriptObjectHolder);
59 0 : }
60 : public:
61 :
62 : // nsIDOMHTMLOptionsCollection interface
63 : NS_DECL_NSIDOMHTMLOPTIONSCOLLECTION
64 :
65 : // nsIDOMHTMLCollection interface, all its methods are defined in
66 : // nsIDOMHTMLOptionsCollection
67 :
68 : virtual Element* GetElementAt(uint32_t aIndex) override;
69 : virtual nsINode* GetParentObject() override;
70 :
71 0 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(HTMLOptionsCollection,
72 : nsIHTMLCollection)
73 :
74 : // Helpers for HTMLSelectElement
75 : /**
76 : * Insert an option
77 : * @param aOption the option to insert
78 : * @param aIndex the index to insert at
79 : */
80 0 : void InsertOptionAt(mozilla::dom::HTMLOptionElement* aOption, uint32_t aIndex)
81 : {
82 0 : mElements.InsertElementAt(aIndex, aOption);
83 0 : }
84 :
85 : /**
86 : * Remove an option
87 : * @param aIndex the index of the option to remove
88 : */
89 0 : void RemoveOptionAt(uint32_t aIndex)
90 : {
91 0 : mElements.RemoveElementAt(aIndex);
92 0 : }
93 :
94 : /**
95 : * Get the option at the index
96 : * @param aIndex the index
97 : * @param aReturn the option returned [OUT]
98 : */
99 0 : mozilla::dom::HTMLOptionElement* ItemAsOption(uint32_t aIndex)
100 : {
101 0 : return mElements.SafeElementAt(aIndex, nullptr);
102 : }
103 :
104 : /**
105 : * Clears out all options
106 : */
107 0 : void Clear()
108 : {
109 0 : mElements.Clear();
110 0 : }
111 :
112 : /**
113 : * Append an option to end of array
114 : */
115 0 : void AppendOption(mozilla::dom::HTMLOptionElement* aOption)
116 : {
117 0 : mElements.AppendElement(aOption);
118 0 : }
119 :
120 : /**
121 : * Drop the reference to the select. Called during select destruction.
122 : */
123 : void DropReference();
124 :
125 : /**
126 : * Finds the index of a given option element.
127 : * If the option isn't part of the collection, return NS_ERROR_FAILURE
128 : * without setting aIndex.
129 : *
130 : * @param aOption the option to get the index of
131 : * @param aStartIndex the index to start looking at
132 : * @param aForward TRUE to look forward, FALSE to look backward
133 : * @return the option index
134 : */
135 : nsresult GetOptionIndex(Element* aOption,
136 : int32_t aStartIndex, bool aForward,
137 : int32_t* aIndex);
138 :
139 0 : HTMLOptionElement* GetNamedItem(const nsAString& aName)
140 : {
141 : bool dummy;
142 0 : return NamedGetter(aName, dummy);
143 : }
144 : HTMLOptionElement* NamedGetter(const nsAString& aName, bool& aFound);
145 : virtual Element*
146 0 : GetFirstNamedElement(const nsAString& aName, bool& aFound) override
147 : {
148 0 : return NamedGetter(aName, aFound);
149 : }
150 :
151 : void Add(const HTMLOptionOrOptGroupElement& aElement,
152 : const Nullable<HTMLElementOrLong>& aBefore,
153 : ErrorResult& aError);
154 : void Remove(int32_t aIndex, ErrorResult& aError);
155 : int32_t GetSelectedIndex(ErrorResult& aError);
156 : void SetSelectedIndex(int32_t aSelectedIndex, ErrorResult& aError);
157 : void IndexedSetter(uint32_t aIndex, HTMLOptionElement* aOption,
158 : ErrorResult& aError);
159 : virtual void GetSupportedNames(nsTArray<nsString>& aNames) override;
160 :
161 : private:
162 : /** The list of options (holds strong references). This is infallible, so
163 : * various members such as InsertOptionAt are also infallible. */
164 : nsTArray<RefPtr<mozilla::dom::HTMLOptionElement> > mElements;
165 : /** The select element that contains this array */
166 : HTMLSelectElement* mSelect;
167 : };
168 :
169 : } // namespace dom
170 : } // namespace mozilla
171 :
172 : #endif // mozilla_dom_HTMLOptionsCollection_h
|