Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
RELNOTES[INC]: goog.format.EmailAddress.parse now ignores unicode bid…
Browse files Browse the repository at this point in the history
…i markers

PiperOrigin-RevId: 331777392
  • Loading branch information
Closure Team authored and 12wrigja committed Sep 17, 2020
1 parent 4c3621c commit ac1c88d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion closure/goog/deps.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 23 additions & 4 deletions closure/goog/format/emailaddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

goog.provide('goog.format.EmailAddress');

goog.require('goog.i18n.bidi');
goog.require('goog.object');
goog.require('goog.string');



/**
* Formats an email address string for display, and allows for extraction of
* the individual components of the address.
Expand Down Expand Up @@ -133,7 +133,6 @@ goog.format.EmailAddress.LOCAL_PART_REGEXP_STR_ =
goog.format.EmailAddress.DOMAIN_PART_REGEXP_STR_ =
'([a-zA-Z0-9-]+\\.)+[a-zA-Z0-9]{2,63}';


/**
* A RegExp to match the local part of an email address.
* @private {!RegExp}
Expand All @@ -158,6 +157,14 @@ goog.format.EmailAddress.EMAIL_ADDRESS_ = new RegExp(
'^' + goog.format.EmailAddress.LOCAL_PART_REGEXP_STR_ + '@' +
goog.format.EmailAddress.DOMAIN_PART_REGEXP_STR_ + '$');

/**
* Regular expression for bidi format character replacement in text.
* @type {!RegExp}
* @private
*/
goog.format.EmailAddress.ALL_BIDI_FORMAT_CHARS_ = new RegExp(
'[' + goog.object.getValues(goog.i18n.bidi.Format).join('') + ']', 'g');


/**
* Get the name associated with the email address.
Expand Down Expand Up @@ -341,7 +348,7 @@ goog.format.EmailAddress.isValidDomainPartSpec = function(str) {
*/
goog.format.EmailAddress.parseInternal = function(addr, ctor) {
'use strict';
// TODO(ecattell): Strip bidi markers.
addr = goog.format.EmailAddress.stripBidiChars_(addr);
var name = '';
var address = '';
for (var i = 0; i < addr.length;) {
Expand Down Expand Up @@ -504,3 +511,15 @@ goog.format.EmailAddress.isAddressSeparator = function(ch) {
'use strict';
return goog.string.contains(goog.format.EmailAddress.ADDRESS_SEPARATORS_, ch);
};

/**
* Returns the input text without Unicode formatting characters
* and directionality string constants as defined in {@link
* goog.i18n.bidi.Format}.
* @param {string} str The given string.
* @return {string} The given string cleaned of formatting characters.
* @private
*/
goog.format.EmailAddress.stripBidiChars_ = function(str) {
return str.replace(goog.format.EmailAddress.ALL_BIDI_FORMAT_CHARS_, '');
};
32 changes: 22 additions & 10 deletions closure/goog/format/emailaddress_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ goog.module('goog.format.EmailAddressTest');
goog.setTestOnly();

const EmailAddress = goog.require('goog.format.EmailAddress');
const Format = goog.require('goog.i18n.bidi.Format');
const googArray = goog.require('goog.array');
const testSuite = goog.require('goog.testing.testSuite');
const LRM = Format.LRM;
const RLM = Format.RLM;
const LRE = Format.LRE;
const RLE = Format.RLE;
const PDF = Format.PDF;

function doIsValidTest(testFunc, valid, invalid) {
googArray.forEach(valid, (str) => {
Expand Down Expand Up @@ -140,6 +146,18 @@ testSuite({
'Failed to parse pre-OS X Mac newlines');
},

testparseListBidiMarks() {
// These bidi marks can be copy pasted from an RTL formatted email
assertParsedList(
`ab ${LRE}${PDF}${RLM}<${LRE}a@b.com${PDF}${RLM}>` +
`${PDF}${RLM},c@d.com${PDF}${RLM} `,
['a@b.com', 'c@d.com']);
assertParsedList(
`${PDF}ab ${RLE}${PDF}${LRM}<${RLE}a@b.com${PDF}${LRM}>` +
`${PDF}${LRM},c@d.com${PDF}${LRM} `,
['a@b.com', 'c@d.com']);
},

testToString() {
const f = (str) => EmailAddress.parse(str).toString();

Expand Down Expand Up @@ -197,17 +215,11 @@ testSuite({

testIsValid() {
const valid = [
'e@b.eu',
'<a.b+foo@c.com>',
'eric <e@b.com>',
'"e" <e@b.com>',
'a@FOO.MUSEUM',
'bla@b.co.ac.uk',
'bla@a.b.com',
'o\'hara@gm.com',
'plus+is+allowed@gmail.com',
'!/#$%&\'*+-=~|`{}?^_@expample.com',
'e@b.eu', '<a.b+foo@c.com>', 'eric <e@b.com>', '"e" <e@b.com>',
'a@FOO.MUSEUM', 'bla@b.co.ac.uk', 'bla@a.b.com', 'o\'hara@gm.com',
'plus+is+allowed@gmail.com', '!/#$%&\'*+-=~|`{}?^_@expample.com',
'confirm-bhk=modulo.org@yahoogroups.com',
`blah blahson ${LRE}${PDF}${RLM}<${LRE}blah@blah.com${PDF}${RLM}>`
];
const invalid = [
'e',
Expand Down

0 comments on commit ac1c88d

Please sign in to comment.