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 "InputTypeNames.h"
30
#include "LocalizedStrings.h"
31
#include "RegularExpression.h"
32
#include <wtf/PassOwnPtr.h>
33
#include <wtf/text/StringBuilder.h>
37
static const char emailPattern[] =
38
"[a-z0-9!#$%&'*+/=?^_`{|}~.-]+" // local part
40
"[a-z0-9-]+(\\.[a-z0-9-]+)*"; // domain part
42
static bool isValidEmailAddress(const String& address)
44
int addressLength = address.length();
48
DEFINE_STATIC_LOCAL(const RegularExpression, regExp, (emailPattern, TextCaseInsensitive));
51
int matchOffset = regExp.match(address, 0, &matchLength);
53
return !matchOffset && matchLength == addressLength;
56
PassOwnPtr<InputType> EmailInputType::create(HTMLInputElement* element)
58
return adoptPtr(new EmailInputType(element));
61
const AtomicString& EmailInputType::formControlType() const
63
return InputTypeNames::email();
66
bool EmailInputType::typeMismatchFor(const String& value) const
70
if (!element()->multiple())
71
return !isValidEmailAddress(value);
72
Vector<String> addresses;
73
value.split(',', true, addresses);
74
for (unsigned i = 0; i < addresses.size(); ++i) {
75
if (!isValidEmailAddress(stripLeadingAndTrailingHTMLSpaces(addresses[i])))
81
bool EmailInputType::typeMismatch() const
83
return typeMismatchFor(element()->value());
86
String EmailInputType::typeMismatchText() const
88
return element()->multiple() ? validationMessageTypeMismatchForMultipleEmailText() : validationMessageTypeMismatchForEmailText();
91
bool EmailInputType::isEmailField() const
96
bool EmailInputType::supportsSelectionAPI() const
101
String EmailInputType::sanitizeValue(const String& proposedValue) const
103
String noLineBreakValue = proposedValue.removeCharacters(isHTMLLineBreak);
104
if (!element()->multiple())
105
return stripLeadingAndTrailingHTMLSpaces(noLineBreakValue);
106
Vector<String> addresses;
107
noLineBreakValue.split(',', true, addresses);
108
StringBuilder strippedValue;
109
for (unsigned i = 0; i < addresses.size(); ++i) {
111
strippedValue.append(",");
112
strippedValue.append(stripLeadingAndTrailingHTMLSpaces(addresses[i]));
114
return strippedValue.toString();
117
} // namespace WebCore