OLD | NEW |
(Empty) | |
| 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@gmail.com'} |
| 11 const format1String = |
| 12 '^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)\\s*$'; |
| 13 // Email address only. |
| 14 // e.g. 'john@chromium.org' |
| 15 // {name: 'john', email: 'john@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@chromium.org>' |
| 21 // {name: 'John doe', email: 'john@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: a–z, A–Z |
| 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 {{name: string, email: string}} 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 }; |
| 76 } |
| 77 |
| 78 matches = format2.exec(str); |
| 79 if (matches) { |
| 80 return { |
| 81 name: matches[1], |
| 82 email: matches[1] + '@' + matches[2] |
| 83 }; |
| 84 } |
| 85 |
| 86 matches = format3.exec(str); |
| 87 if (matches) { |
| 88 return { |
| 89 name: matches[1], |
| 90 email: matches[2] |
| 91 }; |
| 92 } |
| 93 |
| 94 return null; |
| 95 }, |
| 96 |
| 97 /** |
| 98 * Handler for key press event. |
| 99 * @private |
| 100 * @param {!Event} e The keypress event object. |
| 101 */ |
| 102 handleKeyPress_: function(e) { |
| 103 // Enter |
| 104 if (e.keyCode == 13) { |
| 105 var user = this.parse(this.value); |
| 106 if (user) { |
| 107 var e = new Event('add'); |
| 108 e.user = user; |
| 109 this.dispatchEvent(e); |
| 110 } |
| 111 |
| 112 this.select(); |
| 113 } |
| 114 } |
| 115 }; |
| 116 |
| 117 return { |
| 118 UserNameEdit: UserNameEdit |
| 119 }; |
| 120 }); |
| 121 |
OLD | NEW |