| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)@(\\w+\\..+)\\s*$'; | |
| 18 // Full format. | |
| 19 // e.g. '"John Doe" <john@chromium.org>' | |
| 20 // {name: 'John doe', email: 'john@chromium.org'} | |
| 21 const format3String = | |
| 22 '^\\s*"{0,1}([^"]+)"{0,1}\\s*<([^@]+@\\w+\\..+)>\\s*$'; | |
| 23 | |
| 24 /** | |
| 25 * Creates a new user name edit element. | |
| 26 * @param {Object=} opt_propertyBag Optional properties. | |
| 27 * @constructor | |
| 28 * @extends {HTMLInputElement} | |
| 29 */ | |
| 30 var UserNameEdit = cr.ui.define('input'); | |
| 31 | |
| 32 UserNameEdit.prototype = { | |
| 33 __proto__: HTMLInputElement.prototype, | |
| 34 | |
| 35 /** | |
| 36 * Called when an element is decorated as a user name edit. | |
| 37 */ | |
| 38 decorate: function() { | |
| 39 this.pattern = format1String + '|' + format2String + '|' + | |
| 40 format3String; | |
| 41 | |
| 42 this.onkeypress = cr.bind(this.handleKeyPress_, this); | |
| 43 }, | |
| 44 | |
| 45 | |
| 46 /** | |
| 47 * Parses given str for user info. | |
| 48 * | |
| 49 * Note that the email parsing is based on RFC 5322 and does not support | |
| 50 * IMA (Internationalized Email Address). We take only the following chars | |
| 51 * as valid for an email alias (aka local-part): | |
| 52 * - Letters: a–z, A–Z | |
| 53 * - Digits: 0-9 | |
| 54 * - Characters: ! # $ % & ' * + - / = ? ^ _ ` { | } ~ | |
| 55 * - Dot: . (Note that we did not cover the cases that dot should not | |
| 56 * appear as first or last character and should not appear two or | |
| 57 * more times in a row.) | |
| 58 * | |
| 59 * @param {string} str A string to parse. | |
| 60 * @return {Object} User info parsed from the string. | |
| 61 */ | |
| 62 parse: function(str) { | |
| 63 const format1 = new RegExp(format1String); | |
| 64 const format2 = new RegExp(format2String); | |
| 65 const format3 = new RegExp(format3String); | |
| 66 | |
| 67 var matches = format1.exec(str); | |
| 68 if (matches) { | |
| 69 return { | |
| 70 name: matches[1], | |
| 71 email: matches[1] + '@gmail.com', | |
| 72 owner:false | |
| 73 }; | |
| 74 } | |
| 75 | |
| 76 matches = format2.exec(str); | |
| 77 if (matches) { | |
| 78 return { | |
| 79 name: matches[1], | |
| 80 email: matches[1] + '@' + matches[2], | |
| 81 owner:false | |
| 82 }; | |
| 83 } | |
| 84 | |
| 85 matches = format3.exec(str); | |
| 86 if (matches) { | |
| 87 return { | |
| 88 name: matches[1], | |
| 89 email: matches[2], | |
| 90 owner:false | |
| 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 |