2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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-09-18 18:47:15 -07:00
|
|
|
|
|
|
|
#include "nsHTMLFieldSetElement.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMHTMLFormElement.h"
|
2007-05-14 02:11:38 -07:00
|
|
|
#include "nsIDOMEventTarget.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsStyleConsts.h"
|
|
|
|
#include "nsIForm.h"
|
|
|
|
#include "nsIFormControl.h"
|
2011-07-18 16:16:44 -07:00
|
|
|
#include "nsGUIEvent.h"
|
2010-09-18 14:24:58 -07:00
|
|
|
#include "nsEventDispatcher.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMPL_NS_NEW_HTML_ELEMENT(FieldSet)
|
|
|
|
|
|
|
|
|
2010-07-23 02:49:57 -07:00
|
|
|
nsHTMLFieldSetElement::nsHTMLFieldSetElement(already_AddRefed<nsINodeInfo> aNodeInfo)
|
2007-03-22 10:30:00 -07:00
|
|
|
: nsGenericHTMLFormElement(aNodeInfo)
|
2010-09-18 14:33:16 -07:00
|
|
|
, mElements(nsnull)
|
2010-09-18 18:47:15 -07:00
|
|
|
, mFirstLegend(nsnull)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-09-09 22:08:56 -07:00
|
|
|
// <fieldset> is always barred from constraint validation.
|
2011-10-17 07:59:28 -07:00
|
|
|
SetBarredFromConstraintValidation(true);
|
2011-05-31 18:46:57 -07:00
|
|
|
|
|
|
|
// We start out enabled
|
|
|
|
AddStatesSilently(NS_EVENT_STATE_ENABLED);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHTMLFieldSetElement::~nsHTMLFieldSetElement()
|
|
|
|
{
|
2010-11-05 10:15:05 -07:00
|
|
|
PRUint32 length = mDependentElements.Length();
|
2011-11-15 23:50:19 -08:00
|
|
|
for (PRUint32 i = 0; i < length; ++i) {
|
2010-11-05 10:15:05 -07:00
|
|
|
mDependentElements[i]->ForgetFieldSet(this);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsISupports
|
|
|
|
|
2010-11-23 16:09:48 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsHTMLFieldSetElement,
|
|
|
|
nsGenericHTMLFormElement)
|
2010-09-15 00:55:48 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mElements)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsHTMLFieldSetElement)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsHTMLFieldSetElement,
|
|
|
|
nsGenericHTMLFormElement)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mElements, nsIDOMNodeList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(nsHTMLFieldSetElement, nsGenericElement)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsHTMLFieldSetElement, nsGenericElement)
|
|
|
|
|
2010-07-23 02:49:57 -07:00
|
|
|
DOMCI_NODE_DATA(HTMLFieldSetElement, nsHTMLFieldSetElement)
|
2010-01-12 05:08:43 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// QueryInterface implementation for nsHTMLFieldSetElement
|
2010-11-23 16:09:48 -08:00
|
|
|
NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(nsHTMLFieldSetElement)
|
2010-08-21 11:51:38 -07:00
|
|
|
NS_HTML_CONTENT_INTERFACE_TABLE2(nsHTMLFieldSetElement,
|
|
|
|
nsIDOMHTMLFieldSetElement,
|
2010-08-21 11:52:49 -07:00
|
|
|
nsIConstraintValidation)
|
2008-11-03 02:31:47 -08:00
|
|
|
NS_HTML_CONTENT_INTERFACE_TABLE_TO_MAP_SEGUE(nsHTMLFieldSetElement,
|
|
|
|
nsGenericHTMLFormElement)
|
2007-08-20 15:55:06 -07:00
|
|
|
NS_HTML_CONTENT_INTERFACE_TABLE_TAIL_CLASSINFO(HTMLFieldSetElement)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-09-15 00:54:20 -07:00
|
|
|
NS_IMPL_ELEMENT_CLONE(nsHTMLFieldSetElement)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-09-15 00:54:20 -07:00
|
|
|
|
2010-09-18 14:24:58 -07:00
|
|
|
NS_IMPL_BOOL_ATTR(nsHTMLFieldSetElement, Disabled, disabled)
|
2010-09-15 00:54:20 -07:00
|
|
|
NS_IMPL_STRING_ATTR(nsHTMLFieldSetElement, Name, name)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-21 11:52:49 -07:00
|
|
|
// nsIConstraintValidation
|
|
|
|
NS_IMPL_NSICONSTRAINTVALIDATION(nsHTMLFieldSetElement)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-09-18 14:24:58 -07:00
|
|
|
// nsIContent
|
|
|
|
nsresult
|
|
|
|
nsHTMLFieldSetElement::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
|
|
|
|
{
|
|
|
|
// Do not process any DOM events if the element is disabled.
|
2011-10-17 07:59:28 -07:00
|
|
|
aVisitor.mCanHandle = false;
|
2011-07-18 16:16:44 -07:00
|
|
|
if (IsElementDisabledForEvents(aVisitor.mEvent->message, NULL)) {
|
2010-09-18 14:24:58 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsGenericHTMLFormElement::PreHandleEvent(aVisitor);
|
|
|
|
}
|
|
|
|
|
2010-09-18 14:33:16 -07:00
|
|
|
nsresult
|
|
|
|
nsHTMLFieldSetElement::AfterSetAttr(PRInt32 aNameSpaceID, nsIAtom* aName,
|
2012-02-13 18:00:56 -08:00
|
|
|
const nsAttrValue* aValue, bool aNotify)
|
2010-09-18 14:33:16 -07:00
|
|
|
{
|
|
|
|
if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::disabled &&
|
|
|
|
nsINode::GetFirstChild()) {
|
|
|
|
if (!mElements) {
|
|
|
|
mElements = new nsContentList(this, MatchListedElements, nsnull, nsnull,
|
2011-10-17 07:59:28 -07:00
|
|
|
true);
|
2010-09-18 14:33:16 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
PRUint32 length = mElements->Length(true);
|
2010-09-18 14:33:16 -07:00
|
|
|
for (PRUint32 i=0; i<length; ++i) {
|
|
|
|
static_cast<nsGenericHTMLFormElement*>(mElements->GetNodeAt(i))
|
2011-05-31 18:46:57 -07:00
|
|
|
->FieldSetDisabledChanged(aNotify);
|
2010-09-18 14:33:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsGenericHTMLFormElement::AfterSetAttr(aNameSpaceID, aName,
|
|
|
|
aValue, aNotify);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// nsIDOMHTMLFieldSetElement
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLFieldSetElement::GetForm(nsIDOMHTMLFormElement** aForm)
|
|
|
|
{
|
|
|
|
return nsGenericHTMLFormElement::GetForm(aForm);
|
|
|
|
}
|
|
|
|
|
2010-09-15 00:55:06 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLFieldSetElement::GetType(nsAString& aType)
|
|
|
|
{
|
|
|
|
aType.AssignLiteral("fieldset");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-15 00:55:48 -07:00
|
|
|
/* static */
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-09-15 00:55:48 -07:00
|
|
|
nsHTMLFieldSetElement::MatchListedElements(nsIContent* aContent, PRInt32 aNamespaceID,
|
|
|
|
nsIAtom* aAtom, void* aData)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFormControl> formControl = do_QueryInterface(aContent);
|
2012-06-10 16:37:47 -07:00
|
|
|
return formControl && formControl->GetType() != NS_FORM_LABEL &&
|
|
|
|
formControl->GetType() != NS_FORM_METER;
|
2010-09-15 00:55:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLFieldSetElement::GetElements(nsIDOMHTMLCollection** aElements)
|
|
|
|
{
|
|
|
|
if (!mElements) {
|
2010-09-18 14:33:16 -07:00
|
|
|
mElements = new nsContentList(this, MatchListedElements, nsnull, nsnull,
|
2011-10-17 07:59:28 -07:00
|
|
|
true);
|
2010-09-15 00:55:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(*aElements = mElements);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// nsIFormControl
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLFieldSetElement::Reset()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-08-19 14:58:20 -07:00
|
|
|
nsHTMLFieldSetElement::SubmitNamesValues(nsFormSubmission* aFormSubmission)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-09-18 18:47:15 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLFieldSetElement::InsertChildAt(nsIContent* aChild, PRUint32 aIndex,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aNotify)
|
2010-09-18 18:47:15 -07:00
|
|
|
{
|
|
|
|
bool firstLegendHasChanged = false;
|
|
|
|
|
|
|
|
if (aChild->IsHTML(nsGkAtoms::legend)) {
|
|
|
|
if (!mFirstLegend) {
|
|
|
|
mFirstLegend = aChild;
|
|
|
|
// We do not want to notify the first time mFirstElement is set.
|
|
|
|
} else {
|
|
|
|
// If mFirstLegend is before aIndex, we do not change it.
|
|
|
|
// Otherwise, mFirstLegend is now aChild.
|
2010-10-24 02:05:50 -07:00
|
|
|
if (PRInt32(aIndex) <= IndexOf(mFirstLegend)) {
|
2010-09-18 18:47:15 -07:00
|
|
|
mFirstLegend = aChild;
|
|
|
|
firstLegendHasChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = nsGenericHTMLFormElement::InsertChildAt(aChild, aIndex, aNotify);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (firstLegendHasChanged) {
|
2010-10-08 03:19:34 -07:00
|
|
|
NotifyElementsForFirstLegendChange(aNotify);
|
2010-09-18 18:47:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-03-29 14:09:07 -07:00
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
nsHTMLFieldSetElement::RemoveChildAt(PRUint32 aIndex, bool aNotify)
|
2010-09-18 18:47:15 -07:00
|
|
|
{
|
|
|
|
bool firstLegendHasChanged = false;
|
|
|
|
|
2010-10-21 04:11:01 -07:00
|
|
|
if (mFirstLegend && (GetChildAt(aIndex) == mFirstLegend)) {
|
2010-09-18 18:47:15 -07:00
|
|
|
// If we are removing the first legend we have to found another one.
|
2010-10-21 04:11:01 -07:00
|
|
|
nsIContent* child = mFirstLegend->GetNextSibling();
|
|
|
|
mFirstLegend = nsnull;
|
|
|
|
firstLegendHasChanged = true;
|
|
|
|
|
|
|
|
for (; child; child = child->GetNextSibling()) {
|
2010-09-18 18:47:15 -07:00
|
|
|
if (child->IsHTML(nsGkAtoms::legend)) {
|
|
|
|
mFirstLegend = child;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-29 14:09:07 -07:00
|
|
|
nsGenericHTMLFormElement::RemoveChildAt(aIndex, aNotify);
|
2010-09-18 18:47:15 -07:00
|
|
|
|
|
|
|
if (firstLegendHasChanged) {
|
2010-10-08 03:19:34 -07:00
|
|
|
NotifyElementsForFirstLegendChange(aNotify);
|
2010-09-18 18:47:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
nsHTMLFieldSetElement::NotifyElementsForFirstLegendChange(bool aNotify)
|
2010-09-18 18:47:15 -07:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* NOTE: this could be optimized if only call when the fieldset is currently
|
|
|
|
* disabled.
|
|
|
|
* This should also make sure that mElements is set when we happen to be here.
|
|
|
|
* However, this method shouldn't be called very often in normal use cases.
|
|
|
|
*/
|
|
|
|
if (!mElements) {
|
|
|
|
mElements = new nsContentList(this, MatchListedElements, nsnull, nsnull,
|
2011-10-17 07:59:28 -07:00
|
|
|
true);
|
2010-09-18 18:47:15 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
PRUint32 length = mElements->Length(true);
|
2011-11-15 23:50:19 -08:00
|
|
|
for (PRUint32 i = 0; i < length; ++i) {
|
2010-09-18 18:47:15 -07:00
|
|
|
static_cast<nsGenericHTMLFormElement*>(mElements->GetNodeAt(i))
|
2010-10-08 03:19:34 -07:00
|
|
|
->FieldSetFirstLegendChanged(aNotify);
|
2010-09-18 18:47:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|