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