initial import
[vuplus_webkit] / Source / WebCore / html / EmailInputType.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 Google 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 "EmailInputType.h"
26
27 #include "HTMLInputElement.h"
28 #include "HTMLParserIdioms.h"
29 #include "LocalizedStrings.h"
30 #include "RegularExpression.h"
31 #include <wtf/PassOwnPtr.h>
32 #include <wtf/text/StringBuilder.h>
33
34 namespace WebCore {
35
36 static const char emailPattern[] =
37     "[a-z0-9!#$%&'*+/=?^_`{|}~.-]+" // local part
38     "@"
39     "[a-z0-9-]+(\\.[a-z0-9-]+)*"; // domain part
40
41 static bool isValidEmailAddress(const String& address)
42 {
43     int addressLength = address.length();
44     if (!addressLength)
45         return false;
46
47     DEFINE_STATIC_LOCAL(const RegularExpression, regExp, (emailPattern, TextCaseInsensitive));
48
49     int matchLength;
50     int matchOffset = regExp.match(address, 0, &matchLength);
51
52     return !matchOffset && matchLength == addressLength;
53 }
54
55 PassOwnPtr<InputType> EmailInputType::create(HTMLInputElement* element)
56 {
57     return adoptPtr(new EmailInputType(element));
58 }
59
60 const AtomicString& EmailInputType::formControlType() const
61 {
62     return InputTypeNames::email();
63 }
64
65 bool EmailInputType::typeMismatchFor(const String& value) const
66 {
67     if (value.isEmpty())
68         return false;
69     if (!element()->multiple())
70         return !isValidEmailAddress(value);
71     Vector<String> addresses;
72     value.split(',', true, addresses);
73     for (unsigned i = 0; i < addresses.size(); ++i) {
74         if (!isValidEmailAddress(stripLeadingAndTrailingHTMLSpaces(addresses[i])))
75             return true;
76     }
77     return false;
78 }
79
80 bool EmailInputType::typeMismatch() const
81 {
82     return typeMismatchFor(element()->value());
83 }
84
85 String EmailInputType::typeMismatchText() const
86 {
87     return element()->multiple() ? validationMessageTypeMismatchForMultipleEmailText() : validationMessageTypeMismatchForEmailText();
88 }
89
90 bool EmailInputType::isEmailField() const
91 {
92     return true;
93 }
94
95 String EmailInputType::sanitizeValue(const String& proposedValue)
96 {
97     String noLineBreakValue = proposedValue.removeCharacters(isHTMLLineBreak);
98     if (!element()->multiple())
99         return noLineBreakValue;
100     Vector<String> addresses;
101     noLineBreakValue.split(',', true, addresses);
102     StringBuilder strippedValue;
103     for (unsigned i = 0; i < addresses.size(); ++i) {
104         if (i > 0)
105             strippedValue.append(",");
106         strippedValue.append(stripLeadingAndTrailingHTMLSpaces(addresses[i]));
107     }
108     return strippedValue.toString();
109 }
110
111 } // namespace WebCore