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 "mozilla/dom/Document.h"
22 #include "nsIFormControlFrame.h"
23 #include "nsLayoutUtils.h"
24 #include "nsMappedAttributes.h"
25 #include "nsServiceManagerUtils.h"
26 #include "nsStyleConsts.h"
27 #include "jsfriendapi.h"
28
29 namespace mozilla::dom {
30
HTMLOptionsCollection(HTMLSelectElement * aSelect)31 HTMLOptionsCollection::HTMLOptionsCollection(HTMLSelectElement* aSelect)
32 : mSelect(aSelect) {}
33
GetOptionIndex(Element * aOption,int32_t aStartIndex,bool aForward,int32_t * aIndex)34 nsresult HTMLOptionsCollection::GetOptionIndex(Element* aOption,
35 int32_t aStartIndex,
36 bool aForward, int32_t* aIndex) {
37 // NOTE: aIndex shouldn't be set if the returned value isn't NS_OK.
38
39 int32_t index;
40
41 // Make the common case fast
42 if (aStartIndex == 0 && aForward) {
43 index = mElements.IndexOf(aOption);
44 if (index == -1) {
45 return NS_ERROR_FAILURE;
46 }
47
48 *aIndex = index;
49 return NS_OK;
50 }
51
52 int32_t high = mElements.Length();
53 int32_t step = aForward ? 1 : -1;
54
55 for (index = aStartIndex; index < high && index > -1; index += step) {
56 if (mElements[index] == aOption) {
57 *aIndex = index;
58 return NS_OK;
59 }
60 }
61
62 return NS_ERROR_FAILURE;
63 }
64
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(HTMLOptionsCollection,mElements,mSelect)65 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(HTMLOptionsCollection, mElements, mSelect)
66
67 // nsISupports
68
69 // QueryInterface implementation for HTMLOptionsCollection
70 NS_INTERFACE_TABLE_HEAD(HTMLOptionsCollection)
71 NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
72 NS_INTERFACE_TABLE(HTMLOptionsCollection, nsIHTMLCollection)
73 NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(HTMLOptionsCollection)
74 NS_INTERFACE_MAP_END
75
76 NS_IMPL_CYCLE_COLLECTING_ADDREF(HTMLOptionsCollection)
77 NS_IMPL_CYCLE_COLLECTING_RELEASE(HTMLOptionsCollection)
78
79 JSObject* HTMLOptionsCollection::WrapObject(JSContext* aCx,
80 JS::Handle<JSObject*> aGivenProto) {
81 return HTMLOptionsCollection_Binding::Wrap(aCx, this, aGivenProto);
82 }
83
Length()84 uint32_t HTMLOptionsCollection::Length() { return mElements.Length(); }
85
SetLength(uint32_t aLength,ErrorResult & aError)86 void HTMLOptionsCollection::SetLength(uint32_t aLength, ErrorResult& aError) {
87 mSelect->SetLength(aLength, aError);
88 }
89
IndexedSetter(uint32_t aIndex,HTMLOptionElement * aOption,ErrorResult & aError)90 void HTMLOptionsCollection::IndexedSetter(uint32_t aIndex,
91 HTMLOptionElement* aOption,
92 ErrorResult& aError) {
93 // if the new option is null, just remove this option. Note that it's safe
94 // to pass a too-large aIndex in here.
95 if (!aOption) {
96 mSelect->Remove(aIndex);
97
98 // We're done.
99 return;
100 }
101
102 // Now we're going to be setting an option in our collection
103 if (aIndex > mElements.Length()) {
104 // Fill our array with blank options up to (but not including, since we're
105 // about to change it) aIndex, for compat with other browsers.
106 SetLength(aIndex, aError);
107 ENSURE_SUCCESS_VOID(aError);
108 }
109
110 NS_ASSERTION(aIndex <= mElements.Length(), "SetLength lied");
111
112 if (aIndex == mElements.Length()) {
113 mSelect->AppendChild(*aOption, aError);
114 return;
115 }
116
117 // Find the option they're talking about and replace it
118 // hold a strong reference to follow COM rules.
119 RefPtr<HTMLOptionElement> refChild = ItemAsOption(aIndex);
120 if (!refChild) {
121 aError.Throw(NS_ERROR_UNEXPECTED);
122 return;
123 }
124
125 nsCOMPtr<nsINode> parent = refChild->GetParent();
126 if (!parent) {
127 return;
128 }
129
130 parent->ReplaceChild(*aOption, *refChild, aError);
131 }
132
SelectedIndex()133 int32_t HTMLOptionsCollection::SelectedIndex() {
134 return mSelect->SelectedIndex();
135 }
136
SetSelectedIndex(int32_t aSelectedIndex)137 void HTMLOptionsCollection::SetSelectedIndex(int32_t aSelectedIndex) {
138 mSelect->SetSelectedIndex(aSelectedIndex);
139 }
140
GetElementAt(uint32_t aIndex)141 Element* HTMLOptionsCollection::GetElementAt(uint32_t aIndex) {
142 return ItemAsOption(aIndex);
143 }
144
NamedGetter(const nsAString & aName,bool & aFound)145 HTMLOptionElement* HTMLOptionsCollection::NamedGetter(const nsAString& aName,
146 bool& aFound) {
147 uint32_t count = mElements.Length();
148 for (uint32_t i = 0; i < count; i++) {
149 HTMLOptionElement* content = mElements.ElementAt(i);
150 if (content && (content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::name,
151 aName, eCaseMatters) ||
152 content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::id,
153 aName, eCaseMatters))) {
154 aFound = true;
155 return content;
156 }
157 }
158
159 aFound = false;
160 return nullptr;
161 }
162
GetParentObject()163 nsINode* HTMLOptionsCollection::GetParentObject() { return mSelect; }
164
GetDocGroup() const165 DocGroup* HTMLOptionsCollection::GetDocGroup() const {
166 return mSelect ? mSelect->GetDocGroup() : nullptr;
167 }
168
GetSupportedNames(nsTArray<nsString> & aNames)169 void HTMLOptionsCollection::GetSupportedNames(nsTArray<nsString>& aNames) {
170 AutoTArray<nsAtom*, 8> atoms;
171 for (uint32_t i = 0; i < mElements.Length(); ++i) {
172 HTMLOptionElement* content = mElements.ElementAt(i);
173 if (content) {
174 // Note: HasName means the names is exposed on the document,
175 // which is false for options, so we don't check it here.
176 const nsAttrValue* val = content->GetParsedAttr(nsGkAtoms::name);
177 if (val && val->Type() == nsAttrValue::eAtom) {
178 nsAtom* name = val->GetAtomValue();
179 if (!atoms.Contains(name)) {
180 atoms.AppendElement(name);
181 }
182 }
183 if (content->HasID()) {
184 nsAtom* id = content->GetID();
185 if (!atoms.Contains(id)) {
186 atoms.AppendElement(id);
187 }
188 }
189 }
190 }
191
192 uint32_t atomsLen = atoms.Length();
193 nsString* names = aNames.AppendElements(atomsLen);
194 for (uint32_t i = 0; i < atomsLen; ++i) {
195 atoms[i]->ToString(names[i]);
196 }
197 }
198
Add(const HTMLOptionOrOptGroupElement & aElement,const Nullable<HTMLElementOrLong> & aBefore,ErrorResult & aError)199 void HTMLOptionsCollection::Add(const HTMLOptionOrOptGroupElement& aElement,
200 const Nullable<HTMLElementOrLong>& aBefore,
201 ErrorResult& aError) {
202 mSelect->Add(aElement, aBefore, aError);
203 }
204
Remove(int32_t aIndex)205 void HTMLOptionsCollection::Remove(int32_t aIndex) { mSelect->Remove(aIndex); }
206
207 } // namespace mozilla::dom
208