Chromium Code Reviews| 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/14 03:59:56
nit: remove 1 \n
Greg Spencer (Chromium)
2012/08/14 16:54:53
Done.
| |
| 5 | |
| 6 cr.define('options.internet', function() { | |
| 7 /** @const */ var EditableTextField = options.EditableTextField; | |
| 8 | |
| 9 /** | |
| 10 * The regular expression that matches an IP address. String to match against | |
| 11 * should have all whitespace stripped already. | |
| 12 * @const | |
| 13 * @type {RegExp} | |
| 14 */ | |
| 15 var singleIp_ = /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/; | |
| 16 | |
| 17 /** | |
| 18 * Creates a new field specifically for entering IP addresses. | |
| 19 * @constructor | |
| 20 */ | |
| 21 function IPAddressField() { | |
| 22 var el = cr.doc.createElement('div'); | |
| 23 IPAddressField.decorate(el); | |
| 24 return el; | |
| 25 } | |
| 26 | |
| 27 /** | |
| 28 * Decorates an element as a inline-editable list item. Note that this is | |
| 29 * a subclass of IPAddressField. | |
| 30 * @param {!HTMLElement} el The element to decorate. | |
| 31 */ | |
| 32 IPAddressField.decorate = function(el) { | |
| 33 el.__proto__ = IPAddressField.prototype; | |
| 34 el.decorate(); | |
| 35 }; | |
| 36 | |
| 37 IPAddressField.prototype = { | |
| 38 __proto__: EditableTextField.prototype, | |
| 39 | |
| 40 /** @override */ | |
| 41 decorate: function() { | |
| 42 EditableTextField.prototype.decorate.call(this); | |
| 43 }, | |
| 44 | |
| 45 /** | |
| 46 * Indicates whether or not empty values are allowed. | |
| 47 * @type {boolean} | |
| 48 */ | |
| 49 get allowEmpty() { | |
| 50 return this.hasAttribute('allow-empty'); | |
| 51 }, | |
| 52 | |
| 53 /** @override */ | |
| 54 get currentInputIsValid() { | |
| 55 if (!this.editField.value && this.allowEmpty) | |
| 56 return true; | |
| 57 | |
| 58 // Make sure it's only got numbers and ".", there are the correct | |
| 59 // count of them, and they are all within the correct range. | |
| 60 var fieldValue = this.editField.value.replace(/\s/g, ''); | |
| 61 var matches = singleIp_.exec(fieldValue); | |
| 62 var rangeCorrect = true; | |
| 63 if (matches != null) { | |
| 64 for (var i = 1; i < matches.length; ++i) { | |
| 65 var value = parseInt(matches[i], 10); | |
| 66 if (value < 0 || value > 255) { | |
| 67 rangeCorrect = false; | |
| 68 break; | |
| 69 } | |
| 70 } | |
| 71 } | |
| 72 return this.editField.validity.valid && matches != null && | |
| 73 rangeCorrect && matches.length == 5; | |
| 74 }, | |
| 75 | |
| 76 /** @override */ | |
| 77 get hasBeenEdited() { | |
| 78 return this.editField.value != this.model.value; | |
| 79 }, | |
| 80 | |
| 81 /** | |
| 82 * Overrides superclass to mutate the input during a successful commit. For | |
| 83 * the purposes of entering IP addresses, this just means stripping off | |
| 84 * whitespace and leading zeros from each of the octets so that they conform | |
| 85 * to the normal format for IP addresses. | |
| 86 * @override | |
| 87 * @param {String} value Input IP address to be mutated. | |
| 88 * @return {String} mutated IP address. | |
| 89 */ | |
| 90 mutateInput: function(value) { | |
| 91 if (!value) | |
| 92 return value; | |
| 93 | |
| 94 var fieldValue = value.replace(/\s/g, ''); | |
| 95 var matches = singleIp_.exec(fieldValue); | |
| 96 var result = []; | |
| 97 | |
| 98 // If we got this far, matches shouldn't be null, but make sure. | |
| 99 if (matches != null) { | |
| 100 for (var i = 1; i < matches.length; ++i) | |
|
Dan Beam
2012/08/14 03:59:56
nit: might want to comment that you're starting at
Greg Spencer (Chromium)
2012/08/14 16:54:53
Done.
| |
| 101 result.push(parseInt(matches[i], 10)); | |
| 102 } | |
| 103 return result.join('.'); | |
| 104 }, | |
| 105 }; | |
| 106 | |
| 107 return { | |
| 108 IPAddressField: IPAddressField, | |
| 109 }; | |
| 110 }); | |
| OLD | NEW |