1967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch/* 2a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * This file is part of the WebKit project. 3a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * 4a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * Copyright (C) 2009 Michelangelo De Simone <micdesim@gmail.com> 5967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch * Copyright (C) 2010 Google Inc. All rights reserved. 6967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch * 7a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * This library is free software; you can redistribute it and/or 8a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * modify it under the terms of the GNU Library General Public 9a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * License as published by the Free Software Foundation; either 10a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * version 2 of the License, or (at your option) any later version. 11a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * 12a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * This library is distributed in the hope that it will be useful, 13a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * but WITHOUT ANY WARRANTY; without even the implied warranty of 14a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * Library General Public License for more details. 16967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch * 17a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * You should have received a copy of the GNU Library General Public License 18a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * along with this library; see the file COPYING.LIB. If not, write to 19a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 20a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * Boston, MA 02110-1301, USA. 21967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch * 22967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch */ 23967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch 24967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch#include "config.h" 25bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen#include "EmailInputType.h" 26967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch 27a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#include "HTMLInputElement.h" 286b70adc33054f8aee8c54d0f460458a9df11b8a5Russell Brenner#include "LocalizedStrings.h" 29a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#include "RegularExpression.h" 30bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen#include <wtf/PassOwnPtr.h> 31967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch 32967717af5423377c967781471ee106e2bb4e11c8Ben Murdochnamespace WebCore { 33967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch 34a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochstatic const char emailPattern[] = 35a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch "[a-z0-9!#$%&'*+/=?^_`{|}~.-]+" // local part 36a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch "@" 37a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch "[a-z0-9-]+(\\.[a-z0-9-]+)+"; // domain part 38a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch 39a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochstatic bool isValidEmailAddress(const String& address) 40a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{ 41a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch int addressLength = address.length(); 42a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch if (!addressLength) 43a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch return false; 44a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch 45a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch DEFINE_STATIC_LOCAL(const RegularExpression, regExp, (emailPattern, TextCaseInsensitive)); 46a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch 47a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch int matchLength; 48a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch int matchOffset = regExp.match(address, 0, &matchLength); 49a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch 50a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch return !matchOffset && matchLength == addressLength; 51a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch} 52a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch 53bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian MonsenPassOwnPtr<InputType> EmailInputType::create(HTMLInputElement* element) 54967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch{ 55bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen return adoptPtr(new EmailInputType(element)); 56967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch} 57967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch 58bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsenconst AtomicString& EmailInputType::formControlType() const 59967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch{ 60bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen return InputTypeNames::email(); 61967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch} 62967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch 63a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochbool EmailInputType::typeMismatchFor(const String& value) const 64a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{ 65a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch if (value.isEmpty()) 66a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch return false; 67a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch if (!element()->multiple()) 68a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch return !isValidEmailAddress(value); 69a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch Vector<String> addresses; 70a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch value.split(',', addresses); 71a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch for (unsigned i = 0; i < addresses.size(); ++i) { 72a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch if (!isValidEmailAddress(addresses[i])) 73a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch return true; 74a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch } 75a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch return false; 76a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch} 77a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch 78a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochbool EmailInputType::typeMismatch() const 79a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{ 80a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch return typeMismatchFor(element()->value()); 81a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch} 82a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch 836b70adc33054f8aee8c54d0f460458a9df11b8a5Russell BrennerString EmailInputType::typeMismatchText() const 846b70adc33054f8aee8c54d0f460458a9df11b8a5Russell Brenner{ 856b70adc33054f8aee8c54d0f460458a9df11b8a5Russell Brenner return element()->multiple() ? validationMessageTypeMismatchForMultipleEmailText() : validationMessageTypeMismatchForEmailText(); 866b70adc33054f8aee8c54d0f460458a9df11b8a5Russell Brenner} 876b70adc33054f8aee8c54d0f460458a9df11b8a5Russell Brenner 88cad810f21b803229eb11403f9209855525a25d57Steve Blockbool EmailInputType::isEmailField() const 89cad810f21b803229eb11403f9209855525a25d57Steve Block{ 90cad810f21b803229eb11403f9209855525a25d57Steve Block return true; 91cad810f21b803229eb11403f9209855525a25d57Steve Block} 92cad810f21b803229eb11403f9209855525a25d57Steve Block 93967717af5423377c967781471ee106e2bb4e11c8Ben Murdoch} // namespace WebCore 94