2015-05-03 12:32:37 -07: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 04:12:37 -07: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/. */
|
2010-07-13 23:39:48 -07:00
|
|
|
|
2013-02-18 04:24:58 -08:00
|
|
|
#include "mozilla/dom/HTMLOptionElement.h"
|
2013-02-18 04:26:57 -08:00
|
|
|
#include "mozilla/dom/HTMLOptionElementBinding.h"
|
2013-04-04 00:03:33 -07:00
|
|
|
#include "mozilla/dom/HTMLSelectElement.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMHTMLOptGroupElement.h"
|
|
|
|
#include "nsIDOMHTMLFormElement.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsStyleConsts.h"
|
|
|
|
#include "nsIFormControl.h"
|
|
|
|
#include "nsIForm.h"
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDOMHTMLCollection.h"
|
|
|
|
#include "nsISelectControlFrame.h"
|
|
|
|
|
|
|
|
// Notify/query select frame for selected state
|
|
|
|
#include "nsIFormControlFrame.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMHTMLSelectElement.h"
|
|
|
|
#include "nsNodeInfoManager.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2014-04-02 21:18:36 -07:00
|
|
|
#include "mozilla/EventStates.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsContentCreatorFunctions.h"
|
2008-04-11 10:29:06 -07:00
|
|
|
#include "mozAutoDocUpdate.h"
|
2013-04-04 05:01:08 -07:00
|
|
|
#include "nsTextNode.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 06:45:11 -07:00
|
|
|
/**
|
|
|
|
* Implementation of <option>
|
|
|
|
*/
|
|
|
|
|
2013-06-19 07:24:37 -07:00
|
|
|
NS_IMPL_NS_NEW_HTML_ELEMENT(Option)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-02-18 04:24:58 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-06-19 19:01:40 -07:00
|
|
|
HTMLOptionElement::HTMLOptionElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
|
2007-03-22 10:30:00 -07:00
|
|
|
: nsGenericHTMLElement(aNodeInfo),
|
2011-10-17 07:59:28 -07:00
|
|
|
mSelectedChanged(false),
|
|
|
|
mIsSelected(false),
|
|
|
|
mIsInSetDefaultSelected(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-05-31 18:46:57 -07:00
|
|
|
// We start off enabled
|
|
|
|
AddStatesSilently(NS_EVENT_STATE_ENABLED);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-02-18 04:24:58 -08:00
|
|
|
HTMLOptionElement::~HTMLOptionElement()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(HTMLOptionElement, nsGenericHTMLElement,
|
|
|
|
nsIDOMHTMLOptionElement)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-02-18 04:24:58 -08:00
|
|
|
NS_IMPL_ELEMENT_CLONE(HTMLOptionElement)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-02-18 04:24:58 -08:00
|
|
|
HTMLOptionElement::GetForm(nsIDOMHTMLFormElement** aForm)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-02-19 04:20:40 -08:00
|
|
|
NS_IF_ADDREF(*aForm = GetForm());
|
2013-02-18 04:26:57 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-19 07:24:37 -07:00
|
|
|
mozilla::dom::HTMLFormElement*
|
2013-02-18 04:26:57 -08:00
|
|
|
HTMLOptionElement::GetForm()
|
|
|
|
{
|
2013-04-04 00:03:33 -07:00
|
|
|
HTMLSelectElement* selectControl = GetSelect();
|
2013-02-18 04:26:57 -08:00
|
|
|
return selectControl ? selectControl->GetForm() : nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-13 23:39:48 -07:00
|
|
|
void
|
2013-02-18 04:24:58 -08:00
|
|
|
HTMLOptionElement::SetSelectedInternal(bool aValue, bool aNotify)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mSelectedChanged = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
mIsSelected = aValue;
|
|
|
|
|
2011-05-31 18:46:57 -07:00
|
|
|
// When mIsInSetDefaultSelected is true, the state change will be handled by
|
2007-12-04 08:50:32 -08:00
|
|
|
// SetAttr/UnsetAttr.
|
2011-05-31 18:46:57 -07:00
|
|
|
if (!mIsInSetDefaultSelected) {
|
|
|
|
UpdateState(aNotify);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 04:24:58 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
HTMLOptionElement::GetSelected(bool* aValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aValue);
|
2011-06-28 03:45:51 -07:00
|
|
|
*aValue = Selected();
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-02-18 04:24:58 -08:00
|
|
|
HTMLOptionElement::SetSelected(bool aValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Note: The select content obj maintains all the PresState
|
|
|
|
// so defer to it to get the answer
|
2013-04-04 00:03:33 -07:00
|
|
|
HTMLSelectElement* selectInt = GetSelect();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (selectInt) {
|
2013-08-13 23:57:04 -07:00
|
|
|
int32_t index = Index();
|
2013-09-28 12:04:20 -07:00
|
|
|
uint32_t mask = HTMLSelectElement::SET_DISABLED | HTMLSelectElement::NOTIFY;
|
|
|
|
if (aValue) {
|
|
|
|
mask |= HTMLSelectElement::IS_SELECTED;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// This should end up calling SetSelectedInternal
|
2013-09-28 12:04:20 -07:00
|
|
|
selectInt->SetOptionsSelectedByIndex(index, index, mask);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2011-10-17 07:59:28 -07:00
|
|
|
SetSelectedInternal(aValue, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-18 04:24:58 -08:00
|
|
|
NS_IMPL_BOOL_ATTR(HTMLOptionElement, DefaultSelected, selected)
|
2011-08-10 23:07:26 -07:00
|
|
|
// GetText returns a whitespace compressed .textContent value.
|
2013-02-18 04:24:58 -08:00
|
|
|
NS_IMPL_STRING_ATTR_WITH_FALLBACK(HTMLOptionElement, Label, label, GetText)
|
|
|
|
NS_IMPL_STRING_ATTR_WITH_FALLBACK(HTMLOptionElement, Value, value, GetText)
|
|
|
|
NS_IMPL_BOOL_ATTR(HTMLOptionElement, Disabled, disabled)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-24 12:30:54 -08:00
|
|
|
NS_IMETHODIMP
|
2013-02-18 04:24:58 -08:00
|
|
|
HTMLOptionElement::GetIndex(int32_t* aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-13 23:57:04 -07:00
|
|
|
*aIndex = Index();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
HTMLOptionElement::Index()
|
|
|
|
{
|
|
|
|
static int32_t defaultIndex = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-24 12:30:54 -08:00
|
|
|
// Only select elements can contain a list of options.
|
2013-04-04 00:03:33 -07:00
|
|
|
HTMLSelectElement* selectElement = GetSelect();
|
2012-02-24 12:30:54 -08:00
|
|
|
if (!selectElement) {
|
2013-08-13 23:57:04 -07:00
|
|
|
return defaultIndex;
|
2012-02-24 12:30:54 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-03-17 00:55:17 -07:00
|
|
|
HTMLOptionsCollection* options = selectElement->GetOptions();
|
2012-02-24 12:30:54 -08:00
|
|
|
if (!options) {
|
2013-08-13 23:57:04 -07:00
|
|
|
return defaultIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-13 23:57:04 -07:00
|
|
|
int32_t index = defaultIndex;
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
options->GetOptionIndex(this, 0, true, &index)));
|
|
|
|
return index;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-06-28 03:45:51 -07:00
|
|
|
bool
|
2013-02-18 04:24:58 -08:00
|
|
|
HTMLOptionElement::Selected() const
|
2011-06-28 03:45:51 -07:00
|
|
|
{
|
|
|
|
// If we haven't been explictly selected or deselected, use our default value
|
|
|
|
if (!mSelectedChanged) {
|
|
|
|
return DefaultSelected();
|
|
|
|
}
|
|
|
|
|
|
|
|
return mIsSelected;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-02-18 04:24:58 -08:00
|
|
|
HTMLOptionElement::DefaultSelected() const
|
2011-06-28 03:45:51 -07:00
|
|
|
{
|
|
|
|
return HasAttr(kNameSpaceID_None, nsGkAtoms::selected);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsChangeHint
|
2013-02-18 04:24:58 -08:00
|
|
|
HTMLOptionElement::GetAttributeChangeHint(const nsIAtom* aAttribute,
|
|
|
|
int32_t aModType) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsChangeHint retval =
|
|
|
|
nsGenericHTMLElement::GetAttributeChangeHint(aAttribute, aModType);
|
|
|
|
|
|
|
|
if (aAttribute == nsGkAtoms::label ||
|
|
|
|
aAttribute == nsGkAtoms::text) {
|
|
|
|
NS_UpdateHint(retval, NS_STYLE_HINT_REFLOW);
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2007-12-04 08:50:32 -08:00
|
|
|
nsresult
|
2013-02-18 04:24:58 -08:00
|
|
|
HTMLOptionElement::BeforeSetAttr(int32_t aNamespaceID, nsIAtom* aName,
|
2015-07-31 22:14:06 -07:00
|
|
|
nsAttrValueOrString* aValue,
|
2013-02-18 04:24:58 -08:00
|
|
|
bool aNotify)
|
2007-12-04 08:50:32 -08:00
|
|
|
{
|
|
|
|
nsresult rv = nsGenericHTMLElement::BeforeSetAttr(aNamespaceID, aName,
|
|
|
|
aValue, aNotify);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (aNamespaceID != kNameSpaceID_None || aName != nsGkAtoms::selected ||
|
|
|
|
mSelectedChanged) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-02-18 04:24:58 -08:00
|
|
|
|
2013-12-04 05:02:00 -08:00
|
|
|
bool defaultSelected = aValue;
|
|
|
|
// First make sure we actually set our mIsSelected state to reflect our new
|
|
|
|
// defaultSelected state. If that turns out to be wrong,
|
|
|
|
// SetOptionsSelectedByIndex will fix it up. But otherwise we can end up in a
|
|
|
|
// situation where mIsSelected is still false, but mSelectedChanged becomes
|
|
|
|
// true (later in this method, when we compare mIsSelected to
|
|
|
|
// defaultSelected), and then we start returning false for Selected() even
|
|
|
|
// though we're actually selected.
|
|
|
|
mIsSelected = defaultSelected;
|
|
|
|
|
2007-12-04 08:50:32 -08:00
|
|
|
// We just changed out selected state (since we look at the "selected"
|
2011-04-12 05:32:00 -07:00
|
|
|
// attribute when mSelectedChanged is false). Let's tell our select about
|
2007-12-04 08:50:32 -08:00
|
|
|
// it.
|
2013-04-04 00:03:33 -07:00
|
|
|
HTMLSelectElement* selectInt = GetSelect();
|
2007-12-04 08:50:32 -08:00
|
|
|
if (!selectInt) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!mSelectedChanged, "Shouldn't be here");
|
2013-02-18 04:24:58 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool inSetDefaultSelected = mIsInSetDefaultSelected;
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsInSetDefaultSelected = true;
|
2013-02-18 04:24:58 -08:00
|
|
|
|
2013-08-13 23:57:04 -07:00
|
|
|
int32_t index = Index();
|
2013-09-28 12:04:20 -07:00
|
|
|
uint32_t mask = HTMLSelectElement::SET_DISABLED;
|
2013-11-06 10:02:58 -08:00
|
|
|
if (defaultSelected) {
|
2013-09-28 12:04:20 -07:00
|
|
|
mask |= HTMLSelectElement::IS_SELECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aNotify) {
|
|
|
|
mask |= HTMLSelectElement::NOTIFY;
|
|
|
|
}
|
|
|
|
|
2013-12-04 05:02:00 -08:00
|
|
|
// This can end up calling SetSelectedInternal if our selected state needs to
|
|
|
|
// change, which we will allow to take effect so that parts of
|
|
|
|
// SetOptionsSelectedByIndex that might depend on it working don't get
|
|
|
|
// confused.
|
2013-09-28 12:04:20 -07:00
|
|
|
selectInt->SetOptionsSelectedByIndex(index, index, mask);
|
2007-12-04 08:50:32 -08:00
|
|
|
|
|
|
|
// Now reset our members; when we finish the attr set we'll end up with the
|
|
|
|
// rigt selected state.
|
|
|
|
mIsInSetDefaultSelected = inSetDefaultSelected;
|
2013-12-04 05:02:00 -08:00
|
|
|
// mIsSelected might have been changed by SetOptionsSelectedByIndex. Possibly
|
|
|
|
// more than once; make sure our mSelectedChanged state is set correctly.
|
2013-11-06 10:02:58 -08:00
|
|
|
mSelectedChanged = mIsSelected != defaultSelected;
|
2007-12-04 08:50:32 -08:00
|
|
|
|
2013-07-24 00:37:14 -07:00
|
|
|
return NS_OK;
|
2007-12-04 08:50:32 -08:00
|
|
|
}
|
|
|
|
|
2014-05-06 10:19:00 -07:00
|
|
|
nsresult
|
|
|
|
HTMLOptionElement::AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
|
|
|
const nsAttrValue* aValue, bool aNotify)
|
|
|
|
{
|
|
|
|
if (aNameSpaceID == kNameSpaceID_None &&
|
|
|
|
aName == nsGkAtoms::value && Selected()) {
|
|
|
|
// Since this option is selected, changing value
|
|
|
|
// may have changed missing validity state of the
|
|
|
|
// Select element
|
|
|
|
HTMLSelectElement* select = GetSelect();
|
|
|
|
if (select) {
|
|
|
|
select->UpdateValueMissingValidityState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsGenericHTMLElement::AfterSetAttr(aNameSpaceID, aName,
|
|
|
|
aValue, aNotify);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2013-02-18 04:24:58 -08:00
|
|
|
HTMLOptionElement::GetText(nsAString& aText)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsAutoString text;
|
2012-10-21 00:38:40 -07:00
|
|
|
|
|
|
|
nsIContent* child = nsINode::GetFirstChild();
|
|
|
|
while (child) {
|
|
|
|
if (child->NodeType() == nsIDOMNode::TEXT_NODE ||
|
|
|
|
child->NodeType() == nsIDOMNode::CDATA_SECTION_NODE) {
|
|
|
|
child->AppendTextTo(text);
|
|
|
|
}
|
2015-03-03 03:08:59 -08:00
|
|
|
if (child->IsHTMLElement(nsGkAtoms::script) ||
|
|
|
|
child->IsSVGElement(nsGkAtoms::script)) {
|
2012-10-21 00:38:40 -07:00
|
|
|
child = child->GetNextNonChildNode(this);
|
|
|
|
} else {
|
|
|
|
child = child->GetNextNode(this);
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// XXX No CompressWhitespace for nsAString. Sad.
|
2011-10-17 07:59:28 -07:00
|
|
|
text.CompressWhitespace(true, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
aText = text;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-02-18 04:24:58 -08:00
|
|
|
HTMLOptionElement::SetText(const nsAString& aText)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return nsContentUtils::SetNodeTextContent(this, aText, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-01 02:46:43 -07:00
|
|
|
nsresult
|
2013-02-18 04:24:58 -08:00
|
|
|
HTMLOptionElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
|
|
|
|
nsIContent* aBindingParent,
|
|
|
|
bool aCompileEventHandlers)
|
2012-06-01 02:46:43 -07:00
|
|
|
{
|
|
|
|
nsresult rv = nsGenericHTMLElement::BindToTree(aDocument, aParent,
|
|
|
|
aBindingParent,
|
|
|
|
aCompileEventHandlers);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Our new parent might change :disabled/:enabled state.
|
|
|
|
UpdateState(false);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-02-18 04:24:58 -08:00
|
|
|
HTMLOptionElement::UnbindFromTree(bool aDeep, bool aNullParent)
|
2012-06-01 02:46:43 -07:00
|
|
|
{
|
|
|
|
nsGenericHTMLElement::UnbindFromTree(aDeep, aNullParent);
|
|
|
|
|
|
|
|
// Our previous parent could have been involved in :disabled/:enabled state.
|
|
|
|
UpdateState(false);
|
|
|
|
}
|
|
|
|
|
2014-04-02 21:18:36 -07:00
|
|
|
EventStates
|
2013-02-18 04:24:58 -08:00
|
|
|
HTMLOptionElement::IntrinsicState() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-02 21:18:36 -07:00
|
|
|
EventStates state = nsGenericHTMLElement::IntrinsicState();
|
2011-06-28 03:45:51 -07:00
|
|
|
if (Selected()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
state |= NS_EVENT_STATE_CHECKED;
|
|
|
|
}
|
2011-06-28 03:45:51 -07:00
|
|
|
if (DefaultSelected()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
state |= NS_EVENT_STATE_DEFAULT;
|
|
|
|
}
|
|
|
|
|
2012-06-01 02:46:43 -07:00
|
|
|
// An <option> is disabled if it has @disabled set or if it's <optgroup> has
|
|
|
|
// @disabled set.
|
2010-09-18 14:33:16 -07:00
|
|
|
if (HasAttr(kNameSpaceID_None, nsGkAtoms::disabled)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
state |= NS_EVENT_STATE_DISABLED;
|
|
|
|
state &= ~NS_EVENT_STATE_ENABLED;
|
|
|
|
} else {
|
2012-06-01 02:46:43 -07:00
|
|
|
nsIContent* parent = GetParent();
|
2015-03-03 03:08:59 -08:00
|
|
|
if (parent && parent->IsHTMLElement(nsGkAtoms::optgroup) &&
|
2012-06-01 02:46:43 -07:00
|
|
|
parent->HasAttr(kNameSpaceID_None, nsGkAtoms::disabled)) {
|
|
|
|
state |= NS_EVENT_STATE_DISABLED;
|
|
|
|
state &= ~NS_EVENT_STATE_ENABLED;
|
|
|
|
} else {
|
|
|
|
state &= ~NS_EVENT_STATE_DISABLED;
|
|
|
|
state |= NS_EVENT_STATE_ENABLED;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the select content element that contains this option
|
2013-04-04 00:03:33 -07:00
|
|
|
HTMLSelectElement*
|
2013-02-18 04:24:58 -08:00
|
|
|
HTMLOptionElement::GetSelect()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsIContent* parent = this;
|
|
|
|
while ((parent = parent->GetParent()) &&
|
2015-03-03 03:08:59 -08:00
|
|
|
parent->IsHTMLElement()) {
|
2013-04-04 00:03:33 -07:00
|
|
|
HTMLSelectElement* select = HTMLSelectElement::FromContent(parent);
|
2012-09-26 07:17:51 -07:00
|
|
|
if (select) {
|
|
|
|
return select;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2015-03-03 03:09:00 -08:00
|
|
|
if (!parent->IsHTMLElement(nsGkAtoms::optgroup)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-02-18 04:24:58 -08:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-01-22 02:53:13 -08:00
|
|
|
already_AddRefed<HTMLOptionElement>
|
|
|
|
HTMLOptionElement::Option(const GlobalObject& aGlobal,
|
|
|
|
const Optional<nsAString>& aText,
|
|
|
|
const Optional<nsAString>& aValue,
|
|
|
|
const Optional<bool>& aDefaultSelected,
|
|
|
|
const Optional<bool>& aSelected, ErrorResult& aError)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-22 22:17:08 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-01-22 02:53:13 -08:00
|
|
|
nsIDocument* doc;
|
|
|
|
if (!win || !(doc = win->GetExtantDoc())) {
|
|
|
|
aError.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-19 19:01:40 -07:00
|
|
|
already_AddRefed<mozilla::dom::NodeInfo> nodeInfo =
|
2013-01-22 02:53:13 -08:00
|
|
|
doc->NodeInfoManager()->GetNodeInfo(nsGkAtoms::option, nullptr,
|
|
|
|
kNameSpaceID_XHTML,
|
|
|
|
nsIDOMNode::ELEMENT_NODE);
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<HTMLOptionElement> option = new HTMLOptionElement(nodeInfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-01-22 02:53:13 -08:00
|
|
|
if (aText.WasPassed()) {
|
2007-11-27 16:52:37 -08:00
|
|
|
// Create a new text node and append it to the option
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<nsTextNode> textContent =
|
2013-04-04 05:01:08 -07:00
|
|
|
new nsTextNode(option->NodeInfo()->NodeInfoManager());
|
2010-12-03 00:24:17 -08:00
|
|
|
|
2013-01-22 02:53:13 -08:00
|
|
|
textContent->SetText(aText.Value(), false);
|
2013-02-18 04:24:58 -08:00
|
|
|
|
2013-01-22 02:53:13 -08:00
|
|
|
aError = option->AppendChildTo(textContent, false);
|
|
|
|
if (aError.Failed()) {
|
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-01-22 02:53:13 -08:00
|
|
|
if (aValue.WasPassed()) {
|
|
|
|
// Set the value attribute for this element. We're calling SetAttr
|
|
|
|
// directly because we want to pass aNotify == false.
|
|
|
|
aError = option->SetAttr(kNameSpaceID_None, nsGkAtoms::value,
|
|
|
|
aValue.Value(), false);
|
|
|
|
if (aError.Failed()) {
|
|
|
|
return nullptr;
|
2010-12-03 00:24:17 -08:00
|
|
|
}
|
|
|
|
|
2013-01-22 02:53:13 -08:00
|
|
|
if (aDefaultSelected.WasPassed()) {
|
|
|
|
if (aDefaultSelected.Value()) {
|
|
|
|
// We're calling SetAttr directly because we want to pass
|
|
|
|
// aNotify == false.
|
|
|
|
aError = option->SetAttr(kNameSpaceID_None, nsGkAtoms::selected,
|
|
|
|
EmptyString(), false);
|
|
|
|
if (aError.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-01-22 02:53:13 -08:00
|
|
|
if (aSelected.WasPassed()) {
|
|
|
|
option->SetSelected(aSelected.Value(), aError);
|
|
|
|
if (aError.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 02:53:13 -08:00
|
|
|
return option.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-12-10 20:02:13 -08:00
|
|
|
|
|
|
|
nsresult
|
2013-02-18 04:24:58 -08:00
|
|
|
HTMLOptionElement::CopyInnerTo(Element* aDest)
|
2009-12-10 20:02:13 -08:00
|
|
|
{
|
|
|
|
nsresult rv = nsGenericHTMLElement::CopyInnerTo(aDest);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-18 03:53:36 -07:00
|
|
|
if (aDest->OwnerDoc()->IsStaticDocument()) {
|
2013-02-18 04:24:58 -08:00
|
|
|
static_cast<HTMLOptionElement*>(aDest)->SetSelected(Selected());
|
2009-12-10 20:02:13 -08:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-18 04:26:57 -08: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 07:13:33 -07:00
|
|
|
HTMLOptionElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-02-18 04:26:57 -08: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 07:13:33 -07:00
|
|
|
return HTMLOptionElementBinding::Wrap(aCx, this, aGivenProto);
|
2013-02-18 04:26:57 -08:00
|
|
|
}
|
|
|
|
|
2013-02-18 04:24:58 -08:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|