/src/mozilla-central/dom/html/HTMLSelectElement.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/HTMLSelectElement.h" |
8 | | |
9 | | #include "mozAutoDocUpdate.h" |
10 | | #include "mozilla/Attributes.h" |
11 | | #include "mozilla/BasicEvents.h" |
12 | | #include "mozilla/EventDispatcher.h" |
13 | | #include "mozilla/EventStates.h" |
14 | | #include "mozilla/dom/Element.h" |
15 | | #include "mozilla/dom/HTMLFormSubmission.h" |
16 | | #include "mozilla/dom/HTMLOptGroupElement.h" |
17 | | #include "mozilla/dom/HTMLOptionElement.h" |
18 | | #include "mozilla/dom/HTMLSelectElementBinding.h" |
19 | | #include "mozilla/dom/UnionTypes.h" |
20 | | #include "mozilla/MappedDeclarations.h" |
21 | | #include "nsContentCreatorFunctions.h" |
22 | | #include "nsContentList.h" |
23 | | #include "nsError.h" |
24 | | #include "nsGkAtoms.h" |
25 | | #include "nsIComboboxControlFrame.h" |
26 | | #include "nsIDocument.h" |
27 | | #include "nsIFormControlFrame.h" |
28 | | #include "nsIForm.h" |
29 | | #include "nsIFormProcessor.h" |
30 | | #include "nsIFrame.h" |
31 | | #include "nsIListControlFrame.h" |
32 | | #include "nsISelectControlFrame.h" |
33 | | #include "nsLayoutUtils.h" |
34 | | #include "nsMappedAttributes.h" |
35 | | #include "mozilla/PresState.h" |
36 | | #include "nsServiceManagerUtils.h" |
37 | | #include "nsStyleConsts.h" |
38 | | #include "nsTextNode.h" |
39 | | |
40 | | NS_IMPL_NS_NEW_HTML_ELEMENT_CHECK_PARSER(Select) |
41 | | |
42 | | namespace mozilla { |
43 | | namespace dom { |
44 | | |
45 | | //---------------------------------------------------------------------- |
46 | | // |
47 | | // SafeOptionListMutation |
48 | | // |
49 | | |
50 | | SafeOptionListMutation::SafeOptionListMutation(nsIContent* aSelect, |
51 | | nsIContent* aParent, |
52 | | nsIContent* aKid, |
53 | | uint32_t aIndex, |
54 | | bool aNotify) |
55 | | : mSelect(HTMLSelectElement::FromNodeOrNull(aSelect)) |
56 | | , mTopLevelMutation(false) |
57 | | , mNeedsRebuild(false) |
58 | | , mNotify(aNotify) |
59 | | , mInitialSelectedIndex(-1) |
60 | 0 | { |
61 | 0 | if (mSelect) { |
62 | 0 | mInitialSelectedIndex = mSelect->SelectedIndex(); |
63 | 0 | mTopLevelMutation = !mSelect->mMutating; |
64 | 0 | if (mTopLevelMutation) { |
65 | 0 | mSelect->mMutating = true; |
66 | 0 | } else { |
67 | 0 | // This is very unfortunate, but to handle mutation events properly, |
68 | 0 | // option list must be up-to-date before inserting or removing options. |
69 | 0 | // Fortunately this is called only if mutation event listener |
70 | 0 | // adds or removes options. |
71 | 0 | mSelect->RebuildOptionsArray(mNotify); |
72 | 0 | } |
73 | 0 | nsresult rv; |
74 | 0 | if (aKid) { |
75 | 0 | rv = mSelect->WillAddOptions(aKid, aParent, aIndex, mNotify); |
76 | 0 | } else { |
77 | 0 | rv = mSelect->WillRemoveOptions(aParent, aIndex, mNotify); |
78 | 0 | } |
79 | 0 | mNeedsRebuild = NS_FAILED(rv); |
80 | 0 | } |
81 | 0 | } |
82 | | |
83 | | SafeOptionListMutation::~SafeOptionListMutation() |
84 | 0 | { |
85 | 0 | if (mSelect) { |
86 | 0 | if (mNeedsRebuild || (mTopLevelMutation && mGuard.Mutated(1))) { |
87 | 0 | mSelect->RebuildOptionsArray(true); |
88 | 0 | } |
89 | 0 | if (mTopLevelMutation) { |
90 | 0 | mSelect->mMutating = false; |
91 | 0 | } |
92 | 0 | if (mSelect->SelectedIndex() != mInitialSelectedIndex) { |
93 | 0 | // We must have triggered the SelectSomething() codepath, which can cause |
94 | 0 | // our validity to change. Unfortunately, our attempt to update validity |
95 | 0 | // in that case may not have worked correctly, because we actually call it |
96 | 0 | // before we have inserted the new <option>s into the DOM! Go ahead and |
97 | 0 | // update validity here as needed, because by now we know our <option>s |
98 | 0 | // are where they should be. |
99 | 0 | mSelect->UpdateValueMissingValidityState(); |
100 | 0 | mSelect->UpdateState(mNotify); |
101 | 0 | } |
102 | | #ifdef DEBUG |
103 | | mSelect->VerifyOptionsArray(); |
104 | | #endif |
105 | | } |
106 | 0 | } |
107 | | |
108 | | //---------------------------------------------------------------------- |
109 | | // |
110 | | // HTMLSelectElement |
111 | | // |
112 | | |
113 | | // construction, destruction |
114 | | |
115 | | |
116 | | HTMLSelectElement::HTMLSelectElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo, |
117 | | FromParser aFromParser) |
118 | | : nsGenericHTMLFormElementWithState(std::move(aNodeInfo), NS_FORM_SELECT), |
119 | | mOptions(new HTMLOptionsCollection(this)), |
120 | | mAutocompleteAttrState(nsContentUtils::eAutocompleteAttrState_Unknown), |
121 | | mAutocompleteInfoState(nsContentUtils::eAutocompleteAttrState_Unknown), |
122 | | mIsDoneAddingChildren(!aFromParser), |
123 | | mDisabledChanged(false), |
124 | | mMutating(false), |
125 | | mInhibitStateRestoration(!!(aFromParser & FROM_PARSER_FRAGMENT)), |
126 | | mSelectionHasChanged(false), |
127 | | mDefaultSelectionSet(false), |
128 | | mCanShowInvalidUI(true), |
129 | | mCanShowValidUI(true), |
130 | | mNonOptionChildren(0), |
131 | | mOptGroupCount(0), |
132 | | mSelectedIndex(-1) |
133 | 0 | { |
134 | 0 | SetHasWeirdParserInsertionMode(); |
135 | 0 |
|
136 | 0 | // DoneAddingChildren() will be called later if it's from the parser, |
137 | 0 | // otherwise it is |
138 | 0 |
|
139 | 0 | // Set up our default state: enabled, optional, and valid. |
140 | 0 | AddStatesSilently(NS_EVENT_STATE_ENABLED | |
141 | 0 | NS_EVENT_STATE_OPTIONAL | |
142 | 0 | NS_EVENT_STATE_VALID); |
143 | 0 | } |
144 | | |
145 | | HTMLSelectElement::~HTMLSelectElement() |
146 | 0 | { |
147 | 0 | mOptions->DropReference(); |
148 | 0 | } |
149 | | |
150 | | // ISupports |
151 | | |
152 | | NS_IMPL_CYCLE_COLLECTION_CLASS(HTMLSelectElement) |
153 | | |
154 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(HTMLSelectElement, |
155 | 0 | nsGenericHTMLFormElementWithState) |
156 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mValidity) |
157 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOptions) |
158 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSelectedOptions) |
159 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
160 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(HTMLSelectElement, |
161 | 0 | nsGenericHTMLFormElementWithState) |
162 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mValidity) |
163 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mSelectedOptions) |
164 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
165 | | |
166 | | NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(HTMLSelectElement, |
167 | | nsGenericHTMLFormElementWithState, |
168 | | nsIConstraintValidation) |
169 | | |
170 | | |
171 | | // nsIDOMHTMLSelectElement |
172 | | |
173 | | |
174 | | NS_IMPL_ELEMENT_CLONE(HTMLSelectElement) |
175 | | |
176 | | void |
177 | | HTMLSelectElement::SetCustomValidity(const nsAString& aError) |
178 | 0 | { |
179 | 0 | nsIConstraintValidation::SetCustomValidity(aError); |
180 | 0 |
|
181 | 0 | UpdateState(true); |
182 | 0 | } |
183 | | |
184 | | void |
185 | | HTMLSelectElement::GetAutocomplete(DOMString& aValue) |
186 | 0 | { |
187 | 0 | const nsAttrValue* attributeVal = GetParsedAttr(nsGkAtoms::autocomplete); |
188 | 0 |
|
189 | 0 | mAutocompleteAttrState = |
190 | 0 | nsContentUtils::SerializeAutocompleteAttribute(attributeVal, aValue, |
191 | 0 | mAutocompleteAttrState); |
192 | 0 | } |
193 | | |
194 | | void |
195 | | HTMLSelectElement::GetAutocompleteInfo(AutocompleteInfo& aInfo) |
196 | 0 | { |
197 | 0 | const nsAttrValue* attributeVal = GetParsedAttr(nsGkAtoms::autocomplete); |
198 | 0 | mAutocompleteInfoState = |
199 | 0 | nsContentUtils::SerializeAutocompleteAttribute(attributeVal, aInfo, |
200 | 0 | mAutocompleteInfoState, |
201 | 0 | true); |
202 | 0 | } |
203 | | |
204 | | nsresult |
205 | | HTMLSelectElement::InsertChildBefore(nsIContent* aKid, |
206 | | nsIContent* aBeforeThis, |
207 | | bool aNotify) |
208 | 0 | { |
209 | 0 | int32_t index = aBeforeThis ? ComputeIndexOf(aBeforeThis) : GetChildCount(); |
210 | 0 | SafeOptionListMutation safeMutation(this, this, aKid, index, aNotify); |
211 | 0 | nsresult rv = |
212 | 0 | nsGenericHTMLFormElementWithState::InsertChildBefore(aKid, aBeforeThis, |
213 | 0 | aNotify); |
214 | 0 | if (NS_FAILED(rv)) { |
215 | 0 | safeMutation.MutationFailed(); |
216 | 0 | } |
217 | 0 | return rv; |
218 | 0 | } |
219 | | |
220 | | void |
221 | | HTMLSelectElement::RemoveChildNode(nsIContent* aKid, bool aNotify) |
222 | 0 | { |
223 | 0 | SafeOptionListMutation safeMutation(this, this, nullptr, |
224 | 0 | ComputeIndexOf(aKid), aNotify); |
225 | 0 | nsGenericHTMLFormElementWithState::RemoveChildNode(aKid, aNotify); |
226 | 0 | } |
227 | | |
228 | | void |
229 | | HTMLSelectElement::InsertOptionsIntoList(nsIContent* aOptions, |
230 | | int32_t aListIndex, |
231 | | int32_t aDepth, |
232 | | bool aNotify) |
233 | 0 | { |
234 | 0 | MOZ_ASSERT(aDepth == 0 || aDepth == 1); |
235 | 0 | int32_t insertIndex = aListIndex; |
236 | 0 |
|
237 | 0 | HTMLOptionElement* optElement = HTMLOptionElement::FromNode(aOptions); |
238 | 0 | if (optElement) { |
239 | 0 | mOptions->InsertOptionAt(optElement, insertIndex); |
240 | 0 | insertIndex++; |
241 | 0 | } else if (aDepth == 0) { |
242 | 0 | // If it's at the top level, then we just found out there are non-options |
243 | 0 | // at the top level, which will throw off the insert count |
244 | 0 | mNonOptionChildren++; |
245 | 0 |
|
246 | 0 | // Deal with optgroups |
247 | 0 | if (aOptions->IsHTMLElement(nsGkAtoms::optgroup)) { |
248 | 0 | mOptGroupCount++; |
249 | 0 |
|
250 | 0 | for (nsIContent* child = aOptions->GetFirstChild(); |
251 | 0 | child; |
252 | 0 | child = child->GetNextSibling()) { |
253 | 0 | optElement = HTMLOptionElement::FromNode(child); |
254 | 0 | if (optElement) { |
255 | 0 | mOptions->InsertOptionAt(optElement, insertIndex); |
256 | 0 | insertIndex++; |
257 | 0 | } |
258 | 0 | } |
259 | 0 | } |
260 | 0 | } // else ignore even if optgroup; we want to ignore nested optgroups. |
261 | 0 |
|
262 | 0 | // Deal with the selected list |
263 | 0 | if (insertIndex - aListIndex) { |
264 | 0 | // Fix the currently selected index |
265 | 0 | if (aListIndex <= mSelectedIndex) { |
266 | 0 | mSelectedIndex += (insertIndex - aListIndex); |
267 | 0 | SetSelectionChanged(true, aNotify); |
268 | 0 | } |
269 | 0 |
|
270 | 0 | // Get the frame stuff for notification. No need to flush here |
271 | 0 | // since if there's no frame for the select yet the select will |
272 | 0 | // get into the right state once it's created. |
273 | 0 | nsISelectControlFrame* selectFrame = nullptr; |
274 | 0 | AutoWeakFrame weakSelectFrame; |
275 | 0 | bool didGetFrame = false; |
276 | 0 |
|
277 | 0 | // Actually select the options if the added options warrant it |
278 | 0 | for (int32_t i = aListIndex; i < insertIndex; i++) { |
279 | 0 | // Notify the frame that the option is added |
280 | 0 | if (!didGetFrame || (selectFrame && !weakSelectFrame.IsAlive())) { |
281 | 0 | selectFrame = GetSelectFrame(); |
282 | 0 | weakSelectFrame = do_QueryFrame(selectFrame); |
283 | 0 | didGetFrame = true; |
284 | 0 | } |
285 | 0 |
|
286 | 0 | if (selectFrame) { |
287 | 0 | selectFrame->AddOption(i); |
288 | 0 | } |
289 | 0 |
|
290 | 0 | RefPtr<HTMLOptionElement> option = Item(i); |
291 | 0 | if (option && option->Selected()) { |
292 | 0 | // Clear all other options |
293 | 0 | if (!HasAttr(kNameSpaceID_None, nsGkAtoms::multiple)) { |
294 | 0 | uint32_t mask = IS_SELECTED | CLEAR_ALL | SET_DISABLED | NOTIFY; |
295 | 0 | SetOptionsSelectedByIndex(i, i, mask); |
296 | 0 | } |
297 | 0 |
|
298 | 0 | // This is sort of a hack ... we need to notify that the option was |
299 | 0 | // set and change selectedIndex even though we didn't really change |
300 | 0 | // its value. |
301 | 0 | OnOptionSelected(selectFrame, i, true, false, false); |
302 | 0 | } |
303 | 0 | } |
304 | 0 |
|
305 | 0 | CheckSelectSomething(aNotify); |
306 | 0 | } |
307 | 0 | } |
308 | | |
309 | | nsresult |
310 | | HTMLSelectElement::RemoveOptionsFromList(nsIContent* aOptions, |
311 | | int32_t aListIndex, |
312 | | int32_t aDepth, |
313 | | bool aNotify) |
314 | 0 | { |
315 | 0 | MOZ_ASSERT(aDepth == 0 || aDepth == 1); |
316 | 0 | int32_t numRemoved = 0; |
317 | 0 |
|
318 | 0 | HTMLOptionElement* optElement = HTMLOptionElement::FromNode(aOptions); |
319 | 0 | if (optElement) { |
320 | 0 | if (mOptions->ItemAsOption(aListIndex) != optElement) { |
321 | 0 | NS_ERROR("wrong option at index"); |
322 | 0 | return NS_ERROR_UNEXPECTED; |
323 | 0 | } |
324 | 0 | mOptions->RemoveOptionAt(aListIndex); |
325 | 0 | numRemoved++; |
326 | 0 | } else if (aDepth == 0) { |
327 | 0 | // Yay, one less artifact at the top level. |
328 | 0 | mNonOptionChildren--; |
329 | 0 |
|
330 | 0 | // Recurse down deeper for options |
331 | 0 | if (mOptGroupCount && aOptions->IsHTMLElement(nsGkAtoms::optgroup)) { |
332 | 0 | mOptGroupCount--; |
333 | 0 |
|
334 | 0 | for (nsIContent* child = aOptions->GetFirstChild(); |
335 | 0 | child; |
336 | 0 | child = child->GetNextSibling()) { |
337 | 0 | optElement = HTMLOptionElement::FromNode(child); |
338 | 0 | if (optElement) { |
339 | 0 | if (mOptions->ItemAsOption(aListIndex) != optElement) { |
340 | 0 | NS_ERROR("wrong option at index"); |
341 | 0 | return NS_ERROR_UNEXPECTED; |
342 | 0 | } |
343 | 0 | mOptions->RemoveOptionAt(aListIndex); |
344 | 0 | numRemoved++; |
345 | 0 | } |
346 | 0 | } |
347 | 0 | } |
348 | 0 | } // else don't check for an optgroup; we want to ignore nested optgroups |
349 | 0 |
|
350 | 0 | if (numRemoved) { |
351 | 0 | // Tell the widget we removed the options |
352 | 0 | nsISelectControlFrame* selectFrame = GetSelectFrame(); |
353 | 0 | if (selectFrame) { |
354 | 0 | nsAutoScriptBlocker scriptBlocker; |
355 | 0 | for (int32_t i = aListIndex; i < aListIndex + numRemoved; ++i) { |
356 | 0 | selectFrame->RemoveOption(i); |
357 | 0 | } |
358 | 0 | } |
359 | 0 |
|
360 | 0 | // Fix the selected index |
361 | 0 | if (aListIndex <= mSelectedIndex) { |
362 | 0 | if (mSelectedIndex < (aListIndex+numRemoved)) { |
363 | 0 | // aListIndex <= mSelectedIndex < aListIndex+numRemoved |
364 | 0 | // Find a new selected index if it was one of the ones removed. |
365 | 0 | FindSelectedIndex(aListIndex, aNotify); |
366 | 0 | } else { |
367 | 0 | // Shift the selected index if something in front of it was removed |
368 | 0 | // aListIndex+numRemoved <= mSelectedIndex |
369 | 0 | mSelectedIndex -= numRemoved; |
370 | 0 | SetSelectionChanged(true, aNotify); |
371 | 0 | } |
372 | 0 | } |
373 | 0 |
|
374 | 0 | // Select something in case we removed the selected option on a |
375 | 0 | // single select |
376 | 0 | if (!CheckSelectSomething(aNotify) && mSelectedIndex == -1) { |
377 | 0 | // Update the validity state in case of we've just removed the last |
378 | 0 | // option. |
379 | 0 | UpdateValueMissingValidityState(); |
380 | 0 |
|
381 | 0 | UpdateState(aNotify); |
382 | 0 | } |
383 | 0 | } |
384 | 0 |
|
385 | 0 | return NS_OK; |
386 | 0 | } |
387 | | |
388 | | // XXXldb Doing the processing before the content nodes have been added |
389 | | // to the document (as the name of this function seems to require, and |
390 | | // as the callers do), is highly unusual. Passing around unparented |
391 | | // content to other parts of the app can make those things think the |
392 | | // options are the root content node. |
393 | | NS_IMETHODIMP |
394 | | HTMLSelectElement::WillAddOptions(nsIContent* aOptions, |
395 | | nsIContent* aParent, |
396 | | int32_t aContentIndex, |
397 | | bool aNotify) |
398 | 0 | { |
399 | 0 | if (this != aParent && this != aParent->GetParent()) { |
400 | 0 | return NS_OK; |
401 | 0 | } |
402 | 0 | int32_t level = aParent == this ? 0 : 1; |
403 | 0 |
|
404 | 0 | // Get the index where the options will be inserted |
405 | 0 | int32_t ind = -1; |
406 | 0 | if (!mNonOptionChildren) { |
407 | 0 | // If there are no artifacts, aContentIndex == ind |
408 | 0 | ind = aContentIndex; |
409 | 0 | } else { |
410 | 0 | // If there are artifacts, we have to get the index of the option the |
411 | 0 | // hard way |
412 | 0 | int32_t children = aParent->GetChildCount(); |
413 | 0 |
|
414 | 0 | if (aContentIndex >= children) { |
415 | 0 | // If the content insert is after the end of the parent, then we want to get |
416 | 0 | // the next index *after* the parent and insert there. |
417 | 0 | ind = GetOptionIndexAfter(aParent); |
418 | 0 | } else { |
419 | 0 | // If the content insert is somewhere in the middle of the container, then |
420 | 0 | // we want to get the option currently at the index and insert in front of |
421 | 0 | // that. |
422 | 0 | nsIContent* currentKid = aParent->GetChildAt_Deprecated(aContentIndex); |
423 | 0 | NS_ASSERTION(currentKid, "Child not found!"); |
424 | 0 | if (currentKid) { |
425 | 0 | ind = GetOptionIndexAt(currentKid); |
426 | 0 | } else { |
427 | 0 | ind = -1; |
428 | 0 | } |
429 | 0 | } |
430 | 0 | } |
431 | 0 |
|
432 | 0 | InsertOptionsIntoList(aOptions, ind, level, aNotify); |
433 | 0 | return NS_OK; |
434 | 0 | } |
435 | | |
436 | | NS_IMETHODIMP |
437 | | HTMLSelectElement::WillRemoveOptions(nsIContent* aParent, |
438 | | int32_t aContentIndex, |
439 | | bool aNotify) |
440 | 0 | { |
441 | 0 | if (this != aParent && this != aParent->GetParent()) { |
442 | 0 | return NS_OK; |
443 | 0 | } |
444 | 0 | int32_t level = this == aParent ? 0 : 1; |
445 | 0 |
|
446 | 0 | // Get the index where the options will be removed |
447 | 0 | nsIContent* currentKid = aParent->GetChildAt_Deprecated(aContentIndex); |
448 | 0 | if (currentKid) { |
449 | 0 | int32_t ind; |
450 | 0 | if (!mNonOptionChildren) { |
451 | 0 | // If there are no artifacts, aContentIndex == ind |
452 | 0 | ind = aContentIndex; |
453 | 0 | } else { |
454 | 0 | // If there are artifacts, we have to get the index of the option the |
455 | 0 | // hard way |
456 | 0 | ind = GetFirstOptionIndex(currentKid); |
457 | 0 | } |
458 | 0 | if (ind != -1) { |
459 | 0 | nsresult rv = RemoveOptionsFromList(currentKid, ind, level, aNotify); |
460 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
461 | 0 | } |
462 | 0 | } |
463 | 0 |
|
464 | 0 | return NS_OK; |
465 | 0 | } |
466 | | |
467 | | int32_t |
468 | | HTMLSelectElement::GetOptionIndexAt(nsIContent* aOptions) |
469 | 0 | { |
470 | 0 | // Search this node and below. |
471 | 0 | // If not found, find the first one *after* this node. |
472 | 0 | int32_t retval = GetFirstOptionIndex(aOptions); |
473 | 0 | if (retval == -1) { |
474 | 0 | retval = GetOptionIndexAfter(aOptions); |
475 | 0 | } |
476 | 0 |
|
477 | 0 | return retval; |
478 | 0 | } |
479 | | |
480 | | int32_t |
481 | | HTMLSelectElement::GetOptionIndexAfter(nsIContent* aOptions) |
482 | 0 | { |
483 | 0 | // - If this is the select, the next option is the last. |
484 | 0 | // - If not, search all the options after aOptions and up to the last option |
485 | 0 | // in the parent. |
486 | 0 | // - If it's not there, search for the first option after the parent. |
487 | 0 | if (aOptions == this) { |
488 | 0 | return Length(); |
489 | 0 | } |
490 | 0 | |
491 | 0 | int32_t retval = -1; |
492 | 0 |
|
493 | 0 | nsCOMPtr<nsIContent> parent = aOptions->GetParent(); |
494 | 0 |
|
495 | 0 | if (parent) { |
496 | 0 | int32_t index = parent->ComputeIndexOf(aOptions); |
497 | 0 | int32_t count = parent->GetChildCount(); |
498 | 0 |
|
499 | 0 | retval = GetFirstChildOptionIndex(parent, index+1, count); |
500 | 0 |
|
501 | 0 | if (retval == -1) { |
502 | 0 | retval = GetOptionIndexAfter(parent); |
503 | 0 | } |
504 | 0 | } |
505 | 0 |
|
506 | 0 | return retval; |
507 | 0 | } |
508 | | |
509 | | int32_t |
510 | | HTMLSelectElement::GetFirstOptionIndex(nsIContent* aOptions) |
511 | 0 | { |
512 | 0 | int32_t listIndex = -1; |
513 | 0 | HTMLOptionElement* optElement = HTMLOptionElement::FromNode(aOptions); |
514 | 0 | if (optElement) { |
515 | 0 | mOptions->GetOptionIndex(optElement, 0, true, &listIndex); |
516 | 0 | return listIndex; |
517 | 0 | } |
518 | 0 | |
519 | 0 | listIndex = GetFirstChildOptionIndex(aOptions, 0, aOptions->GetChildCount()); |
520 | 0 |
|
521 | 0 | return listIndex; |
522 | 0 | } |
523 | | |
524 | | int32_t |
525 | | HTMLSelectElement::GetFirstChildOptionIndex(nsIContent* aOptions, |
526 | | int32_t aStartIndex, |
527 | | int32_t aEndIndex) |
528 | 0 | { |
529 | 0 | int32_t retval = -1; |
530 | 0 |
|
531 | 0 | for (int32_t i = aStartIndex; i < aEndIndex; ++i) { |
532 | 0 | retval = GetFirstOptionIndex(aOptions->GetChildAt_Deprecated(i)); |
533 | 0 | if (retval != -1) { |
534 | 0 | break; |
535 | 0 | } |
536 | 0 | } |
537 | 0 |
|
538 | 0 | return retval; |
539 | 0 | } |
540 | | |
541 | | nsISelectControlFrame* |
542 | | HTMLSelectElement::GetSelectFrame() |
543 | 0 | { |
544 | 0 | nsIFormControlFrame* form_control_frame = GetFormControlFrame(false); |
545 | 0 |
|
546 | 0 | nsISelectControlFrame* select_frame = nullptr; |
547 | 0 |
|
548 | 0 | if (form_control_frame) { |
549 | 0 | select_frame = do_QueryFrame(form_control_frame); |
550 | 0 | } |
551 | 0 |
|
552 | 0 | return select_frame; |
553 | 0 | } |
554 | | |
555 | | void |
556 | | HTMLSelectElement::Add(const HTMLOptionElementOrHTMLOptGroupElement& aElement, |
557 | | const Nullable<HTMLElementOrLong>& aBefore, |
558 | | ErrorResult& aRv) |
559 | 0 | { |
560 | 0 | nsGenericHTMLElement& element = |
561 | 0 | aElement.IsHTMLOptionElement() ? |
562 | 0 | static_cast<nsGenericHTMLElement&>(aElement.GetAsHTMLOptionElement()) : |
563 | 0 | static_cast<nsGenericHTMLElement&>(aElement.GetAsHTMLOptGroupElement()); |
564 | 0 |
|
565 | 0 | if (aBefore.IsNull()) { |
566 | 0 | Add(element, static_cast<nsGenericHTMLElement*>(nullptr), aRv); |
567 | 0 | } else if (aBefore.Value().IsHTMLElement()) { |
568 | 0 | Add(element, &aBefore.Value().GetAsHTMLElement(), aRv); |
569 | 0 | } else { |
570 | 0 | Add(element, aBefore.Value().GetAsLong(), aRv); |
571 | 0 | } |
572 | 0 | } |
573 | | |
574 | | void |
575 | | HTMLSelectElement::Add(nsGenericHTMLElement& aElement, |
576 | | nsGenericHTMLElement* aBefore, |
577 | | ErrorResult& aError) |
578 | 0 | { |
579 | 0 | if (!aBefore) { |
580 | 0 | Element::AppendChild(aElement, aError); |
581 | 0 | return; |
582 | 0 | } |
583 | 0 | |
584 | 0 | // Just in case we're not the parent, get the parent of the reference |
585 | 0 | // element |
586 | 0 | nsCOMPtr<nsINode> parent = aBefore->Element::GetParentNode(); |
587 | 0 | if (!parent || !nsContentUtils::ContentIsDescendantOf(parent, this)) { |
588 | 0 | // NOT_FOUND_ERR: Raised if before is not a descendant of the SELECT |
589 | 0 | // element. |
590 | 0 | aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR); |
591 | 0 | return; |
592 | 0 | } |
593 | 0 | |
594 | 0 | // If the before parameter is not null, we are equivalent to the |
595 | 0 | // insertBefore method on the parent of before. |
596 | 0 | nsCOMPtr<nsINode> refNode = aBefore; |
597 | 0 | parent->InsertBefore(aElement, refNode, aError); |
598 | 0 | } |
599 | | |
600 | | void |
601 | | HTMLSelectElement::Remove(int32_t aIndex) |
602 | 0 | { |
603 | 0 | nsCOMPtr<nsINode> option = Item(static_cast<uint32_t>(aIndex)); |
604 | 0 | if (!option) { |
605 | 0 | return; |
606 | 0 | } |
607 | 0 | |
608 | 0 | option->Remove(); |
609 | 0 | } |
610 | | |
611 | | void |
612 | | HTMLSelectElement::GetType(nsAString& aType) |
613 | 0 | { |
614 | 0 | if (HasAttr(kNameSpaceID_None, nsGkAtoms::multiple)) { |
615 | 0 | aType.AssignLiteral("select-multiple"); |
616 | 0 | } |
617 | 0 | else { |
618 | 0 | aType.AssignLiteral("select-one"); |
619 | 0 | } |
620 | 0 | } |
621 | | |
622 | 0 | #define MAX_DYNAMIC_SELECT_LENGTH 10000 |
623 | | |
624 | | void |
625 | | HTMLSelectElement::SetLength(uint32_t aLength, ErrorResult& aRv) |
626 | 0 | { |
627 | 0 | uint32_t curlen = Length(); |
628 | 0 |
|
629 | 0 | if (curlen > aLength) { // Remove extra options |
630 | 0 | for (uint32_t i = curlen; i > aLength; --i) { |
631 | 0 | Remove(i - 1); |
632 | 0 | } |
633 | 0 | } else if (aLength > curlen) { |
634 | 0 | if (aLength > MAX_DYNAMIC_SELECT_LENGTH) { |
635 | 0 | aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR); |
636 | 0 | return; |
637 | 0 | } |
638 | 0 | |
639 | 0 | RefPtr<mozilla::dom::NodeInfo> nodeInfo; |
640 | 0 |
|
641 | 0 | nsContentUtils::QNameChanged(mNodeInfo, nsGkAtoms::option, |
642 | 0 | getter_AddRefs(nodeInfo)); |
643 | 0 |
|
644 | 0 | nsCOMPtr<nsINode> node = NS_NewHTMLOptionElement(nodeInfo.forget()); |
645 | 0 |
|
646 | 0 | RefPtr<nsTextNode> text = new nsTextNode(mNodeInfo->NodeInfoManager()); |
647 | 0 |
|
648 | 0 | aRv = node->AppendChildTo(text, false); |
649 | 0 | if (aRv.Failed()) { |
650 | 0 | return; |
651 | 0 | } |
652 | 0 | |
653 | 0 | for (uint32_t i = curlen; i < aLength; i++) { |
654 | 0 | nsINode::AppendChild(*node, aRv); |
655 | 0 | if (aRv.Failed()) { |
656 | 0 | return; |
657 | 0 | } |
658 | 0 | |
659 | 0 | if (i + 1 < aLength) { |
660 | 0 | node = node->CloneNode(true, aRv); |
661 | 0 | if (aRv.Failed()) { |
662 | 0 | return; |
663 | 0 | } |
664 | 0 | MOZ_ASSERT(node); |
665 | 0 | } |
666 | 0 | } |
667 | 0 | } |
668 | 0 | } |
669 | | |
670 | | /* static */ |
671 | | bool |
672 | | HTMLSelectElement::MatchSelectedOptions(Element* aElement, |
673 | | int32_t /* unused */, |
674 | | nsAtom* /* unused */, |
675 | | void* /* unused*/) |
676 | 0 | { |
677 | 0 | HTMLOptionElement* option = HTMLOptionElement::FromNode(aElement); |
678 | 0 | return option && option->Selected(); |
679 | 0 | } |
680 | | |
681 | | nsIHTMLCollection* |
682 | | HTMLSelectElement::SelectedOptions() |
683 | 0 | { |
684 | 0 | if (!mSelectedOptions) { |
685 | 0 | mSelectedOptions = new nsContentList(this, MatchSelectedOptions, nullptr, |
686 | 0 | nullptr, /* deep */ true); |
687 | 0 | } |
688 | 0 | return mSelectedOptions; |
689 | 0 | } |
690 | | |
691 | | nsresult |
692 | | HTMLSelectElement::SetSelectedIndexInternal(int32_t aIndex, bool aNotify) |
693 | 0 | { |
694 | 0 | int32_t oldSelectedIndex = mSelectedIndex; |
695 | 0 | uint32_t mask = IS_SELECTED | CLEAR_ALL | SET_DISABLED; |
696 | 0 | if (aNotify) { |
697 | 0 | mask |= NOTIFY; |
698 | 0 | } |
699 | 0 |
|
700 | 0 | SetOptionsSelectedByIndex(aIndex, aIndex, mask); |
701 | 0 |
|
702 | 0 | nsresult rv = NS_OK; |
703 | 0 | nsISelectControlFrame* selectFrame = GetSelectFrame(); |
704 | 0 | if (selectFrame) { |
705 | 0 | rv = selectFrame->OnSetSelectedIndex(oldSelectedIndex, mSelectedIndex); |
706 | 0 | } |
707 | 0 |
|
708 | 0 | SetSelectionChanged(true, aNotify); |
709 | 0 |
|
710 | 0 | return rv; |
711 | 0 | } |
712 | | |
713 | | bool |
714 | | HTMLSelectElement::IsOptionSelectedByIndex(int32_t aIndex) |
715 | 0 | { |
716 | 0 | HTMLOptionElement* option = Item(static_cast<uint32_t>(aIndex)); |
717 | 0 | return option && option->Selected(); |
718 | 0 | } |
719 | | |
720 | | void |
721 | | HTMLSelectElement::OnOptionSelected(nsISelectControlFrame* aSelectFrame, |
722 | | int32_t aIndex, |
723 | | bool aSelected, |
724 | | bool aChangeOptionState, |
725 | | bool aNotify) |
726 | 0 | { |
727 | 0 | // Set the selected index |
728 | 0 | if (aSelected && (aIndex < mSelectedIndex || mSelectedIndex < 0)) { |
729 | 0 | mSelectedIndex = aIndex; |
730 | 0 | SetSelectionChanged(true, aNotify); |
731 | 0 | } else if (!aSelected && aIndex == mSelectedIndex) { |
732 | 0 | FindSelectedIndex(aIndex + 1, aNotify); |
733 | 0 | } |
734 | 0 |
|
735 | 0 | if (aChangeOptionState) { |
736 | 0 | // Tell the option to get its bad self selected |
737 | 0 | RefPtr<HTMLOptionElement> option = Item(static_cast<uint32_t>(aIndex)); |
738 | 0 | if (option) { |
739 | 0 | option->SetSelectedInternal(aSelected, aNotify); |
740 | 0 | } |
741 | 0 | } |
742 | 0 |
|
743 | 0 | // Let the frame know too |
744 | 0 | if (aSelectFrame) { |
745 | 0 | aSelectFrame->OnOptionSelected(aIndex, aSelected); |
746 | 0 | } |
747 | 0 |
|
748 | 0 | UpdateSelectedOptions(); |
749 | 0 | UpdateValueMissingValidityState(); |
750 | 0 | UpdateState(aNotify); |
751 | 0 | } |
752 | | |
753 | | void |
754 | | HTMLSelectElement::FindSelectedIndex(int32_t aStartIndex, bool aNotify) |
755 | 0 | { |
756 | 0 | mSelectedIndex = -1; |
757 | 0 | SetSelectionChanged(true, aNotify); |
758 | 0 | uint32_t len = Length(); |
759 | 0 | for (int32_t i = aStartIndex; i < int32_t(len); i++) { |
760 | 0 | if (IsOptionSelectedByIndex(i)) { |
761 | 0 | mSelectedIndex = i; |
762 | 0 | SetSelectionChanged(true, aNotify); |
763 | 0 | break; |
764 | 0 | } |
765 | 0 | } |
766 | 0 | } |
767 | | |
768 | | // XXX Consider splitting this into two functions for ease of reading: |
769 | | // SelectOptionsByIndex(startIndex, endIndex, clearAll, checkDisabled) |
770 | | // startIndex, endIndex - the range of options to turn on |
771 | | // (-1, -1) will clear all indices no matter what. |
772 | | // clearAll - will clear all other options unless checkDisabled is on |
773 | | // and all the options attempted to be set are disabled |
774 | | // (note that if it is not multiple, and an option is selected, |
775 | | // everything else will be cleared regardless). |
776 | | // checkDisabled - if this is TRUE, and an option is disabled, it will not be |
777 | | // changed regardless of whether it is selected or not. |
778 | | // Generally the UI passes TRUE and JS passes FALSE. |
779 | | // (setDisabled currently is the opposite) |
780 | | // DeselectOptionsByIndex(startIndex, endIndex, checkDisabled) |
781 | | // startIndex, endIndex - the range of options to turn on |
782 | | // (-1, -1) will clear all indices no matter what. |
783 | | // checkDisabled - if this is TRUE, and an option is disabled, it will not be |
784 | | // changed regardless of whether it is selected or not. |
785 | | // Generally the UI passes TRUE and JS passes FALSE. |
786 | | // (setDisabled currently is the opposite) |
787 | | // |
788 | | // XXXbz the above comment is pretty confusing. Maybe we should actually |
789 | | // document the args to this function too, in addition to documenting what |
790 | | // things might end up looking like? In particular, pay attention to the |
791 | | // setDisabled vs checkDisabled business. |
792 | | bool |
793 | | HTMLSelectElement::SetOptionsSelectedByIndex(int32_t aStartIndex, |
794 | | int32_t aEndIndex, |
795 | | uint32_t aOptionsMask) |
796 | 0 | { |
797 | | #if 0 |
798 | | printf("SetOption(%d-%d, %c, ClearAll=%c)\n", aStartIndex, aEndIndex, |
799 | | (aOptionsMask & IS_SELECTED ? 'Y' : 'N'), |
800 | | (aOptionsMask & CLEAR_ALL ? 'Y' : 'N')); |
801 | | #endif |
802 | | // Don't bother if the select is disabled |
803 | 0 | if (!(aOptionsMask & SET_DISABLED) && IsDisabled()) { |
804 | 0 | return false; |
805 | 0 | } |
806 | 0 | |
807 | 0 | // Don't bother if there are no options |
808 | 0 | uint32_t numItems = Length(); |
809 | 0 | if (numItems == 0) { |
810 | 0 | return false; |
811 | 0 | } |
812 | 0 | |
813 | 0 | // First, find out whether multiple items can be selected |
814 | 0 | bool isMultiple = Multiple(); |
815 | 0 |
|
816 | 0 | // These variables tell us whether any options were selected |
817 | 0 | // or deselected. |
818 | 0 | bool optionsSelected = false; |
819 | 0 | bool optionsDeselected = false; |
820 | 0 |
|
821 | 0 | nsISelectControlFrame* selectFrame = nullptr; |
822 | 0 | bool didGetFrame = false; |
823 | 0 | AutoWeakFrame weakSelectFrame; |
824 | 0 |
|
825 | 0 | if (aOptionsMask & IS_SELECTED) { |
826 | 0 | // Setting selectedIndex to an out-of-bounds index means -1. (HTML5) |
827 | 0 | if (aStartIndex < 0 || AssertedCast<uint32_t>(aStartIndex) >= numItems || |
828 | 0 | aEndIndex < 0 || AssertedCast<uint32_t>(aEndIndex) >= numItems) { |
829 | 0 | aStartIndex = -1; |
830 | 0 | aEndIndex = -1; |
831 | 0 | } |
832 | 0 |
|
833 | 0 | // Only select the first value if it's not multiple |
834 | 0 | if (!isMultiple) { |
835 | 0 | aEndIndex = aStartIndex; |
836 | 0 | } |
837 | 0 |
|
838 | 0 | // This variable tells whether or not all of the options we attempted to |
839 | 0 | // select are disabled. If ClearAll is passed in as true, and we do not |
840 | 0 | // select anything because the options are disabled, we will not clear the |
841 | 0 | // other options. (This is to make the UI work the way one might expect.) |
842 | 0 | bool allDisabled = !(aOptionsMask & SET_DISABLED); |
843 | 0 |
|
844 | 0 | // |
845 | 0 | // Save a little time when clearing other options |
846 | 0 | // |
847 | 0 | int32_t previousSelectedIndex = mSelectedIndex; |
848 | 0 |
|
849 | 0 | // |
850 | 0 | // Select the requested indices |
851 | 0 | // |
852 | 0 | // If index is -1, everything will be deselected (bug 28143) |
853 | 0 | if (aStartIndex != -1) { |
854 | 0 | MOZ_ASSERT(aStartIndex >= 0); |
855 | 0 | MOZ_ASSERT(aEndIndex >= 0); |
856 | 0 | // Loop through the options and select them (if they are not disabled and |
857 | 0 | // if they are not already selected). |
858 | 0 | for (uint32_t optIndex = AssertedCast<uint32_t>(aStartIndex); |
859 | 0 | optIndex <= AssertedCast<uint32_t>(aEndIndex); |
860 | 0 | optIndex++) { |
861 | 0 | RefPtr<HTMLOptionElement> option = Item(optIndex); |
862 | 0 |
|
863 | 0 | // Ignore disabled options. |
864 | 0 | if (!(aOptionsMask & SET_DISABLED)) { |
865 | 0 | if (option && IsOptionDisabled(option)) { |
866 | 0 | continue; |
867 | 0 | } |
868 | 0 | allDisabled = false; |
869 | 0 | } |
870 | 0 |
|
871 | 0 | // If the index is already selected, ignore it. |
872 | 0 | if (option && !option->Selected()) { |
873 | 0 | // To notify the frame if anything gets changed. No need |
874 | 0 | // to flush here, if there's no frame yet we don't need to |
875 | 0 | // force it to be created just to notify it about a change |
876 | 0 | // in the select. |
877 | 0 | selectFrame = GetSelectFrame(); |
878 | 0 | weakSelectFrame = do_QueryFrame(selectFrame); |
879 | 0 | didGetFrame = true; |
880 | 0 |
|
881 | 0 | OnOptionSelected(selectFrame, optIndex, true, true, |
882 | 0 | aOptionsMask & NOTIFY); |
883 | 0 | optionsSelected = true; |
884 | 0 | } |
885 | 0 | } |
886 | 0 | } |
887 | 0 |
|
888 | 0 | // Next remove all other options if single select or all is clear |
889 | 0 | // If index is -1, everything will be deselected (bug 28143) |
890 | 0 | if (((!isMultiple && optionsSelected) |
891 | 0 | || ((aOptionsMask & CLEAR_ALL) && !allDisabled) |
892 | 0 | || aStartIndex == -1) |
893 | 0 | && previousSelectedIndex != -1) { |
894 | 0 | for (uint32_t optIndex = AssertedCast<uint32_t>(previousSelectedIndex); |
895 | 0 | optIndex < numItems; |
896 | 0 | optIndex++) { |
897 | 0 | if (static_cast<int32_t>(optIndex) < aStartIndex || |
898 | 0 | static_cast<int32_t>(optIndex) > aEndIndex) { |
899 | 0 | HTMLOptionElement* option = Item(optIndex); |
900 | 0 | // If the index is already selected, ignore it. |
901 | 0 | if (option && option->Selected()) { |
902 | 0 | if (!didGetFrame || (selectFrame && !weakSelectFrame.IsAlive())) { |
903 | 0 | // To notify the frame if anything gets changed, don't |
904 | 0 | // flush, if the frame doesn't exist we don't need to |
905 | 0 | // create it just to tell it about this change. |
906 | 0 | selectFrame = GetSelectFrame(); |
907 | 0 | weakSelectFrame = do_QueryFrame(selectFrame); |
908 | 0 |
|
909 | 0 | didGetFrame = true; |
910 | 0 | } |
911 | 0 |
|
912 | 0 | OnOptionSelected(selectFrame, optIndex, false, true, |
913 | 0 | aOptionsMask & NOTIFY); |
914 | 0 | optionsDeselected = true; |
915 | 0 |
|
916 | 0 | // Only need to deselect one option if not multiple |
917 | 0 | if (!isMultiple) { |
918 | 0 | break; |
919 | 0 | } |
920 | 0 | } |
921 | 0 | } |
922 | 0 | } |
923 | 0 | } |
924 | 0 | } else { |
925 | 0 | // If we're deselecting, loop through all selected items and deselect |
926 | 0 | // any that are in the specified range. |
927 | 0 | for (int32_t optIndex = aStartIndex; optIndex <= aEndIndex; optIndex++) { |
928 | 0 | HTMLOptionElement* option = Item(optIndex); |
929 | 0 | if (!(aOptionsMask & SET_DISABLED) && IsOptionDisabled(option)) { |
930 | 0 | continue; |
931 | 0 | } |
932 | 0 | |
933 | 0 | // If the index is already selected, ignore it. |
934 | 0 | if (option && option->Selected()) { |
935 | 0 | if (!didGetFrame || (selectFrame && !weakSelectFrame.IsAlive())) { |
936 | 0 | // To notify the frame if anything gets changed, don't |
937 | 0 | // flush, if the frame doesn't exist we don't need to |
938 | 0 | // create it just to tell it about this change. |
939 | 0 | selectFrame = GetSelectFrame(); |
940 | 0 | weakSelectFrame = do_QueryFrame(selectFrame); |
941 | 0 |
|
942 | 0 | didGetFrame = true; |
943 | 0 | } |
944 | 0 |
|
945 | 0 | OnOptionSelected(selectFrame, optIndex, false, true, |
946 | 0 | aOptionsMask & NOTIFY); |
947 | 0 | optionsDeselected = true; |
948 | 0 | } |
949 | 0 | } |
950 | 0 | } |
951 | 0 |
|
952 | 0 | // Make sure something is selected unless we were set to -1 (none) |
953 | 0 | if (optionsDeselected && aStartIndex != -1 && !(aOptionsMask & NO_RESELECT)) { |
954 | 0 | optionsSelected = |
955 | 0 | CheckSelectSomething(aOptionsMask & NOTIFY) || optionsSelected; |
956 | 0 | } |
957 | 0 |
|
958 | 0 | // Let the caller know whether anything was changed |
959 | 0 | return optionsSelected || optionsDeselected; |
960 | 0 | } |
961 | | |
962 | | NS_IMETHODIMP |
963 | | HTMLSelectElement::IsOptionDisabled(int32_t aIndex, bool* aIsDisabled) |
964 | 0 | { |
965 | 0 | *aIsDisabled = false; |
966 | 0 | RefPtr<HTMLOptionElement> option = Item(aIndex); |
967 | 0 | NS_ENSURE_TRUE(option, NS_ERROR_FAILURE); |
968 | 0 |
|
969 | 0 | *aIsDisabled = IsOptionDisabled(option); |
970 | 0 | return NS_OK; |
971 | 0 | } |
972 | | |
973 | | bool |
974 | | HTMLSelectElement::IsOptionDisabled(HTMLOptionElement* aOption) const |
975 | 0 | { |
976 | 0 | MOZ_ASSERT(aOption); |
977 | 0 | if (aOption->Disabled()) { |
978 | 0 | return true; |
979 | 0 | } |
980 | 0 | |
981 | 0 | // Check for disabled optgroups |
982 | 0 | // If there are no artifacts, there are no optgroups |
983 | 0 | if (mNonOptionChildren) { |
984 | 0 | for (nsCOMPtr<Element> node = static_cast<nsINode*>(aOption)->GetParentElement(); |
985 | 0 | node; |
986 | 0 | node = node->GetParentElement()) { |
987 | 0 | // If we reached the select element, we're done |
988 | 0 | if (node->IsHTMLElement(nsGkAtoms::select)) { |
989 | 0 | return false; |
990 | 0 | } |
991 | 0 | |
992 | 0 | RefPtr<HTMLOptGroupElement> optGroupElement = |
993 | 0 | HTMLOptGroupElement::FromNode(node); |
994 | 0 |
|
995 | 0 | if (!optGroupElement) { |
996 | 0 | // If you put something else between you and the optgroup, you're a |
997 | 0 | // moron and you deserve not to have optgroup disabling work. |
998 | 0 | return false; |
999 | 0 | } |
1000 | 0 | |
1001 | 0 | if (optGroupElement->Disabled()) { |
1002 | 0 | return true; |
1003 | 0 | } |
1004 | 0 | } |
1005 | 0 | } |
1006 | 0 |
|
1007 | 0 | return false; |
1008 | 0 | } |
1009 | | |
1010 | | void |
1011 | | HTMLSelectElement::GetValue(DOMString& aValue) |
1012 | 0 | { |
1013 | 0 | int32_t selectedIndex = SelectedIndex(); |
1014 | 0 | if (selectedIndex < 0) { |
1015 | 0 | return; |
1016 | 0 | } |
1017 | 0 | |
1018 | 0 | RefPtr<HTMLOptionElement> option = |
1019 | 0 | Item(static_cast<uint32_t>(selectedIndex)); |
1020 | 0 |
|
1021 | 0 | if (!option) { |
1022 | 0 | return; |
1023 | 0 | } |
1024 | 0 | |
1025 | 0 | option->GetValue(aValue); |
1026 | 0 | } |
1027 | | |
1028 | | void |
1029 | | HTMLSelectElement::SetValue(const nsAString& aValue) |
1030 | 0 | { |
1031 | 0 | uint32_t length = Length(); |
1032 | 0 |
|
1033 | 0 | for (uint32_t i = 0; i < length; i++) { |
1034 | 0 | RefPtr<HTMLOptionElement> option = Item(i); |
1035 | 0 | if (!option) { |
1036 | 0 | continue; |
1037 | 0 | } |
1038 | 0 | |
1039 | 0 | nsAutoString optionVal; |
1040 | 0 | option->GetValue(optionVal); |
1041 | 0 | if (optionVal.Equals(aValue)) { |
1042 | 0 | SetSelectedIndexInternal(int32_t(i), true); |
1043 | 0 | return; |
1044 | 0 | } |
1045 | 0 | } |
1046 | 0 | // No matching option was found. |
1047 | 0 | SetSelectedIndexInternal(-1, true); |
1048 | 0 | } |
1049 | | |
1050 | | int32_t |
1051 | | HTMLSelectElement::TabIndexDefault() |
1052 | 0 | { |
1053 | 0 | return 0; |
1054 | 0 | } |
1055 | | |
1056 | | bool |
1057 | | HTMLSelectElement::IsHTMLFocusable(bool aWithMouse, |
1058 | | bool* aIsFocusable, int32_t* aTabIndex) |
1059 | 0 | { |
1060 | 0 | if (nsGenericHTMLFormElementWithState::IsHTMLFocusable(aWithMouse, aIsFocusable, |
1061 | 0 | aTabIndex)) |
1062 | 0 | { |
1063 | 0 | return true; |
1064 | 0 | } |
1065 | 0 | |
1066 | 0 | *aIsFocusable = !IsDisabled(); |
1067 | 0 |
|
1068 | 0 | return false; |
1069 | 0 | } |
1070 | | |
1071 | | bool |
1072 | | HTMLSelectElement::CheckSelectSomething(bool aNotify) |
1073 | 0 | { |
1074 | 0 | if (mIsDoneAddingChildren) { |
1075 | 0 | if (mSelectedIndex < 0 && IsCombobox()) { |
1076 | 0 | return SelectSomething(aNotify); |
1077 | 0 | } |
1078 | 0 | } |
1079 | 0 | return false; |
1080 | 0 | } |
1081 | | |
1082 | | bool |
1083 | | HTMLSelectElement::SelectSomething(bool aNotify) |
1084 | 0 | { |
1085 | 0 | // If we're not done building the select, don't play with this yet. |
1086 | 0 | if (!mIsDoneAddingChildren) { |
1087 | 0 | return false; |
1088 | 0 | } |
1089 | 0 | |
1090 | 0 | uint32_t count = Length(); |
1091 | 0 | for (uint32_t i = 0; i < count; i++) { |
1092 | 0 | bool disabled; |
1093 | 0 | nsresult rv = IsOptionDisabled(i, &disabled); |
1094 | 0 |
|
1095 | 0 | if (NS_FAILED(rv) || !disabled) { |
1096 | 0 | rv = SetSelectedIndexInternal(i, aNotify); |
1097 | 0 | NS_ENSURE_SUCCESS(rv, false); |
1098 | 0 |
|
1099 | 0 | UpdateValueMissingValidityState(); |
1100 | 0 | UpdateState(aNotify); |
1101 | 0 |
|
1102 | 0 | return true; |
1103 | 0 | } |
1104 | 0 | } |
1105 | 0 |
|
1106 | 0 | return false; |
1107 | 0 | } |
1108 | | |
1109 | | nsresult |
1110 | | HTMLSelectElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent, |
1111 | | nsIContent* aBindingParent) |
1112 | 0 | { |
1113 | 0 | nsresult rv = nsGenericHTMLFormElementWithState::BindToTree(aDocument, aParent, |
1114 | 0 | aBindingParent); |
1115 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
1116 | 0 |
|
1117 | 0 | // If there is a disabled fieldset in the parent chain, the element is now |
1118 | 0 | // barred from constraint validation. |
1119 | 0 | // XXXbz is this still needed now that fieldset changes always call |
1120 | 0 | // FieldSetDisabledChanged? |
1121 | 0 | UpdateBarredFromConstraintValidation(); |
1122 | 0 |
|
1123 | 0 | // And now make sure our state is up to date |
1124 | 0 | UpdateState(false); |
1125 | 0 |
|
1126 | 0 | return rv; |
1127 | 0 | } |
1128 | | |
1129 | | void |
1130 | | HTMLSelectElement::UnbindFromTree(bool aDeep, bool aNullParent) |
1131 | 0 | { |
1132 | 0 | nsGenericHTMLFormElementWithState::UnbindFromTree(aDeep, aNullParent); |
1133 | 0 |
|
1134 | 0 | // We might be no longer disabled because our parent chain changed. |
1135 | 0 | // XXXbz is this still needed now that fieldset changes always call |
1136 | 0 | // FieldSetDisabledChanged? |
1137 | 0 | UpdateBarredFromConstraintValidation(); |
1138 | 0 |
|
1139 | 0 | // And now make sure our state is up to date |
1140 | 0 | UpdateState(false); |
1141 | 0 | } |
1142 | | |
1143 | | nsresult |
1144 | | HTMLSelectElement::BeforeSetAttr(int32_t aNameSpaceID, nsAtom* aName, |
1145 | | const nsAttrValueOrString* aValue, |
1146 | | bool aNotify) |
1147 | 0 | { |
1148 | 0 | if (aNameSpaceID == kNameSpaceID_None) { |
1149 | 0 | if (aName == nsGkAtoms::disabled) { |
1150 | 0 | if (aNotify) { |
1151 | 0 | mDisabledChanged = true; |
1152 | 0 | } |
1153 | 0 | } else if (aName == nsGkAtoms::multiple) { |
1154 | 0 | if (!aValue && aNotify && mSelectedIndex >= 0) { |
1155 | 0 | // We're changing from being a multi-select to a single-select. |
1156 | 0 | // Make sure we only have one option selected before we do that. |
1157 | 0 | // Note that this needs to come before we really unset the attr, |
1158 | 0 | // since SetOptionsSelectedByIndex does some bail-out type |
1159 | 0 | // optimization for cases when the select is not multiple that |
1160 | 0 | // would lead to only a single option getting deselected. |
1161 | 0 | SetSelectedIndexInternal(mSelectedIndex, aNotify); |
1162 | 0 | } |
1163 | 0 | } |
1164 | 0 | } |
1165 | 0 |
|
1166 | 0 | return nsGenericHTMLFormElementWithState::BeforeSetAttr(aNameSpaceID, aName, |
1167 | 0 | aValue, aNotify); |
1168 | 0 | } |
1169 | | |
1170 | | nsresult |
1171 | | HTMLSelectElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName, |
1172 | | const nsAttrValue* aValue, |
1173 | | const nsAttrValue* aOldValue, |
1174 | | nsIPrincipal* aSubjectPrincipal, |
1175 | | bool aNotify) |
1176 | 0 | { |
1177 | 0 | if (aNameSpaceID == kNameSpaceID_None) { |
1178 | 0 | if (aName == nsGkAtoms::disabled) { |
1179 | 0 | // This *has* to be called *before* validity state check because |
1180 | 0 | // UpdateBarredFromConstraintValidation and |
1181 | 0 | // UpdateValueMissingValidityState depend on our disabled state. |
1182 | 0 | UpdateDisabledState(aNotify); |
1183 | 0 |
|
1184 | 0 | UpdateValueMissingValidityState(); |
1185 | 0 | UpdateBarredFromConstraintValidation(); |
1186 | 0 | } else if (aName == nsGkAtoms::required) { |
1187 | 0 | // This *has* to be called *before* UpdateValueMissingValidityState |
1188 | 0 | // because UpdateValueMissingValidityState depends on our required |
1189 | 0 | // state. |
1190 | 0 | UpdateRequiredState(!!aValue, aNotify); |
1191 | 0 |
|
1192 | 0 | UpdateValueMissingValidityState(); |
1193 | 0 | } else if (aName == nsGkAtoms::autocomplete) { |
1194 | 0 | // Clear the cached @autocomplete attribute and autocompleteInfo state. |
1195 | 0 | mAutocompleteAttrState = nsContentUtils::eAutocompleteAttrState_Unknown; |
1196 | 0 | mAutocompleteInfoState = nsContentUtils::eAutocompleteAttrState_Unknown; |
1197 | 0 | } else if (aName == nsGkAtoms::multiple) { |
1198 | 0 | if (!aValue && aNotify) { |
1199 | 0 | // We might have become a combobox; make sure _something_ gets |
1200 | 0 | // selected in that case |
1201 | 0 | CheckSelectSomething(aNotify); |
1202 | 0 | } |
1203 | 0 | } |
1204 | 0 | } |
1205 | 0 |
|
1206 | 0 | return nsGenericHTMLFormElementWithState::AfterSetAttr(aNameSpaceID, aName, |
1207 | 0 | aValue, aOldValue, |
1208 | 0 | aSubjectPrincipal, |
1209 | 0 | aNotify); |
1210 | 0 | } |
1211 | | |
1212 | | void |
1213 | | HTMLSelectElement::DoneAddingChildren(bool aHaveNotified) |
1214 | 0 | { |
1215 | 0 | mIsDoneAddingChildren = true; |
1216 | 0 |
|
1217 | 0 | nsISelectControlFrame* selectFrame = GetSelectFrame(); |
1218 | 0 |
|
1219 | 0 | // If we foolishly tried to restore before we were done adding |
1220 | 0 | // content, restore the rest of the options proper-like |
1221 | 0 | if (mRestoreState) { |
1222 | 0 | RestoreStateTo(*mRestoreState); |
1223 | 0 | mRestoreState = nullptr; |
1224 | 0 | } |
1225 | 0 |
|
1226 | 0 | // Notify the frame |
1227 | 0 | if (selectFrame) { |
1228 | 0 | selectFrame->DoneAddingChildren(true); |
1229 | 0 | } |
1230 | 0 |
|
1231 | 0 | if (!mInhibitStateRestoration) { |
1232 | 0 | nsresult rv = GenerateStateKey(); |
1233 | 0 | if (NS_SUCCEEDED(rv)) { |
1234 | 0 | RestoreFormControlState(); |
1235 | 0 | } |
1236 | 0 | } |
1237 | 0 |
|
1238 | 0 | // Now that we're done, select something (if it's a single select something |
1239 | 0 | // must be selected) |
1240 | 0 | if (!CheckSelectSomething(false)) { |
1241 | 0 | // If an option has @selected set, it will be selected during parsing but |
1242 | 0 | // with an empty value. We have to make sure the select element updates it's |
1243 | 0 | // validity state to take this into account. |
1244 | 0 | UpdateValueMissingValidityState(); |
1245 | 0 |
|
1246 | 0 | // And now make sure we update our content state too |
1247 | 0 | UpdateState(aHaveNotified); |
1248 | 0 | } |
1249 | 0 |
|
1250 | 0 | mDefaultSelectionSet = true; |
1251 | 0 | } |
1252 | | |
1253 | | bool |
1254 | | HTMLSelectElement::ParseAttribute(int32_t aNamespaceID, |
1255 | | nsAtom* aAttribute, |
1256 | | const nsAString& aValue, |
1257 | | nsIPrincipal* aMaybeScriptedPrincipal, |
1258 | | nsAttrValue& aResult) |
1259 | 0 | { |
1260 | 0 | if (kNameSpaceID_None == aNamespaceID) { |
1261 | 0 | if (aAttribute == nsGkAtoms::size) { |
1262 | 0 | return aResult.ParsePositiveIntValue(aValue); |
1263 | 0 | } else if (aAttribute == nsGkAtoms::autocomplete) { |
1264 | 0 | aResult.ParseAtomArray(aValue); |
1265 | 0 | return true; |
1266 | 0 | } |
1267 | 0 | } |
1268 | 0 | return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue, |
1269 | 0 | aMaybeScriptedPrincipal, aResult); |
1270 | 0 | } |
1271 | | |
1272 | | void |
1273 | | HTMLSelectElement::MapAttributesIntoRule(const nsMappedAttributes* aAttributes, |
1274 | | MappedDeclarations& aDecls) |
1275 | 0 | { |
1276 | 0 | nsGenericHTMLFormElementWithState::MapImageAlignAttributeInto(aAttributes, aDecls); |
1277 | 0 | nsGenericHTMLFormElementWithState::MapCommonAttributesInto(aAttributes, aDecls); |
1278 | 0 | } |
1279 | | |
1280 | | nsChangeHint |
1281 | | HTMLSelectElement::GetAttributeChangeHint(const nsAtom* aAttribute, |
1282 | | int32_t aModType) const |
1283 | 0 | { |
1284 | 0 | nsChangeHint retval = |
1285 | 0 | nsGenericHTMLFormElementWithState::GetAttributeChangeHint(aAttribute, aModType); |
1286 | 0 | if (aAttribute == nsGkAtoms::multiple || |
1287 | 0 | aAttribute == nsGkAtoms::size) { |
1288 | 0 | retval |= nsChangeHint_ReconstructFrame; |
1289 | 0 | } |
1290 | 0 | return retval; |
1291 | 0 | } |
1292 | | |
1293 | | NS_IMETHODIMP_(bool) |
1294 | | HTMLSelectElement::IsAttributeMapped(const nsAtom* aAttribute) const |
1295 | 0 | { |
1296 | 0 | static const MappedAttributeEntry* const map[] = { |
1297 | 0 | sCommonAttributeMap, |
1298 | 0 | sImageAlignAttributeMap |
1299 | 0 | }; |
1300 | 0 |
|
1301 | 0 | return FindAttributeDependence(aAttribute, map); |
1302 | 0 | } |
1303 | | |
1304 | | nsMapRuleToAttributesFunc |
1305 | | HTMLSelectElement::GetAttributeMappingFunction() const |
1306 | 0 | { |
1307 | 0 | return &MapAttributesIntoRule; |
1308 | 0 | } |
1309 | | |
1310 | | bool |
1311 | | HTMLSelectElement::IsDisabledForEvents(EventMessage aMessage) |
1312 | 0 | { |
1313 | 0 | nsIFormControlFrame* formControlFrame = GetFormControlFrame(false); |
1314 | 0 | nsIFrame* formFrame = nullptr; |
1315 | 0 | if (formControlFrame) { |
1316 | 0 | formFrame = do_QueryFrame(formControlFrame); |
1317 | 0 | } |
1318 | 0 | return IsElementDisabledForEvents(aMessage, formFrame); |
1319 | 0 | } |
1320 | | |
1321 | | void |
1322 | | HTMLSelectElement::GetEventTargetParent(EventChainPreVisitor& aVisitor) |
1323 | 0 | { |
1324 | 0 | aVisitor.mCanHandle = false; |
1325 | 0 | if (IsDisabledForEvents(aVisitor.mEvent->mMessage)) { |
1326 | 0 | return; |
1327 | 0 | } |
1328 | 0 | |
1329 | 0 | nsGenericHTMLFormElementWithState::GetEventTargetParent(aVisitor); |
1330 | 0 | } |
1331 | | |
1332 | | nsresult |
1333 | | HTMLSelectElement::PostHandleEvent(EventChainPostVisitor& aVisitor) |
1334 | 0 | { |
1335 | 0 | if (aVisitor.mEvent->mMessage == eFocus) { |
1336 | 0 | // If the invalid UI is shown, we should show it while focused and |
1337 | 0 | // update the invalid/valid UI. |
1338 | 0 | mCanShowInvalidUI = !IsValid() && ShouldShowValidityUI(); |
1339 | 0 |
|
1340 | 0 | // If neither invalid UI nor valid UI is shown, we shouldn't show the valid |
1341 | 0 | // UI while focused. |
1342 | 0 | mCanShowValidUI = ShouldShowValidityUI(); |
1343 | 0 |
|
1344 | 0 | // We don't have to update NS_EVENT_STATE_MOZ_UI_INVALID nor |
1345 | 0 | // NS_EVENT_STATE_MOZ_UI_VALID given that the states should not change. |
1346 | 0 | } else if (aVisitor.mEvent->mMessage == eBlur) { |
1347 | 0 | mCanShowInvalidUI = true; |
1348 | 0 | mCanShowValidUI = true; |
1349 | 0 |
|
1350 | 0 | UpdateState(true); |
1351 | 0 | } |
1352 | 0 |
|
1353 | 0 | return nsGenericHTMLFormElementWithState::PostHandleEvent(aVisitor); |
1354 | 0 | } |
1355 | | |
1356 | | EventStates |
1357 | | HTMLSelectElement::IntrinsicState() const |
1358 | 0 | { |
1359 | 0 | EventStates state = nsGenericHTMLFormElementWithState::IntrinsicState(); |
1360 | 0 |
|
1361 | 0 | if (IsCandidateForConstraintValidation()) { |
1362 | 0 | if (IsValid()) { |
1363 | 0 | state |= NS_EVENT_STATE_VALID; |
1364 | 0 | } else { |
1365 | 0 | state |= NS_EVENT_STATE_INVALID; |
1366 | 0 |
|
1367 | 0 | if ((!mForm || !mForm->HasAttr(kNameSpaceID_None, nsGkAtoms::novalidate)) && |
1368 | 0 | (GetValidityState(VALIDITY_STATE_CUSTOM_ERROR) || |
1369 | 0 | (mCanShowInvalidUI && ShouldShowValidityUI()))) { |
1370 | 0 | state |= NS_EVENT_STATE_MOZ_UI_INVALID; |
1371 | 0 | } |
1372 | 0 | } |
1373 | 0 |
|
1374 | 0 | // :-moz-ui-valid applies if all the following are true: |
1375 | 0 | // 1. The element is not focused, or had either :-moz-ui-valid or |
1376 | 0 | // :-moz-ui-invalid applying before it was focused ; |
1377 | 0 | // 2. The element is either valid or isn't allowed to have |
1378 | 0 | // :-moz-ui-invalid applying ; |
1379 | 0 | // 3. The element has no form owner or its form owner doesn't have the |
1380 | 0 | // novalidate attribute set ; |
1381 | 0 | // 4. The element has already been modified or the user tried to submit the |
1382 | 0 | // form owner while invalid. |
1383 | 0 | if ((!mForm || !mForm->HasAttr(kNameSpaceID_None, nsGkAtoms::novalidate)) && |
1384 | 0 | (mCanShowValidUI && ShouldShowValidityUI() && |
1385 | 0 | (IsValid() || (state.HasState(NS_EVENT_STATE_MOZ_UI_INVALID) && |
1386 | 0 | !mCanShowInvalidUI)))) { |
1387 | 0 | state |= NS_EVENT_STATE_MOZ_UI_VALID; |
1388 | 0 | } |
1389 | 0 | } |
1390 | 0 |
|
1391 | 0 | return state; |
1392 | 0 | } |
1393 | | |
1394 | | // nsIFormControl |
1395 | | |
1396 | | NS_IMETHODIMP |
1397 | | HTMLSelectElement::SaveState() |
1398 | 0 | { |
1399 | 0 | PresState* presState = GetPrimaryPresState(); |
1400 | 0 | if (!presState) { |
1401 | 0 | return NS_OK; |
1402 | 0 | } |
1403 | 0 | |
1404 | 0 | SelectContentData state; |
1405 | 0 |
|
1406 | 0 | uint32_t len = Length(); |
1407 | 0 |
|
1408 | 0 | for (uint32_t optIndex = 0; optIndex < len; optIndex++) { |
1409 | 0 | HTMLOptionElement* option = Item(optIndex); |
1410 | 0 | if (option && option->Selected()) { |
1411 | 0 | nsAutoString value; |
1412 | 0 | option->GetValue(value); |
1413 | 0 | if (value.IsEmpty()) { |
1414 | 0 | state.indices().AppendElement(optIndex); |
1415 | 0 | } else { |
1416 | 0 | state.values().AppendElement(std::move(value)); |
1417 | 0 | } |
1418 | 0 | } |
1419 | 0 | } |
1420 | 0 |
|
1421 | 0 | presState->contentData() = std::move(state); |
1422 | 0 |
|
1423 | 0 | if (mDisabledChanged) { |
1424 | 0 | // We do not want to save the real disabled state but the disabled |
1425 | 0 | // attribute. |
1426 | 0 | presState->disabled() = HasAttr(kNameSpaceID_None, nsGkAtoms::disabled); |
1427 | 0 | presState->disabledSet() = true; |
1428 | 0 | } |
1429 | 0 |
|
1430 | 0 | return NS_OK; |
1431 | 0 | } |
1432 | | |
1433 | | bool |
1434 | | HTMLSelectElement::RestoreState(PresState* aState) |
1435 | 0 | { |
1436 | 0 | // Get the presentation state object to retrieve our stuff out of. |
1437 | 0 | const PresContentData& state = aState->contentData(); |
1438 | 0 | if (state.type() == PresContentData::TSelectContentData) { |
1439 | 0 | RestoreStateTo(state.get_SelectContentData()); |
1440 | 0 |
|
1441 | 0 | // Don't flush, if the frame doesn't exist yet it doesn't care if |
1442 | 0 | // we're reset or not. |
1443 | 0 | DispatchContentReset(); |
1444 | 0 | } |
1445 | 0 |
|
1446 | 0 | if (aState->disabledSet() && !aState->disabled()) { |
1447 | 0 | SetDisabled(false, IgnoreErrors()); |
1448 | 0 | } |
1449 | 0 |
|
1450 | 0 | return false; |
1451 | 0 | } |
1452 | | |
1453 | | void |
1454 | | HTMLSelectElement::RestoreStateTo(const SelectContentData& aNewSelected) |
1455 | 0 | { |
1456 | 0 | if (!mIsDoneAddingChildren) { |
1457 | 0 | // Make a copy of the state for us to restore from in the future. |
1458 | 0 | mRestoreState = MakeUnique<SelectContentData>(aNewSelected); |
1459 | 0 | return; |
1460 | 0 | } |
1461 | 0 | |
1462 | 0 | uint32_t len = Length(); |
1463 | 0 | uint32_t mask = IS_SELECTED | CLEAR_ALL | SET_DISABLED | NOTIFY; |
1464 | 0 |
|
1465 | 0 | // First clear all |
1466 | 0 | SetOptionsSelectedByIndex(-1, -1, mask); |
1467 | 0 |
|
1468 | 0 | // Select by index. |
1469 | 0 | for (uint32_t idx : aNewSelected.indices()) { |
1470 | 0 | if (idx < len) { |
1471 | 0 | SetOptionsSelectedByIndex(idx, idx, IS_SELECTED | SET_DISABLED | NOTIFY); |
1472 | 0 | } |
1473 | 0 | } |
1474 | 0 |
|
1475 | 0 | // Select by value. |
1476 | 0 | for (uint32_t i = 0; i < len; ++i) { |
1477 | 0 | HTMLOptionElement* option = Item(i); |
1478 | 0 | if (option) { |
1479 | 0 | nsAutoString value; |
1480 | 0 | option->GetValue(value); |
1481 | 0 | if (aNewSelected.values().Contains(value)) { |
1482 | 0 | SetOptionsSelectedByIndex(i, i, IS_SELECTED | SET_DISABLED | NOTIFY); |
1483 | 0 | } |
1484 | 0 | } |
1485 | 0 | } |
1486 | 0 | } |
1487 | | |
1488 | | NS_IMETHODIMP |
1489 | | HTMLSelectElement::Reset() |
1490 | 0 | { |
1491 | 0 | uint32_t numSelected = 0; |
1492 | 0 |
|
1493 | 0 | // |
1494 | 0 | // Cycle through the options array and reset the options |
1495 | 0 | // |
1496 | 0 | uint32_t numOptions = Length(); |
1497 | 0 |
|
1498 | 0 | for (uint32_t i = 0; i < numOptions; i++) { |
1499 | 0 | RefPtr<HTMLOptionElement> option = Item(i); |
1500 | 0 | if (option) { |
1501 | 0 | // |
1502 | 0 | // Reset the option to its default value |
1503 | 0 | // |
1504 | 0 |
|
1505 | 0 | uint32_t mask = SET_DISABLED | NOTIFY | NO_RESELECT; |
1506 | 0 | if (option->DefaultSelected()) { |
1507 | 0 | mask |= IS_SELECTED; |
1508 | 0 | numSelected++; |
1509 | 0 | } |
1510 | 0 |
|
1511 | 0 | SetOptionsSelectedByIndex(i, i, mask); |
1512 | 0 | option->SetSelectedChanged(false); |
1513 | 0 | } |
1514 | 0 | } |
1515 | 0 |
|
1516 | 0 | // |
1517 | 0 | // If nothing was selected and it's not multiple, select something |
1518 | 0 | // |
1519 | 0 | if (numSelected == 0 && IsCombobox()) { |
1520 | 0 | SelectSomething(true); |
1521 | 0 | } |
1522 | 0 |
|
1523 | 0 | SetSelectionChanged(false, true); |
1524 | 0 |
|
1525 | 0 | // |
1526 | 0 | // Let the frame know we were reset |
1527 | 0 | // |
1528 | 0 | // Don't flush, if there's no frame yet it won't care about us being |
1529 | 0 | // reset even if we forced it to be created now. |
1530 | 0 | // |
1531 | 0 | DispatchContentReset(); |
1532 | 0 |
|
1533 | 0 | return NS_OK; |
1534 | 0 | } |
1535 | | |
1536 | | static NS_DEFINE_CID(kFormProcessorCID, NS_FORMPROCESSOR_CID); |
1537 | | |
1538 | | NS_IMETHODIMP |
1539 | | HTMLSelectElement::SubmitNamesValues(HTMLFormSubmission* aFormSubmission) |
1540 | 0 | { |
1541 | 0 | // Disabled elements don't submit |
1542 | 0 | if (IsDisabled()) { |
1543 | 0 | return NS_OK; |
1544 | 0 | } |
1545 | 0 | |
1546 | 0 | // |
1547 | 0 | // Get the name (if no name, no submit) |
1548 | 0 | // |
1549 | 0 | nsAutoString name; |
1550 | 0 | GetAttr(kNameSpaceID_None, nsGkAtoms::name, name); |
1551 | 0 | if (name.IsEmpty()) { |
1552 | 0 | return NS_OK; |
1553 | 0 | } |
1554 | 0 | |
1555 | 0 | // |
1556 | 0 | // Submit |
1557 | 0 | // |
1558 | 0 | uint32_t len = Length(); |
1559 | 0 |
|
1560 | 0 | nsAutoString mozType; |
1561 | 0 | nsCOMPtr<nsIFormProcessor> keyGenProcessor; |
1562 | 0 | if (GetAttr(kNameSpaceID_None, nsGkAtoms::moztype, mozType) && |
1563 | 0 | mozType.EqualsLiteral("-mozilla-keygen")) { |
1564 | 0 | keyGenProcessor = do_GetService(kFormProcessorCID); |
1565 | 0 | } |
1566 | 0 |
|
1567 | 0 | for (uint32_t optIndex = 0; optIndex < len; optIndex++) { |
1568 | 0 | HTMLOptionElement* option = Item(optIndex); |
1569 | 0 |
|
1570 | 0 | // Don't send disabled options |
1571 | 0 | if (!option || IsOptionDisabled(option)) { |
1572 | 0 | continue; |
1573 | 0 | } |
1574 | 0 | |
1575 | 0 | if (!option->Selected()) { |
1576 | 0 | continue; |
1577 | 0 | } |
1578 | 0 | |
1579 | 0 | nsString value; |
1580 | 0 | option->GetValue(value); |
1581 | 0 |
|
1582 | 0 | if (keyGenProcessor) { |
1583 | 0 | nsString tmp(value); |
1584 | 0 | if (NS_SUCCEEDED(keyGenProcessor->ProcessValue(this, name, tmp))) { |
1585 | 0 | value = tmp; |
1586 | 0 | } |
1587 | 0 | } |
1588 | 0 |
|
1589 | 0 | aFormSubmission->AddNameValuePair(name, value); |
1590 | 0 | } |
1591 | 0 |
|
1592 | 0 | return NS_OK; |
1593 | 0 | } |
1594 | | |
1595 | | void |
1596 | | HTMLSelectElement::DispatchContentReset() |
1597 | 0 | { |
1598 | 0 | nsIFormControlFrame* formControlFrame = GetFormControlFrame(false); |
1599 | 0 | if (formControlFrame) { |
1600 | 0 | // Only dispatch content reset notification if this is a list control |
1601 | 0 | // frame or combo box control frame. |
1602 | 0 | if (IsCombobox()) { |
1603 | 0 | nsIComboboxControlFrame* comboFrame = do_QueryFrame(formControlFrame); |
1604 | 0 | if (comboFrame) { |
1605 | 0 | comboFrame->OnContentReset(); |
1606 | 0 | } |
1607 | 0 | } else { |
1608 | 0 | nsIListControlFrame* listFrame = do_QueryFrame(formControlFrame); |
1609 | 0 | if (listFrame) { |
1610 | 0 | listFrame->OnContentReset(); |
1611 | 0 | } |
1612 | 0 | } |
1613 | 0 | } |
1614 | 0 | } |
1615 | | |
1616 | | static void |
1617 | | AddOptions(nsIContent* aRoot, HTMLOptionsCollection* aArray) |
1618 | 0 | { |
1619 | 0 | for (nsIContent* child = aRoot->GetFirstChild(); |
1620 | 0 | child; |
1621 | 0 | child = child->GetNextSibling()) { |
1622 | 0 | HTMLOptionElement* opt = HTMLOptionElement::FromNode(child); |
1623 | 0 | if (opt) { |
1624 | 0 | aArray->AppendOption(opt); |
1625 | 0 | } else if (child->IsHTMLElement(nsGkAtoms::optgroup)) { |
1626 | 0 | for (nsIContent* grandchild = child->GetFirstChild(); |
1627 | 0 | grandchild; |
1628 | 0 | grandchild = grandchild->GetNextSibling()) { |
1629 | 0 | opt = HTMLOptionElement::FromNode(grandchild); |
1630 | 0 | if (opt) { |
1631 | 0 | aArray->AppendOption(opt); |
1632 | 0 | } |
1633 | 0 | } |
1634 | 0 | } |
1635 | 0 | } |
1636 | 0 | } |
1637 | | |
1638 | | void |
1639 | | HTMLSelectElement::RebuildOptionsArray(bool aNotify) |
1640 | 0 | { |
1641 | 0 | mOptions->Clear(); |
1642 | 0 | AddOptions(this, mOptions); |
1643 | 0 | FindSelectedIndex(0, aNotify); |
1644 | 0 | } |
1645 | | |
1646 | | bool |
1647 | | HTMLSelectElement::IsValueMissing() const |
1648 | 0 | { |
1649 | 0 | if (!Required()) { |
1650 | 0 | return false; |
1651 | 0 | } |
1652 | 0 | |
1653 | 0 | uint32_t length = Length(); |
1654 | 0 |
|
1655 | 0 | for (uint32_t i = 0; i < length; ++i) { |
1656 | 0 | RefPtr<HTMLOptionElement> option = Item(i); |
1657 | 0 | // Check for a placeholder label option, don't count it as a valid value. |
1658 | 0 | if (i == 0 && !Multiple() && Size() <= 1 && option->GetParent() == this) { |
1659 | 0 | nsAutoString value; |
1660 | 0 | option->GetValue(value); |
1661 | 0 | if (value.IsEmpty()) { |
1662 | 0 | continue; |
1663 | 0 | } |
1664 | 0 | } |
1665 | 0 | |
1666 | 0 | if (!option->Selected()) { |
1667 | 0 | continue; |
1668 | 0 | } |
1669 | 0 | |
1670 | 0 | if (IsOptionDisabled(option)) { |
1671 | 0 | continue; |
1672 | 0 | } |
1673 | 0 | |
1674 | 0 | return false; |
1675 | 0 | } |
1676 | 0 |
|
1677 | 0 | return true; |
1678 | 0 | } |
1679 | | |
1680 | | void |
1681 | | HTMLSelectElement::UpdateValueMissingValidityState() |
1682 | 0 | { |
1683 | 0 | SetValidityState(VALIDITY_STATE_VALUE_MISSING, IsValueMissing()); |
1684 | 0 | } |
1685 | | |
1686 | | nsresult |
1687 | | HTMLSelectElement::GetValidationMessage(nsAString& aValidationMessage, |
1688 | | ValidityStateType aType) |
1689 | | { |
1690 | | switch (aType) { |
1691 | | case VALIDITY_STATE_VALUE_MISSING: { |
1692 | | nsAutoString message; |
1693 | | nsresult rv = nsContentUtils::GetLocalizedString(nsContentUtils::eDOM_PROPERTIES, |
1694 | | "FormValidationSelectMissing", |
1695 | | message); |
1696 | | aValidationMessage = message; |
1697 | | return rv; |
1698 | | } |
1699 | | default: { |
1700 | | return nsIConstraintValidation::GetValidationMessage(aValidationMessage, aType); |
1701 | | } |
1702 | | } |
1703 | | } |
1704 | | |
1705 | | #ifdef DEBUG |
1706 | | |
1707 | | void |
1708 | | HTMLSelectElement::VerifyOptionsArray() |
1709 | | { |
1710 | | int32_t index = 0; |
1711 | | for (nsIContent* child = nsINode::GetFirstChild(); |
1712 | | child; |
1713 | | child = child->GetNextSibling()) { |
1714 | | HTMLOptionElement* opt = HTMLOptionElement::FromNode(child); |
1715 | | if (opt) { |
1716 | | NS_ASSERTION(opt == mOptions->ItemAsOption(index++), |
1717 | | "Options collection broken"); |
1718 | | } else if (child->IsHTMLElement(nsGkAtoms::optgroup)) { |
1719 | | for (nsIContent* grandchild = child->GetFirstChild(); |
1720 | | grandchild; |
1721 | | grandchild = grandchild->GetNextSibling()) { |
1722 | | opt = HTMLOptionElement::FromNode(grandchild); |
1723 | | if (opt) { |
1724 | | NS_ASSERTION(opt == mOptions->ItemAsOption(index++), |
1725 | | "Options collection broken"); |
1726 | | } |
1727 | | } |
1728 | | } |
1729 | | } |
1730 | | } |
1731 | | |
1732 | | #endif |
1733 | | |
1734 | | void |
1735 | | HTMLSelectElement::UpdateBarredFromConstraintValidation() |
1736 | 0 | { |
1737 | 0 | SetBarredFromConstraintValidation(IsDisabled()); |
1738 | 0 | } |
1739 | | |
1740 | | void |
1741 | | HTMLSelectElement::FieldSetDisabledChanged(bool aNotify) |
1742 | 0 | { |
1743 | 0 | // This *has* to be called before UpdateBarredFromConstraintValidation and |
1744 | 0 | // UpdateValueMissingValidityState because these two functions depend on our |
1745 | 0 | // disabled state. |
1746 | 0 | nsGenericHTMLFormElementWithState::FieldSetDisabledChanged(aNotify); |
1747 | 0 |
|
1748 | 0 | UpdateValueMissingValidityState(); |
1749 | 0 | UpdateBarredFromConstraintValidation(); |
1750 | 0 | UpdateState(aNotify); |
1751 | 0 | } |
1752 | | |
1753 | | void |
1754 | | HTMLSelectElement::SetSelectionChanged(bool aValue, bool aNotify) |
1755 | 0 | { |
1756 | 0 | if (!mDefaultSelectionSet) { |
1757 | 0 | return; |
1758 | 0 | } |
1759 | 0 | |
1760 | 0 | UpdateSelectedOptions(); |
1761 | 0 |
|
1762 | 0 | bool previousSelectionChangedValue = mSelectionHasChanged; |
1763 | 0 | mSelectionHasChanged = aValue; |
1764 | 0 |
|
1765 | 0 | if (mSelectionHasChanged != previousSelectionChangedValue) { |
1766 | 0 | UpdateState(aNotify); |
1767 | 0 | } |
1768 | 0 | } |
1769 | | |
1770 | | void |
1771 | | HTMLSelectElement::UpdateSelectedOptions() |
1772 | 0 | { |
1773 | 0 | if (mSelectedOptions) { |
1774 | 0 | mSelectedOptions->SetDirty(); |
1775 | 0 | } |
1776 | 0 | } |
1777 | | |
1778 | | bool |
1779 | | HTMLSelectElement::OpenInParentProcess() |
1780 | 0 | { |
1781 | 0 | nsIFormControlFrame* formControlFrame = GetFormControlFrame(false); |
1782 | 0 | nsIComboboxControlFrame* comboFrame = do_QueryFrame(formControlFrame); |
1783 | 0 | if (comboFrame) { |
1784 | 0 | return comboFrame->IsOpenInParentProcess(); |
1785 | 0 | } |
1786 | 0 | |
1787 | 0 | return false; |
1788 | 0 | } |
1789 | | |
1790 | | void |
1791 | | HTMLSelectElement::SetOpenInParentProcess(bool aVal) |
1792 | 0 | { |
1793 | 0 | nsIFormControlFrame* formControlFrame = GetFormControlFrame(false); |
1794 | 0 | nsIComboboxControlFrame* comboFrame = do_QueryFrame(formControlFrame); |
1795 | 0 | if (comboFrame) { |
1796 | 0 | comboFrame->SetOpenInParentProcess(aVal); |
1797 | 0 | } |
1798 | 0 | } |
1799 | | |
1800 | | void |
1801 | | HTMLSelectElement::SetPreviewValue(const nsAString& aValue) |
1802 | 0 | { |
1803 | 0 | mPreviewValue = aValue; |
1804 | 0 | nsContentUtils::RemoveNewlines(mPreviewValue); |
1805 | 0 | nsIFormControlFrame* formControlFrame = GetFormControlFrame(false); |
1806 | 0 | nsIComboboxControlFrame* comboFrame = do_QueryFrame(formControlFrame); |
1807 | 0 | if (comboFrame) { |
1808 | 0 | comboFrame->RedisplaySelectedText(); |
1809 | 0 | } |
1810 | 0 | } |
1811 | | |
1812 | | JSObject* |
1813 | | HTMLSelectElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
1814 | 0 | { |
1815 | 0 | return HTMLSelectElement_Binding::Wrap(aCx, this, aGivenProto); |
1816 | 0 | } |
1817 | | |
1818 | | } // namespace dom |
1819 | | } // namespace mozilla |