Upstream version 9.38.198.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/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"
43
44 namespace blink {
45
46 using namespace HTMLNames;
47
48 HTMLFormControlElement::HTMLFormControlElement(const QualifiedName& tagName, Document& document, HTMLFormElement* form)
49     : LabelableElement(tagName, document)
50     , m_disabled(false)
51     , m_isAutofilled(false)
52     , m_isReadOnly(false)
53     , m_isRequired(false)
54     , m_hasValidationMessage(false)
55     , m_ancestorDisabledState(AncestorDisabledStateUnknown)
56     , m_dataListAncestorState(Unknown)
57     , m_willValidateInitialized(false)
58     , m_willValidate(true)
59     , m_isValid(true)
60     , m_wasChangedSinceLastFormControlChangeEvent(false)
61     , m_wasFocusedByMouse(false)
62 {
63     setHasCustomStyleCallbacks();
64     associateByParser(form);
65 }
66
67 HTMLFormControlElement::~HTMLFormControlElement()
68 {
69 #if !ENABLE(OILPAN)
70     setForm(0);
71 #endif
72 }
73
74 void HTMLFormControlElement::trace(Visitor* visitor)
75 {
76     FormAssociatedElement::trace(visitor);
77     LabelableElement::trace(visitor);
78 }
79
80 String HTMLFormControlElement::formEnctype() const
81 {
82     const AtomicString& formEnctypeAttr = fastGetAttribute(formenctypeAttr);
83     if (formEnctypeAttr.isNull())
84         return emptyString();
85     return FormSubmission::Attributes::parseEncodingType(formEnctypeAttr);
86 }
87
88 void HTMLFormControlElement::setFormEnctype(const AtomicString& value)
89 {
90     setAttribute(formenctypeAttr, value);
91 }
92
93 String HTMLFormControlElement::formMethod() const
94 {
95     const AtomicString& formMethodAttr = fastGetAttribute(formmethodAttr);
96     if (formMethodAttr.isNull())
97         return emptyString();
98     return FormSubmission::Attributes::methodString(FormSubmission::Attributes::parseMethodType(formMethodAttr));
99 }
100
101 void HTMLFormControlElement::setFormMethod(const AtomicString& value)
102 {
103     setAttribute(formmethodAttr, value);
104 }
105
106 bool HTMLFormControlElement::formNoValidate() const
107 {
108     return fastHasAttribute(formnovalidateAttr);
109 }
110
111 void HTMLFormControlElement::updateAncestorDisabledState() const
112 {
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);
120             break;
121         }
122     }
123     m_ancestorDisabledState = (fieldSetAncestor && fieldSetAncestor->isDisabledFormControl() && !(legendAncestor && legendAncestor == fieldSetAncestor->legend())) ? AncestorDisabledStateDisabled : AncestorDisabledStateEnabled;
124 }
125
126 void HTMLFormControlElement::ancestorDisabledStateWasChanged()
127 {
128     m_ancestorDisabledState = AncestorDisabledStateUnknown;
129     disabledAttributeChanged();
130 }
131
132 void HTMLFormControlElement::reset()
133 {
134     setAutofilled(false);
135     resetImpl();
136 }
137
138 void HTMLFormControlElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
139 {
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);
156         }
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);
166     } else
167         HTMLElement::parseAttribute(name, value);
168 }
169
170 void HTMLFormControlElement::disabledAttributeChanged()
171 {
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
178         // here.
179         document().setNeedsFocusedElementCheck();
180     }
181 }
182
183 void HTMLFormControlElement::requiredAttributeChanged()
184 {
185     setNeedsValidityCheck();
186     // Style recalculation is needed because style selectors may include
187     // :required and :optional pseudo-classes.
188     setNeedsStyleRecalc(SubtreeStyleChange);
189 }
190
191 bool HTMLFormControlElement::supportsAutofocus() const
192 {
193     return false;
194 }
195
196 bool HTMLFormControlElement::isAutofocusable() const
197 {
198     return fastHasAttribute(autofocusAttr) && supportsAutofocus();
199 }
200
201 void HTMLFormControlElement::setAutofilled(bool autofilled)
202 {
203     if (autofilled == m_isAutofilled)
204         return;
205
206     m_isAutofilled = autofilled;
207     setNeedsStyleRecalc(SubtreeStyleChange);
208 }
209
210 static bool shouldAutofocusOnAttach(const HTMLFormControlElement* element)
211 {
212     if (!element->isAutofocusable())
213         return false;
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."));
217         return false;
218     }
219
220     return true;
221 }
222
223 void HTMLFormControlElement::attach(const AttachContext& context)
224 {
225     HTMLElement::attach(context);
226
227     if (!renderer())
228         return;
229
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
232     // on the renderer.
233     renderer()->updateFromElement();
234
235     // FIXME: Autofocus handling should be moved to insertedInto according to
236     // the standard.
237     if (shouldAutofocusOnAttach(this))
238         document().setAutofocusElement(this);
239 }
240
241 void HTMLFormControlElement::didMoveToNewDocument(Document& oldDocument)
242 {
243     FormAssociatedElement::didMoveToNewDocument(oldDocument);
244     HTMLElement::didMoveToNewDocument(oldDocument);
245 }
246
247 Node::InsertionNotificationRequest HTMLFormControlElement::insertedInto(ContainerNode* insertionPoint)
248 {
249     m_ancestorDisabledState = AncestorDisabledStateUnknown;
250     m_dataListAncestorState = Unknown;
251     setNeedsWillValidateCheck();
252     HTMLElement::insertedInto(insertionPoint);
253     FormAssociatedElement::insertedInto(insertionPoint);
254     return InsertionDone;
255 }
256
257 void HTMLFormControlElement::removedFrom(ContainerNode* insertionPoint)
258 {
259     hideVisibleValidationMessage();
260     m_hasValidationMessage = false;
261     m_ancestorDisabledState = AncestorDisabledStateUnknown;
262     m_dataListAncestorState = Unknown;
263     HTMLElement::removedFrom(insertionPoint);
264     FormAssociatedElement::removedFrom(insertionPoint);
265 }
266
267 void HTMLFormControlElement::setChangedSinceLastFormControlChangeEvent(bool changed)
268 {
269     m_wasChangedSinceLastFormControlChangeEvent = changed;
270 }
271
272 void HTMLFormControlElement::dispatchChangeEvent()
273 {
274     dispatchScopedEvent(Event::createBubble(EventTypeNames::change));
275 }
276
277 void HTMLFormControlElement::dispatchFormControlChangeEvent()
278 {
279     dispatchChangeEvent();
280     setChangedSinceLastFormControlChangeEvent(false);
281 }
282
283 void HTMLFormControlElement::dispatchFormControlInputEvent()
284 {
285     setChangedSinceLastFormControlChangeEvent(true);
286     HTMLElement::dispatchInputEvent();
287 }
288
289 HTMLFormElement* HTMLFormControlElement::formOwner() const
290 {
291     return FormAssociatedElement::form();
292 }
293
294 bool HTMLFormControlElement::isDisabledFormControl() const
295 {
296     if (m_disabled)
297         return true;
298
299     if (m_ancestorDisabledState == AncestorDisabledStateUnknown)
300         updateAncestorDisabledState();
301     return m_ancestorDisabledState == AncestorDisabledStateDisabled;
302 }
303
304 bool HTMLFormControlElement::isRequired() const
305 {
306     return m_isRequired;
307 }
308
309 String HTMLFormControlElement::resultForDialogSubmit()
310 {
311     return fastGetAttribute(valueAttr);
312 }
313
314 void HTMLFormControlElement::didRecalcStyle(StyleRecalcChange)
315 {
316     if (RenderObject* renderer = this->renderer())
317         renderer->updateFromElement();
318 }
319
320 bool HTMLFormControlElement::supportsFocus() const
321 {
322     return !isDisabledFormControl();
323 }
324
325 bool HTMLFormControlElement::isKeyboardFocusable() const
326 {
327     // Skip tabIndex check in a parent class.
328     return isFocusable();
329 }
330
331 bool HTMLFormControlElement::shouldShowFocusRingOnMouseFocus() const
332 {
333     return false;
334 }
335
336 void HTMLFormControlElement::dispatchFocusEvent(Element* oldFocusedElement, FocusType type)
337 {
338     if (type != FocusTypePage)
339         m_wasFocusedByMouse = type == FocusTypeMouse;
340     HTMLElement::dispatchFocusEvent(oldFocusedElement, type);
341 }
342
343 bool HTMLFormControlElement::shouldHaveFocusAppearance() const
344 {
345     ASSERT(focused());
346     return shouldShowFocusRingOnMouseFocus() || !m_wasFocusedByMouse;
347 }
348
349 void HTMLFormControlElement::willCallDefaultEventHandler(const Event& event)
350 {
351     if (!event.isKeyboardEvent() || event.type() != EventTypeNames::keydown)
352         return;
353     if (!m_wasFocusedByMouse)
354         return;
355     m_wasFocusedByMouse = false;
356     if (renderer())
357         renderer()->paintInvalidationForWholeRenderer();
358 }
359
360
361 short HTMLFormControlElement::tabIndex() const
362 {
363     // Skip the supportsFocus check in HTMLElement.
364     return Element::tabIndex();
365 }
366
367 bool HTMLFormControlElement::recalcWillValidate() const
368 {
369     if (m_dataListAncestorState == Unknown) {
370         if (Traversal<HTMLDataListElement>::firstAncestor(*this))
371             m_dataListAncestorState = InsideDataList;
372         else
373             m_dataListAncestorState = NotInsideDataList;
374     }
375     return m_dataListAncestorState == NotInsideDataList && !isDisabledOrReadOnly();
376 }
377
378 bool HTMLFormControlElement::willValidate() const
379 {
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();
386         }
387     } else {
388         // If the following assertion fails, setNeedsWillValidateCheck() is not
389         // called correctly when something which changes recalcWillValidate() result
390         // is updated.
391         ASSERT(m_willValidate == recalcWillValidate());
392     }
393     return m_willValidate;
394 }
395
396 void HTMLFormControlElement::setNeedsWillValidateCheck()
397 {
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)
401         return;
402     m_willValidateInitialized = true;
403     m_willValidate = newWillValidate;
404     setNeedsValidityCheck();
405     setNeedsStyleRecalc(SubtreeStyleChange);
406     if (!m_willValidate)
407         hideVisibleValidationMessage();
408 }
409
410 void HTMLFormControlElement::findCustomValidationMessageTextDirection(const String& message, TextDirection &messageDir, String& subMessage, TextDirection &subMessageDir)
411 {
412     bool hasStrongDirection;
413     subMessage = fastGetAttribute(titleAttr);
414     messageDir = determineDirectionality(message, hasStrongDirection);
415     if (!subMessage.isEmpty())
416         subMessageDir = renderer()->style()->direction();
417 }
418
419 void HTMLFormControlElement::updateVisibleValidationMessage()
420 {
421     Page* page = document().page();
422     if (!page)
423         return;
424     String message;
425     if (renderer() && willValidate())
426         message = validationMessage().stripWhiteSpace();
427
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);
435     else
436         findCustomValidationMessageTextDirection(message, messageDir, subMessage, subMessageDir);
437     client->showValidationMessage(*this, message, messageDir, subMessage, subMessageDir);
438 }
439
440 void HTMLFormControlElement::hideVisibleValidationMessage()
441 {
442     if (!m_hasValidationMessage)
443         return;
444
445     if (ValidationMessageClient* client = validationMessageClient())
446         client->hideValidationMessage(*this);
447 }
448
449 bool HTMLFormControlElement::isValidationMessageVisible() const
450 {
451     if (!m_hasValidationMessage)
452         return false;
453
454     ValidationMessageClient* client = validationMessageClient();
455     if (!client)
456         return false;
457
458     return client->isValidationMessageVisible(*this);
459 }
460
461 ValidationMessageClient* HTMLFormControlElement::validationMessageClient() const
462 {
463     Page* page = document().page();
464     if (!page)
465         return 0;
466
467     return &page->validationMessageClient();
468 }
469
470 bool HTMLFormControlElement::checkValidity(WillBeHeapVector<RefPtrWillBeMember<FormAssociatedElement> >* unhandledInvalidControls)
471 {
472     if (!willValidate() || isValidFormControlElement())
473         return true;
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);
480     return false;
481 }
482
483 bool HTMLFormControlElement::isValidFormControlElement()
484 {
485     // If the following assertion fails, setNeedsValidityCheck() is not called
486     // correctly when something which changes validity is updated.
487     ASSERT(m_isValid == valid());
488     return m_isValid;
489 }
490
491 void HTMLFormControlElement::setNeedsValidityCheck()
492 {
493     bool newIsValid = valid();
494     if (willValidate() && newIsValid != m_isValid) {
495         // Update style for pseudo classes such as :valid :invalid.
496         setNeedsStyleRecalc(SubtreeStyleChange);
497     }
498     m_isValid = newIsValid;
499
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();
505     }
506 }
507
508 void HTMLFormControlElement::setCustomValidity(const String& error)
509 {
510     FormAssociatedElement::setCustomValidity(error);
511     setNeedsValidityCheck();
512 }
513
514 void HTMLFormControlElement::dispatchBlurEvent(Element* newFocusedElement)
515 {
516     HTMLElement::dispatchBlurEvent(newFocusedElement);
517     hideVisibleValidationMessage();
518 }
519
520 bool HTMLFormControlElement::isSuccessfulSubmitButton() const
521 {
522     return canBeSuccessfulSubmitButton() && !isDisabledFormControl();
523 }
524
525 bool HTMLFormControlElement::isDefaultButtonForForm() const
526 {
527     return isSuccessfulSubmitButton() && form() && form()->defaultButton() == this;
528 }
529
530 HTMLFormControlElement* HTMLFormControlElement::enclosingFormControlElement(Node* node)
531 {
532     if (!node)
533         return 0;
534     return Traversal<HTMLFormControlElement>::firstAncestorOrSelf(*node);
535 }
536
537 String HTMLFormControlElement::nameForAutofill() const
538 {
539     String fullName = name();
540     String trimmedName = fullName.stripWhiteSpace();
541     if (!trimmedName.isEmpty())
542         return trimmedName;
543     fullName = getIdAttribute();
544     trimmedName = fullName.stripWhiteSpace();
545     return trimmedName;
546 }
547
548 void HTMLFormControlElement::setFocus(bool flag)
549 {
550     LabelableElement::setFocus(flag);
551
552     if (!flag && wasChangedSinceLastFormControlChangeEvent())
553         dispatchFormControlChangeEvent();
554 }
555
556 } // namespace Webcore