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 | * Copyright (C) 2018 Apple Inc. All rights reserved. |
7 | * |
8 | * This library is free software; you can redistribute it and/or |
9 | * modify it under the terms of the GNU Library General Public |
10 | * License as published by the Free Software Foundation; either |
11 | * version 2 of the License, or (at your option) any later version. |
12 | * |
13 | * This library is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | * Library General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU Library General Public License |
19 | * along with this library; see the file COPYING.LIB. If not, write to |
20 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | * Boston, MA 02110-1301, USA. |
22 | * |
23 | */ |
24 | |
25 | #include "config.h" |
26 | #include "EmailInputType.h" |
27 | |
28 | #include "HTMLInputElement.h" |
29 | #include "HTMLParserIdioms.h" |
30 | #include "InputTypeNames.h" |
31 | #include "LocalizedStrings.h" |
32 | #include <JavaScriptCore/RegularExpression.h> |
33 | #include <wtf/NeverDestroyed.h> |
34 | #include <wtf/text/StringBuilder.h> |
35 | |
36 | namespace WebCore { |
37 | |
38 | // From https://html.spec.whatwg.org/#valid-e-mail-address. |
39 | static const char emailPattern[] = "^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$" ; |
40 | |
41 | static bool isValidEmailAddress(const String& address) |
42 | { |
43 | int addressLength = address.length(); |
44 | if (!addressLength) |
45 | return false; |
46 | |
47 | static NeverDestroyed<const JSC::Yarr::RegularExpression> regExp(emailPattern, JSC::Yarr::TextCaseInsensitive); |
48 | |
49 | int matchLength; |
50 | int matchOffset = regExp.get().match(address, 0, &matchLength); |
51 | |
52 | return !matchOffset && matchLength == addressLength; |
53 | } |
54 | |
55 | const AtomicString& EmailInputType::formControlType() const |
56 | { |
57 | return InputTypeNames::email(); |
58 | } |
59 | |
60 | bool EmailInputType::typeMismatchFor(const String& value) const |
61 | { |
62 | ASSERT(element()); |
63 | if (value.isEmpty()) |
64 | return false; |
65 | if (!element()->multiple()) |
66 | return !isValidEmailAddress(value); |
67 | for (auto& address : value.splitAllowingEmptyEntries(',')) { |
68 | if (!isValidEmailAddress(stripLeadingAndTrailingHTMLSpaces(address))) |
69 | return true; |
70 | } |
71 | return false; |
72 | } |
73 | |
74 | bool EmailInputType::typeMismatch() const |
75 | { |
76 | ASSERT(element()); |
77 | return typeMismatchFor(element()->value()); |
78 | } |
79 | |
80 | String EmailInputType::typeMismatchText() const |
81 | { |
82 | ASSERT(element()); |
83 | return element()->multiple() ? validationMessageTypeMismatchForMultipleEmailText() : validationMessageTypeMismatchForEmailText(); |
84 | } |
85 | |
86 | bool EmailInputType::isEmailField() const |
87 | { |
88 | return true; |
89 | } |
90 | |
91 | bool EmailInputType::supportsSelectionAPI() const |
92 | { |
93 | return false; |
94 | } |
95 | |
96 | String EmailInputType::sanitizeValue(const String& proposedValue) const |
97 | { |
98 | String noLineBreakValue = proposedValue.removeCharacters(isHTMLLineBreak); |
99 | ASSERT(element()); |
100 | if (!element()->multiple()) |
101 | return stripLeadingAndTrailingHTMLSpaces(noLineBreakValue); |
102 | Vector<String> addresses = noLineBreakValue.splitAllowingEmptyEntries(','); |
103 | StringBuilder strippedValue; |
104 | for (unsigned i = 0; i < addresses.size(); ++i) { |
105 | if (i > 0) |
106 | strippedValue.append(','); |
107 | strippedValue.append(stripLeadingAndTrailingHTMLSpaces(addresses[i])); |
108 | } |
109 | return strippedValue.toString(); |
110 | } |
111 | |
112 | } // namespace WebCore |
113 | |