| 1 |
// Copyright 2014 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Provides functions to parse and manipulate internationalized |
| 17 |
* email addresses. This is useful in the context of Email Address |
| 18 |
* Internationalization (EAI) as defined by RFC6530. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
goog.provide('goog.format.InternationalizedEmailAddress'); |
| 23 |
|
| 24 |
goog.require('goog.format.EmailAddress'); |
| 25 |
|
| 26 |
goog.require('goog.string'); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* Formats an email address string for display, and allows for extraction of |
| 32 |
* the individual components of the address. |
| 33 |
* @param {string=} opt_address The email address. |
| 34 |
* @param {string=} opt_name The name associated with the email address. |
| 35 |
* @constructor |
| 36 |
* @extends {goog.format.EmailAddress} |
| 37 |
*/ |
| 38 |
goog.format.InternationalizedEmailAddress = function(opt_address, opt_name) { |
| 39 |
goog.format.InternationalizedEmailAddress.base( |
| 40 |
this, 'constructor', opt_address, opt_name); |
| 41 |
}; |
| 42 |
goog.inherits( |
| 43 |
goog.format.InternationalizedEmailAddress, goog.format.EmailAddress); |
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* A string representing the RegExp for the local part of an EAI email address. |
| 48 |
* @private |
| 49 |
*/ |
| 50 |
goog.format.InternationalizedEmailAddress.EAI_LOCAL_PART_REGEXP_STR_ = |
| 51 |
'((?!\\s)[+a-zA-Z0-9_.!#$%&\'*\\/=?^`{|}~\u0080-\uFFFFFF-])+'; |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* A string representing the RegExp for a label in the domain part of an EAI |
| 56 |
* email address. |
| 57 |
* @private |
| 58 |
*/ |
| 59 |
goog.format.InternationalizedEmailAddress.EAI_LABEL_CHAR_REGEXP_STR_ = |
| 60 |
'(?!\\s)[a-zA-Z0-9\u0080-\u3001\u3003-\uFF0D\uFF0F-\uFF60\uFF62-\uFFFFFF-]'; |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* A string representing the RegExp for the domain part of an EAI email address. |
| 65 |
* @private |
| 66 |
*/ |
| 67 |
goog.format.InternationalizedEmailAddress.EAI_DOMAIN_PART_REGEXP_STR_ = |
| 68 |
// A unicode character (ASCII or Unicode excluding periods) |
| 69 |
'(' + goog.format.InternationalizedEmailAddress.EAI_LABEL_CHAR_REGEXP_STR_ + |
| 70 |
// Such character 1+ times, followed by a Unicode period. All 1+ times. |
| 71 |
'+[\\.\\uFF0E\\u3002\\uFF61])+' + |
| 72 |
// And same thing but without a period in the end |
| 73 |
goog.format.InternationalizedEmailAddress.EAI_LABEL_CHAR_REGEXP_STR_ + |
| 74 |
'{2,63}'; |
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* Match string for address separators. This list is the result of the |
| 79 |
* discussion in b/16241003. |
| 80 |
* @type {string} |
| 81 |
* @private |
| 82 |
*/ |
| 83 |
goog.format.InternationalizedEmailAddress.ADDRESS_SEPARATORS_ = |
| 84 |
',' + // U+002C ( , ) COMMA |
| 85 |
';' + // U+003B ( ; ) SEMICOLON |
| 86 |
'\u055D' + // ( ՝ ) ARMENIAN COMMA |
| 87 |
'\u060C' + // ( ، ) ARABIC COMMA |
| 88 |
'\u1363' + // ( ፣ ) ETHIOPIC COMMA |
| 89 |
'\u1802' + // ( ᠂ ) MONGOLIAN COMMA |
| 90 |
'\u1808' + // ( ᠈ ) MONGOLIAN MANCHU COMMA |
| 91 |
'\u2E41' + // ( ⹁ ) REVERSED COMMA |
| 92 |
'\u3001' + // ( 、 ) IDEOGRAPHIC COMMA |
| 93 |
'\uFF0C' + // ( , ) FULLWIDTH COMMA |
| 94 |
'\u061B' + // ( ؛ ) ARABIC SEMICOLON |
| 95 |
'\u1364' + // ( ፤ ) ETHIOPIC SEMICOLON |
| 96 |
'\uFF1B' + // ( ; ) FULLWIDTH SEMICOLON |
| 97 |
'\uFF64' + // ( 、 ) HALFWIDTH IDEOGRAPHIC COMMA |
| 98 |
'\u104A'; // ( ၊ ) MYANMAR SIGN LITTLE SECTION |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* Match string for characters that, when in a display name, require it to be |
| 103 |
* quoted. |
| 104 |
* @type {string} |
| 105 |
* @private |
| 106 |
*/ |
| 107 |
goog.format.InternationalizedEmailAddress.CHARS_REQUIRE_QUOTES_ = |
| 108 |
goog.format.EmailAddress.SPECIAL_CHARS + |
| 109 |
goog.format.InternationalizedEmailAddress.ADDRESS_SEPARATORS_; |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* A RegExp to match the local part of an EAI email address. |
| 114 |
* @private {!RegExp} |
| 115 |
*/ |
| 116 |
goog.format.InternationalizedEmailAddress.EAI_LOCAL_PART_ = new RegExp( |
| 117 |
'^' + goog.format.InternationalizedEmailAddress.EAI_LOCAL_PART_REGEXP_STR_ + |
| 118 |
'$'); |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* A RegExp to match the domain part of an EAI email address. |
| 123 |
* @private {!RegExp} |
| 124 |
*/ |
| 125 |
goog.format.InternationalizedEmailAddress.EAI_DOMAIN_PART_ = new RegExp( |
| 126 |
'^' + |
| 127 |
goog.format.InternationalizedEmailAddress.EAI_DOMAIN_PART_REGEXP_STR_ + |
| 128 |
'$'); |
| 129 |
|
| 130 |
|
| 131 |
/** |
| 132 |
* A RegExp to match an EAI email address. |
| 133 |
* @private {!RegExp} |
| 134 |
*/ |
| 135 |
goog.format.InternationalizedEmailAddress.EAI_EMAIL_ADDRESS_ = new RegExp( |
| 136 |
'^' + goog.format.InternationalizedEmailAddress.EAI_LOCAL_PART_REGEXP_STR_ + |
| 137 |
'@' + |
| 138 |
goog.format.InternationalizedEmailAddress.EAI_DOMAIN_PART_REGEXP_STR_ + |
| 139 |
'$'); |
| 140 |
|
| 141 |
|
| 142 |
/** |
| 143 |
* Checks if the provided string is a valid local part (part before the '@') of |
| 144 |
* an EAI email address. |
| 145 |
* @param {string} str The local part to check. |
| 146 |
* @return {boolean} Whether the provided string is a valid local part. |
| 147 |
*/ |
| 148 |
goog.format.InternationalizedEmailAddress.isValidLocalPartSpec = function(str) { |
| 149 |
if (!goog.isDefAndNotNull(str)) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
return goog.format.InternationalizedEmailAddress.EAI_LOCAL_PART_.test(str); |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
/** |
| 157 |
* Checks if the provided string is a valid domain part (part after the '@') of |
| 158 |
* an EAI email address. |
| 159 |
* @param {string} str The domain part to check. |
| 160 |
* @return {boolean} Whether the provided string is a valid domain part. |
| 161 |
*/ |
| 162 |
goog.format.InternationalizedEmailAddress.isValidDomainPartSpec = function( |
| 163 |
str) { |
| 164 |
if (!goog.isDefAndNotNull(str)) { |
| 165 |
return false; |
| 166 |
} |
| 167 |
return goog.format.InternationalizedEmailAddress.EAI_DOMAIN_PART_.test(str); |
| 168 |
}; |
| 169 |
|
| 170 |
|
| 171 |
/** @override */ |
| 172 |
goog.format.InternationalizedEmailAddress.prototype.isValid = function() { |
| 173 |
return goog.format.InternationalizedEmailAddress.isValidAddrSpec( |
| 174 |
this.address); |
| 175 |
}; |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Checks if the provided string is a valid email address. Supports both |
| 180 |
* simple email addresses (address specs) and addresses that contain display |
| 181 |
* names. |
| 182 |
* @param {string} str The email address to check. |
| 183 |
* @return {boolean} Whether the provided string is a valid address. |
| 184 |
*/ |
| 185 |
goog.format.InternationalizedEmailAddress.isValidAddress = function(str) { |
| 186 |
if (!goog.isDefAndNotNull(str)) { |
| 187 |
return false; |
| 188 |
} |
| 189 |
return goog.format.InternationalizedEmailAddress.parse(str).isValid(); |
| 190 |
}; |
| 191 |
|
| 192 |
|
| 193 |
/** |
| 194 |
* Checks if the provided string is a valid address spec ([email protected]). |
| 195 |
* @param {string} str The email address to check. |
| 196 |
* @return {boolean} Whether the provided string is a valid address spec. |
| 197 |
*/ |
| 198 |
goog.format.InternationalizedEmailAddress.isValidAddrSpec = function(str) { |
| 199 |
if (!goog.isDefAndNotNull(str)) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
// This is a fairly naive implementation, but it covers 99% of use cases. |
| 204 |
// For more details, see http://en.wikipedia.org/wiki/Email_address#Syntax |
| 205 |
return goog.format.InternationalizedEmailAddress.EAI_EMAIL_ADDRESS_.test(str); |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
/** |
| 210 |
* Parses a string containing email addresses of the form |
| 211 |
* "name" <address> into an array of email addresses. |
| 212 |
* @param {string} str The address list. |
| 213 |
* @return {!Array<!goog.format.EmailAddress>} The parsed emails. |
| 214 |
*/ |
| 215 |
goog.format.InternationalizedEmailAddress.parseList = function(str) { |
| 216 |
return goog.format.EmailAddress.parseListInternal( |
| 217 |
str, goog.format.InternationalizedEmailAddress.parse, |
| 218 |
goog.format.InternationalizedEmailAddress.isAddressSeparator); |
| 219 |
}; |
| 220 |
|
| 221 |
|
| 222 |
/** |
| 223 |
* Parses an email address of the form "name" <address> into |
| 224 |
* an email address. |
| 225 |
* @param {string} addr The address string. |
| 226 |
* @return {!goog.format.EmailAddress} The parsed address. |
| 227 |
*/ |
| 228 |
goog.format.InternationalizedEmailAddress.parse = function(addr) { |
| 229 |
return goog.format.EmailAddress.parseInternal( |
| 230 |
addr, goog.format.InternationalizedEmailAddress); |
| 231 |
}; |
| 232 |
|
| 233 |
|
| 234 |
/** |
| 235 |
* @param {string} ch The character to test. |
| 236 |
* @return {boolean} Whether the provided character is an address separator. |
| 237 |
*/ |
| 238 |
goog.format.InternationalizedEmailAddress.isAddressSeparator = function(ch) { |
| 239 |
return goog.string.contains( |
| 240 |
goog.format.InternationalizedEmailAddress.ADDRESS_SEPARATORS_, ch); |
| 241 |
}; |
| 242 |
|
| 243 |
|
| 244 |
/** |
| 245 |
* Return the address in a standard format: |
| 246 |
* - remove extra spaces. |
| 247 |
* - Surround name with quotes if it contains special characters. |
| 248 |
* @return {string} The cleaned address. |
| 249 |
* @override |
| 250 |
*/ |
| 251 |
goog.format.InternationalizedEmailAddress.prototype.toString = function() { |
| 252 |
return this.toStringInternal( |
| 253 |
goog.format.InternationalizedEmailAddress.CHARS_REQUIRE_QUOTES_); |
| 254 |
}; |