Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Unified Diff: third_party/polymer/v1_0/components/iron-input/iron-input.html

Issue 1187823002: Update Polymer components and re-run reproduce.sh (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/polymer/v1_0/components/iron-input/iron-input.html
diff --git a/third_party/polymer/v1_0/components/iron-input/iron-input.html b/third_party/polymer/v1_0/components/iron-input/iron-input.html
index 623a605974f21ef587a7f26f83bce8567c8733ee..3d8cccbf70960058fce7f83680ae870c2978487e 100644
--- a/third_party/polymer/v1_0/components/iron-input/iron-input.html
+++ b/third_party/polymer/v1_0/components/iron-input/iron-input.html
@@ -86,13 +86,18 @@ is separate from validation, and `allowed-pattern` does not affect how the input
_previousValidInput: {
type: String,
value: ''
+ },
+
+ _patternAlreadyChecked: {
+ type: Boolean,
+ value: false
}
},
listeners: {
'input': '_onInput',
- 'keydown': '_onKeydown'
+ 'keypress': '_onKeypress'
},
get _patternRegExp() {
@@ -117,33 +122,54 @@ is separate from validation, and `allowed-pattern` does not affect how the input
_bindValueChanged: function() {
if (this.value !== this.bindValue) {
- this.value = this.bindValue;
+ this.value = !this.bindValue ? '' : this.bindValue;
}
// manually notify because we don't want to notify until after setting value
this.fire('bind-value-changed', {value: this.bindValue});
},
_onInput: function() {
- this.bindValue = this.value;
- },
+ // Need to validate each of the characters pasted if they haven't
+ // been validated inside `_onKeypress` already.
+ if (this.preventInvalidInput && !this._patternAlreadyChecked) {
+ var valid = this._checkPatternValidity();
+ if (!valid) {
+ this.value = this._previousValidInput;
+ }
+ }
- _isPrintable: function(keyCode) {
- var printable =
- (keyCode > 47 && keyCode < 58) || // number keys
- keyCode == 32 || keyCode == 13 || // spacebar & return key
- (keyCode > 64 && keyCode < 91) || // letter keys
- (keyCode > 95 && keyCode < 112) || // numpad keys
- (keyCode > 185 && keyCode < 193) || // ;=,-./` (in order)
- (keyCode > 218 && keyCode < 223); // [\]' (in order)
- return printable;
+ this.bindValue = this.value;
+ this._previousValidInput = this.value;
+ this._patternAlreadyChecked = false;
},
- // convert printable numpad keys to number keys
- _correctNumpadKeys: function(keyCode) {
- return (keyCode > 95 && keyCode < 112) ? keyCode - 48 : keyCode;
+ _isPrintable: function(event) {
+ // What a control/printable character is varies wildly based on the browser.
+ // - most control characters (arrows, backspace) do not send a `keypress` event
+ // in Chrome, but the *do* on Firefox
+ // - in Firefox, when they do send a `keypress` event, control chars have
+ // a charCode = 0, keyCode = xx (for ex. 40 for down arrow)
+ // - printable characters always send a keypress event.
+ // - in Firefox, printable chars always have a keyCode = 0. In Chrome, the keyCode
+ // always matches the charCode.
+ // None of this makes any sense.
+
+ var nonPrintable =
+ (event.keyCode == 8) || // backspace
+ (event.keyCode == 19) || // pause
+ (event.keyCode == 20) || // caps lock
+ (event.keyCode == 27) || // escape
+ (event.keyCode == 45) || // insert
+ (event.keyCode == 46) || // delete
+ (event.keyCode == 144) || // num lock
+ (event.keyCode == 145) || // scroll lock
+ (event.keyCode > 32 && event.keyCode < 41) || // page up/down, end, home, arrows
+ (event.keyCode > 111 && event.keyCode < 124); // fn keys
+
+ return !(event.charCode == 0 && nonPrintable);
},
- _onKeydown: function(event) {
+ _onKeypress: function(event) {
if (!this.preventInvalidInput && this.type !== 'number') {
return;
}
@@ -151,22 +177,45 @@ is separate from validation, and `allowed-pattern` does not affect how the input
if (!regexp) {
return;
}
- var thisChar = String.fromCharCode(this._correctNumpadKeys(event.keyCode));
- if (this._isPrintable(event.keyCode) && !regexp.test(thisChar)) {
+
+ // Handle special keys and backspace
+ if (event.metaKey || event.ctrlKey || event.altKey)
+ return;
+
+ // Check the pattern either here or in `_onInput`, but not in both.
+ this._patternAlreadyChecked = true;
+
+ var thisChar = String.fromCharCode(event.charCode);
+ if (this._isPrintable(event) && !regexp.test(thisChar)) {
event.preventDefault();
}
},
+ _checkPatternValidity: function() {
+ var regexp = this._patternRegExp;
+ if (!regexp) {
+ return true;
+ }
+ for (var i = 0; i < this.value.length; i++) {
+ if (!regexp.test(this.value[i])) {
+ return false;
+ }
+ }
+ return true;
+ },
+
/**
* Returns true if `value` is valid. The validator provided in `validator` will be used first,
* then any constraints.
- * @return {Boolean} True if the value is valid.
+ * @return {boolean} True if the value is valid.
*/
validate: function() {
// Empty, non-required input is valid.
- if (!this.required && this.value == '')
+ if (!this.required && this.value == '') {
+ this.invalid = false;
return true;
-
+ }
+
var valid;
if (this.hasValidator()) {
valid = Polymer.IronValidatableBehavior.validate.call(this, this.value);

Powered by Google App Engine
This is Rietveld 408576698