OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
Dan Beam
2012/08/11 01:38:02
nit: one less \n here, IMO
| |
5 | |
6 cr.define('options.internet', function() { | |
7 /** @const */ var EditableTextField = options.EditableTextField; | |
8 | |
9 /** | |
10 * The regular expression that matches an IP address, allowing for | |
11 * leading/trailing/embedded whitespace. | |
12 * @const | |
13 * @private | |
Dan Beam
2012/08/11 01:38:02
don't really need @private if this isn't a member
| |
14 * @type {RegExp} | |
15 */ | |
16 var singleIp_ = new RegExp('^\\s*([0-9]+)\\s*\\.\\s*([0-9]+)\\s*\\.' + | |
Dan Beam
2012/08/11 01:38:02
I'd highly recommend writing this in literal form
Greg Spencer (Chromium)
2012/08/13 19:20:04
Yeah, I tried that already, but the problem is tha
Dan Beam
2012/08/13 19:48:55
were you passing the /g (global) flag to the RegEx
Greg Spencer (Chromium)
2012/08/13 22:17:44
Yeah, I'm pretty familiar with regexps, and no, I
Dan Beam
2012/08/13 22:56:18
Yeah, OK.
| |
17 '\\s*([0-9]+)\\s*\\.\\s*([0-9]+)\\s*$'); | |
18 | |
19 /** | |
20 * Creates a new field specifically for entering IP addresses. | |
21 * @constructor | |
22 */ | |
23 function IPAddressField() { | |
24 var el = cr.doc.createElement('div'); | |
25 IPAddressField.decorate(el); | |
26 return el; | |
27 }; | |
Dan Beam
2012/08/11 01:38:02
nit: no ; when using function declarations() { }
Greg Spencer (Chromium)
2012/08/13 19:20:04
Done.
| |
28 | |
29 /** | |
30 * Decorates an element as a inline-editable list item. Note that this is | |
31 * a subclass of IPAddressField. | |
32 * @param {!HTMLElement} el The element to decorate. | |
33 */ | |
34 IPAddressField.decorate = function(el) { | |
35 el.__proto__ = IPAddressField.prototype; | |
36 el.decorate(); | |
37 }; | |
38 | |
39 IPAddressField.prototype = { | |
40 __proto__: EditableTextField.prototype, | |
41 | |
42 /** @inheritDoc */ | |
Dan Beam
2012/08/11 01:38:02
@inheritDoc is deprecated now, use @override inste
Greg Spencer (Chromium)
2012/08/13 19:20:04
Done.
| |
43 decorate: function() { | |
44 EditableTextField.prototype.decorate.call(this); | |
45 }, | |
46 | |
47 /** | |
48 * Indicates whether or not empty values are allowed. | |
49 * @type {boolean} | |
50 */ | |
51 get allowEmpty() { | |
52 return this.hasAttribute('allow-empty'); | |
53 }, | |
54 | |
55 /** @inheritDoc */ | |
56 get currentInputIsValid() { | |
57 if (!this.editField.value && this.allowEmpty) | |
58 return true; | |
59 | |
60 // Make sure it's only got numbers and ".", there are the correct | |
61 // count of them, and they are all within the correct range. | |
62 var matches = singleIp_.exec(this.editField.value); | |
63 var range_correct = true; | |
Dan Beam
2012/08/11 01:38:02
var rangeCorrent (camelCase in JavaScript)
Greg Spencer (Chromium)
2012/08/13 19:20:04
aaagh! Switching back and forth between C++ and J
| |
64 if (matches != null) { | |
65 for (var i = 1; i < matches.length; ++i) { | |
66 var value = parseInt(matches[i], 10); | |
67 if (value < 0 || value > 255) | |
68 range_correct = false; | |
Dan Beam
2012/08/11 01:38:02
why not break; here?
Greg Spencer (Chromium)
2012/08/13 19:20:04
Why indeed. Done.
| |
69 } | |
70 } | |
71 return (this.editField.validity.valid && matches != null && | |
Dan Beam
2012/08/11 01:38:02
nit: less ()
Greg Spencer (Chromium)
2012/08/13 19:20:04
Done.
| |
72 range_correct && matches.length == 5); | |
73 }, | |
74 | |
75 /** @inheritDoc */ | |
76 get hasBeenEdited() { | |
77 return this.editField.value != this.model.value; | |
78 }, | |
79 | |
80 /** | |
81 * Mutates the input during a successful commit. For the purposes of | |
Dan Beam
2012/08/11 01:38:02
nit: 1 \s between sentences in comments (you have
Greg Spencer (Chromium)
2012/08/13 19:20:04
Done.
| |
82 * entering IP addresses, this just means stripping off whitespace and | |
83 * leading zeros from each of the octets so that they conform to the normal | |
84 * format for IP addresses. | |
85 * @param {Object} value Input IP address to be mutated. | |
Dan Beam
2012/08/11 01:38:02
shouldn't @param and @return be a {string}?
Greg Spencer (Chromium)
2012/08/13 19:20:04
Yeah, I guess so. I was originally going to mutat
| |
86 * @return {Object} Mutated IP address. | |
87 */ | |
88 mutateInput: function(value) { | |
89 if (!value) | |
90 return value; | |
Dan Beam
2012/08/11 01:38:02
nit: \n after this if ()
Greg Spencer (Chromium)
2012/08/13 19:20:04
Done.
| |
91 var model = this.model; | |
92 var matches = singleIp_.exec(value); | |
Dan Beam
2012/08/11 01:38:02
seems like you could simplify this to:
return
Greg Spencer (Chromium)
2012/08/13 19:20:04
Well, and the fact that it doesn't quite work.. :)
| |
93 var result = []; | |
94 if (matches != null) { // should never be null if we got this far | |
Dan Beam
2012/08/11 01:38:02
make a full sentence if this loop sticks around
Greg Spencer (Chromium)
2012/08/13 19:20:04
Done.
| |
95 for (var i = 1; i < matches.length; ++i) { | |
96 var octet = parseInt(matches[i], 10); | |
97 result.push(octet); | |
Dan Beam
2012/08/11 01:38:02
inline octect if this loop sticks around
Greg Spencer (Chromium)
2012/08/13 19:20:04
Done.
| |
98 } | |
99 } | |
100 return result.join('.'); | |
101 }, | |
102 }; | |
103 | |
104 return { | |
105 IPAddressField: IPAddressField, | |
106 }; | |
107 }); | |
OLD | NEW |