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