~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/html/EmailInputType.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "RegularExpression.h"
 
32
#include <wtf/PassOwnPtr.h>
 
33
#include <wtf/text/StringBuilder.h>
 
34
 
 
35
namespace WebCore {
 
36
 
 
37
static const char emailPattern[] =
 
38
    "[a-z0-9!#$%&'*+/=?^_`{|}~.-]+" // local part
 
39
    "@"
 
40
    "[a-z0-9-]+(\\.[a-z0-9-]+)*"; // domain part
 
41
 
 
42
static bool isValidEmailAddress(const String& address)
 
43
{
 
44
    int addressLength = address.length();
 
45
    if (!addressLength)
 
46
        return false;
 
47
 
 
48
    DEFINE_STATIC_LOCAL(const RegularExpression, regExp, (emailPattern, TextCaseInsensitive));
 
49
 
 
50
    int matchLength;
 
51
    int matchOffset = regExp.match(address, 0, &matchLength);
 
52
 
 
53
    return !matchOffset && matchLength == addressLength;
 
54
}
 
55
 
 
56
PassOwnPtr<InputType> EmailInputType::create(HTMLInputElement* element)
 
57
{
 
58
    return adoptPtr(new EmailInputType(element));
 
59
}
 
60
 
 
61
const AtomicString& EmailInputType::formControlType() const
 
62
{
 
63
    return InputTypeNames::email();
 
64
}
 
65
 
 
66
bool EmailInputType::typeMismatchFor(const String& value) const
 
67
{
 
68
    if (value.isEmpty())
 
69
        return false;
 
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])))
 
76
            return true;
 
77
    }
 
78
    return false;
 
79
}
 
80
 
 
81
bool EmailInputType::typeMismatch() const
 
82
{
 
83
    return typeMismatchFor(element()->value());
 
84
}
 
85
 
 
86
String EmailInputType::typeMismatchText() const
 
87
{
 
88
    return element()->multiple() ? validationMessageTypeMismatchForMultipleEmailText() : validationMessageTypeMismatchForEmailText();
 
89
}
 
90
 
 
91
bool EmailInputType::isEmailField() const
 
92
{
 
93
    return true;
 
94
}
 
95
 
 
96
bool EmailInputType::supportsSelectionAPI() const
 
97
{
 
98
    return false;
 
99
}
 
100
 
 
101
String EmailInputType::sanitizeValue(const String& proposedValue) const
 
102
{
 
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) {
 
110
        if (i > 0)
 
111
            strippedValue.append(",");
 
112
        strippedValue.append(stripLeadingAndTrailingHTMLSpaces(addresses[i]));
 
113
    }
 
114
    return strippedValue.toString();
 
115
}
 
116
 
 
117
} // namespace WebCore