2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-08-28 11:09:32 +04:00
|
|
|
|
2013-04-04 11:03:33 +04:00
|
|
|
#include "mozilla/dom/HTMLSelectElement.h"
|
2011-05-28 11:43:53 +04:00
|
|
|
|
2013-03-17 11:55:17 +04:00
|
|
|
#include "mozAutoDocUpdate.h"
|
2013-07-10 13:56:47 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-03-17 10:56:53 +04:00
|
|
|
#include "mozilla/BasicEvents.h"
|
2014-03-18 08:48:21 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2014-04-03 08:18:36 +04:00
|
|
|
#include "mozilla/EventStates.h"
|
2013-03-17 11:55:17 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2013-04-13 11:01:04 +04:00
|
|
|
#include "mozilla/dom/HTMLOptGroupElement.h"
|
2013-02-18 16:24:58 +04:00
|
|
|
#include "mozilla/dom/HTMLOptionElement.h"
|
2013-04-13 11:01:59 +04:00
|
|
|
#include "mozilla/dom/HTMLSelectElementBinding.h"
|
2014-10-01 22:43:26 +04:00
|
|
|
#include "mozilla/dom/UnionTypes.h"
|
2006-07-19 08:36:36 +04:00
|
|
|
#include "nsContentCreatorFunctions.h"
|
2013-09-02 21:23:27 +04:00
|
|
|
#include "nsContentList.h"
|
2013-03-17 11:55:17 +04:00
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsFormSubmission.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsIComboboxControlFrame.h"
|
1999-01-30 04:02:28 +03:00
|
|
|
#include "nsIDocument.h"
|
1999-03-02 06:31:17 +03:00
|
|
|
#include "nsIFormControlFrame.h"
|
2013-03-17 11:55:17 +04:00
|
|
|
#include "nsIForm.h"
|
|
|
|
#include "nsIFormProcessor.h"
|
1999-01-30 04:02:28 +03:00
|
|
|
#include "nsIFrame.h"
|
2013-03-17 11:55:17 +04:00
|
|
|
#include "nsIListControlFrame.h"
|
|
|
|
#include "nsISelectControlFrame.h"
|
|
|
|
#include "nsLayoutUtils.h"
|
|
|
|
#include "nsMappedAttributes.h"
|
|
|
|
#include "nsPresState.h"
|
2004-07-20 10:11:27 +04:00
|
|
|
#include "nsRuleData.h"
|
2013-03-17 11:55:17 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsStyleConsts.h"
|
2013-04-04 16:01:08 +04:00
|
|
|
#include "nsTextNode.h"
|
2010-05-09 22:32:57 +04:00
|
|
|
|
2013-04-04 11:03:33 +04:00
|
|
|
NS_IMPL_NS_NEW_HTML_ELEMENT_CHECK_PARSER(Select)
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(SelectState, SelectState)
|
2002-08-17 05:56:04 +04:00
|
|
|
|
2007-08-28 11:09:32 +04:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
2013-04-04 11:03:33 +04:00
|
|
|
// SafeOptionListMutation
|
2007-08-28 11:09:32 +04:00
|
|
|
//
|
2002-08-17 05:56:04 +04:00
|
|
|
|
2013-04-04 11:03:33 +04:00
|
|
|
SafeOptionListMutation::SafeOptionListMutation(nsIContent* aSelect,
|
|
|
|
nsIContent* aParent,
|
|
|
|
nsIContent* aKid,
|
|
|
|
uint32_t aIndex,
|
|
|
|
bool aNotify)
|
|
|
|
: mSelect(HTMLSelectElement::FromContentOrNull(aSelect))
|
2011-10-17 18:59:28 +04:00
|
|
|
, mTopLevelMutation(false)
|
|
|
|
, mNeedsRebuild(false)
|
2007-08-28 11:09:32 +04:00
|
|
|
{
|
2011-04-12 16:32:00 +04:00
|
|
|
if (mSelect) {
|
|
|
|
mTopLevelMutation = !mSelect->mMutating;
|
2007-08-28 11:09:32 +04:00
|
|
|
if (mTopLevelMutation) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mSelect->mMutating = true;
|
2002-10-03 07:43:11 +04:00
|
|
|
} else {
|
2007-08-28 11:09:32 +04:00
|
|
|
// This is very unfortunate, but to handle mutation events properly,
|
|
|
|
// option list must be up-to-date before inserting or removing options.
|
|
|
|
// Fortunately this is called only if mutation event listener
|
|
|
|
// adds or removes options.
|
2011-04-12 16:32:00 +04:00
|
|
|
mSelect->RebuildOptionsArray(aNotify);
|
2002-10-03 07:43:11 +04:00
|
|
|
}
|
2007-08-28 11:09:32 +04:00
|
|
|
nsresult rv;
|
|
|
|
if (aKid) {
|
2010-11-17 02:41:19 +03:00
|
|
|
rv = mSelect->WillAddOptions(aKid, aParent, aIndex, aNotify);
|
2007-08-28 11:09:32 +04:00
|
|
|
} else {
|
2010-11-17 02:41:19 +03:00
|
|
|
rv = mSelect->WillRemoveOptions(aParent, aIndex, aNotify);
|
2007-08-28 11:09:32 +04:00
|
|
|
}
|
|
|
|
mNeedsRebuild = NS_FAILED(rv);
|
2002-10-03 07:43:11 +04:00
|
|
|
}
|
2007-08-28 11:09:32 +04:00
|
|
|
}
|
2002-10-03 07:43:11 +04:00
|
|
|
|
2013-04-04 11:03:33 +04:00
|
|
|
SafeOptionListMutation::~SafeOptionListMutation()
|
1998-09-03 03:53:16 +04:00
|
|
|
{
|
2007-08-28 11:09:32 +04:00
|
|
|
if (mSelect) {
|
|
|
|
if (mNeedsRebuild || (mTopLevelMutation && mGuard.Mutated(1))) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mSelect->RebuildOptionsArray(true);
|
2007-08-28 11:09:32 +04:00
|
|
|
}
|
|
|
|
if (mTopLevelMutation) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mSelect->mMutating = false;
|
2007-08-28 11:09:32 +04:00
|
|
|
}
|
2006-03-03 00:30:23 +03:00
|
|
|
#ifdef DEBUG
|
2011-04-12 16:32:00 +04:00
|
|
|
mSelect->VerifyOptionsArray();
|
2006-03-03 00:30:23 +03:00
|
|
|
#endif
|
2007-08-28 11:09:32 +04:00
|
|
|
}
|
|
|
|
}
|
1998-09-23 21:16:51 +04:00
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
2013-04-04 11:03:33 +04:00
|
|
|
// HTMLSelectElement
|
2001-11-02 10:40:01 +03:00
|
|
|
//
|
1998-09-23 21:16:51 +04:00
|
|
|
|
|
|
|
// construction, destruction
|
|
|
|
|
2004-05-19 00:58:12 +04:00
|
|
|
|
2014-06-20 06:01:40 +04:00
|
|
|
HTMLSelectElement::HTMLSelectElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo,
|
2013-04-04 11:03:33 +04:00
|
|
|
FromParser aFromParser)
|
2013-08-02 05:21:31 +04:00
|
|
|
: nsGenericHTMLFormElementWithState(aNodeInfo),
|
2015-01-07 08:39:46 +03:00
|
|
|
mOptions(new HTMLOptionsCollection(this)),
|
2014-07-24 18:55:00 +04:00
|
|
|
mAutocompleteAttrState(nsContentUtils::eAutocompleteAttrState_Unknown),
|
2004-05-19 00:58:12 +04:00
|
|
|
mIsDoneAddingChildren(!aFromParser),
|
2011-10-17 18:59:28 +04:00
|
|
|
mDisabledChanged(false),
|
|
|
|
mMutating(false),
|
2010-10-25 16:17:38 +04:00
|
|
|
mInhibitStateRestoration(!!(aFromParser & FROM_PARSER_FRAGMENT)),
|
2011-10-17 18:59:28 +04:00
|
|
|
mSelectionHasChanged(false),
|
|
|
|
mDefaultSelectionSet(false),
|
|
|
|
mCanShowInvalidUI(true),
|
|
|
|
mCanShowValidUI(true),
|
2004-05-19 00:58:12 +04:00
|
|
|
mNonOptionChildren(0),
|
|
|
|
mOptGroupCount(0),
|
2006-03-03 00:30:23 +03:00
|
|
|
mSelectedIndex(-1)
|
1998-09-03 03:53:16 +04:00
|
|
|
{
|
2013-12-17 17:58:32 +04:00
|
|
|
SetHasWeirdParserInsertionMode();
|
|
|
|
|
2002-03-31 14:14:01 +04:00
|
|
|
// DoneAddingChildren() will be called later if it's from the parser,
|
|
|
|
// otherwise it is
|
2011-06-01 05:46:57 +04:00
|
|
|
|
|
|
|
// Set up our default state: enabled, optional, and valid.
|
|
|
|
AddStatesSilently(NS_EVENT_STATE_ENABLED |
|
|
|
|
NS_EVENT_STATE_OPTIONAL |
|
|
|
|
NS_EVENT_STATE_VALID);
|
1998-09-03 03:53:16 +04:00
|
|
|
}
|
|
|
|
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::~HTMLSelectElement()
|
1998-09-03 03:53:16 +04:00
|
|
|
{
|
2011-10-15 11:26:41 +04:00
|
|
|
mOptions->DropReference();
|
1998-09-03 03:53:16 +04:00
|
|
|
}
|
|
|
|
|
1998-09-23 21:16:51 +04:00
|
|
|
// ISupports
|
1998-09-03 03:53:16 +04:00
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(HTMLSelectElement)
|
|
|
|
|
2013-04-04 11:03:33 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(HTMLSelectElement,
|
2013-08-02 05:21:31 +04:00
|
|
|
nsGenericHTMLFormElementWithState)
|
2013-01-16 22:01:01 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mValidity)
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOptions)
|
2013-09-02 21:23:27 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSelectedOptions)
|
2007-03-08 14:17:16 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2013-04-04 11:03:33 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(HTMLSelectElement,
|
2013-08-02 05:21:31 +04:00
|
|
|
nsGenericHTMLFormElementWithState)
|
2013-01-16 22:01:01 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mValidity)
|
2013-09-02 21:23:27 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSelectedOptions)
|
2013-03-17 11:55:17 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2007-03-08 14:17:16 +03:00
|
|
|
|
2013-04-04 11:03:33 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(HTMLSelectElement, Element)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(HTMLSelectElement, Element)
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2013-04-04 11:03:33 +04:00
|
|
|
// QueryInterface implementation for HTMLSelectElement
|
|
|
|
NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(HTMLSelectElement)
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_INTERFACE_TABLE_INHERITED(HTMLSelectElement,
|
|
|
|
nsIDOMHTMLSelectElement,
|
|
|
|
nsIConstraintValidation)
|
2013-08-08 00:23:08 +04:00
|
|
|
NS_INTERFACE_TABLE_TAIL_INHERITING(nsGenericHTMLFormElementWithState)
|
2000-12-23 13:56:31 +03:00
|
|
|
|
1998-09-03 03:53:16 +04:00
|
|
|
|
1998-09-23 21:16:51 +04:00
|
|
|
// nsIDOMHTMLSelectElement
|
|
|
|
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2013-04-04 11:03:33 +04:00
|
|
|
NS_IMPL_ELEMENT_CLONE(HTMLSelectElement)
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2010-08-21 22:52:49 +04:00
|
|
|
// nsIConstraintValidation
|
2013-04-04 11:03:33 +04:00
|
|
|
NS_IMPL_NSICONSTRAINTVALIDATION_EXCEPT_SETCUSTOMVALIDITY(HTMLSelectElement)
|
2010-08-21 21:52:57 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::SetCustomValidity(const nsAString& aError)
|
2010-08-21 21:52:57 +04:00
|
|
|
{
|
2010-08-21 22:52:49 +04:00
|
|
|
nsIConstraintValidation::SetCustomValidity(aError);
|
2010-08-21 21:52:57 +04:00
|
|
|
|
2011-06-01 05:46:57 +04:00
|
|
|
UpdateState(true);
|
2010-08-21 21:52:57 +04:00
|
|
|
|
2010-08-21 22:51:38 +04:00
|
|
|
return NS_OK;
|
2010-08-21 21:52:57 +04:00
|
|
|
}
|
2010-08-18 22:28:08 +04:00
|
|
|
|
2014-07-24 18:55:00 +04:00
|
|
|
void
|
|
|
|
HTMLSelectElement::GetAutocomplete(DOMString& aValue)
|
|
|
|
{
|
|
|
|
const nsAttrValue* attributeVal = GetParsedAttr(nsGkAtoms::autocomplete);
|
|
|
|
|
|
|
|
mAutocompleteAttrState =
|
|
|
|
nsContentUtils::SerializeAutocompleteAttribute(attributeVal, aValue,
|
|
|
|
mAutocompleteAttrState);
|
|
|
|
}
|
|
|
|
|
2000-12-23 13:56:31 +03:00
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::GetForm(nsIDOMHTMLFormElement** aForm)
|
2000-12-23 13:56:31 +03:00
|
|
|
{
|
2013-08-02 05:21:31 +04:00
|
|
|
return nsGenericHTMLFormElementWithState::GetForm(aForm);
|
1998-09-03 03:53:16 +04:00
|
|
|
}
|
|
|
|
|
2004-01-10 02:54:21 +03:00
|
|
|
nsresult
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::InsertChildAt(nsIContent* aKid,
|
|
|
|
uint32_t aIndex,
|
|
|
|
bool aNotify)
|
2006-03-01 04:10:20 +03:00
|
|
|
{
|
2013-04-04 11:03:33 +04:00
|
|
|
SafeOptionListMutation safeMutation(this, this, aKid, aIndex, aNotify);
|
2013-08-02 05:21:31 +04:00
|
|
|
nsresult rv = nsGenericHTMLFormElementWithState::InsertChildAt(aKid, aIndex,
|
|
|
|
aNotify);
|
2007-08-28 11:09:32 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
safeMutation.MutationFailed();
|
2006-03-03 00:30:23 +03:00
|
|
|
}
|
2007-08-28 11:09:32 +04:00
|
|
|
return rv;
|
2006-03-03 00:30:23 +03:00
|
|
|
}
|
|
|
|
|
2012-03-30 01:09:07 +04:00
|
|
|
void
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::RemoveChildAt(uint32_t aIndex, bool aNotify)
|
2006-03-03 00:30:23 +03:00
|
|
|
{
|
2013-04-04 11:03:33 +04:00
|
|
|
SafeOptionListMutation safeMutation(this, this, nullptr, aIndex, aNotify);
|
2013-08-02 05:21:31 +04:00
|
|
|
nsGenericHTMLFormElementWithState::RemoveChildAt(aIndex, aNotify);
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-24 11:35:08 +04:00
|
|
|
void
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::InsertOptionsIntoList(nsIContent* aOptions,
|
|
|
|
int32_t aListIndex,
|
|
|
|
int32_t aDepth,
|
|
|
|
bool aNotify)
|
2001-11-02 10:40:01 +03:00
|
|
|
{
|
2015-12-01 20:01:56 +03:00
|
|
|
MOZ_ASSERT(aDepth == 0 || aDepth == 1);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t insertIndex = aListIndex;
|
2015-10-13 12:28:00 +03:00
|
|
|
|
|
|
|
HTMLOptionElement* optElement = HTMLOptionElement::FromContent(aOptions);
|
|
|
|
if (optElement) {
|
|
|
|
mOptions->InsertOptionAt(optElement, insertIndex);
|
|
|
|
insertIndex++;
|
2015-12-01 20:01:56 +03:00
|
|
|
} else if (aDepth == 0) {
|
2015-10-13 12:28:00 +03:00
|
|
|
// If it's at the top level, then we just found out there are non-options
|
|
|
|
// at the top level, which will throw off the insert count
|
2015-12-01 20:01:56 +03:00
|
|
|
mNonOptionChildren++;
|
2015-10-13 12:28:00 +03:00
|
|
|
|
|
|
|
// Deal with optgroups
|
|
|
|
if (aOptions->IsHTMLElement(nsGkAtoms::optgroup)) {
|
|
|
|
mOptGroupCount++;
|
|
|
|
|
|
|
|
for (nsIContent* child = aOptions->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
|
|
|
optElement = HTMLOptionElement::FromContent(child);
|
|
|
|
if (optElement) {
|
|
|
|
mOptions->InsertOptionAt(optElement, insertIndex);
|
|
|
|
insertIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-01 20:01:56 +03:00
|
|
|
} // else ignore even if optgroup; we want to ignore nested optgroups.
|
2001-11-02 10:40:01 +03:00
|
|
|
|
|
|
|
// Deal with the selected list
|
|
|
|
if (insertIndex - aListIndex) {
|
|
|
|
// Fix the currently selected index
|
|
|
|
if (aListIndex <= mSelectedIndex) {
|
|
|
|
mSelectedIndex += (insertIndex - aListIndex);
|
2011-10-17 18:59:28 +04:00
|
|
|
SetSelectionChanged(true, aNotify);
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
2001-11-06 12:10:07 +03:00
|
|
|
// Get the frame stuff for notification. No need to flush here
|
|
|
|
// since if there's no frame for the select yet the select will
|
|
|
|
// get into the right state once it's created.
|
2012-07-30 18:20:58 +04:00
|
|
|
nsISelectControlFrame* selectFrame = nullptr;
|
2009-02-18 06:27:25 +03:00
|
|
|
nsWeakFrame weakSelectFrame;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool didGetFrame = false;
|
2001-11-02 10:40:01 +03:00
|
|
|
|
|
|
|
// Actually select the options if the added options warrant it
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = aListIndex; i < insertIndex; i++) {
|
2001-11-02 10:40:01 +03:00
|
|
|
// Notify the frame that the option is added
|
2009-02-18 06:27:25 +03:00
|
|
|
if (!didGetFrame || (selectFrame && !weakSelectFrame.IsAlive())) {
|
|
|
|
selectFrame = GetSelectFrame();
|
|
|
|
weakSelectFrame = do_QueryFrame(selectFrame);
|
2011-10-17 18:59:28 +04:00
|
|
|
didGetFrame = true;
|
2009-02-18 06:27:25 +03:00
|
|
|
}
|
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
if (selectFrame) {
|
2009-02-18 06:27:25 +03:00
|
|
|
selectFrame->AddOption(i);
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HTMLOptionElement> option = Item(i);
|
2013-07-24 11:35:44 +04:00
|
|
|
if (option && option->Selected()) {
|
|
|
|
// Clear all other options
|
|
|
|
if (!HasAttr(kNameSpaceID_None, nsGkAtoms::multiple)) {
|
2013-09-28 23:04:20 +04:00
|
|
|
uint32_t mask = IS_SELECTED | CLEAR_ALL | SET_DISABLED | NOTIFY;
|
|
|
|
SetOptionsSelectedByIndex(i, i, mask);
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
2013-07-24 11:35:44 +04:00
|
|
|
|
|
|
|
// This is sort of a hack ... we need to notify that the option was
|
|
|
|
// set and change selectedIndex even though we didn't really change
|
|
|
|
// its value.
|
|
|
|
OnOptionSelected(selectFrame, i, true, false, false);
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-17 02:41:19 +03:00
|
|
|
CheckSelectSomething(aNotify);
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::RemoveOptionsFromList(nsIContent* aOptions,
|
|
|
|
int32_t aListIndex,
|
|
|
|
int32_t aDepth,
|
|
|
|
bool aNotify)
|
2001-11-02 10:40:01 +03:00
|
|
|
{
|
2015-12-01 20:01:56 +03:00
|
|
|
MOZ_ASSERT(aDepth == 0 || aDepth == 1);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t numRemoved = 0;
|
2015-10-13 12:28:00 +03:00
|
|
|
|
|
|
|
HTMLOptionElement* optElement = HTMLOptionElement::FromContent(aOptions);
|
|
|
|
if (optElement) {
|
|
|
|
if (mOptions->ItemAsOption(aListIndex) != optElement) {
|
|
|
|
NS_ERROR("wrong option at index");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
mOptions->RemoveOptionAt(aListIndex);
|
|
|
|
numRemoved++;
|
2015-12-01 20:01:56 +03:00
|
|
|
} else if (aDepth == 0) {
|
2015-10-13 12:28:00 +03:00
|
|
|
// Yay, one less artifact at the top level.
|
2015-12-01 20:01:56 +03:00
|
|
|
mNonOptionChildren--;
|
2015-10-13 12:28:00 +03:00
|
|
|
|
|
|
|
// Recurse down deeper for options
|
|
|
|
if (mOptGroupCount && aOptions->IsHTMLElement(nsGkAtoms::optgroup)) {
|
|
|
|
mOptGroupCount--;
|
|
|
|
|
|
|
|
for (nsIContent* child = aOptions->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
|
|
|
optElement = HTMLOptionElement::FromContent(child);
|
|
|
|
if (optElement) {
|
|
|
|
if (mOptions->ItemAsOption(aListIndex) != optElement) {
|
|
|
|
NS_ERROR("wrong option at index");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
mOptions->RemoveOptionAt(aListIndex);
|
|
|
|
numRemoved++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-01 20:01:56 +03:00
|
|
|
} // else don't check for an optgroup; we want to ignore nested optgroups
|
2002-08-06 08:59:15 +04:00
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
if (numRemoved) {
|
|
|
|
// Tell the widget we removed the options
|
2001-12-12 10:31:15 +03:00
|
|
|
nsISelectControlFrame* selectFrame = GetSelectFrame();
|
2001-11-06 12:10:07 +03:00
|
|
|
if (selectFrame) {
|
2009-02-18 06:27:25 +03:00
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = aListIndex; i < aListIndex + numRemoved; ++i) {
|
2009-02-18 06:27:25 +03:00
|
|
|
selectFrame->RemoveOption(i);
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix the selected index
|
|
|
|
if (aListIndex <= mSelectedIndex) {
|
|
|
|
if (mSelectedIndex < (aListIndex+numRemoved)) {
|
|
|
|
// aListIndex <= mSelectedIndex < aListIndex+numRemoved
|
|
|
|
// Find a new selected index if it was one of the ones removed.
|
2010-11-24 13:09:01 +03:00
|
|
|
FindSelectedIndex(aListIndex, aNotify);
|
2001-11-02 10:40:01 +03:00
|
|
|
} else {
|
|
|
|
// Shift the selected index if something in front of it was removed
|
|
|
|
// aListIndex+numRemoved <= mSelectedIndex
|
|
|
|
mSelectedIndex -= numRemoved;
|
2011-10-17 18:59:28 +04:00
|
|
|
SetSelectionChanged(true, aNotify);
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select something in case we removed the selected option on a
|
|
|
|
// single select
|
2010-11-17 02:41:19 +03:00
|
|
|
if (!CheckSelectSomething(aNotify) && mSelectedIndex == -1) {
|
|
|
|
// Update the validity state in case of we've just removed the last
|
|
|
|
// option.
|
|
|
|
UpdateValueMissingValidityState();
|
|
|
|
|
2011-06-01 05:46:57 +04:00
|
|
|
UpdateState(aNotify);
|
2010-11-17 02:41:19 +03:00
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-08-19 22:35:09 +04:00
|
|
|
// XXXldb Doing the processing before the content nodes have been added
|
|
|
|
// to the document (as the name of this function seems to require, and
|
|
|
|
// as the callers do), is highly unusual. Passing around unparented
|
|
|
|
// content to other parts of the app can make those things think the
|
|
|
|
// options are the root content node.
|
2001-11-02 10:40:01 +03:00
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::WillAddOptions(nsIContent* aOptions,
|
|
|
|
nsIContent* aParent,
|
|
|
|
int32_t aContentIndex,
|
|
|
|
bool aNotify)
|
2001-11-02 10:40:01 +03:00
|
|
|
{
|
2015-10-13 12:28:00 +03:00
|
|
|
if (this != aParent && this != aParent->GetParent()) {
|
|
|
|
return NS_OK;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
2015-10-13 12:28:00 +03:00
|
|
|
int32_t level = aParent == this ? 0 : 1;
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
// Get the index where the options will be inserted
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t ind = -1;
|
2004-01-10 02:54:21 +03:00
|
|
|
if (!mNonOptionChildren) {
|
2002-08-20 01:25:51 +04:00
|
|
|
// If there are no artifacts, aContentIndex == ind
|
|
|
|
ind = aContentIndex;
|
2001-11-02 10:40:01 +03:00
|
|
|
} else {
|
2002-08-20 01:25:51 +04:00
|
|
|
// If there are artifacts, we have to get the index of the option the
|
|
|
|
// hard way
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t children = aParent->GetChildCount();
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2002-08-20 01:25:51 +04:00
|
|
|
if (aContentIndex >= children) {
|
|
|
|
// If the content insert is after the end of the parent, then we want to get
|
|
|
|
// the next index *after* the parent and insert there.
|
|
|
|
ind = GetOptionIndexAfter(aParent);
|
|
|
|
} else {
|
|
|
|
// If the content insert is somewhere in the middle of the container, then
|
|
|
|
// we want to get the option currently at the index and insert in front of
|
|
|
|
// that.
|
2013-02-18 16:24:58 +04:00
|
|
|
nsIContent* currentKid = aParent->GetChildAt(aContentIndex);
|
2002-08-20 01:25:51 +04:00
|
|
|
NS_ASSERTION(currentKid, "Child not found!");
|
|
|
|
if (currentKid) {
|
|
|
|
ind = GetOptionIndexAt(currentKid);
|
|
|
|
} else {
|
|
|
|
ind = -1;
|
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
2000-07-12 00:55:21 +04:00
|
|
|
}
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2013-07-24 11:35:08 +04:00
|
|
|
InsertOptionsIntoList(aOptions, ind, level, aNotify);
|
|
|
|
return NS_OK;
|
2000-07-12 00:55:21 +04:00
|
|
|
}
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2000-07-12 00:55:21 +04:00
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::WillRemoveOptions(nsIContent* aParent,
|
|
|
|
int32_t aContentIndex,
|
|
|
|
bool aNotify)
|
2001-11-02 10:40:01 +03:00
|
|
|
{
|
2015-10-13 12:28:00 +03:00
|
|
|
if (this != aParent && this != aParent->GetParent()) {
|
|
|
|
return NS_OK;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
2015-10-13 12:28:00 +03:00
|
|
|
int32_t level = this == aParent ? 0 : 1;
|
2001-11-02 10:40:01 +03:00
|
|
|
|
|
|
|
// Get the index where the options will be removed
|
2013-02-18 16:24:58 +04:00
|
|
|
nsIContent* currentKid = aParent->GetChildAt(aContentIndex);
|
2001-11-02 10:40:01 +03:00
|
|
|
if (currentKid) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t ind;
|
2004-01-10 02:54:21 +03:00
|
|
|
if (!mNonOptionChildren) {
|
2002-08-20 01:25:51 +04:00
|
|
|
// If there are no artifacts, aContentIndex == ind
|
|
|
|
ind = aContentIndex;
|
|
|
|
} else {
|
|
|
|
// If there are artifacts, we have to get the index of the option the
|
|
|
|
// hard way
|
|
|
|
ind = GetFirstOptionIndex(currentKid);
|
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
if (ind != -1) {
|
2010-11-17 02:41:19 +03:00
|
|
|
nsresult rv = RemoveOptionsFromList(currentKid, ind, level, aNotify);
|
2006-03-03 00:30:23 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-03 00:30:23 +03:00
|
|
|
return NS_OK;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::GetOptionIndexAt(nsIContent* aOptions)
|
2001-11-02 10:40:01 +03:00
|
|
|
{
|
|
|
|
// Search this node and below.
|
|
|
|
// If not found, find the first one *after* this node.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t retval = GetFirstOptionIndex(aOptions);
|
2002-08-06 08:59:15 +04:00
|
|
|
if (retval == -1) {
|
|
|
|
retval = GetOptionIndexAfter(aOptions);
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
2002-08-06 08:59:15 +04:00
|
|
|
return retval;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::GetOptionIndexAfter(nsIContent* aOptions)
|
2001-11-02 10:40:01 +03:00
|
|
|
{
|
|
|
|
// - If this is the select, the next option is the last.
|
|
|
|
// - If not, search all the options after aOptions and up to the last option
|
|
|
|
// in the parent.
|
|
|
|
// - If it's not there, search for the first option after the parent.
|
|
|
|
if (aOptions == this) {
|
2015-10-13 12:28:00 +03:00
|
|
|
return Length();
|
2002-08-06 08:59:15 +04:00
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t retval = -1;
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2003-07-29 01:09:56 +04:00
|
|
|
nsCOMPtr<nsIContent> parent = aOptions->GetParent();
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2002-08-06 08:59:15 +04:00
|
|
|
if (parent) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t index = parent->IndexOf(aOptions);
|
|
|
|
int32_t count = parent->GetChildCount();
|
2002-08-06 08:59:15 +04:00
|
|
|
|
|
|
|
retval = GetFirstChildOptionIndex(parent, index+1, count);
|
|
|
|
|
|
|
|
if (retval == -1) {
|
|
|
|
retval = GetOptionIndexAfter(parent);
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-06 08:59:15 +04:00
|
|
|
return retval;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::GetFirstOptionIndex(nsIContent* aOptions)
|
2000-07-12 00:55:21 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t listIndex = -1;
|
2013-02-18 16:24:58 +04:00
|
|
|
HTMLOptionElement* optElement = HTMLOptionElement::FromContent(aOptions);
|
2001-11-02 10:40:01 +03:00
|
|
|
if (optElement) {
|
2011-10-17 18:59:28 +04:00
|
|
|
GetOptionIndex(optElement, 0, true, &listIndex);
|
2002-08-06 08:59:15 +04:00
|
|
|
return listIndex;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
2003-09-27 08:18:26 +04:00
|
|
|
listIndex = GetFirstChildOptionIndex(aOptions, 0, aOptions->GetChildCount());
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2002-08-06 08:59:15 +04:00
|
|
|
return listIndex;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::GetFirstChildOptionIndex(nsIContent* aOptions,
|
|
|
|
int32_t aStartIndex,
|
|
|
|
int32_t aEndIndex)
|
2001-11-02 10:40:01 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t retval = -1;
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = aStartIndex; i < aEndIndex; ++i) {
|
2003-09-27 08:18:26 +04:00
|
|
|
retval = GetFirstOptionIndex(aOptions->GetChildAt(i));
|
2002-08-06 08:59:15 +04:00
|
|
|
if (retval != -1) {
|
|
|
|
break;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
2000-07-12 00:55:21 +04:00
|
|
|
}
|
1999-07-28 09:26:55 +04:00
|
|
|
|
2002-08-06 08:59:15 +04:00
|
|
|
return retval;
|
1999-07-28 09:26:55 +04:00
|
|
|
}
|
|
|
|
|
2013-02-18 16:24:58 +04:00
|
|
|
nsISelectControlFrame*
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::GetSelectFrame()
|
2001-11-06 12:10:07 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
nsIFormControlFrame* form_control_frame = GetFormControlFrame(false);
|
2001-11-06 12:10:07 +03:00
|
|
|
|
2013-02-18 16:24:58 +04:00
|
|
|
nsISelectControlFrame* select_frame = nullptr;
|
2001-11-06 12:10:07 +03:00
|
|
|
|
|
|
|
if (form_control_frame) {
|
2009-01-12 22:20:59 +03:00
|
|
|
select_frame = do_QueryFrame(form_control_frame);
|
2001-11-06 12:10:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return select_frame;
|
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2013-04-13 11:01:04 +04:00
|
|
|
void
|
|
|
|
HTMLSelectElement::Add(const HTMLOptionElementOrHTMLOptGroupElement& aElement,
|
|
|
|
const Nullable<HTMLElementOrLong>& aBefore,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsGenericHTMLElement& element =
|
|
|
|
aElement.IsHTMLOptionElement() ?
|
|
|
|
static_cast<nsGenericHTMLElement&>(aElement.GetAsHTMLOptionElement()) :
|
|
|
|
static_cast<nsGenericHTMLElement&>(aElement.GetAsHTMLOptGroupElement());
|
|
|
|
|
|
|
|
if (aBefore.IsNull()) {
|
|
|
|
Add(element, static_cast<nsGenericHTMLElement*>(nullptr), aRv);
|
|
|
|
} else if (aBefore.Value().IsHTMLElement()) {
|
|
|
|
Add(element, &aBefore.Value().GetAsHTMLElement(), aRv);
|
|
|
|
} else {
|
|
|
|
Add(element, aBefore.Value().GetAsLong(), aRv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-11 03:30:15 +04:00
|
|
|
void
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::Add(nsGenericHTMLElement& aElement,
|
|
|
|
nsGenericHTMLElement* aBefore,
|
|
|
|
ErrorResult& aError)
|
1998-09-23 21:16:51 +04:00
|
|
|
{
|
2003-01-08 06:07:03 +03:00
|
|
|
if (!aBefore) {
|
2013-08-08 00:23:08 +04:00
|
|
|
Element::AppendChild(aElement, aError);
|
2012-11-11 03:30:15 +04:00
|
|
|
return;
|
2003-01-08 06:07:03 +03:00
|
|
|
}
|
1999-05-05 00:53:26 +04:00
|
|
|
|
2003-01-08 06:07:03 +03:00
|
|
|
// Just in case we're not the parent, get the parent of the reference
|
|
|
|
// element
|
2013-08-08 00:23:08 +04:00
|
|
|
nsINode* parent = aBefore->Element::GetParentNode();
|
2013-04-24 15:05:59 +04:00
|
|
|
if (!parent || !nsContentUtils::ContentIsDescendantOf(parent, this)) {
|
2003-01-08 06:07:03 +03:00
|
|
|
// NOT_FOUND_ERR: Raised if before is not a descendant of the SELECT
|
|
|
|
// element.
|
2012-11-11 03:30:15 +04:00
|
|
|
aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR);
|
|
|
|
return;
|
1999-05-05 00:53:26 +04:00
|
|
|
}
|
|
|
|
|
2003-01-08 06:07:03 +03:00
|
|
|
// If the before parameter is not null, we are equivalent to the
|
|
|
|
// insertBefore method on the parent of before.
|
2012-11-11 03:30:15 +04:00
|
|
|
parent->InsertBefore(aElement, aBefore, aError);
|
1998-09-23 21:16:51 +04:00
|
|
|
}
|
|
|
|
|
2011-07-21 14:16:28 +04:00
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::Add(nsIDOMHTMLElement* aElement,
|
|
|
|
nsIVariant* aBefore)
|
2011-07-21 14:16:28 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t dataType;
|
2011-07-21 14:16:28 +04:00
|
|
|
nsresult rv = aBefore->GetDataType(&dataType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-11-11 03:30:15 +04:00
|
|
|
nsCOMPtr<nsIContent> element = do_QueryInterface(aElement);
|
|
|
|
nsGenericHTMLElement* htmlElement =
|
|
|
|
nsGenericHTMLElement::FromContentOrNull(element);
|
|
|
|
if (!htmlElement) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
2011-08-01 15:28:52 +04:00
|
|
|
// aBefore is omitted, undefined or null
|
|
|
|
if (dataType == nsIDataType::VTYPE_EMPTY ||
|
|
|
|
dataType == nsIDataType::VTYPE_VOID) {
|
2012-11-11 03:30:15 +04:00
|
|
|
ErrorResult error;
|
|
|
|
Add(*htmlElement, (nsGenericHTMLElement*)nullptr, error);
|
2015-04-27 16:18:51 +03:00
|
|
|
return error.StealNSResult();
|
2011-07-21 14:16:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> supports;
|
|
|
|
nsCOMPtr<nsIDOMHTMLElement> beforeElement;
|
|
|
|
|
|
|
|
// whether aBefore is nsIDOMHTMLElement...
|
|
|
|
if (NS_SUCCEEDED(aBefore->GetAsISupports(getter_AddRefs(supports)))) {
|
2012-11-11 03:30:15 +04:00
|
|
|
nsCOMPtr<nsIContent> beforeElement = do_QueryInterface(supports);
|
|
|
|
nsGenericHTMLElement* beforeHTMLElement =
|
|
|
|
nsGenericHTMLElement::FromContentOrNull(beforeElement);
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(beforeHTMLElement, NS_ERROR_DOM_SYNTAX_ERR);
|
2011-07-21 14:16:28 +04:00
|
|
|
|
2012-11-11 03:30:15 +04:00
|
|
|
ErrorResult error;
|
|
|
|
Add(*htmlElement, beforeHTMLElement, error);
|
2015-04-27 16:18:51 +03:00
|
|
|
return error.StealNSResult();
|
2011-07-21 14:16:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, whether aBefore is long
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t index;
|
2011-07-21 14:16:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(aBefore->GetAsInt32(&index), NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-11-11 03:30:15 +04:00
|
|
|
ErrorResult error;
|
|
|
|
Add(*htmlElement, index, error);
|
2015-04-27 16:18:51 +03:00
|
|
|
return error.StealNSResult();
|
2011-07-21 14:16:28 +04:00
|
|
|
}
|
|
|
|
|
2004-01-10 02:54:21 +03:00
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::Remove(int32_t aIndex)
|
1998-09-23 21:16:51 +04:00
|
|
|
{
|
2013-07-24 11:36:54 +04:00
|
|
|
nsCOMPtr<nsINode> option = Item(static_cast<uint32_t>(aIndex));
|
|
|
|
if (!option) {
|
|
|
|
return NS_OK;
|
1998-09-23 21:16:51 +04:00
|
|
|
}
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2013-07-24 11:36:54 +04:00
|
|
|
option->Remove();
|
2003-01-08 06:07:03 +03:00
|
|
|
return NS_OK;
|
1998-09-03 03:53:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::GetOptions(nsIDOMHTMLOptionsCollection** aValue)
|
1998-09-03 03:53:16 +04:00
|
|
|
{
|
2010-05-09 22:32:57 +04:00
|
|
|
NS_IF_ADDREF(*aValue = GetOptions());
|
2000-12-23 13:56:31 +03:00
|
|
|
|
1998-09-03 03:53:16 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::GetType(nsAString& aType)
|
1998-09-03 03:53:16 +04:00
|
|
|
{
|
2010-10-08 16:15:00 +04:00
|
|
|
if (HasAttr(kNameSpaceID_None, nsGkAtoms::multiple)) {
|
2004-06-17 04:13:25 +04:00
|
|
|
aType.AssignLiteral("select-multiple");
|
2003-01-08 06:07:03 +03:00
|
|
|
}
|
|
|
|
else {
|
2004-06-17 04:13:25 +04:00
|
|
|
aType.AssignLiteral("select-one");
|
1998-10-20 21:07:23 +04:00
|
|
|
}
|
2003-01-08 06:07:03 +03:00
|
|
|
|
1998-10-20 21:07:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::GetLength(uint32_t* aLength)
|
1998-10-20 21:07:23 +04:00
|
|
|
{
|
2001-11-02 10:40:01 +03:00
|
|
|
return mOptions->GetLength(aLength);
|
1999-05-05 00:53:26 +04:00
|
|
|
}
|
|
|
|
|
2008-11-12 06:36:20 +03:00
|
|
|
#define MAX_DYNAMIC_SELECT_LENGTH 10000
|
|
|
|
|
2000-02-24 03:08:00 +03:00
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::SetLength(uint32_t aLength)
|
2000-02-24 03:08:00 +03:00
|
|
|
{
|
2013-07-24 11:36:59 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
SetLength(aLength, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-07-24 11:36:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HTMLSelectElement::SetLength(uint32_t aLength, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
uint32_t curlen = Length();
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2008-11-12 06:36:20 +03:00
|
|
|
if (curlen > aLength) { // Remove extra options
|
2013-07-24 11:36:59 +04:00
|
|
|
for (uint32_t i = curlen; i > aLength; --i) {
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(Remove(i - 1)));
|
2008-11-12 06:36:20 +03:00
|
|
|
}
|
|
|
|
} else if (aLength > curlen) {
|
|
|
|
if (aLength > MAX_DYNAMIC_SELECT_LENGTH) {
|
2013-07-24 11:36:59 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return;
|
2000-04-20 16:34:35 +04:00
|
|
|
}
|
2013-07-24 11:36:59 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<mozilla::dom::NodeInfo> nodeInfo;
|
2000-05-10 17:13:39 +04:00
|
|
|
|
2006-12-26 20:47:52 +03:00
|
|
|
nsContentUtils::NameChanged(mNodeInfo, nsGkAtoms::option,
|
2004-06-25 16:26:02 +04:00
|
|
|
getter_AddRefs(nodeInfo));
|
2000-04-20 16:34:35 +04:00
|
|
|
|
2013-07-24 11:36:59 +04:00
|
|
|
nsCOMPtr<nsINode> node = NS_NewHTMLOptionElement(nodeInfo.forget());
|
2000-04-20 16:34:35 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsTextNode> text = new nsTextNode(mNodeInfo->NodeInfoManager());
|
2000-04-20 16:34:35 +04:00
|
|
|
|
2013-07-24 11:36:59 +04:00
|
|
|
aRv = node->AppendChildTo(text, false);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2000-04-20 16:34:35 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = curlen; i < aLength; i++) {
|
2013-07-24 11:36:59 +04:00
|
|
|
nsINode::AppendChild(*node, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2000-04-20 16:34:35 +04:00
|
|
|
|
2010-03-12 09:50:13 +03:00
|
|
|
if (i + 1 < aLength) {
|
2013-07-24 11:36:59 +04:00
|
|
|
node = node->CloneNode(true, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(node);
|
2000-04-20 16:34:35 +04:00
|
|
|
}
|
2000-02-24 03:08:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-02 21:23:27 +04:00
|
|
|
/* static */
|
|
|
|
bool
|
|
|
|
HTMLSelectElement::MatchSelectedOptions(nsIContent* aContent,
|
|
|
|
int32_t /* unused */,
|
|
|
|
nsIAtom* /* unused */,
|
|
|
|
void* /* unused*/)
|
|
|
|
{
|
|
|
|
HTMLOptionElement* option = HTMLOptionElement::FromContent(aContent);
|
|
|
|
return option && option->Selected();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIHTMLCollection*
|
|
|
|
HTMLSelectElement::SelectedOptions()
|
|
|
|
{
|
|
|
|
if (!mSelectedOptions) {
|
|
|
|
mSelectedOptions = new nsContentList(this, MatchSelectedOptions, nullptr,
|
|
|
|
nullptr, /* deep */ true);
|
|
|
|
}
|
|
|
|
return mSelectedOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
HTMLSelectElement::GetSelectedOptions(nsIDOMHTMLCollection** aSelectedOptions)
|
|
|
|
{
|
|
|
|
NS_ADDREF(*aSelectedOptions = SelectedOptions());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-04 11:03:33 +04:00
|
|
|
//NS_IMPL_INT_ATTR(HTMLSelectElement, SelectedIndex, selectedindex)
|
2000-12-23 13:56:31 +03:00
|
|
|
|
1999-01-30 04:02:28 +03:00
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::GetSelectedIndex(int32_t* aValue)
|
1999-01-30 04:02:28 +03:00
|
|
|
{
|
2013-04-13 11:01:04 +04:00
|
|
|
*aValue = SelectedIndex();
|
2000-03-22 17:45:20 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
1999-01-30 04:02:28 +03:00
|
|
|
}
|
|
|
|
|
2010-11-24 13:09:01 +03:00
|
|
|
nsresult
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::SetSelectedIndexInternal(int32_t aIndex, bool aNotify)
|
1999-01-30 04:02:28 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t oldSelectedIndex = mSelectedIndex;
|
2013-09-28 23:04:20 +04:00
|
|
|
uint32_t mask = IS_SELECTED | CLEAR_ALL | SET_DISABLED;
|
|
|
|
if (aNotify) {
|
|
|
|
mask |= NOTIFY;
|
|
|
|
}
|
2005-02-05 01:56:13 +03:00
|
|
|
|
2013-09-28 23:04:20 +04:00
|
|
|
SetOptionsSelectedByIndex(aIndex, aIndex, mask);
|
2005-02-05 01:56:13 +03:00
|
|
|
|
2013-07-24 11:37:14 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsISelectControlFrame* selectFrame = GetSelectFrame();
|
|
|
|
if (selectFrame) {
|
|
|
|
rv = selectFrame->OnSetSelectedIndex(oldSelectedIndex, mSelectedIndex);
|
2005-02-05 01:56:13 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
SetSelectionChanged(true, aNotify);
|
2010-11-24 13:09:01 +03:00
|
|
|
|
2005-02-05 01:56:13 +03:00
|
|
|
return rv;
|
2000-09-05 17:37:16 +04:00
|
|
|
}
|
|
|
|
|
2010-11-24 13:09:01 +03:00
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::SetSelectedIndex(int32_t aIndex)
|
2010-11-24 13:09:01 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return SetSelectedIndexInternal(aIndex, true);
|
2010-11-24 13:09:01 +03:00
|
|
|
}
|
|
|
|
|
2002-04-16 09:09:35 +04:00
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::GetOptionIndex(nsIDOMHTMLOptionElement* aOption,
|
|
|
|
int32_t aStartIndex, bool aForward,
|
|
|
|
int32_t* aIndex)
|
2000-09-05 17:37:16 +04:00
|
|
|
{
|
2010-07-31 07:44:45 +04:00
|
|
|
nsCOMPtr<nsINode> option = do_QueryInterface(aOption);
|
|
|
|
return mOptions->GetOptionIndex(option->AsElement(), aStartIndex, aForward, aIndex);
|
2000-09-05 17:37:16 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::IsOptionSelectedByIndex(int32_t aIndex)
|
2001-11-02 10:40:01 +03:00
|
|
|
{
|
2013-07-24 11:37:03 +04:00
|
|
|
HTMLOptionElement* option = Item(static_cast<uint32_t>(aIndex));
|
|
|
|
return option && option->Selected();
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
2000-09-05 17:37:16 +04:00
|
|
|
|
2002-08-06 08:59:15 +04:00
|
|
|
void
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::OnOptionSelected(nsISelectControlFrame* aSelectFrame,
|
|
|
|
int32_t aIndex,
|
|
|
|
bool aSelected,
|
|
|
|
bool aChangeOptionState,
|
|
|
|
bool aNotify)
|
2001-11-02 10:40:01 +03:00
|
|
|
{
|
|
|
|
// Set the selected index
|
|
|
|
if (aSelected && (aIndex < mSelectedIndex || mSelectedIndex < 0)) {
|
|
|
|
mSelectedIndex = aIndex;
|
2011-10-17 18:59:28 +04:00
|
|
|
SetSelectionChanged(true, aNotify);
|
2001-11-02 10:40:01 +03:00
|
|
|
} else if (!aSelected && aIndex == mSelectedIndex) {
|
2011-11-16 11:50:19 +04:00
|
|
|
FindSelectedIndex(aIndex + 1, aNotify);
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
2007-12-04 19:50:32 +03:00
|
|
|
if (aChangeOptionState) {
|
|
|
|
// Tell the option to get its bad self selected
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HTMLOptionElement> option = Item(static_cast<uint32_t>(aIndex));
|
2007-12-04 19:50:32 +03:00
|
|
|
if (option) {
|
2013-07-24 11:37:07 +04:00
|
|
|
option->SetSelectedInternal(aSelected, aNotify);
|
2007-12-04 19:50:32 +03:00
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Let the frame know too
|
|
|
|
if (aSelectFrame) {
|
2009-02-18 06:27:25 +03:00
|
|
|
aSelectFrame->OnOptionSelected(aIndex, aSelected);
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
2010-11-17 02:41:19 +03:00
|
|
|
|
2013-09-02 21:23:27 +04:00
|
|
|
UpdateSelectedOptions();
|
2010-11-17 02:41:19 +03:00
|
|
|
UpdateValueMissingValidityState();
|
2011-06-01 05:46:57 +04:00
|
|
|
UpdateState(aNotify);
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
2002-08-06 08:59:15 +04:00
|
|
|
void
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::FindSelectedIndex(int32_t aStartIndex, bool aNotify)
|
2001-11-02 10:40:01 +03:00
|
|
|
{
|
|
|
|
mSelectedIndex = -1;
|
2011-10-17 18:59:28 +04:00
|
|
|
SetSelectionChanged(true, aNotify);
|
2013-07-24 11:37:10 +04:00
|
|
|
uint32_t len = Length();
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = aStartIndex; i < int32_t(len); i++) {
|
2002-08-06 08:59:15 +04:00
|
|
|
if (IsOptionSelectedByIndex(i)) {
|
2001-11-02 10:40:01 +03:00
|
|
|
mSelectedIndex = i;
|
2011-10-17 18:59:28 +04:00
|
|
|
SetSelectionChanged(true, aNotify);
|
2001-11-02 10:40:01 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-09-05 17:37:16 +04:00
|
|
|
}
|
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
// XXX Consider splitting this into two functions for ease of reading:
|
|
|
|
// SelectOptionsByIndex(startIndex, endIndex, clearAll, checkDisabled)
|
|
|
|
// startIndex, endIndex - the range of options to turn on
|
|
|
|
// (-1, -1) will clear all indices no matter what.
|
|
|
|
// clearAll - will clear all other options unless checkDisabled is on
|
|
|
|
// and all the options attempted to be set are disabled
|
|
|
|
// (note that if it is not multiple, and an option is selected,
|
|
|
|
// everything else will be cleared regardless).
|
|
|
|
// checkDisabled - if this is TRUE, and an option is disabled, it will not be
|
|
|
|
// changed regardless of whether it is selected or not.
|
|
|
|
// Generally the UI passes TRUE and JS passes FALSE.
|
|
|
|
// (setDisabled currently is the opposite)
|
|
|
|
// DeselectOptionsByIndex(startIndex, endIndex, checkDisabled)
|
|
|
|
// startIndex, endIndex - the range of options to turn on
|
|
|
|
// (-1, -1) will clear all indices no matter what.
|
|
|
|
// checkDisabled - if this is TRUE, and an option is disabled, it will not be
|
|
|
|
// changed regardless of whether it is selected or not.
|
|
|
|
// Generally the UI passes TRUE and JS passes FALSE.
|
|
|
|
// (setDisabled currently is the opposite)
|
2007-12-04 19:50:32 +03:00
|
|
|
//
|
|
|
|
// XXXbz the above comment is pretty confusing. Maybe we should actually
|
|
|
|
// document the args to this function too, in addition to documenting what
|
|
|
|
// things might end up looking like? In particular, pay attention to the
|
|
|
|
// setDisabled vs checkDisabled business.
|
2013-07-24 11:37:14 +04:00
|
|
|
bool
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::SetOptionsSelectedByIndex(int32_t aStartIndex,
|
|
|
|
int32_t aEndIndex,
|
2013-09-28 23:04:20 +04:00
|
|
|
uint32_t aOptionsMask)
|
2000-09-05 17:37:16 +04:00
|
|
|
{
|
2001-11-02 10:40:01 +03:00
|
|
|
#if 0
|
|
|
|
printf("SetOption(%d-%d, %c, ClearAll=%c)\n", aStartIndex, aEndIndex,
|
2013-09-28 23:04:20 +04:00
|
|
|
(aOptionsMask & IS_SELECTED ? 'Y' : 'N'),
|
|
|
|
(aOptionsMask & CLEAR_ALL ? 'Y' : 'N'));
|
2001-11-02 10:40:01 +03:00
|
|
|
#endif
|
|
|
|
// Don't bother if the select is disabled
|
2013-09-28 23:04:20 +04:00
|
|
|
if (!(aOptionsMask & SET_DISABLED) && IsDisabled()) {
|
2013-07-24 11:37:14 +04:00
|
|
|
return false;
|
2000-09-05 17:37:16 +04:00
|
|
|
}
|
2000-02-12 19:26:24 +03:00
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
// Don't bother if there are no options
|
2013-07-24 11:37:14 +04:00
|
|
|
uint32_t numItems = Length();
|
2001-11-02 10:40:01 +03:00
|
|
|
if (numItems == 0) {
|
2013-07-24 11:37:14 +04:00
|
|
|
return false;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
2000-02-12 19:26:24 +03:00
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
// First, find out whether multiple items can be selected
|
2013-07-24 11:37:14 +04:00
|
|
|
bool isMultiple = Multiple();
|
2000-09-05 17:37:16 +04:00
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
// These variables tell us whether any options were selected
|
|
|
|
// or deselected.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool optionsSelected = false;
|
|
|
|
bool optionsDeselected = false;
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2013-02-18 16:24:58 +04:00
|
|
|
nsISelectControlFrame* selectFrame = nullptr;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool didGetFrame = false;
|
2009-02-18 06:27:25 +03:00
|
|
|
nsWeakFrame weakSelectFrame;
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2013-09-28 23:04:20 +04:00
|
|
|
if (aOptionsMask & IS_SELECTED) {
|
2009-01-02 18:54:48 +03:00
|
|
|
// Setting selectedIndex to an out-of-bounds index means -1. (HTML5)
|
2014-08-25 23:17:32 +04:00
|
|
|
if (aStartIndex < 0 || AssertedCast<uint32_t>(aStartIndex) >= numItems ||
|
|
|
|
aEndIndex < 0 || AssertedCast<uint32_t>(aEndIndex) >= numItems) {
|
2009-01-02 18:54:48 +03:00
|
|
|
aStartIndex = -1;
|
|
|
|
aEndIndex = -1;
|
|
|
|
}
|
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
// Only select the first value if it's not multiple
|
|
|
|
if (!isMultiple) {
|
|
|
|
aEndIndex = aStartIndex;
|
2000-09-05 17:37:16 +04:00
|
|
|
}
|
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
// This variable tells whether or not all of the options we attempted to
|
|
|
|
// select are disabled. If ClearAll is passed in as true, and we do not
|
|
|
|
// select anything because the options are disabled, we will not clear the
|
|
|
|
// other options. (This is to make the UI work the way one might expect.)
|
2013-09-28 23:04:20 +04:00
|
|
|
bool allDisabled = !(aOptionsMask & SET_DISABLED);
|
2001-11-02 10:40:01 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// Save a little time when clearing other options
|
|
|
|
//
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t previousSelectedIndex = mSelectedIndex;
|
2001-11-02 10:40:01 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// Select the requested indices
|
|
|
|
//
|
|
|
|
// If index is -1, everything will be deselected (bug 28143)
|
|
|
|
if (aStartIndex != -1) {
|
2013-07-24 11:37:14 +04:00
|
|
|
MOZ_ASSERT(aStartIndex >= 0);
|
|
|
|
MOZ_ASSERT(aEndIndex >= 0);
|
2001-11-02 10:40:01 +03:00
|
|
|
// Loop through the options and select them (if they are not disabled and
|
|
|
|
// if they are not already selected).
|
2014-08-25 23:17:32 +04:00
|
|
|
for (uint32_t optIndex = AssertedCast<uint32_t>(aStartIndex);
|
|
|
|
optIndex <= AssertedCast<uint32_t>(aEndIndex);
|
2013-07-24 11:37:14 +04:00
|
|
|
optIndex++) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HTMLOptionElement> option = Item(optIndex);
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
// Ignore disabled options.
|
2013-09-28 23:04:20 +04:00
|
|
|
if (!(aOptionsMask & SET_DISABLED)) {
|
2013-07-24 11:37:14 +04:00
|
|
|
if (option && IsOptionDisabled(option)) {
|
2001-11-02 10:40:01 +03:00
|
|
|
continue;
|
2000-03-21 18:46:43 +03:00
|
|
|
}
|
2013-07-24 11:37:14 +04:00
|
|
|
allDisabled = false;
|
2000-02-12 19:26:24 +03:00
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2013-07-24 11:37:14 +04:00
|
|
|
// If the index is already selected, ignore it.
|
|
|
|
if (option && !option->Selected()) {
|
|
|
|
// To notify the frame if anything gets changed. No need
|
|
|
|
// to flush here, if there's no frame yet we don't need to
|
|
|
|
// force it to be created just to notify it about a change
|
|
|
|
// in the select.
|
|
|
|
selectFrame = GetSelectFrame();
|
|
|
|
weakSelectFrame = do_QueryFrame(selectFrame);
|
|
|
|
didGetFrame = true;
|
|
|
|
|
2013-09-28 23:04:20 +04:00
|
|
|
OnOptionSelected(selectFrame, optIndex, true, true,
|
|
|
|
aOptionsMask & NOTIFY);
|
2013-07-24 11:37:14 +04:00
|
|
|
optionsSelected = true;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next remove all other options if single select or all is clear
|
|
|
|
// If index is -1, everything will be deselected (bug 28143)
|
|
|
|
if (((!isMultiple && optionsSelected)
|
2013-09-28 23:04:20 +04:00
|
|
|
|| ((aOptionsMask & CLEAR_ALL) && !allDisabled)
|
2001-11-02 10:40:01 +03:00
|
|
|
|| aStartIndex == -1)
|
|
|
|
&& previousSelectedIndex != -1) {
|
2014-08-25 23:17:32 +04:00
|
|
|
for (uint32_t optIndex = AssertedCast<uint32_t>(previousSelectedIndex);
|
2013-07-24 11:37:14 +04:00
|
|
|
optIndex < numItems;
|
2001-11-02 10:40:01 +03:00
|
|
|
optIndex++) {
|
2013-07-24 11:37:14 +04:00
|
|
|
if (static_cast<int32_t>(optIndex) < aStartIndex ||
|
|
|
|
static_cast<int32_t>(optIndex) > aEndIndex) {
|
|
|
|
HTMLOptionElement* option = Item(optIndex);
|
|
|
|
// If the index is already selected, ignore it.
|
|
|
|
if (option && option->Selected()) {
|
|
|
|
if (!didGetFrame || (selectFrame && !weakSelectFrame.IsAlive())) {
|
|
|
|
// To notify the frame if anything gets changed, don't
|
|
|
|
// flush, if the frame doesn't exist we don't need to
|
|
|
|
// create it just to tell it about this change.
|
|
|
|
selectFrame = GetSelectFrame();
|
|
|
|
weakSelectFrame = do_QueryFrame(selectFrame);
|
|
|
|
|
|
|
|
didGetFrame = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
OnOptionSelected(selectFrame, optIndex, false, true,
|
2013-09-28 23:04:20 +04:00
|
|
|
aOptionsMask & NOTIFY);
|
2013-07-24 11:37:14 +04:00
|
|
|
optionsDeselected = true;
|
|
|
|
|
|
|
|
// Only need to deselect one option if not multiple
|
|
|
|
if (!isMultiple) {
|
|
|
|
break;
|
2000-09-05 17:37:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-03-21 18:46:43 +03:00
|
|
|
}
|
2000-09-05 17:37:16 +04:00
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
} else {
|
|
|
|
// If we're deselecting, loop through all selected items and deselect
|
|
|
|
// any that are in the specified range.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t optIndex = aStartIndex; optIndex <= aEndIndex; optIndex++) {
|
2013-07-24 11:37:14 +04:00
|
|
|
HTMLOptionElement* option = Item(optIndex);
|
2013-09-28 23:04:20 +04:00
|
|
|
if (!(aOptionsMask & SET_DISABLED) && IsOptionDisabled(option)) {
|
2013-07-24 11:37:14 +04:00
|
|
|
continue;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
2013-07-24 11:37:14 +04:00
|
|
|
// If the index is already selected, ignore it.
|
|
|
|
if (option && option->Selected()) {
|
|
|
|
if (!didGetFrame || (selectFrame && !weakSelectFrame.IsAlive())) {
|
|
|
|
// To notify the frame if anything gets changed, don't
|
|
|
|
// flush, if the frame doesn't exist we don't need to
|
|
|
|
// create it just to tell it about this change.
|
|
|
|
selectFrame = GetSelectFrame();
|
|
|
|
weakSelectFrame = do_QueryFrame(selectFrame);
|
|
|
|
|
|
|
|
didGetFrame = true;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
2013-07-24 11:37:14 +04:00
|
|
|
|
2013-09-28 23:04:20 +04:00
|
|
|
OnOptionSelected(selectFrame, optIndex, false, true,
|
|
|
|
aOptionsMask & NOTIFY);
|
2013-07-24 11:37:14 +04:00
|
|
|
optionsDeselected = true;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
2000-09-05 17:37:16 +04:00
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
2003-03-14 09:18:20 +03:00
|
|
|
// Make sure something is selected unless we were set to -1 (none)
|
|
|
|
if (optionsDeselected && aStartIndex != -1) {
|
2013-09-28 23:04:20 +04:00
|
|
|
optionsSelected =
|
|
|
|
CheckSelectSomething(aOptionsMask & NOTIFY) || optionsSelected;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Let the caller know whether anything was changed
|
2013-07-24 11:37:14 +04:00
|
|
|
return optionsSelected || optionsDeselected;
|
1999-01-30 04:02:28 +03:00
|
|
|
}
|
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::IsOptionDisabled(int32_t aIndex, bool* aIsDisabled)
|
1999-06-15 03:14:17 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*aIsDisabled = false;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HTMLOptionElement> option = Item(aIndex);
|
2013-07-24 11:31:49 +04:00
|
|
|
NS_ENSURE_TRUE(option, NS_ERROR_FAILURE);
|
2002-04-19 01:54:57 +04:00
|
|
|
|
2013-07-24 11:31:49 +04:00
|
|
|
*aIsDisabled = IsOptionDisabled(option);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HTMLSelectElement::IsOptionDisabled(HTMLOptionElement* aOption)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aOption);
|
|
|
|
if (aOption->Disabled()) {
|
|
|
|
return true;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
1999-09-17 11:24:02 +04:00
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
// Check for disabled optgroups
|
|
|
|
// If there are no artifacts, there are no optgroups
|
2002-08-20 01:25:51 +04:00
|
|
|
if (mNonOptionChildren) {
|
2013-07-24 11:36:59 +04:00
|
|
|
for (nsCOMPtr<Element> node = static_cast<nsINode*>(aOption)->GetParentElement();
|
2013-07-24 11:31:49 +04:00
|
|
|
node;
|
|
|
|
node = node->GetParentElement()) {
|
2001-11-02 10:40:01 +03:00
|
|
|
// If we reached the select element, we're done
|
2015-03-03 14:08:59 +03:00
|
|
|
if (node->IsHTMLElement(nsGkAtoms::select)) {
|
2013-07-24 11:31:49 +04:00
|
|
|
return false;
|
1999-09-11 02:54:24 +04:00
|
|
|
}
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HTMLOptGroupElement> optGroupElement =
|
2013-07-24 11:31:49 +04:00
|
|
|
HTMLOptGroupElement::FromContent(node);
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2013-07-24 11:31:49 +04:00
|
|
|
if (!optGroupElement) {
|
2001-11-02 10:40:01 +03:00
|
|
|
// If you put something else between you and the optgroup, you're a
|
|
|
|
// moron and you deserve not to have optgroup disabling work.
|
2013-07-24 11:31:49 +04:00
|
|
|
return false;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
2013-07-24 11:31:49 +04:00
|
|
|
if (optGroupElement->Disabled()) {
|
|
|
|
return true;
|
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
}
|
2004-01-10 02:54:21 +03:00
|
|
|
|
2013-07-24 11:31:49 +04:00
|
|
|
return false;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::GetValue(nsAString& aValue)
|
2001-11-02 10:40:01 +03:00
|
|
|
{
|
2013-04-13 11:01:04 +04:00
|
|
|
DOMString value;
|
|
|
|
GetValue(value);
|
|
|
|
value.ToString(aValue);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2013-04-13 11:01:04 +04:00
|
|
|
void
|
|
|
|
HTMLSelectElement::GetValue(DOMString& aValue)
|
|
|
|
{
|
|
|
|
int32_t selectedIndex = SelectedIndex();
|
|
|
|
if (selectedIndex < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HTMLOptionElement> option =
|
2013-04-13 11:01:04 +04:00
|
|
|
Item(static_cast<uint32_t>(selectedIndex));
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2013-04-13 11:01:04 +04:00
|
|
|
if (!option) {
|
|
|
|
return;
|
1999-06-15 03:14:17 +04:00
|
|
|
}
|
|
|
|
|
2013-04-13 11:01:04 +04:00
|
|
|
DebugOnly<nsresult> rv = option->GetValue(aValue);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
1999-06-15 03:14:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::SetValue(const nsAString& aValue)
|
1999-06-15 03:14:17 +04:00
|
|
|
{
|
2013-04-13 11:01:04 +04:00
|
|
|
uint32_t length = Length();
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < length; i++) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HTMLOptionElement> option = Item(i);
|
2011-11-16 11:50:19 +04:00
|
|
|
if (!option) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-04-13 11:01:04 +04:00
|
|
|
|
2011-11-16 11:50:19 +04:00
|
|
|
nsAutoString optionVal;
|
|
|
|
option->GetValue(optionVal);
|
|
|
|
if (optionVal.Equals(aValue)) {
|
2012-08-22 19:56:38 +04:00
|
|
|
SetSelectedIndexInternal(int32_t(i), true);
|
2015-09-12 13:34:37 +03:00
|
|
|
return NS_OK;
|
1999-06-15 03:14:17 +04:00
|
|
|
}
|
|
|
|
}
|
2015-09-12 13:34:37 +03:00
|
|
|
// No matching option was found.
|
|
|
|
SetSelectedIndexInternal(-1, true);
|
2013-04-13 11:01:04 +04:00
|
|
|
return NS_OK;
|
1999-06-15 03:14:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-04 11:03:33 +04:00
|
|
|
NS_IMPL_BOOL_ATTR(HTMLSelectElement, Autofocus, autofocus)
|
|
|
|
NS_IMPL_BOOL_ATTR(HTMLSelectElement, Disabled, disabled)
|
|
|
|
NS_IMPL_BOOL_ATTR(HTMLSelectElement, Multiple, multiple)
|
|
|
|
NS_IMPL_STRING_ATTR(HTMLSelectElement, Name, name)
|
|
|
|
NS_IMPL_BOOL_ATTR(HTMLSelectElement, Required, required)
|
|
|
|
NS_IMPL_UINT_ATTR(HTMLSelectElement, Size, size)
|
2012-10-06 11:19:51 +04:00
|
|
|
|
|
|
|
int32_t
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::TabIndexDefault()
|
2012-10-06 11:19:51 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
1998-09-03 03:53:16 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::IsHTMLFocusable(bool aWithMouse,
|
|
|
|
bool* aIsFocusable, int32_t* aTabIndex)
|
1999-03-02 22:19:24 +03:00
|
|
|
{
|
2013-08-02 05:21:31 +04:00
|
|
|
if (nsGenericHTMLFormElementWithState::IsHTMLFocusable(aWithMouse, aIsFocusable,
|
|
|
|
aTabIndex))
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2000-09-08 05:46:00 +04:00
|
|
|
}
|
2010-06-21 16:37:34 +04:00
|
|
|
|
2010-09-19 01:33:16 +04:00
|
|
|
*aIsFocusable = !IsDisabled();
|
2010-06-21 16:37:34 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
1999-03-02 22:19:24 +03:00
|
|
|
}
|
|
|
|
|
2004-01-10 02:54:21 +03:00
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::Item(uint32_t aIndex, nsIDOMNode** aReturn)
|
1999-06-16 09:19:08 +04:00
|
|
|
{
|
2001-11-02 10:40:01 +03:00
|
|
|
return mOptions->Item(aIndex, aReturn);
|
2000-02-24 03:08:00 +03:00
|
|
|
}
|
|
|
|
|
2004-01-10 02:54:21 +03:00
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::NamedItem(const nsAString& aName, nsIDOMNode** aReturn)
|
2000-02-24 03:08:00 +03:00
|
|
|
{
|
2001-11-02 10:40:01 +03:00
|
|
|
return mOptions->NamedItem(aName, aReturn);
|
1999-06-16 09:19:08 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::CheckSelectSomething(bool aNotify)
|
2001-09-07 02:54:44 +04:00
|
|
|
{
|
2002-03-31 14:14:01 +04:00
|
|
|
if (mIsDoneAddingChildren) {
|
2005-06-21 03:26:35 +04:00
|
|
|
if (mSelectedIndex < 0 && IsCombobox()) {
|
2010-11-17 02:41:19 +03:00
|
|
|
return SelectSomething(aNotify);
|
2001-09-07 02:54:44 +04:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2001-09-07 02:54:44 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::SelectSomething(bool aNotify)
|
1999-05-05 00:53:26 +04:00
|
|
|
{
|
2001-11-02 10:40:01 +03:00
|
|
|
// If we're not done building the select, don't play with this yet.
|
2002-03-31 14:14:01 +04:00
|
|
|
if (!mIsDoneAddingChildren) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
1999-05-19 07:46:25 +04:00
|
|
|
}
|
1999-05-05 00:53:26 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count;
|
2001-11-02 10:40:01 +03:00
|
|
|
GetLength(&count);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool disabled;
|
2001-11-28 09:52:56 +03:00
|
|
|
nsresult rv = IsOptionDisabled(i, &disabled);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || !disabled) {
|
2010-11-24 13:09:01 +03:00
|
|
|
rv = SetSelectedIndexInternal(i, aNotify);
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2010-11-17 02:41:19 +03:00
|
|
|
|
|
|
|
UpdateValueMissingValidityState();
|
2011-06-01 05:46:57 +04:00
|
|
|
UpdateState(aNotify);
|
2010-11-17 02:41:19 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2000-02-12 19:26:24 +03:00
|
|
|
}
|
|
|
|
}
|
2003-03-14 09:18:20 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
1999-05-05 00:53:26 +04:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:33:16 +04:00
|
|
|
nsresult
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
|
|
|
|
nsIContent* aBindingParent,
|
|
|
|
bool aCompileEventHandlers)
|
2010-09-19 01:33:16 +04:00
|
|
|
{
|
2013-08-02 05:21:31 +04:00
|
|
|
nsresult rv = nsGenericHTMLFormElementWithState::BindToTree(aDocument, aParent,
|
|
|
|
aBindingParent,
|
|
|
|
aCompileEventHandlers);
|
2010-09-19 01:33:16 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// If there is a disabled fieldset in the parent chain, the element is now
|
|
|
|
// barred from constraint validation.
|
2011-06-01 05:46:57 +04:00
|
|
|
// XXXbz is this still needed now that fieldset changes always call
|
|
|
|
// FieldSetDisabledChanged?
|
2010-09-19 01:33:16 +04:00
|
|
|
UpdateBarredFromConstraintValidation();
|
|
|
|
|
2011-06-01 05:46:57 +04:00
|
|
|
// And now make sure our state is up to date
|
|
|
|
UpdateState(false);
|
|
|
|
|
2010-09-19 01:33:16 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-06-01 05:46:57 +04:00
|
|
|
void
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::UnbindFromTree(bool aDeep, bool aNullParent)
|
2011-06-01 05:46:57 +04:00
|
|
|
{
|
2013-08-02 05:21:31 +04:00
|
|
|
nsGenericHTMLFormElementWithState::UnbindFromTree(aDeep, aNullParent);
|
2011-06-01 05:46:57 +04:00
|
|
|
|
|
|
|
// We might be no longer disabled because our parent chain changed.
|
2011-06-01 05:46:57 +04:00
|
|
|
// XXXbz is this still needed now that fieldset changes always call
|
|
|
|
// FieldSetDisabledChanged?
|
2011-06-01 05:46:57 +04:00
|
|
|
UpdateBarredFromConstraintValidation();
|
2011-06-01 05:46:57 +04:00
|
|
|
|
|
|
|
// And now make sure our state is up to date
|
|
|
|
UpdateState(false);
|
2011-06-01 05:46:57 +04:00
|
|
|
}
|
|
|
|
|
2006-11-28 06:17:03 +03:00
|
|
|
nsresult
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::BeforeSetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
2015-08-01 08:14:06 +03:00
|
|
|
nsAttrValueOrString* aValue,
|
2013-04-04 11:03:33 +04:00
|
|
|
bool aNotify)
|
2006-11-28 06:17:03 +03:00
|
|
|
{
|
2006-12-26 20:47:52 +03:00
|
|
|
if (aNotify && aName == nsGkAtoms::disabled &&
|
2006-11-28 06:17:03 +03:00
|
|
|
aNameSpaceID == kNameSpaceID_None) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mDisabledChanged = true;
|
2006-11-28 06:17:03 +03:00
|
|
|
}
|
|
|
|
|
2013-08-02 05:21:31 +04:00
|
|
|
return nsGenericHTMLFormElementWithState::BeforeSetAttr(aNameSpaceID, aName,
|
|
|
|
aValue, aNotify);
|
2006-11-28 06:17:03 +03:00
|
|
|
}
|
|
|
|
|
2010-09-10 09:08:56 +04:00
|
|
|
nsresult
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
|
|
|
const nsAttrValue* aValue, bool aNotify)
|
2010-09-10 09:08:56 +04:00
|
|
|
{
|
2010-11-17 02:41:19 +03:00
|
|
|
if (aNameSpaceID == kNameSpaceID_None) {
|
|
|
|
if (aName == nsGkAtoms::disabled) {
|
|
|
|
UpdateBarredFromConstraintValidation();
|
|
|
|
} else if (aName == nsGkAtoms::required) {
|
|
|
|
UpdateValueMissingValidityState();
|
2014-07-24 18:55:00 +04:00
|
|
|
} else if (aName == nsGkAtoms::autocomplete) {
|
|
|
|
// Clear the cached @autocomplete attribute state
|
|
|
|
mAutocompleteAttrState = nsContentUtils::eAutocompleteAttrState_Unknown;
|
2010-11-17 02:41:19 +03:00
|
|
|
}
|
|
|
|
|
2011-06-01 05:46:57 +04:00
|
|
|
UpdateState(aNotify);
|
2010-09-10 09:08:56 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 05:21:31 +04:00
|
|
|
return nsGenericHTMLFormElementWithState::AfterSetAttr(aNameSpaceID, aName,
|
|
|
|
aValue, aNotify);
|
2010-09-10 09:08:56 +04:00
|
|
|
}
|
|
|
|
|
2005-09-29 18:30:32 +04:00
|
|
|
nsresult
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::UnsetAttr(int32_t aNameSpaceID, nsIAtom* aAttribute,
|
|
|
|
bool aNotify)
|
2005-09-29 18:30:32 +04:00
|
|
|
{
|
|
|
|
if (aNotify && aNameSpaceID == kNameSpaceID_None &&
|
2006-12-26 20:47:52 +03:00
|
|
|
aAttribute == nsGkAtoms::multiple) {
|
2005-09-29 18:30:32 +04:00
|
|
|
// We're changing from being a multi-select to a single-select.
|
|
|
|
// Make sure we only have one option selected before we do that.
|
|
|
|
// Note that this needs to come before we really unset the attr,
|
|
|
|
// since SetOptionsSelectedByIndex does some bail-out type
|
|
|
|
// optimization for cases when the select is not multiple that
|
|
|
|
// would lead to only a single option getting deselected.
|
|
|
|
if (mSelectedIndex >= 0) {
|
2010-11-24 13:09:01 +03:00
|
|
|
SetSelectedIndexInternal(mSelectedIndex, aNotify);
|
2005-09-29 18:30:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:21:31 +04:00
|
|
|
nsresult rv = nsGenericHTMLFormElementWithState::UnsetAttr(aNameSpaceID, aAttribute,
|
|
|
|
aNotify);
|
2005-09-29 18:30:32 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (aNotify && aNameSpaceID == kNameSpaceID_None &&
|
2006-12-26 20:47:52 +03:00
|
|
|
aAttribute == nsGkAtoms::multiple) {
|
2005-09-29 18:30:32 +04:00
|
|
|
// We might have become a combobox; make sure _something_ gets
|
|
|
|
// selected in that case
|
2010-11-17 02:41:19 +03:00
|
|
|
CheckSelectSomething(aNotify);
|
2005-09-29 18:30:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-11-16 11:50:18 +04:00
|
|
|
void
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::DoneAddingChildren(bool aHaveNotified)
|
1999-11-12 01:13:20 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDoneAddingChildren = true;
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2002-03-31 14:14:01 +04:00
|
|
|
nsISelectControlFrame* selectFrame = GetSelectFrame();
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
// If we foolishly tried to restore before we were done adding
|
|
|
|
// content, restore the rest of the options proper-like
|
2002-03-31 14:14:01 +04:00
|
|
|
if (mRestoreState) {
|
2001-11-02 10:40:01 +03:00
|
|
|
RestoreStateTo(mRestoreState);
|
2012-07-30 18:20:58 +04:00
|
|
|
mRestoreState = nullptr;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2002-03-31 14:14:01 +04:00
|
|
|
// Notify the frame
|
|
|
|
if (selectFrame) {
|
2011-10-17 18:59:28 +04:00
|
|
|
selectFrame->DoneAddingChildren(true);
|
1999-11-12 01:13:20 +03:00
|
|
|
}
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2010-06-09 10:45:32 +04:00
|
|
|
if (!mInhibitStateRestoration) {
|
2013-08-02 05:21:31 +04:00
|
|
|
nsresult rv = GenerateStateKey();
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
RestoreFormControlState();
|
|
|
|
}
|
2010-06-09 10:45:32 +04:00
|
|
|
}
|
2002-03-31 14:14:01 +04:00
|
|
|
|
|
|
|
// Now that we're done, select something (if it's a single select something
|
|
|
|
// must be selected)
|
2011-10-17 18:59:28 +04:00
|
|
|
if (!CheckSelectSomething(false)) {
|
2010-11-17 02:41:19 +03:00
|
|
|
// If an option has @selected set, it will be selected during parsing but
|
|
|
|
// with an empty value. We have to make sure the select element updates it's
|
|
|
|
// validity state to take this into account.
|
|
|
|
UpdateValueMissingValidityState();
|
2011-06-01 05:46:57 +04:00
|
|
|
|
|
|
|
// And now make sure we update our content state too
|
|
|
|
UpdateState(aHaveNotified);
|
2010-11-17 02:41:19 +03:00
|
|
|
}
|
2006-11-04 08:45:02 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mDefaultSelectionSet = true;
|
1999-11-12 01:13:20 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::ParseAttribute(int32_t aNamespaceID,
|
|
|
|
nsIAtom* aAttribute,
|
|
|
|
const nsAString& aValue,
|
|
|
|
nsAttrValue& aResult)
|
1998-09-03 03:53:16 +04:00
|
|
|
{
|
2014-07-24 18:55:00 +04:00
|
|
|
if (kNameSpaceID_None == aNamespaceID) {
|
|
|
|
if (aAttribute == nsGkAtoms::size) {
|
|
|
|
return aResult.ParsePositiveIntValue(aValue);
|
|
|
|
} else if (aAttribute == nsGkAtoms::autocomplete) {
|
|
|
|
aResult.ParseAtomArray(aValue);
|
|
|
|
return true;
|
|
|
|
}
|
1998-09-23 21:16:51 +04:00
|
|
|
}
|
2005-11-29 19:37:15 +03:00
|
|
|
return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
|
|
|
|
aResult);
|
1998-09-03 03:53:16 +04:00
|
|
|
}
|
|
|
|
|
2013-11-19 23:21:29 +04:00
|
|
|
void
|
|
|
|
HTMLSelectElement::MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
|
|
|
|
nsRuleData* aData)
|
1998-09-03 03:53:16 +04:00
|
|
|
{
|
2013-08-02 05:21:31 +04:00
|
|
|
nsGenericHTMLFormElementWithState::MapImageAlignAttributeInto(aAttributes, aData);
|
|
|
|
nsGenericHTMLFormElementWithState::MapCommonAttributesInto(aAttributes, aData);
|
1998-09-03 03:53:16 +04:00
|
|
|
}
|
|
|
|
|
2004-10-24 21:10:32 +04:00
|
|
|
nsChangeHint
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::GetAttributeChangeHint(const nsIAtom* aAttribute,
|
|
|
|
int32_t aModType) const
|
1999-07-07 05:24:40 +04:00
|
|
|
{
|
2004-10-24 21:10:32 +04:00
|
|
|
nsChangeHint retval =
|
2013-08-02 05:21:31 +04:00
|
|
|
nsGenericHTMLFormElementWithState::GetAttributeChangeHint(aAttribute, aModType);
|
2006-12-26 20:47:52 +03:00
|
|
|
if (aAttribute == nsGkAtoms::multiple ||
|
|
|
|
aAttribute == nsGkAtoms::size) {
|
2004-10-24 21:10:32 +04:00
|
|
|
NS_UpdateHint(retval, NS_STYLE_HINT_FRAMECHANGE);
|
2003-07-12 01:16:12 +04:00
|
|
|
}
|
2004-10-24 21:10:32 +04:00
|
|
|
return retval;
|
2003-07-12 01:16:12 +04:00
|
|
|
}
|
2003-04-17 00:54:20 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP_(bool)
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::IsAttributeMapped(const nsIAtom* aAttribute) const
|
2003-07-12 01:16:12 +04:00
|
|
|
{
|
2004-02-26 00:04:50 +03:00
|
|
|
static const MappedAttributeEntry* const map[] = {
|
2003-04-17 00:54:20 +04:00
|
|
|
sCommonAttributeMap,
|
2003-07-12 01:16:12 +04:00
|
|
|
sImageAlignAttributeMap
|
2003-04-17 00:54:20 +04:00
|
|
|
};
|
|
|
|
|
2011-12-18 14:09:27 +04:00
|
|
|
return FindAttributeDependence(aAttribute, map);
|
1999-07-07 05:24:40 +04:00
|
|
|
}
|
|
|
|
|
2005-01-12 22:45:38 +03:00
|
|
|
nsMapRuleToAttributesFunc
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::GetAttributeMappingFunction() const
|
1998-09-05 08:00:06 +04:00
|
|
|
{
|
2005-01-12 22:45:38 +03:00
|
|
|
return &MapAttributesIntoRule;
|
1998-09-05 08:00:06 +04:00
|
|
|
}
|
|
|
|
|
2013-01-03 19:17:36 +04:00
|
|
|
bool
|
2015-08-26 15:56:59 +03:00
|
|
|
HTMLSelectElement::IsDisabledForEvents(EventMessage aMessage)
|
1998-09-03 03:53:16 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
nsIFormControlFrame* formControlFrame = GetFormControlFrame(false);
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIFrame* formFrame = nullptr;
|
2011-07-19 03:16:44 +04:00
|
|
|
if (formControlFrame) {
|
|
|
|
formFrame = do_QueryFrame(formControlFrame);
|
|
|
|
}
|
2013-01-03 19:17:36 +04:00
|
|
|
return IsElementDisabledForEvents(aMessage, formFrame);
|
|
|
|
}
|
2000-08-09 03:38:00 +04:00
|
|
|
|
2013-01-03 19:17:36 +04:00
|
|
|
nsresult
|
2014-03-18 08:48:19 +04:00
|
|
|
HTMLSelectElement::PreHandleEvent(EventChainPreVisitor& aVisitor)
|
2013-01-03 19:17:36 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
aVisitor.mCanHandle = false;
|
2015-08-22 04:34:51 +03:00
|
|
|
if (IsDisabledForEvents(aVisitor.mEvent->mMessage)) {
|
2011-07-19 03:16:44 +04:00
|
|
|
return NS_OK;
|
2000-08-09 03:38:00 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 05:21:31 +04:00
|
|
|
return nsGenericHTMLFormElementWithState::PreHandleEvent(aVisitor);
|
1998-09-03 03:53:16 +04:00
|
|
|
}
|
1998-09-23 21:16:51 +04:00
|
|
|
|
2010-11-24 13:09:31 +03:00
|
|
|
nsresult
|
2014-03-18 08:48:20 +04:00
|
|
|
HTMLSelectElement::PostHandleEvent(EventChainPostVisitor& aVisitor)
|
2010-11-24 13:09:31 +03:00
|
|
|
{
|
2015-09-02 09:08:00 +03:00
|
|
|
if (aVisitor.mEvent->mMessage == eFocus) {
|
2010-11-24 13:09:31 +03:00
|
|
|
// If the invalid UI is shown, we should show it while focused and
|
|
|
|
// update the invalid/valid UI.
|
2011-01-20 14:05:29 +03:00
|
|
|
mCanShowInvalidUI = !IsValid() && ShouldShowValidityUI();
|
2010-11-24 13:09:31 +03:00
|
|
|
|
|
|
|
// If neither invalid UI nor valid UI is shown, we shouldn't show the valid
|
|
|
|
// UI while focused.
|
2011-01-20 14:05:29 +03:00
|
|
|
mCanShowValidUI = ShouldShowValidityUI();
|
2010-11-24 13:09:31 +03:00
|
|
|
|
|
|
|
// We don't have to update NS_EVENT_STATE_MOZ_UI_INVALID nor
|
|
|
|
// NS_EVENT_STATE_MOZ_UI_VALID given that the states should not change.
|
2015-09-02 09:08:00 +03:00
|
|
|
} else if (aVisitor.mEvent->mMessage == eBlur) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mCanShowInvalidUI = true;
|
|
|
|
mCanShowValidUI = true;
|
2010-11-24 13:09:31 +03:00
|
|
|
|
2011-06-01 05:46:57 +04:00
|
|
|
UpdateState(true);
|
2010-11-24 13:09:31 +03:00
|
|
|
}
|
|
|
|
|
2013-08-02 05:21:31 +04:00
|
|
|
return nsGenericHTMLFormElementWithState::PostHandleEvent(aVisitor);
|
2010-11-24 13:09:31 +03:00
|
|
|
}
|
|
|
|
|
2014-04-03 08:18:36 +04:00
|
|
|
EventStates
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::IntrinsicState() const
|
2010-08-19 04:03:20 +04:00
|
|
|
{
|
2014-04-03 08:18:36 +04:00
|
|
|
EventStates state = nsGenericHTMLFormElementWithState::IntrinsicState();
|
2010-08-21 21:52:57 +04:00
|
|
|
|
2010-08-21 22:51:38 +04:00
|
|
|
if (IsCandidateForConstraintValidation()) {
|
2010-11-24 13:09:01 +03:00
|
|
|
if (IsValid()) {
|
|
|
|
state |= NS_EVENT_STATE_VALID;
|
|
|
|
} else {
|
|
|
|
state |= NS_EVENT_STATE_INVALID;
|
|
|
|
|
2011-01-27 16:51:45 +03:00
|
|
|
if ((!mForm || !mForm->HasAttr(kNameSpaceID_None, nsGkAtoms::novalidate)) &&
|
|
|
|
(GetValidityState(VALIDITY_STATE_CUSTOM_ERROR) ||
|
|
|
|
(mCanShowInvalidUI && ShouldShowValidityUI()))) {
|
2010-11-24 13:09:01 +03:00
|
|
|
state |= NS_EVENT_STATE_MOZ_UI_INVALID;
|
|
|
|
}
|
|
|
|
}
|
2010-11-24 13:09:31 +03:00
|
|
|
|
|
|
|
// :-moz-ui-valid applies if all the following are true:
|
|
|
|
// 1. The element is not focused, or had either :-moz-ui-valid or
|
|
|
|
// :-moz-ui-invalid applying before it was focused ;
|
|
|
|
// 2. The element is either valid or isn't allowed to have
|
|
|
|
// :-moz-ui-invalid applying ;
|
2011-01-27 16:51:45 +03:00
|
|
|
// 3. The element has no form owner or its form owner doesn't have the
|
|
|
|
// novalidate attribute set ;
|
|
|
|
// 4. The element has already been modified or the user tried to submit the
|
|
|
|
// form owner while invalid.
|
|
|
|
if ((!mForm || !mForm->HasAttr(kNameSpaceID_None, nsGkAtoms::novalidate)) &&
|
|
|
|
(mCanShowValidUI && ShouldShowValidityUI() &&
|
|
|
|
(IsValid() || (state.HasState(NS_EVENT_STATE_MOZ_UI_INVALID) &&
|
|
|
|
!mCanShowInvalidUI)))) {
|
2010-11-24 13:09:31 +03:00
|
|
|
state |= NS_EVENT_STATE_MOZ_UI_VALID;
|
|
|
|
}
|
2010-08-21 21:52:57 +04:00
|
|
|
}
|
|
|
|
|
2010-11-16 17:40:21 +03:00
|
|
|
if (HasAttr(kNameSpaceID_None, nsGkAtoms::required)) {
|
|
|
|
state |= NS_EVENT_STATE_REQUIRED;
|
|
|
|
} else {
|
|
|
|
state |= NS_EVENT_STATE_OPTIONAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
2010-08-19 04:03:20 +04:00
|
|
|
}
|
|
|
|
|
1998-09-23 21:16:51 +04:00
|
|
|
// nsIFormControl
|
|
|
|
|
1999-05-05 00:53:26 +04:00
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::SaveState()
|
1999-05-05 00:53:26 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SelectState> state = new SelectState();
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2013-07-24 11:37:20 +04:00
|
|
|
uint32_t len = Length();
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t optIndex = 0; optIndex < len; optIndex++) {
|
2013-07-24 11:37:20 +04:00
|
|
|
HTMLOptionElement* option = Item(optIndex);
|
|
|
|
if (option && option->Selected()) {
|
|
|
|
nsAutoString value;
|
|
|
|
option->GetValue(value);
|
|
|
|
state->PutOption(optIndex, value);
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
1999-05-05 00:53:26 +04:00
|
|
|
}
|
|
|
|
|
2013-07-24 11:38:13 +04:00
|
|
|
nsPresState* presState = GetPrimaryPresState();
|
2002-08-17 05:56:04 +04:00
|
|
|
if (presState) {
|
2008-12-12 22:25:22 +03:00
|
|
|
presState->SetStateProperty(state);
|
2006-11-28 06:17:03 +03:00
|
|
|
|
|
|
|
if (mDisabledChanged) {
|
2010-09-19 01:33:16 +04:00
|
|
|
// We do not want to save the real disabled state but the disabled
|
|
|
|
// attribute.
|
|
|
|
presState->SetDisabled(HasAttr(kNameSpaceID_None, nsGkAtoms::disabled));
|
2006-11-28 06:17:03 +03:00
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
2002-08-17 05:56:04 +04:00
|
|
|
|
2013-07-24 11:38:13 +04:00
|
|
|
return NS_OK;
|
1999-05-05 00:53:26 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::RestoreState(nsPresState* aState)
|
2001-11-02 10:40:01 +03:00
|
|
|
{
|
|
|
|
// Get the presentation state object to retrieve our stuff out of.
|
2013-04-04 11:03:33 +04:00
|
|
|
nsCOMPtr<SelectState> state(
|
2008-12-12 22:25:22 +03:00
|
|
|
do_QueryInterface(aState->GetStateProperty()));
|
|
|
|
|
|
|
|
if (state) {
|
|
|
|
RestoreStateTo(state);
|
1999-05-05 00:53:26 +04:00
|
|
|
|
2007-10-10 07:39:16 +04:00
|
|
|
// Don't flush, if the frame doesn't exist yet it doesn't care if
|
|
|
|
// we're reset or not.
|
|
|
|
DispatchContentReset();
|
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2008-12-12 22:25:22 +03:00
|
|
|
if (aState->IsDisabledSet()) {
|
|
|
|
SetDisabled(aState->GetDisabled());
|
2006-11-28 06:17:03 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
2002-08-06 08:59:15 +04:00
|
|
|
void
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::RestoreStateTo(SelectState* aNewSelected)
|
1998-09-23 21:16:51 +04:00
|
|
|
{
|
2002-03-31 14:14:01 +04:00
|
|
|
if (!mIsDoneAddingChildren) {
|
2002-08-17 05:56:04 +04:00
|
|
|
mRestoreState = aNewSelected;
|
2002-08-06 08:59:15 +04:00
|
|
|
return;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2013-07-24 11:37:24 +04:00
|
|
|
uint32_t len = Length();
|
2013-09-28 23:04:20 +04:00
|
|
|
uint32_t mask = IS_SELECTED | CLEAR_ALL | SET_DISABLED | NOTIFY;
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
// First clear all
|
2013-09-28 23:04:20 +04:00
|
|
|
SetOptionsSelectedByIndex(-1, -1, mask);
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
// Next set the proper ones
|
2013-07-24 11:37:24 +04:00
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
|
|
|
HTMLOptionElement* option = Item(i);
|
2002-08-17 05:56:04 +04:00
|
|
|
if (option) {
|
|
|
|
nsAutoString value;
|
2008-12-12 22:25:22 +03:00
|
|
|
nsresult rv = option->GetValue(value);
|
|
|
|
if (NS_SUCCEEDED(rv) && aNewSelected->ContainsOption(i, value)) {
|
2013-09-28 23:04:20 +04:00
|
|
|
SetOptionsSelectedByIndex(i, i, IS_SELECTED | SET_DISABLED | NOTIFY);
|
2002-08-17 05:56:04 +04:00
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-06 08:59:15 +04:00
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::Reset()
|
2001-11-02 10:40:01 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numSelected = 0;
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2002-08-06 08:59:15 +04:00
|
|
|
//
|
|
|
|
// Cycle through the options array and reset the options
|
|
|
|
//
|
2013-07-24 11:37:28 +04:00
|
|
|
uint32_t numOptions = Length();
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < numOptions; i++) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HTMLOptionElement> option = Item(i);
|
2001-11-02 10:40:01 +03:00
|
|
|
if (option) {
|
2002-08-06 08:59:15 +04:00
|
|
|
//
|
|
|
|
// Reset the option to its default value
|
|
|
|
//
|
2013-09-28 23:04:20 +04:00
|
|
|
|
|
|
|
uint32_t mask = SET_DISABLED | NOTIFY;
|
|
|
|
if (option->DefaultSelected()) {
|
|
|
|
mask |= IS_SELECTED;
|
2002-08-06 08:59:15 +04:00
|
|
|
numSelected++;
|
|
|
|
}
|
2013-09-28 23:04:20 +04:00
|
|
|
|
|
|
|
SetOptionsSelectedByIndex(i, i, mask);
|
1998-09-23 21:16:51 +04:00
|
|
|
}
|
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2002-08-06 08:59:15 +04:00
|
|
|
//
|
|
|
|
// If nothing was selected and it's not multiple, select something
|
|
|
|
//
|
2005-06-21 03:26:35 +04:00
|
|
|
if (numSelected == 0 && IsCombobox()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
SelectSomething(true);
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
SetSelectionChanged(false, true);
|
2010-11-24 13:09:01 +03:00
|
|
|
|
2002-08-06 08:59:15 +04:00
|
|
|
//
|
|
|
|
// Let the frame know we were reset
|
|
|
|
//
|
2001-11-06 12:10:07 +03:00
|
|
|
// Don't flush, if there's no frame yet it won't care about us being
|
|
|
|
// reset even if we forced it to be created now.
|
2002-08-06 08:59:15 +04:00
|
|
|
//
|
2005-12-13 02:53:06 +03:00
|
|
|
DispatchContentReset();
|
2001-11-02 10:40:01 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:16 +03:00
|
|
|
static NS_DEFINE_CID(kFormProcessorCID, NS_FORMPROCESSOR_CID);
|
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
NS_IMETHODIMP
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::SubmitNamesValues(nsFormSubmission* aFormSubmission)
|
2001-11-02 10:40:01 +03:00
|
|
|
{
|
2002-02-16 04:19:24 +03:00
|
|
|
// Disabled elements don't submit
|
2010-09-19 01:33:16 +04:00
|
|
|
if (IsDisabled()) {
|
|
|
|
return NS_OK;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
//
|
|
|
|
// Get the name (if no name, no submit)
|
|
|
|
//
|
2001-11-02 10:40:01 +03:00
|
|
|
nsAutoString name;
|
2010-02-25 08:57:54 +03:00
|
|
|
GetAttr(kNameSpaceID_None, nsGkAtoms::name, name);
|
|
|
|
if (name.IsEmpty()) {
|
2005-10-28 15:25:24 +04:00
|
|
|
return NS_OK;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
//
|
|
|
|
// Submit
|
|
|
|
//
|
2013-07-24 11:37:32 +04:00
|
|
|
uint32_t len = Length();
|
2001-11-02 10:40:01 +03:00
|
|
|
|
2010-02-25 08:58:16 +03:00
|
|
|
nsAutoString mozType;
|
|
|
|
nsCOMPtr<nsIFormProcessor> keyGenProcessor;
|
2013-08-10 02:08:56 +04:00
|
|
|
if (GetAttr(kNameSpaceID_None, nsGkAtoms::moztype, mozType) &&
|
2010-02-25 08:58:16 +03:00
|
|
|
mozType.EqualsLiteral("-mozilla-keygen")) {
|
2011-11-16 11:50:19 +04:00
|
|
|
keyGenProcessor = do_GetService(kFormProcessorCID);
|
2010-02-25 08:58:16 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t optIndex = 0; optIndex < len; optIndex++) {
|
2013-07-24 11:37:32 +04:00
|
|
|
HTMLOptionElement* option = Item(optIndex);
|
|
|
|
|
2001-11-02 10:40:01 +03:00
|
|
|
// Don't send disabled options
|
2013-07-24 11:37:32 +04:00
|
|
|
if (!option || IsOptionDisabled(option)) {
|
2001-11-02 10:40:01 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-07-24 11:37:32 +04:00
|
|
|
if (!option->Selected()) {
|
2002-02-16 04:19:24 +03:00
|
|
|
continue;
|
2001-11-02 10:40:01 +03:00
|
|
|
}
|
|
|
|
|
2013-07-24 11:37:32 +04:00
|
|
|
nsString value;
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(option->GetValue(value)));
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2010-02-25 08:58:16 +03:00
|
|
|
if (keyGenProcessor) {
|
2013-07-24 11:37:32 +04:00
|
|
|
nsString tmp(value);
|
|
|
|
if (NS_SUCCEEDED(keyGenProcessor->ProcessValue(this, name, tmp))) {
|
2010-02-25 08:58:16 +03:00
|
|
|
value = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-24 11:37:32 +04:00
|
|
|
aFormSubmission->AddNameValuePair(name, value);
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
2001-11-02 10:40:01 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-16 11:50:19 +04:00
|
|
|
void
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::DispatchContentReset()
|
2011-11-16 11:50:19 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
nsIFormControlFrame* formControlFrame = GetFormControlFrame(false);
|
2005-12-13 02:53:06 +03:00
|
|
|
if (formControlFrame) {
|
|
|
|
// Only dispatch content reset notification if this is a list control
|
|
|
|
// frame or combo box control frame.
|
|
|
|
if (IsCombobox()) {
|
2009-01-12 22:20:59 +03:00
|
|
|
nsIComboboxControlFrame* comboFrame = do_QueryFrame(formControlFrame);
|
2005-12-13 02:53:06 +03:00
|
|
|
if (comboFrame) {
|
|
|
|
comboFrame->OnContentReset();
|
|
|
|
}
|
|
|
|
} else {
|
2009-01-12 22:20:59 +03:00
|
|
|
nsIListControlFrame* listFrame = do_QueryFrame(formControlFrame);
|
2005-12-13 02:53:06 +03:00
|
|
|
if (listFrame) {
|
|
|
|
listFrame->OnContentReset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-03 00:30:23 +03:00
|
|
|
static void
|
2015-10-13 12:28:00 +03:00
|
|
|
AddOptions(nsIContent* aRoot, HTMLOptionsCollection* aArray)
|
2006-03-03 00:30:23 +03:00
|
|
|
{
|
2015-10-13 12:28:00 +03:00
|
|
|
for (nsIContent* child = aRoot->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
|
|
|
HTMLOptionElement* opt = HTMLOptionElement::FromContent(child);
|
2006-03-03 00:30:23 +03:00
|
|
|
if (opt) {
|
|
|
|
aArray->AppendOption(opt);
|
2015-10-13 12:28:00 +03:00
|
|
|
} else if (child->IsHTMLElement(nsGkAtoms::optgroup)) {
|
|
|
|
for (nsIContent* grandchild = child->GetFirstChild();
|
|
|
|
grandchild;
|
|
|
|
grandchild = grandchild->GetNextSibling()) {
|
|
|
|
opt = HTMLOptionElement::FromContent(grandchild);
|
|
|
|
if (opt) {
|
|
|
|
aArray->AppendOption(opt);
|
|
|
|
}
|
|
|
|
}
|
2006-03-03 00:30:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::RebuildOptionsArray(bool aNotify)
|
2006-03-03 00:30:23 +03:00
|
|
|
{
|
|
|
|
mOptions->Clear();
|
2015-10-13 12:28:00 +03:00
|
|
|
AddOptions(this, mOptions);
|
2010-11-24 13:09:01 +03:00
|
|
|
FindSelectedIndex(0, aNotify);
|
2006-03-03 00:30:23 +03:00
|
|
|
}
|
|
|
|
|
2010-11-17 02:41:19 +03:00
|
|
|
bool
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::IsValueMissing()
|
2010-11-17 02:41:19 +03:00
|
|
|
{
|
2013-07-24 11:37:36 +04:00
|
|
|
if (!Required()) {
|
2010-11-17 02:41:19 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-24 11:37:36 +04:00
|
|
|
uint32_t length = Length();
|
2010-11-17 02:41:19 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HTMLOptionElement> option = Item(i);
|
2013-07-24 11:37:36 +04:00
|
|
|
if (!option->Selected()) {
|
2010-11-17 02:41:19 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-07-24 11:37:36 +04:00
|
|
|
if (IsOptionDisabled(option)) {
|
2010-11-17 02:41:19 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString value;
|
2013-07-24 11:37:36 +04:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(option->GetValue(value)));
|
2010-11-17 02:41:19 +03:00
|
|
|
if (!value.IsEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::UpdateValueMissingValidityState()
|
2010-11-17 02:41:19 +03:00
|
|
|
{
|
|
|
|
SetValidityState(VALIDITY_STATE_VALUE_MISSING, IsValueMissing());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::GetValidationMessage(nsAString& aValidationMessage,
|
|
|
|
ValidityStateType aType)
|
2011-11-16 11:50:19 +04:00
|
|
|
{
|
|
|
|
switch (aType) {
|
|
|
|
case VALIDITY_STATE_VALUE_MISSING: {
|
|
|
|
nsXPIDLString message;
|
|
|
|
nsresult rv = nsContentUtils::GetLocalizedString(nsContentUtils::eDOM_PROPERTIES,
|
|
|
|
"FormValidationSelectMissing",
|
|
|
|
message);
|
|
|
|
aValidationMessage = message;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return nsIConstraintValidation::GetValidationMessage(aValidationMessage, aType);
|
|
|
|
}
|
2010-11-17 02:41:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-03 00:30:23 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
2015-10-13 12:28:00 +03:00
|
|
|
void
|
|
|
|
HTMLSelectElement::VerifyOptionsArray()
|
2006-03-03 00:30:23 +03:00
|
|
|
{
|
2015-10-13 12:28:00 +03:00
|
|
|
int32_t index = 0;
|
|
|
|
for (nsIContent* child = nsINode::GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
|
|
|
HTMLOptionElement* opt = HTMLOptionElement::FromContent(child);
|
2006-03-03 00:30:23 +03:00
|
|
|
if (opt) {
|
2015-10-13 12:28:00 +03:00
|
|
|
NS_ASSERTION(opt == mOptions->ItemAsOption(index++),
|
2006-03-03 00:30:23 +03:00
|
|
|
"Options collection broken");
|
2015-10-13 12:28:00 +03:00
|
|
|
} else if (child->IsHTMLElement(nsGkAtoms::optgroup)) {
|
|
|
|
for (nsIContent* grandchild = child->GetFirstChild();
|
|
|
|
grandchild;
|
|
|
|
grandchild = grandchild->GetNextSibling()) {
|
|
|
|
opt = HTMLOptionElement::FromContent(grandchild);
|
|
|
|
if (opt) {
|
|
|
|
NS_ASSERTION(opt == mOptions->ItemAsOption(index++),
|
|
|
|
"Options collection broken");
|
|
|
|
}
|
|
|
|
}
|
2006-03-03 00:30:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-09-19 01:33:16 +04:00
|
|
|
void
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::UpdateBarredFromConstraintValidation()
|
2010-09-19 01:33:16 +04:00
|
|
|
{
|
|
|
|
SetBarredFromConstraintValidation(IsDisabled());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::FieldSetDisabledChanged(bool aNotify)
|
2010-09-19 01:33:16 +04:00
|
|
|
{
|
|
|
|
UpdateBarredFromConstraintValidation();
|
|
|
|
|
2013-08-02 05:21:31 +04:00
|
|
|
nsGenericHTMLFormElementWithState::FieldSetDisabledChanged(aNotify);
|
2010-09-19 01:33:16 +04:00
|
|
|
}
|
|
|
|
|
2010-11-24 13:09:01 +03:00
|
|
|
void
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement::SetSelectionChanged(bool aValue, bool aNotify)
|
2010-11-24 13:09:01 +03:00
|
|
|
{
|
|
|
|
if (!mDefaultSelectionSet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-02 21:23:27 +04:00
|
|
|
UpdateSelectedOptions();
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool previousSelectionChangedValue = mSelectionHasChanged;
|
2010-11-24 13:09:01 +03:00
|
|
|
mSelectionHasChanged = aValue;
|
|
|
|
|
2011-06-01 05:46:57 +04:00
|
|
|
if (mSelectionHasChanged != previousSelectionChangedValue) {
|
|
|
|
UpdateState(aNotify);
|
2010-11-24 13:09:01 +03:00
|
|
|
}
|
|
|
|
}
|
2013-04-04 11:03:33 +04:00
|
|
|
|
2013-09-02 21:23:27 +04:00
|
|
|
void
|
|
|
|
HTMLSelectElement::UpdateSelectedOptions()
|
|
|
|
{
|
|
|
|
if (mSelectedOptions) {
|
|
|
|
mSelectedOptions->SetDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-13 11:01:59 +04:00
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
HTMLSelectElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-04-13 11:01:59 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return HTMLSelectElementBinding::Wrap(aCx, this, aGivenProto);
|
2013-04-13 11:01:59 +04:00
|
|
|
}
|
|
|
|
|
2013-04-04 11:03:33 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|