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 "LocalizedStrings.h"
29 #include "RegularExpression.h"
30 #include <wtf/PassOwnPtr.h>
31 
32 namespace WebCore {
33 
34 static const char emailPattern[] =
35     "[a-z0-9!#$%&'*+/=?^_`{|}~.-]+" // local part
36     "@"
37     "[a-z0-9-]+(\\.[a-z0-9-]+)+"; // domain part
38 
isValidEmailAddress(const String & address)39 static bool isValidEmailAddress(const String& address)
40 {
41     int addressLength = address.length();
42     if (!addressLength)
43         return false;
44 
45     DEFINE_STATIC_LOCAL(const RegularExpression, regExp, (emailPattern, TextCaseInsensitive));
46 
47     int matchLength;
48     int matchOffset = regExp.match(address, 0, &matchLength);
49 
50     return !matchOffset && matchLength == addressLength;
51 }
52 
create(HTMLInputElement * element)53 PassOwnPtr<InputType> EmailInputType::create(HTMLInputElement* element)
54 {
55     return adoptPtr(new EmailInputType(element));
56 }
57 
formControlType() const58 const AtomicString& EmailInputType::formControlType() const
59 {
60     return InputTypeNames::email();
61 }
62 
typeMismatchFor(const String & value) const63 bool EmailInputType::typeMismatchFor(const String& value) const
64 {
65     if (value.isEmpty())
66         return false;
67     if (!element()->multiple())
68         return !isValidEmailAddress(value);
69     Vector<String> addresses;
70     value.split(',', addresses);
71     for (unsigned i = 0; i < addresses.size(); ++i) {
72         if (!isValidEmailAddress(addresses[i]))
73             return true;
74     }
75     return false;
76 }
77 
typeMismatch() const78 bool EmailInputType::typeMismatch() const
79 {
80     return typeMismatchFor(element()->value());
81 }
82 
typeMismatchText() const83 String EmailInputType::typeMismatchText() const
84 {
85     return element()->multiple() ? validationMessageTypeMismatchForMultipleEmailText() : validationMessageTypeMismatchForEmailText();
86 }
87 
isEmailField() const88 bool EmailInputType::isEmailField() const
89 {
90     return true;
91 }
92 
93 } // namespace WebCore
94