1 // Copyright (c) 2012 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.
5 cr.define('options.accounts', function() {
7 * Email alias only, assuming it's a gmail address.
9 * {name: 'john', email: 'john@gmail.com'}
13 '^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)\\s*$';
16 * e.g. 'john@chromium.org'
17 * {name: 'john', email: 'john@chromium.org'}
21 '^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)@' +
22 '([A-Za-z0-9\-]{2,63}\\..+)\\s*$';
25 * e.g. '"John Doe" <john@chromium.org>'
26 * {name: 'John doe', email: 'john@chromium.org'}
30 '^\\s*"{0,1}([^"]+)"{0,1}\\s*' +
31 '<([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+@' +
32 '[A-Za-z0-9\-]{2,63}\\..+)>\\s*$';
35 * Creates a new user name edit element.
36 * @param {Object=} opt_propertyBag Optional properties.
38 * @extends {HTMLInputElement}
40 var UserNameEdit = cr.ui.define('input');
42 UserNameEdit.prototype = {
43 __proto__: HTMLInputElement.prototype,
46 * Called when an element is decorated as a user name edit.
48 decorate: function() {
49 this.pattern = format1String + '|' + format2String + '|' +
52 this.onkeydown = this.handleKeyDown_.bind(this);
57 * Parses given str for user info.
59 * Note that the email parsing is based on RFC 5322 and does not support
60 * IMA (Internationalized Email Address). We take only the following chars
61 * as valid for an email alias (aka local-part):
64 * - Characters: ! # $ % & ' * + - / = ? ^ _ ` { | } ~
65 * - Dot: . (Note that we did not cover the cases that dot should not
66 * appear as first or last character and should not appear two or
67 * more times in a row.)
69 * @param {string} str A string to parse.
70 * @return {?{name: string, email: string}} User info parsed from the
73 parse: function(str) {
74 /** @const */ var format1 = new RegExp(format1String);
75 /** @const */ var format2 = new RegExp(format2String);
76 /** @const */ var format3 = new RegExp(format3String);
78 var matches = format1.exec(str);
82 email: matches[1] + '@gmail.com'
86 matches = format2.exec(str);
90 email: matches[1] + '@' + matches[2]
94 matches = format3.exec(str);
106 * Handler for key down event.
108 * @param {Event} e The keydown event object.
110 handleKeyDown_: function(e) {
111 if (e.keyIdentifier == 'Enter') {
112 var user = this.parse(this.value);
114 var event = new Event('add');
116 this.dispatchEvent(event);
119 // Avoid double-handling so the dialog doesn't close.
126 UserNameEdit: UserNameEdit