Extract code handling PrinterProviderAPI from PrintPreviewHandler
[chromium-blink-merge.git] / chrome / browser / resources / options / chromeos / accounts_user_name_edit.js
blob22a3b37b0e908b57aa1b48e7d476592d0663d44f
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() {
6 /**
7 * Email alias only, assuming it's a gmail address.
8 * e.g. 'john'
9 * {name: 'john', email: 'john@gmail.com'}
10 * @const
12 var format1String =
13 '^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)\\s*$';
14 /**
15 * Email address only.
16 * e.g. 'john@chromium.org'
17 * {name: 'john', email: 'john@chromium.org'}
18 * @const
20 var format2String =
21 '^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)@' +
22 '([A-Za-z0-9\-]{2,63}\\..+)\\s*$';
23 /**
24 * Full format.
25 * e.g. '"John Doe" <john@chromium.org>'
26 * {name: 'John doe', email: 'john@chromium.org'}
27 * @const
29 var format3String =
30 '^\\s*"{0,1}([^"]+)"{0,1}\\s*' +
31 '<([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+@' +
32 '[A-Za-z0-9\-]{2,63}\\..+)>\\s*$';
34 /**
35 * Creates a new user name edit element.
36 * @param {Object=} opt_propertyBag Optional properties.
37 * @constructor
38 * @extends {HTMLInputElement}
40 var UserNameEdit = cr.ui.define('input');
42 UserNameEdit.prototype = {
43 __proto__: HTMLInputElement.prototype,
45 /**
46 * Called when an element is decorated as a user name edit.
48 decorate: function() {
49 this.pattern = format1String + '|' + format2String + '|' +
50 format3String;
52 this.onkeydown = this.handleKeyDown_.bind(this);
56 /**
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):
62 * - Letters: a–z, A–Z
63 * - Digits: 0-9
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
71 * string.
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);
79 if (matches) {
80 return {
81 name: matches[1],
82 email: matches[1] + '@gmail.com'
86 matches = format2.exec(str);
87 if (matches) {
88 return {
89 name: matches[1],
90 email: matches[1] + '@' + matches[2]
94 matches = format3.exec(str);
95 if (matches) {
96 return {
97 name: matches[1],
98 email: matches[2]
102 return null;
106 * Handler for key down event.
107 * @private
108 * @param {Event} e The keydown event object.
110 handleKeyDown_: function(e) {
111 if (e.keyIdentifier == 'Enter') {
112 var user = this.parse(this.value);
113 if (user) {
114 var event = new Event('add');
115 event.user = user;
116 this.dispatchEvent(event);
118 this.select();
119 // Avoid double-handling so the dialog doesn't close.
120 e.stopPropagation();
125 return {
126 UserNameEdit: UserNameEdit