2 * This file is part of the WebKit project.
4 * Copyright (C) 2009 Michelangelo De Simone <micdesim@gmail.com>
5 * Copyright (C) 2010 Google Inc. All rights reserved.
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.
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.
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.
25 #include "EmailInputType.h"
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>
36 static const char emailPattern[] =
37 "[a-z0-9!#$%&'*+/=?^_`{|}~.-]+" // local part
39 "[a-z0-9-]+(\\.[a-z0-9-]+)*"; // domain part
41 static bool isValidEmailAddress(const String& address)
43 int addressLength = address.length();
47 DEFINE_STATIC_LOCAL(const RegularExpression, regExp, (emailPattern, TextCaseInsensitive));
50 int matchOffset = regExp.match(address, 0, &matchLength);
52 return !matchOffset && matchLength == addressLength;
55 PassOwnPtr<InputType> EmailInputType::create(HTMLInputElement* element)
57 return adoptPtr(new EmailInputType(element));
60 const AtomicString& EmailInputType::formControlType() const
62 return InputTypeNames::email();
65 bool EmailInputType::typeMismatchFor(const String& value) const
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])))
80 bool EmailInputType::typeMismatch() const
82 return typeMismatchFor(element()->value());
85 String EmailInputType::typeMismatchText() const
87 return element()->multiple() ? validationMessageTypeMismatchForMultipleEmailText() : validationMessageTypeMismatchForEmailText();
90 bool EmailInputType::isEmailField() const
95 String EmailInputType::sanitizeValue(const String& proposedValue)
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) {
105 strippedValue.append(",");
106 strippedValue.append(stripLeadingAndTrailingHTMLSpaces(addresses[i]));
108 return strippedValue.toString();
111 } // namespace WebCore