Bumping gaia.json for 8 gaia revision(s) a=gaia-bump
[gecko.git] / dom / html / HTMLOptionsCollection.cpp
blob08f986b5b1f23cb29a574459e96c4d7502c74b80
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/dom/HTMLOptionsCollection.h"
8 #include "HTMLOptGroupElement.h"
9 #include "mozAutoDocUpdate.h"
10 #include "mozilla/dom/BindingUtils.h"
11 #include "mozilla/dom/Element.h"
12 #include "mozilla/dom/HTMLOptionElement.h"
13 #include "mozilla/dom/HTMLOptionsCollectionBinding.h"
14 #include "mozilla/dom/HTMLSelectElement.h"
15 #include "nsContentCreatorFunctions.h"
16 #include "nsError.h"
17 #include "nsFormSubmission.h"
18 #include "nsGkAtoms.h"
19 #include "nsIComboboxControlFrame.h"
20 #include "nsIDocument.h"
21 #include "nsIDOMHTMLOptGroupElement.h"
22 #include "nsIFormControlFrame.h"
23 #include "nsIForm.h"
24 #include "nsIFormProcessor.h"
25 #include "nsIListControlFrame.h"
26 #include "nsLayoutUtils.h"
27 #include "nsMappedAttributes.h"
28 #include "nsRuleData.h"
29 #include "nsServiceManagerUtils.h"
30 #include "nsStyleConsts.h"
31 #include "jsfriendapi.h"
33 namespace mozilla {
34 namespace dom {
36 HTMLOptionsCollection::HTMLOptionsCollection(HTMLSelectElement* aSelect)
38 // Do not maintain a reference counted reference. When
39 // the select goes away, it will let us know.
40 mSelect = aSelect;
43 HTMLOptionsCollection::~HTMLOptionsCollection()
45 DropReference();
48 void
49 HTMLOptionsCollection::DropReference()
51 // Drop our (non ref-counted) reference
52 mSelect = nullptr;
55 nsresult
56 HTMLOptionsCollection::GetOptionIndex(Element* aOption,
57 int32_t aStartIndex,
58 bool aForward,
59 int32_t* aIndex)
61 // NOTE: aIndex shouldn't be set if the returned value isn't NS_OK.
63 int32_t index;
65 // Make the common case fast
66 if (aStartIndex == 0 && aForward) {
67 index = mElements.IndexOf(aOption);
68 if (index == -1) {
69 return NS_ERROR_FAILURE;
72 *aIndex = index;
73 return NS_OK;
76 int32_t high = mElements.Length();
77 int32_t step = aForward ? 1 : -1;
79 for (index = aStartIndex; index < high && index > -1; index += step) {
80 if (mElements[index] == aOption) {
81 *aIndex = index;
82 return NS_OK;
86 return NS_ERROR_FAILURE;
90 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(HTMLOptionsCollection, mElements)
92 // nsISupports
94 // QueryInterface implementation for HTMLOptionsCollection
95 NS_INTERFACE_TABLE_HEAD(HTMLOptionsCollection)
96 NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
97 NS_INTERFACE_TABLE(HTMLOptionsCollection,
98 nsIHTMLCollection,
99 nsIDOMHTMLOptionsCollection,
100 nsIDOMHTMLCollection)
101 NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(HTMLOptionsCollection)
102 NS_INTERFACE_MAP_END
105 NS_IMPL_CYCLE_COLLECTING_ADDREF(HTMLOptionsCollection)
106 NS_IMPL_CYCLE_COLLECTING_RELEASE(HTMLOptionsCollection)
109 JSObject*
110 HTMLOptionsCollection::WrapObject(JSContext* aCx)
112 return HTMLOptionsCollectionBinding::Wrap(aCx, this);
115 NS_IMETHODIMP
116 HTMLOptionsCollection::GetLength(uint32_t* aLength)
118 *aLength = mElements.Length();
120 return NS_OK;
123 NS_IMETHODIMP
124 HTMLOptionsCollection::SetLength(uint32_t aLength)
126 if (!mSelect) {
127 return NS_ERROR_UNEXPECTED;
130 return mSelect->SetLength(aLength);
133 NS_IMETHODIMP
134 HTMLOptionsCollection::SetOption(uint32_t aIndex,
135 nsIDOMHTMLOptionElement* aOption)
137 if (!mSelect) {
138 return NS_OK;
141 // if the new option is null, just remove this option. Note that it's safe
142 // to pass a too-large aIndex in here.
143 if (!aOption) {
144 mSelect->Remove(aIndex);
146 // We're done.
147 return NS_OK;
150 nsresult rv = NS_OK;
152 uint32_t index = uint32_t(aIndex);
154 // Now we're going to be setting an option in our collection
155 if (index > mElements.Length()) {
156 // Fill our array with blank options up to (but not including, since we're
157 // about to change it) aIndex, for compat with other browsers.
158 rv = SetLength(index);
159 NS_ENSURE_SUCCESS(rv, rv);
162 NS_ASSERTION(index <= mElements.Length(), "SetLength lied");
164 nsCOMPtr<nsIDOMNode> ret;
165 if (index == mElements.Length()) {
166 nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aOption);
167 rv = mSelect->AppendChild(node, getter_AddRefs(ret));
168 } else {
169 // Find the option they're talking about and replace it
170 // hold a strong reference to follow COM rules.
171 nsRefPtr<HTMLOptionElement> refChild = ItemAsOption(index);
172 NS_ENSURE_TRUE(refChild, NS_ERROR_UNEXPECTED);
174 nsCOMPtr<nsINode> parent = refChild->GetParent();
175 if (parent) {
176 nsCOMPtr<nsINode> node = do_QueryInterface(aOption);
177 ErrorResult res;
178 parent->ReplaceChild(*node, *refChild, res);
179 rv = res.ErrorCode();
183 return rv;
186 int32_t
187 HTMLOptionsCollection::GetSelectedIndex(ErrorResult& aError)
189 if (!mSelect) {
190 aError.Throw(NS_ERROR_UNEXPECTED);
191 return 0;
194 int32_t selectedIndex;
195 aError = mSelect->GetSelectedIndex(&selectedIndex);
196 return selectedIndex;
199 NS_IMETHODIMP
200 HTMLOptionsCollection::GetSelectedIndex(int32_t* aSelectedIndex)
202 ErrorResult rv;
203 *aSelectedIndex = GetSelectedIndex(rv);
204 return rv.ErrorCode();
207 void
208 HTMLOptionsCollection::SetSelectedIndex(int32_t aSelectedIndex,
209 ErrorResult& aError)
211 if (!mSelect) {
212 aError.Throw(NS_ERROR_UNEXPECTED);
213 return;
216 aError = mSelect->SetSelectedIndex(aSelectedIndex);
219 NS_IMETHODIMP
220 HTMLOptionsCollection::SetSelectedIndex(int32_t aSelectedIndex)
222 ErrorResult rv;
223 SetSelectedIndex(aSelectedIndex, rv);
224 return rv.ErrorCode();
227 NS_IMETHODIMP
228 HTMLOptionsCollection::Item(uint32_t aIndex, nsIDOMNode** aReturn)
230 nsISupports* item = GetElementAt(aIndex);
231 if (!item) {
232 *aReturn = nullptr;
234 return NS_OK;
237 return CallQueryInterface(item, aReturn);
240 Element*
241 HTMLOptionsCollection::GetElementAt(uint32_t aIndex)
243 return ItemAsOption(aIndex);
246 HTMLOptionElement*
247 HTMLOptionsCollection::NamedGetter(const nsAString& aName, bool& aFound)
249 uint32_t count = mElements.Length();
250 for (uint32_t i = 0; i < count; i++) {
251 HTMLOptionElement* content = mElements.ElementAt(i);
252 if (content &&
253 (content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::name, aName,
254 eCaseMatters) ||
255 content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::id, aName,
256 eCaseMatters))) {
257 aFound = true;
258 return content;
262 aFound = false;
263 return nullptr;
266 nsINode*
267 HTMLOptionsCollection::GetParentObject()
269 return mSelect;
272 NS_IMETHODIMP
273 HTMLOptionsCollection::NamedItem(const nsAString& aName,
274 nsIDOMNode** aReturn)
276 NS_IF_ADDREF(*aReturn = GetNamedItem(aName));
278 return NS_OK;
281 void
282 HTMLOptionsCollection::GetSupportedNames(unsigned aFlags,
283 nsTArray<nsString>& aNames)
285 if (!(aFlags & JSITER_HIDDEN)) {
286 return;
289 nsAutoTArray<nsIAtom*, 8> atoms;
290 for (uint32_t i = 0; i < mElements.Length(); ++i) {
291 HTMLOptionElement* content = mElements.ElementAt(i);
292 if (content) {
293 // Note: HasName means the names is exposed on the document,
294 // which is false for options, so we don't check it here.
295 const nsAttrValue* val = content->GetParsedAttr(nsGkAtoms::name);
296 if (val && val->Type() == nsAttrValue::eAtom) {
297 nsIAtom* name = val->GetAtomValue();
298 if (!atoms.Contains(name)) {
299 atoms.AppendElement(name);
302 if (content->HasID()) {
303 nsIAtom* id = content->GetID();
304 if (!atoms.Contains(id)) {
305 atoms.AppendElement(id);
311 aNames.SetCapacity(atoms.Length());
312 for (uint32_t i = 0; i < atoms.Length(); ++i) {
313 aNames.AppendElement(nsDependentAtomString(atoms[i]));
317 NS_IMETHODIMP
318 HTMLOptionsCollection::GetSelect(nsIDOMHTMLSelectElement** aReturn)
320 NS_IF_ADDREF(*aReturn = mSelect);
321 return NS_OK;
324 NS_IMETHODIMP
325 HTMLOptionsCollection::Add(nsIDOMHTMLOptionElement* aOption,
326 nsIVariant* aBefore)
328 if (!aOption) {
329 return NS_ERROR_INVALID_ARG;
332 if (!mSelect) {
333 return NS_ERROR_NOT_INITIALIZED;
336 nsCOMPtr<nsIDOMHTMLElement> elem = do_QueryInterface(aOption);
337 return mSelect->Add(elem, aBefore);
340 void
341 HTMLOptionsCollection::Add(const HTMLOptionOrOptGroupElement& aElement,
342 const Nullable<HTMLElementOrLong>& aBefore,
343 ErrorResult& aError)
345 if (!mSelect) {
346 aError.Throw(NS_ERROR_NOT_INITIALIZED);
347 return;
350 mSelect->Add(aElement, aBefore, aError);
353 void
354 HTMLOptionsCollection::Remove(int32_t aIndex, ErrorResult& aError)
356 if (!mSelect) {
357 aError.Throw(NS_ERROR_UNEXPECTED);
358 return;
361 uint32_t len = 0;
362 mSelect->GetLength(&len);
363 if (aIndex < 0 || (uint32_t)aIndex >= len)
364 aIndex = 0;
366 aError = mSelect->Remove(aIndex);
369 NS_IMETHODIMP
370 HTMLOptionsCollection::Remove(int32_t aIndex)
372 ErrorResult rv;
373 Remove(aIndex, rv);
374 return rv.ErrorCode();
377 } // namespace dom
378 } // namespace mozilla