1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/dom/HTMLOutputElement.h"
8 #include "mozAutoDocUpdate.h"
9 #include "mozilla/EventStates.h"
10 #include "mozilla/dom/HTMLFormElement.h"
11 #include "mozilla/dom/HTMLOutputElementBinding.h"
12 #include "nsContentUtils.h"
13 #include "nsDOMSettableTokenList.h"
14 #include "nsFormSubmission.h"
16 NS_IMPL_NS_NEW_HTML_ELEMENT_CHECK_PARSER(Output
)
21 HTMLOutputElement::HTMLOutputElement(already_AddRefed
<mozilla::dom::NodeInfo
>& aNodeInfo
,
22 FromParser aFromParser
)
23 : nsGenericHTMLFormElement(aNodeInfo
)
24 , mValueModeFlag(eModeDefault
)
25 , mIsDoneAddingChildren(!aFromParser
)
27 AddMutationObserver(this);
29 // We start out valid and ui-valid (since we have no form).
30 AddStatesSilently(NS_EVENT_STATE_VALID
| NS_EVENT_STATE_MOZ_UI_VALID
);
33 HTMLOutputElement::~HTMLOutputElement()
37 NS_IMPL_CYCLE_COLLECTION_CLASS(HTMLOutputElement
)
39 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(HTMLOutputElement
,
40 nsGenericHTMLFormElement
)
41 NS_IMPL_CYCLE_COLLECTION_UNLINK(mValidity
)
42 NS_IMPL_CYCLE_COLLECTION_UNLINK(mTokenList
)
43 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
44 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(HTMLOutputElement
,
45 nsGenericHTMLFormElement
)
46 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mValidity
)
47 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTokenList
)
48 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
50 NS_IMPL_ADDREF_INHERITED(HTMLOutputElement
, Element
)
51 NS_IMPL_RELEASE_INHERITED(HTMLOutputElement
, Element
)
53 NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(HTMLOutputElement
)
54 NS_INTERFACE_TABLE_INHERITED(HTMLOutputElement
,
56 nsIConstraintValidation
)
57 NS_INTERFACE_TABLE_TAIL_INHERITING(nsGenericHTMLFormElement
)
59 NS_IMPL_ELEMENT_CLONE(HTMLOutputElement
)
62 HTMLOutputElement::SetCustomValidity(const nsAString
& aError
)
64 nsIConstraintValidation::SetCustomValidity(aError
);
70 HTMLOutputElement::Reset()
72 mValueModeFlag
= eModeDefault
;
73 return nsContentUtils::SetNodeTextContent(this, mDefaultValue
, true);
77 HTMLOutputElement::SubmitNamesValues(nsFormSubmission
* aFormSubmission
)
79 // The output element is not submittable.
84 HTMLOutputElement::ParseAttribute(int32_t aNamespaceID
, nsIAtom
* aAttribute
,
85 const nsAString
& aValue
, nsAttrValue
& aResult
)
87 if (aNamespaceID
== kNameSpaceID_None
) {
88 if (aAttribute
== nsGkAtoms::_for
) {
89 aResult
.ParseAtomArray(aValue
);
94 return nsGenericHTMLFormElement::ParseAttribute(aNamespaceID
, aAttribute
,
99 HTMLOutputElement::DoneAddingChildren(bool aHaveNotified
)
101 mIsDoneAddingChildren
= true;
105 HTMLOutputElement::IntrinsicState() const
107 EventStates states
= nsGenericHTMLFormElement::IntrinsicState();
109 // We don't have to call IsCandidateForConstraintValidation()
110 // because <output> can't be barred from constraint validation.
112 states
|= NS_EVENT_STATE_VALID
;
113 if (!mForm
|| !mForm
->HasAttr(kNameSpaceID_None
, nsGkAtoms::novalidate
)) {
114 states
|= NS_EVENT_STATE_MOZ_UI_VALID
;
117 states
|= NS_EVENT_STATE_INVALID
;
118 if (!mForm
|| !mForm
->HasAttr(kNameSpaceID_None
, nsGkAtoms::novalidate
)) {
119 states
|= NS_EVENT_STATE_MOZ_UI_INVALID
;
127 HTMLOutputElement::BindToTree(nsIDocument
* aDocument
, nsIContent
* aParent
,
128 nsIContent
* aBindingParent
,
129 bool aCompileEventHandlers
)
131 nsresult rv
= nsGenericHTMLFormElement::BindToTree(aDocument
, aParent
,
133 aCompileEventHandlers
);
134 NS_ENSURE_SUCCESS(rv
, rv
);
136 // Unfortunately, we can actually end up having to change our state
137 // as a result of being bound to a tree even from the parser: we
138 // might end up a in a novalidate form, and unlike other form
139 // controls that on its own is enough to make change ui-valid state.
140 // So just go ahead and update our state now.
147 HTMLOutputElement::GetValue(nsAString
& aValue
)
149 if (!nsContentUtils::GetNodeTextContent(this, true, aValue
)) {
150 NS_RUNTIMEABORT("OOM");
155 HTMLOutputElement::SetValue(const nsAString
& aValue
, ErrorResult
& aRv
)
157 mValueModeFlag
= eModeValue
;
158 aRv
= nsContentUtils::SetNodeTextContent(this, aValue
, true);
162 HTMLOutputElement::SetDefaultValue(const nsAString
& aDefaultValue
, ErrorResult
& aRv
)
164 mDefaultValue
= aDefaultValue
;
165 if (mValueModeFlag
== eModeDefault
) {
166 aRv
= nsContentUtils::SetNodeTextContent(this, mDefaultValue
, true);
170 nsDOMSettableTokenList
*
171 HTMLOutputElement::HtmlFor()
174 mTokenList
= new nsDOMSettableTokenList(this, nsGkAtoms::_for
);
179 void HTMLOutputElement::DescendantsChanged()
181 if (mIsDoneAddingChildren
&& mValueModeFlag
== eModeDefault
) {
182 if (!nsContentUtils::GetNodeTextContent(this, true, mDefaultValue
)) {
183 NS_RUNTIMEABORT("OOM");
188 // nsIMutationObserver
190 void HTMLOutputElement::CharacterDataChanged(nsIDocument
* aDocument
,
191 nsIContent
* aContent
,
192 CharacterDataChangeInfo
* aInfo
)
194 DescendantsChanged();
197 void HTMLOutputElement::ContentAppended(nsIDocument
* aDocument
,
198 nsIContent
* aContainer
,
199 nsIContent
* aFirstNewContent
,
200 int32_t aNewIndexInContainer
)
202 DescendantsChanged();
205 void HTMLOutputElement::ContentInserted(nsIDocument
* aDocument
,
206 nsIContent
* aContainer
,
208 int32_t aIndexInContainer
)
210 DescendantsChanged();
213 void HTMLOutputElement::ContentRemoved(nsIDocument
* aDocument
,
214 nsIContent
* aContainer
,
216 int32_t aIndexInContainer
,
217 nsIContent
* aPreviousSibling
)
219 DescendantsChanged();
223 HTMLOutputElement::WrapNode(JSContext
* aCx
)
225 return HTMLOutputElementBinding::Wrap(aCx
, this);
229 } // namespace mozilla