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.org code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation
18 : * Portions created by the Initial Developer are Copyright (C) 2010
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Mounir Lamouri <mounir.lamouri@mozilla.com> (original author)
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 "nsIDOMHTMLOutputElement.h"
39 : #include "nsGenericHTMLElement.h"
40 : #include "nsFormSubmission.h"
41 : #include "nsDOMSettableTokenList.h"
42 : #include "nsStubMutationObserver.h"
43 : #include "nsIConstraintValidation.h"
44 : #include "nsEventStates.h"
45 : #include "mozAutoDocUpdate.h"
46 : #include "nsHTMLFormElement.h"
47 :
48 :
49 : class nsHTMLOutputElement : public nsGenericHTMLFormElement,
50 : public nsIDOMHTMLOutputElement,
51 : public nsStubMutationObserver,
52 : public nsIConstraintValidation
53 : {
54 : public:
55 : using nsIConstraintValidation::GetValidationMessage;
56 :
57 : nsHTMLOutputElement(already_AddRefed<nsINodeInfo> aNodeInfo);
58 : virtual ~nsHTMLOutputElement();
59 :
60 : // nsISupports
61 : NS_DECL_ISUPPORTS_INHERITED
62 :
63 : // nsIDOMNode
64 0 : NS_FORWARD_NSIDOMNODE(nsGenericHTMLFormElement::)
65 :
66 : // nsIDOMElement
67 0 : NS_FORWARD_NSIDOMELEMENT(nsGenericHTMLFormElement::)
68 :
69 : // nsIDOMHTMLElement
70 0 : NS_FORWARD_NSIDOMHTMLELEMENT(nsGenericHTMLFormElement::)
71 :
72 : // nsIDOMHTMLOutputElement
73 : NS_DECL_NSIDOMHTMLOUTPUTELEMENT
74 :
75 : // nsIFormControl
76 0 : NS_IMETHOD_(PRUint32) GetType() const { return NS_FORM_OUTPUT; }
77 : NS_IMETHOD Reset();
78 : NS_IMETHOD SubmitNamesValues(nsFormSubmission* aFormSubmission);
79 :
80 0 : virtual bool IsDisabled() const { return false; }
81 :
82 : nsresult Clone(nsINodeInfo* aNodeInfo, nsINode** aResult) const;
83 :
84 : bool ParseAttribute(PRInt32 aNamespaceID, nsIAtom* aAttribute,
85 : const nsAString& aValue, nsAttrValue& aResult);
86 :
87 : nsEventStates IntrinsicState() const;
88 :
89 : virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
90 : nsIContent* aBindingParent,
91 : bool aCompileEventHandlers);
92 :
93 : // This function is called when a callback function from nsIMutationObserver
94 : // has to be used to update the defaultValue attribute.
95 : void DescendantsChanged();
96 :
97 : // nsIMutationObserver
98 : NS_DECL_NSIMUTATIONOBSERVER_CHARACTERDATACHANGED
99 : NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
100 : NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
101 : NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
102 :
103 1464 : NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsHTMLOutputElement,
104 : nsGenericHTMLFormElement)
105 :
106 : virtual nsXPCClassInfo* GetClassInfo();
107 : protected:
108 : enum ValueModeFlag {
109 : eModeDefault,
110 : eModeValue
111 : };
112 :
113 : ValueModeFlag mValueModeFlag;
114 : nsString mDefaultValue;
115 : nsRefPtr<nsDOMSettableTokenList> mTokenList;
116 : };
117 :
118 :
119 0 : NS_IMPL_NS_NEW_HTML_ELEMENT(Output)
120 :
121 :
122 0 : nsHTMLOutputElement::nsHTMLOutputElement(already_AddRefed<nsINodeInfo> aNodeInfo)
123 : : nsGenericHTMLFormElement(aNodeInfo)
124 0 : , mValueModeFlag(eModeDefault)
125 : {
126 0 : AddMutationObserver(this);
127 :
128 : // We start out valid and ui-valid (since we have no form).
129 0 : AddStatesSilently(NS_EVENT_STATE_VALID | NS_EVENT_STATE_MOZ_UI_VALID);
130 0 : }
131 :
132 0 : nsHTMLOutputElement::~nsHTMLOutputElement()
133 : {
134 0 : if (mTokenList) {
135 0 : mTokenList->DropReference();
136 : }
137 0 : }
138 :
139 1464 : NS_IMPL_CYCLE_COLLECTION_CLASS(nsHTMLOutputElement)
140 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsHTMLOutputElement,
141 : nsGenericHTMLFormElement)
142 0 : if (tmp->mTokenList) {
143 0 : tmp->mTokenList->DropReference();
144 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mTokenList)
145 : }
146 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
147 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsHTMLOutputElement,
148 : nsGenericHTMLFormElement)
149 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mTokenList,
150 : nsDOMTokenList)
151 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
152 :
153 0 : NS_IMPL_ADDREF_INHERITED(nsHTMLOutputElement, nsGenericElement)
154 0 : NS_IMPL_RELEASE_INHERITED(nsHTMLOutputElement, nsGenericElement)
155 :
156 0 : DOMCI_NODE_DATA(HTMLOutputElement, nsHTMLOutputElement)
157 :
158 0 : NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(nsHTMLOutputElement)
159 0 : NS_HTML_CONTENT_INTERFACE_TABLE3(nsHTMLOutputElement,
160 : nsIDOMHTMLOutputElement,
161 : nsIMutationObserver,
162 : nsIConstraintValidation)
163 0 : NS_HTML_CONTENT_INTERFACE_TABLE_TO_MAP_SEGUE(nsHTMLOutputElement,
164 : nsGenericHTMLFormElement)
165 0 : NS_HTML_CONTENT_INTERFACE_TABLE_TAIL_CLASSINFO(HTMLOutputElement)
166 :
167 0 : NS_IMPL_ELEMENT_CLONE(nsHTMLOutputElement)
168 :
169 :
170 0 : NS_IMPL_STRING_ATTR(nsHTMLOutputElement, Name, name)
171 :
172 : // nsIConstraintValidation
173 0 : NS_IMPL_NSICONSTRAINTVALIDATION_EXCEPT_SETCUSTOMVALIDITY(nsHTMLOutputElement)
174 :
175 : NS_IMETHODIMP
176 0 : nsHTMLOutputElement::SetCustomValidity(const nsAString& aError)
177 : {
178 0 : nsIConstraintValidation::SetCustomValidity(aError);
179 :
180 0 : UpdateState(true);
181 :
182 0 : return NS_OK;
183 : }
184 :
185 : NS_IMETHODIMP
186 0 : nsHTMLOutputElement::Reset()
187 : {
188 0 : mValueModeFlag = eModeDefault;
189 0 : return nsContentUtils::SetNodeTextContent(this, mDefaultValue, true);
190 : }
191 :
192 : NS_IMETHODIMP
193 0 : nsHTMLOutputElement::SubmitNamesValues(nsFormSubmission* aFormSubmission)
194 : {
195 : // The output element is not submittable.
196 0 : return NS_OK;
197 : }
198 :
199 : bool
200 0 : nsHTMLOutputElement::ParseAttribute(PRInt32 aNamespaceID, nsIAtom* aAttribute,
201 : const nsAString& aValue, nsAttrValue& aResult)
202 : {
203 0 : if (aNamespaceID == kNameSpaceID_None) {
204 0 : if (aAttribute == nsGkAtoms::_for) {
205 0 : aResult.ParseAtomArray(aValue);
206 0 : return true;
207 : }
208 : }
209 :
210 : return nsGenericHTMLFormElement::ParseAttribute(aNamespaceID, aAttribute,
211 0 : aValue, aResult);
212 : }
213 :
214 : nsEventStates
215 0 : nsHTMLOutputElement::IntrinsicState() const
216 : {
217 0 : nsEventStates states = nsGenericHTMLFormElement::IntrinsicState();
218 :
219 : // We don't have to call IsCandidateForConstraintValidation()
220 : // because <output> can't be barred from constraint validation.
221 0 : if (IsValid()) {
222 0 : states |= NS_EVENT_STATE_VALID;
223 0 : if (!mForm || !mForm->HasAttr(kNameSpaceID_None, nsGkAtoms::novalidate)) {
224 0 : states |= NS_EVENT_STATE_MOZ_UI_VALID;
225 : }
226 : } else {
227 0 : states |= NS_EVENT_STATE_INVALID;
228 0 : if (!mForm || !mForm->HasAttr(kNameSpaceID_None, nsGkAtoms::novalidate)) {
229 0 : states |= NS_EVENT_STATE_MOZ_UI_INVALID;
230 : }
231 : }
232 :
233 : return states;
234 : }
235 :
236 : nsresult
237 0 : nsHTMLOutputElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
238 : nsIContent* aBindingParent,
239 : bool aCompileEventHandlers)
240 : {
241 : nsresult rv = nsGenericHTMLFormElement::BindToTree(aDocument, aParent,
242 : aBindingParent,
243 0 : aCompileEventHandlers);
244 0 : NS_ENSURE_SUCCESS(rv, rv);
245 :
246 : // Unfortunately, we can actually end up having to change our state
247 : // as a result of being bound to a tree even from the parser: we
248 : // might end up a in a novalidate form, and unlike other form
249 : // controls that on its own is enough to make change ui-valid state.
250 : // So just go ahead and update our state now.
251 0 : UpdateState(false);
252 :
253 0 : return rv;
254 : }
255 :
256 : NS_IMETHODIMP
257 0 : nsHTMLOutputElement::GetForm(nsIDOMHTMLFormElement** aForm)
258 : {
259 0 : return nsGenericHTMLFormElement::GetForm(aForm);
260 : }
261 :
262 : NS_IMETHODIMP
263 0 : nsHTMLOutputElement::GetType(nsAString& aType)
264 : {
265 0 : aType.AssignLiteral("output");
266 0 : return NS_OK;
267 : }
268 :
269 : NS_IMETHODIMP
270 0 : nsHTMLOutputElement::GetValue(nsAString& aValue)
271 : {
272 0 : nsContentUtils::GetNodeTextContent(this, true, aValue);
273 0 : return NS_OK;
274 : }
275 :
276 : NS_IMETHODIMP
277 0 : nsHTMLOutputElement::SetValue(const nsAString& aValue)
278 : {
279 0 : mValueModeFlag = eModeValue;
280 0 : return nsContentUtils::SetNodeTextContent(this, aValue, true);
281 : }
282 :
283 : NS_IMETHODIMP
284 0 : nsHTMLOutputElement::GetDefaultValue(nsAString& aDefaultValue)
285 : {
286 0 : aDefaultValue = mDefaultValue;
287 0 : return NS_OK;
288 : }
289 :
290 : NS_IMETHODIMP
291 0 : nsHTMLOutputElement::SetDefaultValue(const nsAString& aDefaultValue)
292 : {
293 0 : mDefaultValue = aDefaultValue;
294 0 : if (mValueModeFlag == eModeDefault) {
295 0 : return nsContentUtils::SetNodeTextContent(this, mDefaultValue, true);
296 : }
297 :
298 0 : return NS_OK;
299 : }
300 :
301 : NS_IMETHODIMP
302 0 : nsHTMLOutputElement::GetHtmlFor(nsIDOMDOMSettableTokenList** aResult)
303 : {
304 0 : if (!mTokenList) {
305 0 : mTokenList = new nsDOMSettableTokenList(this, nsGkAtoms::_for);
306 : }
307 :
308 0 : NS_ADDREF(*aResult = mTokenList);
309 :
310 0 : return NS_OK;
311 : }
312 :
313 0 : void nsHTMLOutputElement::DescendantsChanged()
314 : {
315 0 : if (mValueModeFlag == eModeDefault) {
316 0 : nsContentUtils::GetNodeTextContent(this, true, mDefaultValue);
317 : }
318 0 : }
319 :
320 : // nsIMutationObserver
321 :
322 0 : void nsHTMLOutputElement::CharacterDataChanged(nsIDocument* aDocument,
323 : nsIContent* aContent,
324 : CharacterDataChangeInfo* aInfo)
325 : {
326 0 : DescendantsChanged();
327 0 : }
328 :
329 0 : void nsHTMLOutputElement::ContentAppended(nsIDocument* aDocument,
330 : nsIContent* aContainer,
331 : nsIContent* aFirstNewContent,
332 : PRInt32 aNewIndexInContainer)
333 : {
334 0 : DescendantsChanged();
335 0 : }
336 :
337 0 : void nsHTMLOutputElement::ContentInserted(nsIDocument* aDocument,
338 : nsIContent* aContainer,
339 : nsIContent* aChild,
340 : PRInt32 aIndexInContainer)
341 : {
342 0 : DescendantsChanged();
343 0 : }
344 :
345 0 : void nsHTMLOutputElement::ContentRemoved(nsIDocument* aDocument,
346 : nsIContent* aContainer,
347 : nsIContent* aChild,
348 : PRInt32 aIndexInContainer,
349 : nsIContent* aPreviousSibling)
350 : {
351 0 : DescendantsChanged();
352 4392 : }
353 :
|