/src/mozilla-central/dom/html/HTMLOutputElement.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "mozilla/dom/HTMLOutputElement.h" |
8 | | |
9 | | #include "mozAutoDocUpdate.h" |
10 | | #include "mozilla/EventStates.h" |
11 | | #include "mozilla/dom/HTMLFormElement.h" |
12 | | #include "mozilla/dom/HTMLFormSubmission.h" |
13 | | #include "mozilla/dom/HTMLOutputElementBinding.h" |
14 | | #include "nsContentUtils.h" |
15 | | #include "nsDOMTokenList.h" |
16 | | |
17 | | NS_IMPL_NS_NEW_HTML_ELEMENT_CHECK_PARSER(Output) |
18 | | |
19 | | namespace mozilla { |
20 | | namespace dom { |
21 | | |
22 | | HTMLOutputElement::HTMLOutputElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo, |
23 | | FromParser aFromParser) |
24 | | : nsGenericHTMLFormElement(std::move(aNodeInfo), NS_FORM_OUTPUT) |
25 | | , mValueModeFlag(eModeDefault) |
26 | | , mIsDoneAddingChildren(!aFromParser) |
27 | 0 | { |
28 | 0 | AddMutationObserver(this); |
29 | 0 |
|
30 | 0 | // We start out valid and ui-valid (since we have no form). |
31 | 0 | AddStatesSilently(NS_EVENT_STATE_VALID | NS_EVENT_STATE_MOZ_UI_VALID); |
32 | 0 | } |
33 | | |
34 | | HTMLOutputElement::~HTMLOutputElement() |
35 | 0 | { |
36 | 0 | } |
37 | | |
38 | | NS_IMPL_CYCLE_COLLECTION_INHERITED(HTMLOutputElement, |
39 | | nsGenericHTMLFormElement, |
40 | | mValidity, |
41 | | mTokenList) |
42 | | |
43 | | NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(HTMLOutputElement, |
44 | | nsGenericHTMLFormElement, |
45 | | nsIMutationObserver, |
46 | | nsIConstraintValidation) |
47 | | |
48 | | NS_IMPL_ELEMENT_CLONE(HTMLOutputElement) |
49 | | |
50 | | void |
51 | | HTMLOutputElement::SetCustomValidity(const nsAString& aError) |
52 | 0 | { |
53 | 0 | nsIConstraintValidation::SetCustomValidity(aError); |
54 | 0 |
|
55 | 0 | UpdateState(true); |
56 | 0 | } |
57 | | |
58 | | NS_IMETHODIMP |
59 | | HTMLOutputElement::Reset() |
60 | 0 | { |
61 | 0 | mValueModeFlag = eModeDefault; |
62 | 0 | return nsContentUtils::SetNodeTextContent(this, mDefaultValue, true); |
63 | 0 | } |
64 | | |
65 | | NS_IMETHODIMP |
66 | | HTMLOutputElement::SubmitNamesValues(HTMLFormSubmission* aFormSubmission) |
67 | 0 | { |
68 | 0 | // The output element is not submittable. |
69 | 0 | return NS_OK; |
70 | 0 | } |
71 | | |
72 | | bool |
73 | | HTMLOutputElement::ParseAttribute(int32_t aNamespaceID, |
74 | | nsAtom* aAttribute, |
75 | | const nsAString& aValue, |
76 | | nsIPrincipal* aMaybeScriptedPrincipal, |
77 | | nsAttrValue& aResult) |
78 | 0 | { |
79 | 0 | if (aNamespaceID == kNameSpaceID_None) { |
80 | 0 | if (aAttribute == nsGkAtoms::_for) { |
81 | 0 | aResult.ParseAtomArray(aValue); |
82 | 0 | return true; |
83 | 0 | } |
84 | 0 | } |
85 | 0 | |
86 | 0 | return nsGenericHTMLFormElement::ParseAttribute(aNamespaceID, aAttribute, |
87 | 0 | aValue, |
88 | 0 | aMaybeScriptedPrincipal, |
89 | 0 | aResult); |
90 | 0 | } |
91 | | |
92 | | void |
93 | | HTMLOutputElement::DoneAddingChildren(bool aHaveNotified) |
94 | 0 | { |
95 | 0 | mIsDoneAddingChildren = true; |
96 | 0 | } |
97 | | |
98 | | EventStates |
99 | | HTMLOutputElement::IntrinsicState() const |
100 | 0 | { |
101 | 0 | EventStates states = nsGenericHTMLFormElement::IntrinsicState(); |
102 | 0 |
|
103 | 0 | // We don't have to call IsCandidateForConstraintValidation() |
104 | 0 | // because <output> can't be barred from constraint validation. |
105 | 0 | if (IsValid()) { |
106 | 0 | states |= NS_EVENT_STATE_VALID; |
107 | 0 | if (!mForm || !mForm->HasAttr(kNameSpaceID_None, nsGkAtoms::novalidate)) { |
108 | 0 | states |= NS_EVENT_STATE_MOZ_UI_VALID; |
109 | 0 | } |
110 | 0 | } else { |
111 | 0 | states |= NS_EVENT_STATE_INVALID; |
112 | 0 | if (!mForm || !mForm->HasAttr(kNameSpaceID_None, nsGkAtoms::novalidate)) { |
113 | 0 | states |= NS_EVENT_STATE_MOZ_UI_INVALID; |
114 | 0 | } |
115 | 0 | } |
116 | 0 |
|
117 | 0 | return states; |
118 | 0 | } |
119 | | |
120 | | nsresult |
121 | | HTMLOutputElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent, |
122 | | nsIContent* aBindingParent) |
123 | 0 | { |
124 | 0 | nsresult rv = nsGenericHTMLFormElement::BindToTree(aDocument, aParent, |
125 | 0 | aBindingParent); |
126 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
127 | 0 |
|
128 | 0 | // Unfortunately, we can actually end up having to change our state |
129 | 0 | // as a result of being bound to a tree even from the parser: we |
130 | 0 | // might end up a in a novalidate form, and unlike other form |
131 | 0 | // controls that on its own is enough to make change ui-valid state. |
132 | 0 | // So just go ahead and update our state now. |
133 | 0 | UpdateState(false); |
134 | 0 |
|
135 | 0 | return rv; |
136 | 0 | } |
137 | | |
138 | | void |
139 | | HTMLOutputElement::GetValue(nsAString& aValue) |
140 | 0 | { |
141 | 0 | nsContentUtils::GetNodeTextContent(this, true, aValue); |
142 | 0 | } |
143 | | |
144 | | void |
145 | | HTMLOutputElement::SetValue(const nsAString& aValue, ErrorResult& aRv) |
146 | 0 | { |
147 | 0 | mValueModeFlag = eModeValue; |
148 | 0 | aRv = nsContentUtils::SetNodeTextContent(this, aValue, true); |
149 | 0 | } |
150 | | |
151 | | void |
152 | | HTMLOutputElement::SetDefaultValue(const nsAString& aDefaultValue, ErrorResult& aRv) |
153 | 0 | { |
154 | 0 | mDefaultValue = aDefaultValue; |
155 | 0 | if (mValueModeFlag == eModeDefault) { |
156 | 0 | // We can't pass mDefaultValue, because it'll be truncated when |
157 | 0 | // the element's descendants are changed. |
158 | 0 | aRv = nsContentUtils::SetNodeTextContent(this, aDefaultValue, true); |
159 | 0 | } |
160 | 0 | } |
161 | | |
162 | | nsDOMTokenList* |
163 | | HTMLOutputElement::HtmlFor() |
164 | 0 | { |
165 | 0 | if (!mTokenList) { |
166 | 0 | mTokenList = new nsDOMTokenList(this, nsGkAtoms::_for); |
167 | 0 | } |
168 | 0 | return mTokenList; |
169 | 0 | } |
170 | | |
171 | | void HTMLOutputElement::DescendantsChanged() |
172 | 0 | { |
173 | 0 | if (mIsDoneAddingChildren && mValueModeFlag == eModeDefault) { |
174 | 0 | nsContentUtils::GetNodeTextContent(this, true, mDefaultValue); |
175 | 0 | } |
176 | 0 | } |
177 | | |
178 | | // nsIMutationObserver |
179 | | |
180 | | void HTMLOutputElement::CharacterDataChanged(nsIContent* aContent, |
181 | | const CharacterDataChangeInfo&) |
182 | 0 | { |
183 | 0 | DescendantsChanged(); |
184 | 0 | } |
185 | | |
186 | | void HTMLOutputElement::ContentAppended(nsIContent* aFirstNewContent) |
187 | 0 | { |
188 | 0 | DescendantsChanged(); |
189 | 0 | } |
190 | | |
191 | | void HTMLOutputElement::ContentInserted(nsIContent* aChild) |
192 | 0 | { |
193 | 0 | DescendantsChanged(); |
194 | 0 | } |
195 | | |
196 | | void HTMLOutputElement::ContentRemoved(nsIContent* aChild, |
197 | | nsIContent* aPreviousSibling) |
198 | 0 | { |
199 | 0 | DescendantsChanged(); |
200 | 0 | } |
201 | | |
202 | | JSObject* |
203 | | HTMLOutputElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
204 | 0 | { |
205 | 0 | return HTMLOutputElement_Binding::Wrap(aCx, this, aGivenProto); |
206 | 0 | } |
207 | | |
208 | | } // namespace dom |
209 | | } // namespace mozilla |