Home | History | Annotate | Download | only in chromeos
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 cr.define('options.accounts', function() {
      6   const Event = cr.Event;
      7 
      8   // Email alias only, assuming it's a gmail address.
      9   //   e.g. 'john'
     10   //        {name: 'john', email: 'john (a] gmail.com'}
     11   const format1String =
     12       '^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)\\s*$';
     13   // Email address only.
     14   //   e.g. 'john (a] chromium.org'
     15   //        {name: 'john', email: 'john (a] chromium.org'}
     16   const format2String =
     17       '^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)@' +
     18       '([A-Za-z0-9\-]{2,63}\\..+)\\s*$';
     19   // Full format.
     20   //   e.g. '"John Doe" <john (a] chromium.org>'
     21   //        {name: 'John doe', email: 'john (a] chromium.org'}
     22   const format3String =
     23       '^\\s*"{0,1}([^"]+)"{0,1}\\s*' +
     24       '<([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+@' +
     25       '[A-Za-z0-9\-]{2,63}\\..+)>\\s*$';
     26 
     27   /**
     28    * Creates a new user name edit element.
     29    * @param {Object=} opt_propertyBag Optional properties.
     30    * @constructor
     31    * @extends {HTMLInputElement}
     32    */
     33   var UserNameEdit = cr.ui.define('input');
     34 
     35   UserNameEdit.prototype = {
     36     __proto__: HTMLInputElement.prototype,
     37 
     38     /**
     39      * Called when an element is decorated as a user name edit.
     40      */
     41     decorate: function() {
     42       this.pattern = format1String + '|' + format2String + '|' +
     43                      format3String;
     44 
     45       this.onkeypress = this.handleKeyPress_.bind(this);
     46     },
     47 
     48 
     49     /**
     50      * Parses given str for user info.
     51      *
     52      * Note that the email parsing is based on RFC 5322 and does not support
     53      * IMA (Internationalized Email Address). We take only the following chars
     54      * as valid for an email alias (aka local-part):
     55      *   - Letters: az, AZ
     56      *   - Digits: 0-9
     57      *   - Characters: ! # $ % & ' * + - / = ? ^ _ ` { | } ~
     58      *   - Dot: . (Note that we did not cover the cases that dot should not
     59      *       appear as first or last character and should not appear two or
     60      *       more times in a row.)
     61      *
     62      * @param {string} str A string to parse.
     63      * @return {Object} User info parsed from the string.
     64      */
     65     parse: function(str) {
     66       const format1 = new RegExp(format1String);
     67       const format2 = new RegExp(format2String);
     68       const format3 = new RegExp(format3String);
     69 
     70       var matches = format1.exec(str);
     71       if (matches) {
     72         return {
     73           name: matches[1],
     74           email: matches[1] + '@gmail.com',
     75           owner:false
     76         };
     77       }
     78 
     79       matches = format2.exec(str);
     80       if (matches) {
     81         return {
     82           name: matches[1],
     83           email: matches[1] + '@' + matches[2],
     84           owner:false
     85         };
     86       }
     87 
     88       matches = format3.exec(str);
     89       if (matches) {
     90         return {
     91           name: matches[1],
     92           email: matches[2],
     93           owner:false
     94         };
     95       }
     96 
     97       return null;
     98     },
     99 
    100     /**
    101      * Handler for key press event.
    102      * @private
    103      * @param {!Event} e The keypress event object.
    104      */
    105     handleKeyPress_: function(e) {
    106       // Enter
    107       if (e.keyCode == 13) {
    108         var user = this.parse(this.value);
    109         if (user) {
    110           var e = new Event('add');
    111           e.user = user;
    112           this.dispatchEvent(e);
    113         }
    114 
    115         this.select();
    116       }
    117     }
    118   };
    119 
    120   return {
    121     UserNameEdit: UserNameEdit
    122   };
    123 });
    124 
    125