1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsHTMLFieldSetElement.h"
39 : #include "nsIDOMHTMLFormElement.h"
40 : #include "nsIDOMEventTarget.h"
41 : #include "nsStyleConsts.h"
42 : #include "nsIForm.h"
43 : #include "nsIFormControl.h"
44 : #include "nsGUIEvent.h"
45 : #include "nsEventDispatcher.h"
46 :
47 :
48 0 : NS_IMPL_NS_NEW_HTML_ELEMENT(FieldSet)
49 :
50 :
51 0 : nsHTMLFieldSetElement::nsHTMLFieldSetElement(already_AddRefed<nsINodeInfo> aNodeInfo)
52 : : nsGenericHTMLFormElement(aNodeInfo)
53 : , mElements(nsnull)
54 0 : , mFirstLegend(nsnull)
55 : {
56 : // <fieldset> is always barred from constraint validation.
57 0 : SetBarredFromConstraintValidation(true);
58 :
59 : // We start out enabled
60 0 : AddStatesSilently(NS_EVENT_STATE_ENABLED);
61 0 : }
62 :
63 0 : nsHTMLFieldSetElement::~nsHTMLFieldSetElement()
64 : {
65 0 : PRUint32 length = mDependentElements.Length();
66 0 : for (PRUint32 i = 0; i < length; ++i) {
67 0 : mDependentElements[i]->ForgetFieldSet(this);
68 : }
69 0 : }
70 :
71 : // nsISupports
72 :
73 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsHTMLFieldSetElement,
74 : nsGenericHTMLFormElement)
75 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mElements)
76 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
77 :
78 1464 : NS_IMPL_CYCLE_COLLECTION_CLASS(nsHTMLFieldSetElement)
79 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsHTMLFieldSetElement,
80 : nsGenericHTMLFormElement)
81 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mElements, nsIDOMNodeList)
82 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
83 :
84 0 : NS_IMPL_ADDREF_INHERITED(nsHTMLFieldSetElement, nsGenericElement)
85 0 : NS_IMPL_RELEASE_INHERITED(nsHTMLFieldSetElement, nsGenericElement)
86 :
87 0 : DOMCI_NODE_DATA(HTMLFieldSetElement, nsHTMLFieldSetElement)
88 :
89 : // QueryInterface implementation for nsHTMLFieldSetElement
90 0 : NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(nsHTMLFieldSetElement)
91 0 : NS_HTML_CONTENT_INTERFACE_TABLE2(nsHTMLFieldSetElement,
92 : nsIDOMHTMLFieldSetElement,
93 : nsIConstraintValidation)
94 0 : NS_HTML_CONTENT_INTERFACE_TABLE_TO_MAP_SEGUE(nsHTMLFieldSetElement,
95 : nsGenericHTMLFormElement)
96 0 : NS_HTML_CONTENT_INTERFACE_TABLE_TAIL_CLASSINFO(HTMLFieldSetElement)
97 :
98 0 : NS_IMPL_ELEMENT_CLONE(nsHTMLFieldSetElement)
99 :
100 :
101 0 : NS_IMPL_BOOL_ATTR(nsHTMLFieldSetElement, Disabled, disabled)
102 0 : NS_IMPL_STRING_ATTR(nsHTMLFieldSetElement, Name, name)
103 :
104 : // nsIConstraintValidation
105 0 : NS_IMPL_NSICONSTRAINTVALIDATION(nsHTMLFieldSetElement)
106 :
107 : // nsIContent
108 : nsresult
109 0 : nsHTMLFieldSetElement::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
110 : {
111 : // Do not process any DOM events if the element is disabled.
112 0 : aVisitor.mCanHandle = false;
113 0 : if (IsElementDisabledForEvents(aVisitor.mEvent->message, NULL)) {
114 0 : return NS_OK;
115 : }
116 :
117 0 : return nsGenericHTMLFormElement::PreHandleEvent(aVisitor);
118 : }
119 :
120 : nsresult
121 0 : nsHTMLFieldSetElement::AfterSetAttr(PRInt32 aNameSpaceID, nsIAtom* aName,
122 : const nsAttrValue* aValue, bool aNotify)
123 : {
124 0 : if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::disabled &&
125 0 : nsINode::GetFirstChild()) {
126 0 : if (!mElements) {
127 : mElements = new nsContentList(this, MatchListedElements, nsnull, nsnull,
128 0 : true);
129 : }
130 :
131 0 : PRUint32 length = mElements->Length(true);
132 0 : for (PRUint32 i=0; i<length; ++i) {
133 0 : static_cast<nsGenericHTMLFormElement*>(mElements->GetNodeAt(i))
134 0 : ->FieldSetDisabledChanged(aNotify);
135 : }
136 : }
137 :
138 : return nsGenericHTMLFormElement::AfterSetAttr(aNameSpaceID, aName,
139 0 : aValue, aNotify);
140 : }
141 :
142 : // nsIDOMHTMLFieldSetElement
143 :
144 : NS_IMETHODIMP
145 0 : nsHTMLFieldSetElement::GetForm(nsIDOMHTMLFormElement** aForm)
146 : {
147 0 : return nsGenericHTMLFormElement::GetForm(aForm);
148 : }
149 :
150 : NS_IMETHODIMP
151 0 : nsHTMLFieldSetElement::GetType(nsAString& aType)
152 : {
153 0 : aType.AssignLiteral("fieldset");
154 0 : return NS_OK;
155 : }
156 :
157 : /* static */
158 : bool
159 0 : nsHTMLFieldSetElement::MatchListedElements(nsIContent* aContent, PRInt32 aNamespaceID,
160 : nsIAtom* aAtom, void* aData)
161 : {
162 0 : nsCOMPtr<nsIFormControl> formControl = do_QueryInterface(aContent);
163 0 : return formControl && formControl->GetType() != NS_FORM_LABEL &&
164 0 : formControl->GetType() != NS_FORM_PROGRESS;
165 : }
166 :
167 : NS_IMETHODIMP
168 0 : nsHTMLFieldSetElement::GetElements(nsIDOMHTMLCollection** aElements)
169 : {
170 0 : if (!mElements) {
171 : mElements = new nsContentList(this, MatchListedElements, nsnull, nsnull,
172 0 : true);
173 : }
174 :
175 0 : NS_ADDREF(*aElements = mElements);
176 0 : return NS_OK;
177 : }
178 :
179 : // nsIFormControl
180 :
181 : nsresult
182 0 : nsHTMLFieldSetElement::Reset()
183 : {
184 0 : return NS_OK;
185 : }
186 :
187 : NS_IMETHODIMP
188 0 : nsHTMLFieldSetElement::SubmitNamesValues(nsFormSubmission* aFormSubmission)
189 : {
190 0 : return NS_OK;
191 : }
192 :
193 : nsresult
194 0 : nsHTMLFieldSetElement::InsertChildAt(nsIContent* aChild, PRUint32 aIndex,
195 : bool aNotify)
196 : {
197 0 : bool firstLegendHasChanged = false;
198 :
199 0 : if (aChild->IsHTML(nsGkAtoms::legend)) {
200 0 : if (!mFirstLegend) {
201 0 : mFirstLegend = aChild;
202 : // We do not want to notify the first time mFirstElement is set.
203 : } else {
204 : // If mFirstLegend is before aIndex, we do not change it.
205 : // Otherwise, mFirstLegend is now aChild.
206 0 : if (PRInt32(aIndex) <= IndexOf(mFirstLegend)) {
207 0 : mFirstLegend = aChild;
208 0 : firstLegendHasChanged = true;
209 : }
210 : }
211 : }
212 :
213 0 : nsresult rv = nsGenericHTMLFormElement::InsertChildAt(aChild, aIndex, aNotify);
214 0 : NS_ENSURE_SUCCESS(rv, rv);
215 :
216 0 : if (firstLegendHasChanged) {
217 0 : NotifyElementsForFirstLegendChange(aNotify);
218 : }
219 :
220 0 : return rv;
221 : }
222 :
223 : nsresult
224 0 : nsHTMLFieldSetElement::RemoveChildAt(PRUint32 aIndex, bool aNotify)
225 : {
226 0 : bool firstLegendHasChanged = false;
227 :
228 0 : if (mFirstLegend && (GetChildAt(aIndex) == mFirstLegend)) {
229 : // If we are removing the first legend we have to found another one.
230 0 : nsIContent* child = mFirstLegend->GetNextSibling();
231 0 : mFirstLegend = nsnull;
232 0 : firstLegendHasChanged = true;
233 :
234 0 : for (; child; child = child->GetNextSibling()) {
235 0 : if (child->IsHTML(nsGkAtoms::legend)) {
236 0 : mFirstLegend = child;
237 0 : break;
238 : }
239 : }
240 : }
241 :
242 0 : nsresult rv = nsGenericHTMLFormElement::RemoveChildAt(aIndex, aNotify);
243 0 : NS_ENSURE_SUCCESS(rv, rv);
244 :
245 0 : if (firstLegendHasChanged) {
246 0 : NotifyElementsForFirstLegendChange(aNotify);
247 : }
248 :
249 0 : return rv;
250 : }
251 :
252 : void
253 0 : nsHTMLFieldSetElement::NotifyElementsForFirstLegendChange(bool aNotify)
254 : {
255 : /**
256 : * NOTE: this could be optimized if only call when the fieldset is currently
257 : * disabled.
258 : * This should also make sure that mElements is set when we happen to be here.
259 : * However, this method shouldn't be called very often in normal use cases.
260 : */
261 0 : if (!mElements) {
262 : mElements = new nsContentList(this, MatchListedElements, nsnull, nsnull,
263 0 : true);
264 : }
265 :
266 0 : PRUint32 length = mElements->Length(true);
267 0 : for (PRUint32 i = 0; i < length; ++i) {
268 0 : static_cast<nsGenericHTMLFormElement*>(mElements->GetNodeAt(i))
269 0 : ->FieldSetFirstLegendChanged(aNotify);
270 : }
271 4392 : }
272 :
|