0565035ad51cca3ff96ec03009876837e6be341f
[WebKit.git] / WebCore / html / ValidityState.cpp
1 /*
2  * This file is part of the WebKit project.
3  *
4  * Copyright (C) 2009 Michelangelo De Simone <micdesim@gmail.com>
5  * Copyright (C) 2010 Apple Inc. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23
24 #include "config.h"
25 #include "ValidityState.h"
26
27 #include "HTMLInputElement.h"
28 #include "HTMLNames.h"
29 #include "HTMLSelectElement.h"
30 #include "HTMLTextAreaElement.h"
31 #include "HTMLTreeBuilder.h"
32 #include "LocalizedStrings.h"
33 #include <wtf/StdLibExtras.h>
34
35 namespace WebCore {
36
37 using namespace HTMLNames;
38
39 String ValidityState::validationMessage() const
40 {
41     if (!toHTMLElement(m_control)->willValidate())
42         return String();
43
44     if (customError())
45         return m_customErrorMessage;
46     HTMLElement* element = toHTMLElement(m_control);
47     bool isInputElement = element->isFormControlElement() && element->hasTagName(inputTag);
48     bool isTextAreaElement = element->isFormControlElement() && element->hasTagName(textareaTag);
49     // The order of the following checks is meaningful. e.g. We'd like to show the
50     // valueMissing message even if the control has other validation errors.
51     if (valueMissing()) {
52         if (element->hasTagName(selectTag))
53             return validationMessageValueMissingForSelectText();
54         if (isInputElement)
55             return static_cast<HTMLInputElement*>(element)->valueMissingText();
56         return validationMessageValueMissingText();
57     }
58     if (typeMismatch()) {
59         if (isInputElement)
60             return static_cast<HTMLInputElement*>(element)->typeMismatchText();
61         return validationMessageTypeMismatchText();
62     }
63     if (patternMismatch())
64         return validationMessagePatternMismatchText();
65     if (tooLong()) {
66         if (!isInputElement && !isTextAreaElement) {
67             ASSERT_NOT_REACHED();
68             return String();
69         }
70         HTMLTextFormControlElement* text = static_cast<HTMLTextFormControlElement*>(element);
71         return validationMessageTooLongText(numGraphemeClusters(text->value()), text->maxLength());
72     }
73     if (rangeUnderflow()) {
74         if (!isInputElement) {
75             ASSERT_NOT_REACHED();
76             return String();
77         }
78         return validationMessageRangeUnderflowText(static_cast<HTMLInputElement*>(element)->minimumString());
79     }
80     if (rangeOverflow()) {
81         if (!isInputElement) {
82             ASSERT_NOT_REACHED();
83             return String();
84         }
85         return validationMessageRangeOverflowText(static_cast<HTMLInputElement*>(element)->maximumString());
86     }
87     if (stepMismatch()) {
88         if (!isInputElement) {
89             ASSERT_NOT_REACHED();
90             return String();
91         }
92         HTMLInputElement* input = static_cast<HTMLInputElement*>(element);
93         return validationMessageStepMismatchText(input->stepBaseString(), input->stepString());
94     }
95
96     return String();
97 }
98
99 void ValidityState::setCustomErrorMessage(const String& message)
100 {
101     m_customErrorMessage = message;
102     if (m_control->isFormControlElement())
103         static_cast<HTMLFormControlElement*>(m_control)->setNeedsValidityCheck();
104 }
105
106 bool ValidityState::valueMissing() const
107 {
108     HTMLElement* element = toHTMLElement(m_control);
109     if (!element->willValidate())
110         return false;
111
112     if (element->hasTagName(inputTag)) {
113         HTMLInputElement* input = static_cast<HTMLInputElement*>(element);
114         return input->valueMissing(input->value());
115     }
116     if (element->hasTagName(textareaTag)) {
117         HTMLTextAreaElement* textArea = static_cast<HTMLTextAreaElement*>(element);
118         return textArea->valueMissing(textArea->value());
119     }
120     if (element->hasTagName(selectTag)) {
121         HTMLSelectElement* select = static_cast<HTMLSelectElement*>(element);
122         return select->valueMissing();
123     }
124     return false;
125 }
126
127 bool ValidityState::typeMismatch() const
128 {
129     if (!toHTMLElement(m_control)->hasTagName(inputTag))
130         return false;
131     return static_cast<HTMLInputElement*>(m_control)->typeMismatch();
132 }
133
134 bool ValidityState::patternMismatch() const
135 {
136     if (!toHTMLElement(m_control)->hasTagName(inputTag))
137         return false;
138     HTMLInputElement* input = static_cast<HTMLInputElement*>(m_control);
139     return input->patternMismatch(input->value());
140 }
141
142 bool ValidityState::tooLong() const
143 {
144     if (toHTMLElement(m_control)->hasTagName(inputTag)) {
145         HTMLInputElement* input = static_cast<HTMLInputElement*>(m_control);
146         return input->tooLong(input->value(), HTMLTextFormControlElement::CheckDirtyFlag);
147     }
148     if (toHTMLElement(m_control)->hasTagName(textareaTag)) {
149         HTMLTextAreaElement* textArea = static_cast<HTMLTextAreaElement*>(m_control);
150         return textArea->tooLong(textArea->value(), HTMLTextFormControlElement::CheckDirtyFlag);
151     }
152     return false;
153 }
154
155 bool ValidityState::rangeUnderflow() const
156 {
157     if (!toHTMLElement(m_control)->hasTagName(inputTag))
158         return false;
159     HTMLInputElement* input = static_cast<HTMLInputElement*>(m_control);
160     return input->rangeUnderflow(input->value());
161 }
162
163 bool ValidityState::rangeOverflow() const
164 {
165     if (!toHTMLElement(m_control)->hasTagName(inputTag))
166         return false;
167     HTMLInputElement* input = static_cast<HTMLInputElement*>(m_control);
168     return input->rangeOverflow(input->value());
169 }
170
171 bool ValidityState::stepMismatch() const
172 {
173     if (!toHTMLElement(m_control)->hasTagName(inputTag))
174         return false;
175     HTMLInputElement* input = static_cast<HTMLInputElement*>(m_control);
176     return input->stepMismatch(input->value());
177 }
178
179 bool ValidityState::valid() const
180 {
181     bool someError = typeMismatch() || stepMismatch() || rangeUnderflow() || rangeOverflow()
182         || tooLong() || patternMismatch() || valueMissing() || customError();
183     return !someError;
184 }
185
186 } // namespace