2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
6 * (C) 2006 Alexey Proskuryakov (ap@nypop.com)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
26 #include "core/html/HTMLFormControlElement.h"
28 #include "core/events/Event.h"
29 #include "core/html/HTMLDataListElement.h"
30 #include "core/html/HTMLFieldSetElement.h"
31 #include "core/html/HTMLFormElement.h"
32 #include "core/html/HTMLInputElement.h"
33 #include "core/html/HTMLLegendElement.h"
34 #include "core/html/ValidityState.h"
35 #include "core/frame/UseCounter.h"
36 #include "core/inspector/ConsoleMessage.h"
37 #include "core/page/Page.h"
38 #include "core/page/ValidationMessageClient.h"
39 #include "core/rendering/RenderBox.h"
40 #include "core/rendering/RenderTheme.h"
41 #include "platform/text/BidiTextRun.h"
42 #include "wtf/Vector.h"
46 using namespace HTMLNames;
48 HTMLFormControlElement::HTMLFormControlElement(const QualifiedName& tagName, Document& document, HTMLFormElement* form)
49 : LabelableElement(tagName, document)
51 , m_isAutofilled(false)
54 , m_hasValidationMessage(false)
55 , m_ancestorDisabledState(AncestorDisabledStateUnknown)
56 , m_dataListAncestorState(Unknown)
57 , m_willValidateInitialized(false)
58 , m_willValidate(true)
60 , m_wasChangedSinceLastFormControlChangeEvent(false)
61 , m_wasFocusedByMouse(false)
63 setHasCustomStyleCallbacks();
64 associateByParser(form);
67 HTMLFormControlElement::~HTMLFormControlElement()
74 void HTMLFormControlElement::trace(Visitor* visitor)
76 FormAssociatedElement::trace(visitor);
77 LabelableElement::trace(visitor);
80 String HTMLFormControlElement::formEnctype() const
82 const AtomicString& formEnctypeAttr = fastGetAttribute(formenctypeAttr);
83 if (formEnctypeAttr.isNull())
85 return FormSubmission::Attributes::parseEncodingType(formEnctypeAttr);
88 void HTMLFormControlElement::setFormEnctype(const AtomicString& value)
90 setAttribute(formenctypeAttr, value);
93 String HTMLFormControlElement::formMethod() const
95 const AtomicString& formMethodAttr = fastGetAttribute(formmethodAttr);
96 if (formMethodAttr.isNull())
98 return FormSubmission::Attributes::methodString(FormSubmission::Attributes::parseMethodType(formMethodAttr));
101 void HTMLFormControlElement::setFormMethod(const AtomicString& value)
103 setAttribute(formmethodAttr, value);
106 bool HTMLFormControlElement::formNoValidate() const
108 return fastHasAttribute(formnovalidateAttr);
111 void HTMLFormControlElement::updateAncestorDisabledState() const
113 HTMLFieldSetElement* fieldSetAncestor = 0;
114 ContainerNode* legendAncestor = 0;
115 for (HTMLElement* ancestor = Traversal<HTMLElement>::firstAncestor(*this); ancestor; ancestor = Traversal<HTMLElement>::firstAncestor(*ancestor)) {
116 if (!legendAncestor && isHTMLLegendElement(*ancestor))
117 legendAncestor = ancestor;
118 if (isHTMLFieldSetElement(*ancestor)) {
119 fieldSetAncestor = toHTMLFieldSetElement(ancestor);
123 m_ancestorDisabledState = (fieldSetAncestor && fieldSetAncestor->isDisabledFormControl() && !(legendAncestor && legendAncestor == fieldSetAncestor->legend())) ? AncestorDisabledStateDisabled : AncestorDisabledStateEnabled;
126 void HTMLFormControlElement::ancestorDisabledStateWasChanged()
128 m_ancestorDisabledState = AncestorDisabledStateUnknown;
129 disabledAttributeChanged();
132 void HTMLFormControlElement::reset()
134 setAutofilled(false);
138 void HTMLFormControlElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
140 if (name == formAttr) {
141 formAttributeChanged();
142 UseCounter::count(document(), UseCounter::FormAttribute);
143 } else if (name == disabledAttr) {
144 bool oldDisabled = m_disabled;
145 m_disabled = !value.isNull();
146 if (oldDisabled != m_disabled)
147 disabledAttributeChanged();
148 } else if (name == readonlyAttr) {
149 bool wasReadOnly = m_isReadOnly;
150 m_isReadOnly = !value.isNull();
151 if (wasReadOnly != m_isReadOnly) {
152 setNeedsWillValidateCheck();
153 setNeedsStyleRecalc(SubtreeStyleChange);
154 if (renderer() && renderer()->style()->hasAppearance())
155 RenderTheme::theme().stateChanged(renderer(), ReadOnlyControlState);
157 } else if (name == requiredAttr) {
158 bool wasRequired = m_isRequired;
159 m_isRequired = !value.isNull();
160 if (wasRequired != m_isRequired)
161 requiredAttributeChanged();
162 UseCounter::count(document(), UseCounter::RequiredAttribute);
163 } else if (name == autofocusAttr) {
164 HTMLElement::parseAttribute(name, value);
165 UseCounter::count(document(), UseCounter::AutoFocusAttribute);
167 HTMLElement::parseAttribute(name, value);
170 void HTMLFormControlElement::disabledAttributeChanged()
172 setNeedsWillValidateCheck();
173 didAffectSelector(AffectedSelectorDisabled | AffectedSelectorEnabled);
174 if (renderer() && renderer()->style()->hasAppearance())
175 RenderTheme::theme().stateChanged(renderer(), EnabledControlState);
176 if (isDisabledFormControl() && treeScope().adjustedFocusedElement() == this) {
177 // We might want to call blur(), but it's dangerous to dispatch events
179 document().setNeedsFocusedElementCheck();
183 void HTMLFormControlElement::requiredAttributeChanged()
185 setNeedsValidityCheck();
186 // Style recalculation is needed because style selectors may include
187 // :required and :optional pseudo-classes.
188 setNeedsStyleRecalc(SubtreeStyleChange);
191 bool HTMLFormControlElement::supportsAutofocus() const
196 bool HTMLFormControlElement::isAutofocusable() const
198 return fastHasAttribute(autofocusAttr) && supportsAutofocus();
201 void HTMLFormControlElement::setAutofilled(bool autofilled)
203 if (autofilled == m_isAutofilled)
206 m_isAutofilled = autofilled;
207 setNeedsStyleRecalc(SubtreeStyleChange);
210 static bool shouldAutofocusOnAttach(const HTMLFormControlElement* element)
212 if (!element->isAutofocusable())
214 if (element->document().isSandboxed(SandboxAutomaticFeatures)) {
215 // FIXME: This message should be moved off the console once a solution to https://bugs.webkit.org/show_bug.cgi?id=103274 exists.
216 element->document().addConsoleMessage(ConsoleMessage::create(SecurityMessageSource, ErrorMessageLevel, "Blocked autofocusing on a form control because the form's frame is sandboxed and the 'allow-scripts' permission is not set."));
223 void HTMLFormControlElement::attach(const AttachContext& context)
225 HTMLElement::attach(context);
230 // The call to updateFromElement() needs to go after the call through
231 // to the base class's attach() because that can sometimes do a close
233 renderer()->updateFromElement();
235 // FIXME: Autofocus handling should be moved to insertedInto according to
237 if (shouldAutofocusOnAttach(this))
238 document().setAutofocusElement(this);
241 void HTMLFormControlElement::didMoveToNewDocument(Document& oldDocument)
243 FormAssociatedElement::didMoveToNewDocument(oldDocument);
244 HTMLElement::didMoveToNewDocument(oldDocument);
247 Node::InsertionNotificationRequest HTMLFormControlElement::insertedInto(ContainerNode* insertionPoint)
249 m_ancestorDisabledState = AncestorDisabledStateUnknown;
250 m_dataListAncestorState = Unknown;
251 setNeedsWillValidateCheck();
252 HTMLElement::insertedInto(insertionPoint);
253 FormAssociatedElement::insertedInto(insertionPoint);
254 return InsertionDone;
257 void HTMLFormControlElement::removedFrom(ContainerNode* insertionPoint)
259 hideVisibleValidationMessage();
260 m_hasValidationMessage = false;
261 m_ancestorDisabledState = AncestorDisabledStateUnknown;
262 m_dataListAncestorState = Unknown;
263 HTMLElement::removedFrom(insertionPoint);
264 FormAssociatedElement::removedFrom(insertionPoint);
267 void HTMLFormControlElement::setChangedSinceLastFormControlChangeEvent(bool changed)
269 m_wasChangedSinceLastFormControlChangeEvent = changed;
272 void HTMLFormControlElement::dispatchChangeEvent()
274 dispatchScopedEvent(Event::createBubble(EventTypeNames::change));
277 void HTMLFormControlElement::dispatchFormControlChangeEvent()
279 dispatchChangeEvent();
280 setChangedSinceLastFormControlChangeEvent(false);
283 void HTMLFormControlElement::dispatchFormControlInputEvent()
285 setChangedSinceLastFormControlChangeEvent(true);
286 HTMLElement::dispatchInputEvent();
289 HTMLFormElement* HTMLFormControlElement::formOwner() const
291 return FormAssociatedElement::form();
294 bool HTMLFormControlElement::isDisabledFormControl() const
299 if (m_ancestorDisabledState == AncestorDisabledStateUnknown)
300 updateAncestorDisabledState();
301 return m_ancestorDisabledState == AncestorDisabledStateDisabled;
304 bool HTMLFormControlElement::isRequired() const
309 String HTMLFormControlElement::resultForDialogSubmit()
311 return fastGetAttribute(valueAttr);
314 void HTMLFormControlElement::didRecalcStyle(StyleRecalcChange)
316 if (RenderObject* renderer = this->renderer())
317 renderer->updateFromElement();
320 bool HTMLFormControlElement::supportsFocus() const
322 return !isDisabledFormControl();
325 bool HTMLFormControlElement::isKeyboardFocusable() const
327 // Skip tabIndex check in a parent class.
328 return isFocusable();
331 bool HTMLFormControlElement::shouldShowFocusRingOnMouseFocus() const
336 void HTMLFormControlElement::dispatchFocusEvent(Element* oldFocusedElement, FocusType type)
338 if (type != FocusTypePage)
339 m_wasFocusedByMouse = type == FocusTypeMouse;
340 HTMLElement::dispatchFocusEvent(oldFocusedElement, type);
343 bool HTMLFormControlElement::shouldHaveFocusAppearance() const
346 return shouldShowFocusRingOnMouseFocus() || !m_wasFocusedByMouse;
349 void HTMLFormControlElement::willCallDefaultEventHandler(const Event& event)
351 if (!event.isKeyboardEvent() || event.type() != EventTypeNames::keydown)
353 if (!m_wasFocusedByMouse)
355 m_wasFocusedByMouse = false;
357 renderer()->paintInvalidationForWholeRenderer();
361 short HTMLFormControlElement::tabIndex() const
363 // Skip the supportsFocus check in HTMLElement.
364 return Element::tabIndex();
367 bool HTMLFormControlElement::recalcWillValidate() const
369 if (m_dataListAncestorState == Unknown) {
370 if (Traversal<HTMLDataListElement>::firstAncestor(*this))
371 m_dataListAncestorState = InsideDataList;
373 m_dataListAncestorState = NotInsideDataList;
375 return m_dataListAncestorState == NotInsideDataList && !isDisabledOrReadOnly();
378 bool HTMLFormControlElement::willValidate() const
380 if (!m_willValidateInitialized || m_dataListAncestorState == Unknown) {
381 m_willValidateInitialized = true;
382 bool newWillValidate = recalcWillValidate();
383 if (m_willValidate != newWillValidate) {
384 m_willValidate = newWillValidate;
385 const_cast<HTMLFormControlElement*>(this)->setNeedsValidityCheck();
388 // If the following assertion fails, setNeedsWillValidateCheck() is not
389 // called correctly when something which changes recalcWillValidate() result
391 ASSERT(m_willValidate == recalcWillValidate());
393 return m_willValidate;
396 void HTMLFormControlElement::setNeedsWillValidateCheck()
398 // We need to recalculate willValidate immediately because willValidate change can causes style change.
399 bool newWillValidate = recalcWillValidate();
400 if (m_willValidateInitialized && m_willValidate == newWillValidate)
402 m_willValidateInitialized = true;
403 m_willValidate = newWillValidate;
404 setNeedsValidityCheck();
405 setNeedsStyleRecalc(SubtreeStyleChange);
407 hideVisibleValidationMessage();
410 void HTMLFormControlElement::findCustomValidationMessageTextDirection(const String& message, TextDirection &messageDir, String& subMessage, TextDirection &subMessageDir)
412 bool hasStrongDirection;
413 subMessage = fastGetAttribute(titleAttr);
414 messageDir = determineDirectionality(message, hasStrongDirection);
415 if (!subMessage.isEmpty())
416 subMessageDir = renderer()->style()->direction();
419 void HTMLFormControlElement::updateVisibleValidationMessage()
421 Page* page = document().page();
425 if (renderer() && willValidate())
426 message = validationMessage().stripWhiteSpace();
428 m_hasValidationMessage = true;
429 ValidationMessageClient* client = &page->validationMessageClient();
430 TextDirection messageDir = LTR;
431 TextDirection subMessageDir = LTR;
432 String subMessage = String();
433 if (message.isEmpty())
434 client->hideValidationMessage(*this);
436 findCustomValidationMessageTextDirection(message, messageDir, subMessage, subMessageDir);
437 client->showValidationMessage(*this, message, messageDir, subMessage, subMessageDir);
440 void HTMLFormControlElement::hideVisibleValidationMessage()
442 if (!m_hasValidationMessage)
445 if (ValidationMessageClient* client = validationMessageClient())
446 client->hideValidationMessage(*this);
449 bool HTMLFormControlElement::isValidationMessageVisible() const
451 if (!m_hasValidationMessage)
454 ValidationMessageClient* client = validationMessageClient();
458 return client->isValidationMessageVisible(*this);
461 ValidationMessageClient* HTMLFormControlElement::validationMessageClient() const
463 Page* page = document().page();
467 return &page->validationMessageClient();
470 bool HTMLFormControlElement::checkValidity(WillBeHeapVector<RefPtrWillBeMember<FormAssociatedElement> >* unhandledInvalidControls)
472 if (!willValidate() || isValidFormControlElement())
474 // An event handler can deref this object.
475 RefPtrWillBeRawPtr<HTMLFormControlElement> protector(this);
476 RefPtrWillBeRawPtr<Document> originalDocument(document());
477 bool needsDefaultAction = dispatchEvent(Event::createCancelable(EventTypeNames::invalid));
478 if (needsDefaultAction && unhandledInvalidControls && inDocument() && originalDocument == document())
479 unhandledInvalidControls->append(this);
483 bool HTMLFormControlElement::isValidFormControlElement()
485 // If the following assertion fails, setNeedsValidityCheck() is not called
486 // correctly when something which changes validity is updated.
487 ASSERT(m_isValid == valid());
491 void HTMLFormControlElement::setNeedsValidityCheck()
493 bool newIsValid = valid();
494 if (willValidate() && newIsValid != m_isValid) {
495 // Update style for pseudo classes such as :valid :invalid.
496 setNeedsStyleRecalc(SubtreeStyleChange);
498 m_isValid = newIsValid;
500 // Updates only if this control already has a validation message.
501 if (isValidationMessageVisible()) {
502 // Calls updateVisibleValidationMessage() even if m_isValid is not
503 // changed because a validation message can be changed.
504 updateVisibleValidationMessage();
508 void HTMLFormControlElement::setCustomValidity(const String& error)
510 FormAssociatedElement::setCustomValidity(error);
511 setNeedsValidityCheck();
514 void HTMLFormControlElement::dispatchBlurEvent(Element* newFocusedElement)
516 HTMLElement::dispatchBlurEvent(newFocusedElement);
517 hideVisibleValidationMessage();
520 bool HTMLFormControlElement::isSuccessfulSubmitButton() const
522 return canBeSuccessfulSubmitButton() && !isDisabledFormControl();
525 bool HTMLFormControlElement::isDefaultButtonForForm() const
527 return isSuccessfulSubmitButton() && form() && form()->defaultButton() == this;
530 HTMLFormControlElement* HTMLFormControlElement::enclosingFormControlElement(Node* node)
534 return Traversal<HTMLFormControlElement>::firstAncestorOrSelf(*node);
537 String HTMLFormControlElement::nameForAutofill() const
539 String fullName = name();
540 String trimmedName = fullName.stripWhiteSpace();
541 if (!trimmedName.isEmpty())
543 fullName = getIdAttribute();
544 trimmedName = fullName.stripWhiteSpace();
548 void HTMLFormControlElement::setFocus(bool flag)
550 LabelableElement::setFocus(flag);
552 if (!flag && wasChangedSinceLastFormControlChangeEvent())
553 dispatchFormControlChangeEvent();
556 } // namespace Webcore