Path: blob/trunk/third_party/closure/goog/format/internationalizedemailaddress.js
2868 views
// Copyright 2014 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Provides functions to parse and manipulate internationalized16* email addresses. This is useful in the context of Email Address17* Internationalization (EAI) as defined by RFC6530.18*19*/2021goog.provide('goog.format.InternationalizedEmailAddress');2223goog.require('goog.format.EmailAddress');2425goog.require('goog.string');26272829/**30* Formats an email address string for display, and allows for extraction of31* the individual components of the address.32* @param {string=} opt_address The email address.33* @param {string=} opt_name The name associated with the email address.34* @constructor35* @extends {goog.format.EmailAddress}36*/37goog.format.InternationalizedEmailAddress = function(opt_address, opt_name) {38goog.format.InternationalizedEmailAddress.base(39this, 'constructor', opt_address, opt_name);40};41goog.inherits(42goog.format.InternationalizedEmailAddress, goog.format.EmailAddress);434445/**46* A string representing the RegExp for the local part of an EAI email address.47* @private48*/49goog.format.InternationalizedEmailAddress.EAI_LOCAL_PART_REGEXP_STR_ =50'((?!\\s)[+a-zA-Z0-9_.!#$%&\'*\\/=?^`{|}~\u0080-\uFFFFFF-])+';515253/**54* A string representing the RegExp for a label in the domain part of an EAI55* email address.56* @private57*/58goog.format.InternationalizedEmailAddress.EAI_LABEL_CHAR_REGEXP_STR_ =59'(?!\\s)[a-zA-Z0-9\u0080-\u3001\u3003-\uFF0D\uFF0F-\uFF60\uFF62-\uFFFFFF-]';606162/**63* A string representing the RegExp for the domain part of an EAI email address.64* @private65*/66goog.format.InternationalizedEmailAddress.EAI_DOMAIN_PART_REGEXP_STR_ =67// A unicode character (ASCII or Unicode excluding periods)68'(' + goog.format.InternationalizedEmailAddress.EAI_LABEL_CHAR_REGEXP_STR_ +69// Such character 1+ times, followed by a Unicode period. All 1+ times.70'+[\\.\\uFF0E\\u3002\\uFF61])+' +71// And same thing but without a period in the end72goog.format.InternationalizedEmailAddress.EAI_LABEL_CHAR_REGEXP_STR_ +73'{2,63}';747576/**77* Match string for address separators. This list is the result of the78* discussion in b/16241003.79* @type {string}80* @private81*/82goog.format.InternationalizedEmailAddress.ADDRESS_SEPARATORS_ =83',' + // U+002C ( , ) COMMA84';' + // U+003B ( ; ) SEMICOLON85'\u055D' + // ( ՝ ) ARMENIAN COMMA86'\u060C' + // ( ، ) ARABIC COMMA87'\u1363' + // ( ፣ ) ETHIOPIC COMMA88'\u1802' + // ( ᠂ ) MONGOLIAN COMMA89'\u1808' + // ( ᠈ ) MONGOLIAN MANCHU COMMA90'\u2E41' + // ( ⹁ ) REVERSED COMMA91'\u3001' + // ( 、 ) IDEOGRAPHIC COMMA92'\uFF0C' + // ( , ) FULLWIDTH COMMA93'\u061B' + // ( ؛ ) ARABIC SEMICOLON94'\u1364' + // ( ፤ ) ETHIOPIC SEMICOLON95'\uFF1B' + // ( ; ) FULLWIDTH SEMICOLON96'\uFF64' + // ( 、 ) HALFWIDTH IDEOGRAPHIC COMMA97'\u104A'; // ( ၊ ) MYANMAR SIGN LITTLE SECTION9899100/**101* Match string for characters that, when in a display name, require it to be102* quoted.103* @type {string}104* @private105*/106goog.format.InternationalizedEmailAddress.CHARS_REQUIRE_QUOTES_ =107goog.format.EmailAddress.SPECIAL_CHARS +108goog.format.InternationalizedEmailAddress.ADDRESS_SEPARATORS_;109110111/**112* A RegExp to match the local part of an EAI email address.113* @private {!RegExp}114*/115goog.format.InternationalizedEmailAddress.EAI_LOCAL_PART_ = new RegExp(116'^' + goog.format.InternationalizedEmailAddress.EAI_LOCAL_PART_REGEXP_STR_ +117'$');118119120/**121* A RegExp to match the domain part of an EAI email address.122* @private {!RegExp}123*/124goog.format.InternationalizedEmailAddress.EAI_DOMAIN_PART_ = new RegExp(125'^' +126goog.format.InternationalizedEmailAddress.EAI_DOMAIN_PART_REGEXP_STR_ +127'$');128129130/**131* A RegExp to match an EAI email address.132* @private {!RegExp}133*/134goog.format.InternationalizedEmailAddress.EAI_EMAIL_ADDRESS_ = new RegExp(135'^' + goog.format.InternationalizedEmailAddress.EAI_LOCAL_PART_REGEXP_STR_ +136'@' +137goog.format.InternationalizedEmailAddress.EAI_DOMAIN_PART_REGEXP_STR_ +138'$');139140141/**142* Checks if the provided string is a valid local part (part before the '@') of143* an EAI email address.144* @param {string} str The local part to check.145* @return {boolean} Whether the provided string is a valid local part.146*/147goog.format.InternationalizedEmailAddress.isValidLocalPartSpec = function(str) {148if (!goog.isDefAndNotNull(str)) {149return false;150}151return goog.format.InternationalizedEmailAddress.EAI_LOCAL_PART_.test(str);152};153154155/**156* Checks if the provided string is a valid domain part (part after the '@') of157* an EAI email address.158* @param {string} str The domain part to check.159* @return {boolean} Whether the provided string is a valid domain part.160*/161goog.format.InternationalizedEmailAddress.isValidDomainPartSpec = function(162str) {163if (!goog.isDefAndNotNull(str)) {164return false;165}166return goog.format.InternationalizedEmailAddress.EAI_DOMAIN_PART_.test(str);167};168169170/** @override */171goog.format.InternationalizedEmailAddress.prototype.isValid = function() {172return goog.format.InternationalizedEmailAddress.isValidAddrSpec(173this.address);174};175176177/**178* Checks if the provided string is a valid email address. Supports both179* simple email addresses (address specs) and addresses that contain display180* names.181* @param {string} str The email address to check.182* @return {boolean} Whether the provided string is a valid address.183*/184goog.format.InternationalizedEmailAddress.isValidAddress = function(str) {185if (!goog.isDefAndNotNull(str)) {186return false;187}188return goog.format.InternationalizedEmailAddress.parse(str).isValid();189};190191192/**193* Checks if the provided string is a valid address spec ([email protected]).194* @param {string} str The email address to check.195* @return {boolean} Whether the provided string is a valid address spec.196*/197goog.format.InternationalizedEmailAddress.isValidAddrSpec = function(str) {198if (!goog.isDefAndNotNull(str)) {199return false;200}201202// This is a fairly naive implementation, but it covers 99% of use cases.203// For more details, see http://en.wikipedia.org/wiki/Email_address#Syntax204return goog.format.InternationalizedEmailAddress.EAI_EMAIL_ADDRESS_.test(str);205};206207208/**209* Parses a string containing email addresses of the form210* "name" <address> into an array of email addresses.211* @param {string} str The address list.212* @return {!Array<!goog.format.EmailAddress>} The parsed emails.213*/214goog.format.InternationalizedEmailAddress.parseList = function(str) {215return goog.format.EmailAddress.parseListInternal(216str, goog.format.InternationalizedEmailAddress.parse,217goog.format.InternationalizedEmailAddress.isAddressSeparator);218};219220221/**222* Parses an email address of the form "name" <address> into223* an email address.224* @param {string} addr The address string.225* @return {!goog.format.EmailAddress} The parsed address.226*/227goog.format.InternationalizedEmailAddress.parse = function(addr) {228return goog.format.EmailAddress.parseInternal(229addr, goog.format.InternationalizedEmailAddress);230};231232233/**234* @param {string} ch The character to test.235* @return {boolean} Whether the provided character is an address separator.236*/237goog.format.InternationalizedEmailAddress.isAddressSeparator = function(ch) {238return goog.string.contains(239goog.format.InternationalizedEmailAddress.ADDRESS_SEPARATORS_, ch);240};241242243/**244* Return the address in a standard format:245* - remove extra spaces.246* - Surround name with quotes if it contains special characters.247* @return {string} The cleaned address.248* @override249*/250goog.format.InternationalizedEmailAddress.prototype.toString = function() {251return this.toStringInternal(252goog.format.InternationalizedEmailAddress.CHARS_REQUIRE_QUOTES_);253};254255256