Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLFormControlElement.cpp
1 /*
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)
7  *
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.
12  *
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.
17  *
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.
22  *
23  */
24
25 #include "config.h"
26 #include "core/html/HTMLFormControlElement.h"
27
28 #include "core/dom/PostAttachCallbacks.h"
29 #include "core/events/Event.h"
30 #include "core/events/ThreadLocalEventNames.h"
31 #include "core/html/HTMLFieldSetElement.h"
32 #include "core/html/HTMLFormElement.h"
33 #include "core/html/HTMLInputElement.h"
34 #include "core/html/HTMLLegendElement.h"
35 #include "core/html/ValidityState.h"
36 #include "core/html/forms/ValidationMessage.h"
37 #include "core/frame/UseCounter.h"
38 #include "core/rendering/RenderBox.h"
39 #include "core/rendering/RenderTheme.h"
40 #include "wtf/Vector.h"
41
42 namespace WebCore {
43
44 using namespace HTMLNames;
45 using namespace std;
46
47 HTMLFormControlElement::HTMLFormControlElement(const QualifiedName& tagName, Document& document, HTMLFormElement* form)
48     : LabelableElement(tagName, document)
49     , m_disabled(false)
50     , m_isAutofilled(false)
51     , m_isReadOnly(false)
52     , m_isRequired(false)
53     , m_valueMatchesRenderer(false)
54     , m_ancestorDisabledState(AncestorDisabledStateUnknown)
55     , m_dataListAncestorState(Unknown)
56     , m_willValidateInitialized(false)
57     , m_willValidate(true)
58     , m_isValid(true)
59     , m_wasChangedSinceLastFormControlChangeEvent(false)
60     , m_wasFocusedByMouse(false)
61 {
62     setHasCustomStyleCallbacks();
63     associateByParser(form);
64 }
65
66 HTMLFormControlElement::~HTMLFormControlElement()
67 {
68     setForm(0);
69 }
70
71 String HTMLFormControlElement::formEnctype() const
72 {
73     const AtomicString& formEnctypeAttr = fastGetAttribute(formenctypeAttr);
74     if (formEnctypeAttr.isNull())
75         return emptyString();
76     return FormSubmission::Attributes::parseEncodingType(formEnctypeAttr);
77 }
78
79 void HTMLFormControlElement::setFormEnctype(const AtomicString& value)
80 {
81     setAttribute(formenctypeAttr, value);
82 }
83
84 String HTMLFormControlElement::formMethod() const
85 {
86     const AtomicString& formMethodAttr = fastGetAttribute(formmethodAttr);
87     if (formMethodAttr.isNull())
88         return emptyString();
89     return FormSubmission::Attributes::methodString(FormSubmission::Attributes::parseMethodType(formMethodAttr));
90 }
91
92 void HTMLFormControlElement::setFormMethod(const AtomicString& value)
93 {
94     setAttribute(formmethodAttr, value);
95 }
96
97 bool HTMLFormControlElement::formNoValidate() const
98 {
99     return fastHasAttribute(formnovalidateAttr);
100 }
101
102 void HTMLFormControlElement::updateAncestorDisabledState() const
103 {
104     HTMLFieldSetElement* fieldSetAncestor = 0;
105     ContainerNode* legendAncestor = 0;
106     for (ContainerNode* ancestor = parentNode(); ancestor; ancestor = ancestor->parentNode()) {
107         if (!legendAncestor && ancestor->hasTagName(legendTag))
108             legendAncestor = ancestor;
109         if (ancestor->hasTagName(fieldsetTag)) {
110             fieldSetAncestor = toHTMLFieldSetElement(ancestor);
111             break;
112         }
113     }
114     m_ancestorDisabledState = (fieldSetAncestor && fieldSetAncestor->isDisabledFormControl() && !(legendAncestor && legendAncestor == fieldSetAncestor->legend())) ? AncestorDisabledStateDisabled : AncestorDisabledStateEnabled;
115 }
116
117 void HTMLFormControlElement::ancestorDisabledStateWasChanged()
118 {
119     m_ancestorDisabledState = AncestorDisabledStateUnknown;
120     disabledAttributeChanged();
121 }
122
123 void HTMLFormControlElement::reset()
124 {
125     setAutofilled(false);
126     resetImpl();
127 }
128
129 void HTMLFormControlElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
130 {
131     if (name == formAttr) {
132         formAttributeChanged();
133         UseCounter::count(document(), UseCounter::FormAttribute);
134     } else if (name == disabledAttr) {
135         bool oldDisabled = m_disabled;
136         m_disabled = !value.isNull();
137         if (oldDisabled != m_disabled)
138             disabledAttributeChanged();
139     } else if (name == readonlyAttr) {
140         bool wasReadOnly = m_isReadOnly;
141         m_isReadOnly = !value.isNull();
142         if (wasReadOnly != m_isReadOnly) {
143             setNeedsWillValidateCheck();
144             setNeedsStyleRecalc(SubtreeStyleChange);
145             if (renderer() && renderer()->style()->hasAppearance())
146                 RenderTheme::theme().stateChanged(renderer(), ReadOnlyState);
147         }
148     } else if (name == requiredAttr) {
149         bool wasRequired = m_isRequired;
150         m_isRequired = !value.isNull();
151         if (wasRequired != m_isRequired)
152             requiredAttributeChanged();
153         UseCounter::count(document(), UseCounter::RequiredAttribute);
154     } else if (name == autofocusAttr) {
155         HTMLElement::parseAttribute(name, value);
156         UseCounter::count(document(), UseCounter::AutoFocusAttribute);
157     } else
158         HTMLElement::parseAttribute(name, value);
159 }
160
161 void HTMLFormControlElement::disabledAttributeChanged()
162 {
163     setNeedsWillValidateCheck();
164     didAffectSelector(AffectedSelectorDisabled | AffectedSelectorEnabled);
165     if (renderer() && renderer()->style()->hasAppearance())
166         RenderTheme::theme().stateChanged(renderer(), EnabledState);
167     if (isDisabledFormControl() && treeScope().adjustedFocusedElement() == this) {
168         // We might want to call blur(), but it's dangerous to dispatch events
169         // here.
170         document().setNeedsFocusedElementCheck();
171     }
172 }
173
174 void HTMLFormControlElement::requiredAttributeChanged()
175 {
176     setNeedsValidityCheck();
177     // Style recalculation is needed because style selectors may include
178     // :required and :optional pseudo-classes.
179     setNeedsStyleRecalc(SubtreeStyleChange);
180 }
181
182 bool HTMLFormControlElement::supportsAutofocus() const
183 {
184     return false;
185 }
186
187 bool HTMLFormControlElement::isAutofocusable() const
188 {
189     return fastHasAttribute(autofocusAttr) && supportsAutofocus();
190 }
191
192 void HTMLFormControlElement::setAutofilled(bool autofilled)
193 {
194     if (autofilled == m_isAutofilled)
195         return;
196
197     m_isAutofilled = autofilled;
198     setNeedsStyleRecalc(SubtreeStyleChange);
199 }
200
201 static bool shouldAutofocusOnAttach(const HTMLFormControlElement* element)
202 {
203     if (!element->isAutofocusable())
204         return false;
205     if (element->document().isSandboxed(SandboxAutomaticFeatures)) {
206         // FIXME: This message should be moved off the console once a solution to https://bugs.webkit.org/show_bug.cgi?id=103274 exists.
207         element->document().addConsoleMessage(SecurityMessageSource, ErrorMessageLevel, "Blocked autofocusing on a form control because the form's frame is sandboxed and the 'allow-scripts' permission is not set.");
208         return false;
209     }
210
211     return true;
212 }
213
214 void HTMLFormControlElement::attach(const AttachContext& context)
215 {
216     HTMLElement::attach(context);
217
218     if (!renderer())
219         return;
220
221     // The call to updateFromElement() needs to go after the call through
222     // to the base class's attach() because that can sometimes do a close
223     // on the renderer.
224     renderer()->updateFromElement();
225
226     // FIXME: Autofocus handling should be moved to insertedInto according to
227     // the standard.
228     if (shouldAutofocusOnAttach(this))
229         document().setAutofocusElement(this);
230 }
231
232 void HTMLFormControlElement::didMoveToNewDocument(Document& oldDocument)
233 {
234     FormAssociatedElement::didMoveToNewDocument(oldDocument);
235     HTMLElement::didMoveToNewDocument(oldDocument);
236 }
237
238 Node::InsertionNotificationRequest HTMLFormControlElement::insertedInto(ContainerNode* insertionPoint)
239 {
240     m_ancestorDisabledState = AncestorDisabledStateUnknown;
241     m_dataListAncestorState = Unknown;
242     setNeedsWillValidateCheck();
243     HTMLElement::insertedInto(insertionPoint);
244     FormAssociatedElement::insertedInto(insertionPoint);
245     return InsertionDone;
246 }
247
248 void HTMLFormControlElement::removedFrom(ContainerNode* insertionPoint)
249 {
250     m_validationMessage = nullptr;
251     m_ancestorDisabledState = AncestorDisabledStateUnknown;
252     m_dataListAncestorState = Unknown;
253     HTMLElement::removedFrom(insertionPoint);
254     FormAssociatedElement::removedFrom(insertionPoint);
255 }
256
257 void HTMLFormControlElement::setChangedSinceLastFormControlChangeEvent(bool changed)
258 {
259     m_wasChangedSinceLastFormControlChangeEvent = changed;
260 }
261
262 void HTMLFormControlElement::dispatchChangeEvent()
263 {
264     dispatchScopedEvent(Event::createBubble(EventTypeNames::change));
265 }
266
267 void HTMLFormControlElement::dispatchFormControlChangeEvent()
268 {
269     dispatchChangeEvent();
270     setChangedSinceLastFormControlChangeEvent(false);
271 }
272
273 void HTMLFormControlElement::dispatchFormControlInputEvent()
274 {
275     setChangedSinceLastFormControlChangeEvent(true);
276     HTMLElement::dispatchInputEvent();
277 }
278
279 HTMLFormElement* HTMLFormControlElement::formOwner() const
280 {
281     return FormAssociatedElement::form();
282 }
283
284 bool HTMLFormControlElement::isDisabledFormControl() const
285 {
286     if (m_disabled)
287         return true;
288
289     if (m_ancestorDisabledState == AncestorDisabledStateUnknown)
290         updateAncestorDisabledState();
291     return m_ancestorDisabledState == AncestorDisabledStateDisabled;
292 }
293
294 bool HTMLFormControlElement::isRequired() const
295 {
296     return m_isRequired;
297 }
298
299 String HTMLFormControlElement::resultForDialogSubmit()
300 {
301     return fastGetAttribute(valueAttr);
302 }
303
304 void HTMLFormControlElement::didRecalcStyle(StyleRecalcChange)
305 {
306     if (RenderObject* renderer = this->renderer())
307         renderer->updateFromElement();
308 }
309
310 bool HTMLFormControlElement::supportsFocus() const
311 {
312     return !isDisabledFormControl();
313 }
314
315 bool HTMLFormControlElement::isKeyboardFocusable() const
316 {
317     // Skip tabIndex check in a parent class.
318     return isFocusable();
319 }
320
321 bool HTMLFormControlElement::shouldShowFocusRingOnMouseFocus() const
322 {
323     return false;
324 }
325
326 void HTMLFormControlElement::dispatchFocusEvent(Element* oldFocusedElement, FocusType type)
327 {
328     if (type != FocusTypePage)
329         m_wasFocusedByMouse = type == FocusTypeMouse;
330     HTMLElement::dispatchFocusEvent(oldFocusedElement, type);
331 }
332
333 bool HTMLFormControlElement::shouldHaveFocusAppearance() const
334 {
335     ASSERT(focused());
336     return shouldShowFocusRingOnMouseFocus() || !m_wasFocusedByMouse;
337 }
338
339 void HTMLFormControlElement::willCallDefaultEventHandler(const Event& event)
340 {
341     if (!event.isKeyboardEvent() || event.type() != EventTypeNames::keydown)
342         return;
343     if (!m_wasFocusedByMouse)
344         return;
345     m_wasFocusedByMouse = false;
346     if (renderer())
347         renderer()->repaint();
348 }
349
350
351 short HTMLFormControlElement::tabIndex() const
352 {
353     // Skip the supportsFocus check in HTMLElement.
354     return Element::tabIndex();
355 }
356
357 bool HTMLFormControlElement::recalcWillValidate() const
358 {
359     if (m_dataListAncestorState == Unknown) {
360         for (ContainerNode* ancestor = parentNode(); ancestor; ancestor = ancestor->parentNode()) {
361             if (ancestor->hasTagName(datalistTag)) {
362                 m_dataListAncestorState = InsideDataList;
363                 break;
364             }
365         }
366         if (m_dataListAncestorState == Unknown)
367             m_dataListAncestorState = NotInsideDataList;
368     }
369     return m_dataListAncestorState == NotInsideDataList && !isDisabledOrReadOnly();
370 }
371
372 bool HTMLFormControlElement::willValidate() const
373 {
374     if (!m_willValidateInitialized || m_dataListAncestorState == Unknown) {
375         m_willValidateInitialized = true;
376         bool newWillValidate = recalcWillValidate();
377         if (m_willValidate != newWillValidate) {
378             m_willValidate = newWillValidate;
379             const_cast<HTMLFormControlElement*>(this)->setNeedsValidityCheck();
380         }
381     } else {
382         // If the following assertion fails, setNeedsWillValidateCheck() is not
383         // called correctly when something which changes recalcWillValidate() result
384         // is updated.
385         ASSERT(m_willValidate == recalcWillValidate());
386     }
387     return m_willValidate;
388 }
389
390 void HTMLFormControlElement::setNeedsWillValidateCheck()
391 {
392     // We need to recalculate willValidate immediately because willValidate change can causes style change.
393     bool newWillValidate = recalcWillValidate();
394     if (m_willValidateInitialized && m_willValidate == newWillValidate)
395         return;
396     m_willValidateInitialized = true;
397     m_willValidate = newWillValidate;
398     setNeedsValidityCheck();
399     setNeedsStyleRecalc(SubtreeStyleChange);
400     if (!m_willValidate)
401         hideVisibleValidationMessage();
402 }
403
404 void HTMLFormControlElement::updateVisibleValidationMessage()
405 {
406     Page* page = document().page();
407     if (!page)
408         return;
409     String message;
410     if (renderer() && willValidate())
411         message = validationMessage().stripWhiteSpace();
412     if (!m_validationMessage)
413         m_validationMessage = ValidationMessage::create(this);
414     m_validationMessage->updateValidationMessage(message);
415 }
416
417 void HTMLFormControlElement::hideVisibleValidationMessage()
418 {
419     if (m_validationMessage)
420         m_validationMessage->requestToHideMessage();
421 }
422
423 bool HTMLFormControlElement::checkValidity(Vector<RefPtr<FormAssociatedElement> >* unhandledInvalidControls, CheckValidityDispatchEvents dispatchEvents)
424 {
425     if (!willValidate() || isValidFormControlElement())
426         return true;
427     if (dispatchEvents == CheckValidityDispatchEventsNone)
428         return false;
429     // An event handler can deref this object.
430     RefPtr<HTMLFormControlElement> protector(this);
431     RefPtr<Document> originalDocument(document());
432     bool needsDefaultAction = dispatchEvent(Event::createCancelable(EventTypeNames::invalid));
433     if (needsDefaultAction && unhandledInvalidControls && inDocument() && originalDocument == document())
434         unhandledInvalidControls->append(this);
435     return false;
436 }
437
438 bool HTMLFormControlElement::isValidFormControlElement()
439 {
440     // If the following assertion fails, setNeedsValidityCheck() is not called
441     // correctly when something which changes validity is updated.
442     ASSERT(m_isValid == valid());
443     return m_isValid;
444 }
445
446 void HTMLFormControlElement::setNeedsValidityCheck()
447 {
448     bool newIsValid = valid();
449     if (willValidate() && newIsValid != m_isValid) {
450         // Update style for pseudo classes such as :valid :invalid.
451         setNeedsStyleRecalc(SubtreeStyleChange);
452     }
453     m_isValid = newIsValid;
454
455     // Updates only if this control already has a validation message.
456     if (m_validationMessage && m_validationMessage->isVisible()) {
457         // Calls updateVisibleValidationMessage() even if m_isValid is not
458         // changed because a validation message can be chagned.
459         updateVisibleValidationMessage();
460     }
461 }
462
463 void HTMLFormControlElement::setCustomValidity(const String& error)
464 {
465     FormAssociatedElement::setCustomValidity(error);
466     setNeedsValidityCheck();
467 }
468
469 void HTMLFormControlElement::dispatchBlurEvent(Element* newFocusedElement)
470 {
471     HTMLElement::dispatchBlurEvent(newFocusedElement);
472     hideVisibleValidationMessage();
473 }
474
475 bool HTMLFormControlElement::isSuccessfulSubmitButton() const
476 {
477     return canBeSuccessfulSubmitButton() && !isDisabledFormControl();
478 }
479
480 bool HTMLFormControlElement::isDefaultButtonForForm() const
481 {
482     return isSuccessfulSubmitButton() && form() && form()->defaultButton() == this;
483 }
484
485 HTMLFormControlElement* HTMLFormControlElement::enclosingFormControlElement(Node* node)
486 {
487     for (; node; node = node->parentNode()) {
488         if (node->isElementNode() && toElement(node)->isFormControlElement())
489             return toHTMLFormControlElement(node);
490     }
491     return 0;
492 }
493
494 String HTMLFormControlElement::nameForAutofill() const
495 {
496     String fullName = name();
497     String trimmedName = fullName.stripWhiteSpace();
498     if (!trimmedName.isEmpty())
499         return trimmedName;
500     fullName = getIdAttribute();
501     trimmedName = fullName.stripWhiteSpace();
502     return trimmedName;
503 }
504
505 void HTMLFormControlElement::setFocus(bool flag)
506 {
507     LabelableElement::setFocus(flag);
508
509     if (!flag && wasChangedSinceLastFormControlChangeEvent())
510         dispatchFormControlChangeEvent();
511 }
512
513 } // namespace Webcore