/src/mozilla-central/dom/html/HTMLOptionsCollection.cpp
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 | | |
7 | | #include "mozilla/dom/HTMLOptionsCollection.h" |
8 | | |
9 | | #include "HTMLOptGroupElement.h" |
10 | | #include "mozAutoDocUpdate.h" |
11 | | #include "mozilla/dom/BindingUtils.h" |
12 | | #include "mozilla/dom/Element.h" |
13 | | #include "mozilla/MappedDeclarations.h" |
14 | | #include "mozilla/dom/HTMLFormSubmission.h" |
15 | | #include "mozilla/dom/HTMLOptionElement.h" |
16 | | #include "mozilla/dom/HTMLOptionsCollectionBinding.h" |
17 | | #include "mozilla/dom/HTMLSelectElement.h" |
18 | | #include "nsContentCreatorFunctions.h" |
19 | | #include "nsError.h" |
20 | | #include "nsGkAtoms.h" |
21 | | #include "nsIComboboxControlFrame.h" |
22 | | #include "nsIDocument.h" |
23 | | #include "nsIFormControlFrame.h" |
24 | | #include "nsIForm.h" |
25 | | #include "nsIFormProcessor.h" |
26 | | #include "nsIListControlFrame.h" |
27 | | #include "nsLayoutUtils.h" |
28 | | #include "nsMappedAttributes.h" |
29 | | #include "nsServiceManagerUtils.h" |
30 | | #include "nsStyleConsts.h" |
31 | | #include "jsfriendapi.h" |
32 | | |
33 | | namespace mozilla { |
34 | | namespace dom { |
35 | | |
36 | | HTMLOptionsCollection::HTMLOptionsCollection(HTMLSelectElement* aSelect) |
37 | 0 | { |
38 | 0 | // Do not maintain a reference counted reference. When |
39 | 0 | // the select goes away, it will let us know. |
40 | 0 | mSelect = aSelect; |
41 | 0 | } |
42 | | |
43 | | HTMLOptionsCollection::~HTMLOptionsCollection() |
44 | 0 | { |
45 | 0 | DropReference(); |
46 | 0 | } |
47 | | |
48 | | void |
49 | | HTMLOptionsCollection::DropReference() |
50 | 0 | { |
51 | 0 | // Drop our (non ref-counted) reference |
52 | 0 | mSelect = nullptr; |
53 | 0 | } |
54 | | |
55 | | nsresult |
56 | | HTMLOptionsCollection::GetOptionIndex(Element* aOption, |
57 | | int32_t aStartIndex, |
58 | | bool aForward, |
59 | | int32_t* aIndex) |
60 | 0 | { |
61 | 0 | // NOTE: aIndex shouldn't be set if the returned value isn't NS_OK. |
62 | 0 |
|
63 | 0 | int32_t index; |
64 | 0 |
|
65 | 0 | // Make the common case fast |
66 | 0 | if (aStartIndex == 0 && aForward) { |
67 | 0 | index = mElements.IndexOf(aOption); |
68 | 0 | if (index == -1) { |
69 | 0 | return NS_ERROR_FAILURE; |
70 | 0 | } |
71 | 0 | |
72 | 0 | *aIndex = index; |
73 | 0 | return NS_OK; |
74 | 0 | } |
75 | 0 | |
76 | 0 | int32_t high = mElements.Length(); |
77 | 0 | int32_t step = aForward ? 1 : -1; |
78 | 0 |
|
79 | 0 | for (index = aStartIndex; index < high && index > -1; index += step) { |
80 | 0 | if (mElements[index] == aOption) { |
81 | 0 | *aIndex = index; |
82 | 0 | return NS_OK; |
83 | 0 | } |
84 | 0 | } |
85 | 0 |
|
86 | 0 | return NS_ERROR_FAILURE; |
87 | 0 | } |
88 | | |
89 | | |
90 | | NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(HTMLOptionsCollection, mElements) |
91 | | |
92 | | // nsISupports |
93 | | |
94 | | // QueryInterface implementation for HTMLOptionsCollection |
95 | 0 | NS_INTERFACE_TABLE_HEAD(HTMLOptionsCollection) |
96 | 0 | NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY |
97 | 0 | NS_INTERFACE_TABLE(HTMLOptionsCollection, |
98 | 0 | nsIHTMLCollection) |
99 | 0 | NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(HTMLOptionsCollection) |
100 | 0 | NS_INTERFACE_MAP_END |
101 | | |
102 | | NS_IMPL_CYCLE_COLLECTING_ADDREF(HTMLOptionsCollection) |
103 | | NS_IMPL_CYCLE_COLLECTING_RELEASE(HTMLOptionsCollection) |
104 | | |
105 | | JSObject* |
106 | | HTMLOptionsCollection::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
107 | 0 | { |
108 | 0 | return HTMLOptionsCollection_Binding::Wrap(aCx, this, aGivenProto); |
109 | 0 | } |
110 | | |
111 | | uint32_t |
112 | | HTMLOptionsCollection::Length() |
113 | 0 | { |
114 | 0 | return mElements.Length(); |
115 | 0 | } |
116 | | |
117 | | void |
118 | | HTMLOptionsCollection::SetLength(uint32_t aLength, ErrorResult& aError) |
119 | 0 | { |
120 | 0 | if (!mSelect) { |
121 | 0 | aError.Throw(NS_ERROR_UNEXPECTED); |
122 | 0 | return; |
123 | 0 | } |
124 | 0 | |
125 | 0 | mSelect->SetLength(aLength, aError); |
126 | 0 | } |
127 | | |
128 | | void |
129 | | HTMLOptionsCollection::IndexedSetter(uint32_t aIndex, |
130 | | HTMLOptionElement* aOption, |
131 | | ErrorResult& aError) |
132 | 0 | { |
133 | 0 | if (!mSelect) { |
134 | 0 | return; |
135 | 0 | } |
136 | 0 | |
137 | 0 | // if the new option is null, just remove this option. Note that it's safe |
138 | 0 | // to pass a too-large aIndex in here. |
139 | 0 | if (!aOption) { |
140 | 0 | mSelect->Remove(aIndex); |
141 | 0 |
|
142 | 0 | // We're done. |
143 | 0 | return; |
144 | 0 | } |
145 | 0 | |
146 | 0 | // Now we're going to be setting an option in our collection |
147 | 0 | if (aIndex > mElements.Length()) { |
148 | 0 | // Fill our array with blank options up to (but not including, since we're |
149 | 0 | // about to change it) aIndex, for compat with other browsers. |
150 | 0 | SetLength(aIndex, aError); |
151 | 0 | ENSURE_SUCCESS_VOID(aError); |
152 | 0 | } |
153 | 0 |
|
154 | 0 | NS_ASSERTION(aIndex <= mElements.Length(), "SetLength lied"); |
155 | 0 |
|
156 | 0 | if (aIndex == mElements.Length()) { |
157 | 0 | mSelect->AppendChild(*aOption, aError); |
158 | 0 | return; |
159 | 0 | } |
160 | 0 | |
161 | 0 | // Find the option they're talking about and replace it |
162 | 0 | // hold a strong reference to follow COM rules. |
163 | 0 | RefPtr<HTMLOptionElement> refChild = ItemAsOption(aIndex); |
164 | 0 | if (!refChild) { |
165 | 0 | aError.Throw(NS_ERROR_UNEXPECTED); |
166 | 0 | return; |
167 | 0 | } |
168 | 0 | |
169 | 0 | nsCOMPtr<nsINode> parent = refChild->GetParent(); |
170 | 0 | if (!parent) { |
171 | 0 | return; |
172 | 0 | } |
173 | 0 | |
174 | 0 | parent->ReplaceChild(*aOption, *refChild, aError); |
175 | 0 | } |
176 | | |
177 | | int32_t |
178 | | HTMLOptionsCollection::GetSelectedIndex(ErrorResult& aError) |
179 | 0 | { |
180 | 0 | if (!mSelect) { |
181 | 0 | aError.Throw(NS_ERROR_UNEXPECTED); |
182 | 0 | return 0; |
183 | 0 | } |
184 | 0 | |
185 | 0 | return mSelect->SelectedIndex(); |
186 | 0 | } |
187 | | |
188 | | void |
189 | | HTMLOptionsCollection::SetSelectedIndex(int32_t aSelectedIndex, |
190 | | ErrorResult& aError) |
191 | 0 | { |
192 | 0 | if (!mSelect) { |
193 | 0 | aError.Throw(NS_ERROR_UNEXPECTED); |
194 | 0 | return; |
195 | 0 | } |
196 | 0 | |
197 | 0 | mSelect->SetSelectedIndex(aSelectedIndex, aError); |
198 | 0 | } |
199 | | |
200 | | Element* |
201 | | HTMLOptionsCollection::GetElementAt(uint32_t aIndex) |
202 | 0 | { |
203 | 0 | return ItemAsOption(aIndex); |
204 | 0 | } |
205 | | |
206 | | HTMLOptionElement* |
207 | | HTMLOptionsCollection::NamedGetter(const nsAString& aName, bool& aFound) |
208 | 0 | { |
209 | 0 | uint32_t count = mElements.Length(); |
210 | 0 | for (uint32_t i = 0; i < count; i++) { |
211 | 0 | HTMLOptionElement* content = mElements.ElementAt(i); |
212 | 0 | if (content && |
213 | 0 | (content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::name, aName, |
214 | 0 | eCaseMatters) || |
215 | 0 | content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::id, aName, |
216 | 0 | eCaseMatters))) { |
217 | 0 | aFound = true; |
218 | 0 | return content; |
219 | 0 | } |
220 | 0 | } |
221 | 0 |
|
222 | 0 | aFound = false; |
223 | 0 | return nullptr; |
224 | 0 | } |
225 | | |
226 | | nsINode* |
227 | | HTMLOptionsCollection::GetParentObject() |
228 | 0 | { |
229 | 0 | return mSelect; |
230 | 0 | } |
231 | | |
232 | | DocGroup* |
233 | | HTMLOptionsCollection::GetDocGroup() const |
234 | 0 | { |
235 | 0 | return mSelect ? mSelect->GetDocGroup() : nullptr; |
236 | 0 | } |
237 | | |
238 | | void |
239 | | HTMLOptionsCollection::GetSupportedNames(nsTArray<nsString>& aNames) |
240 | 0 | { |
241 | 0 | AutoTArray<nsAtom*, 8> atoms; |
242 | 0 | for (uint32_t i = 0; i < mElements.Length(); ++i) { |
243 | 0 | HTMLOptionElement* content = mElements.ElementAt(i); |
244 | 0 | if (content) { |
245 | 0 | // Note: HasName means the names is exposed on the document, |
246 | 0 | // which is false for options, so we don't check it here. |
247 | 0 | const nsAttrValue* val = content->GetParsedAttr(nsGkAtoms::name); |
248 | 0 | if (val && val->Type() == nsAttrValue::eAtom) { |
249 | 0 | nsAtom* name = val->GetAtomValue(); |
250 | 0 | if (!atoms.Contains(name)) { |
251 | 0 | atoms.AppendElement(name); |
252 | 0 | } |
253 | 0 | } |
254 | 0 | if (content->HasID()) { |
255 | 0 | nsAtom* id = content->GetID(); |
256 | 0 | if (!atoms.Contains(id)) { |
257 | 0 | atoms.AppendElement(id); |
258 | 0 | } |
259 | 0 | } |
260 | 0 | } |
261 | 0 | } |
262 | 0 |
|
263 | 0 | uint32_t atomsLen = atoms.Length(); |
264 | 0 | nsString* names = aNames.AppendElements(atomsLen); |
265 | 0 | for (uint32_t i = 0; i < atomsLen; ++i) { |
266 | 0 | atoms[i]->ToString(names[i]); |
267 | 0 | } |
268 | 0 | } |
269 | | |
270 | | void |
271 | | HTMLOptionsCollection::Add(const HTMLOptionOrOptGroupElement& aElement, |
272 | | const Nullable<HTMLElementOrLong>& aBefore, |
273 | | ErrorResult& aError) |
274 | 0 | { |
275 | 0 | if (!mSelect) { |
276 | 0 | aError.Throw(NS_ERROR_NOT_INITIALIZED); |
277 | 0 | return; |
278 | 0 | } |
279 | 0 | |
280 | 0 | mSelect->Add(aElement, aBefore, aError); |
281 | 0 | } |
282 | | |
283 | | void |
284 | | HTMLOptionsCollection::Remove(int32_t aIndex, ErrorResult& aError) |
285 | 0 | { |
286 | 0 | if (!mSelect) { |
287 | 0 | aError.Throw(NS_ERROR_UNEXPECTED); |
288 | 0 | return; |
289 | 0 | } |
290 | 0 | |
291 | 0 | uint32_t len = mSelect->Length(); |
292 | 0 | if (aIndex < 0 || (uint32_t)aIndex >= len) |
293 | 0 | aIndex = 0; |
294 | 0 |
|
295 | 0 | mSelect->Remove(aIndex); |
296 | 0 | } |
297 | | |
298 | | } // namespace dom |
299 | | } // namespace mozilla |