Skip to content

Commit

Permalink
Merge pull request #131 from PolymerElements/auto-cleanup
Browse files Browse the repository at this point in the history
Automatic format!
  • Loading branch information
Elliott Marquez authored Apr 19, 2018
2 parents 5d019e7 + 0937c17 commit 9952b18
Show file tree
Hide file tree
Showing 9 changed files with 1,696 additions and 281 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ before_script:
npm run update-types && git diff --exit-code || (echo -e
'\n\033[31mERROR:\033[0m Typings are stale. Please run "npm run
update-types".' && false)
- >-
npm run format && git diff --exit-code || (echo -e '\n\033[31mERROR:\033[0m
Project is not formatted. Please run "npm run format".' && false)
env:
global:
- secure: >-
Expand Down
4 changes: 1 addition & 3 deletions demo/cats-only.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
Polymer({
is: 'cats-only',

behaviors: [
Polymer.IronValidatorBehavior
],
behaviors: [Polymer.IronValidatorBehavior],

validate: function(value) {
return value === 'cat'
Expand Down
22 changes: 12 additions & 10 deletions iron-input.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,18 @@ interface IronInputElement extends Polymer.Element, Polymer.IronValidatableBehav
readonly value: string|null|undefined;

/**
* Regex-like list of characters allowed as input; all characters not in the list
* will be rejected. The recommended format should be a list of allowed characters,
* for example, `[a-zA-Z0-9.+-!;:]`.
* Regex-like list of characters allowed as input; all characters not in the
* list will be rejected. The recommended format should be a list of allowed
* characters, for example, `[a-zA-Z0-9.+-!;:]`.
*
* This pattern represents the allowed characters for the field; as the user inputs text,
* each individual character will be checked against the pattern (rather than checking
* the entire value as a whole). If a character is not a match, it will be rejected.
* This pattern represents the allowed characters for the field; as the user
* inputs text, each individual character will be checked against the
* pattern (rather than checking the entire value as a whole). If a
* character is not a match, it will be rejected.
*
* Pasted input will have each character checked individually; if any character
* doesn't match `allowedPattern`, the entire pasted string will be rejected.
* Pasted input will have each character checked individually; if any
* character doesn't match `allowedPattern`, the entire pasted string will
* be rejected.
*
* Note: if you were using `iron-input` in 1.0, you were also required to
* set `prevent-invalid-input`. This is no longer needed as of Polymer 2.0,
Expand All @@ -138,8 +140,8 @@ interface IronInputElement extends Polymer.Element, Polymer.IronValidatableBehav
readonly _patternRegExp: any;

/**
* Returns true if `value` is valid. The validator provided in `validator` will be used first,
* then any constraints.
* Returns true if `value` is valid. The validator provided in `validator`
* will be used first, then any constraints.
*
* @returns True if the value is valid.
*/
Expand Down
116 changes: 52 additions & 64 deletions iron-input.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@
Polymer({
is: 'iron-input',

behaviors: [
Polymer.IronValidatableBehavior
],
behaviors: [Polymer.IronValidatableBehavior],

/**
* Fired whenever `validate()` is called.
Expand All @@ -120,64 +118,50 @@
* set a default value for the input. **Do not** use the distributed
* input's `value` property to set a default value.
*/
bindValue: {
type: String,
value: ''
},
bindValue: {type: String, value: ''},

/**
* Computed property that echoes `bindValue` (mostly used for Polymer 1.0
* backcompatibility, if you were one-way binding to the Polymer 1.0
* `input is="iron-input"` value attribute).
*/
value: {
type: String,
computed: '_computeValue(bindValue)'
},
value: {type: String, computed: '_computeValue(bindValue)'},

/**
* Regex-like list of characters allowed as input; all characters not in the list
* will be rejected. The recommended format should be a list of allowed characters,
* for example, `[a-zA-Z0-9.+-!;:]`.
* Regex-like list of characters allowed as input; all characters not in the
* list will be rejected. The recommended format should be a list of allowed
* characters, for example, `[a-zA-Z0-9.+-!;:]`.
*
* This pattern represents the allowed characters for the field; as the user inputs text,
* each individual character will be checked against the pattern (rather than checking
* the entire value as a whole). If a character is not a match, it will be rejected.
* This pattern represents the allowed characters for the field; as the user
* inputs text, each individual character will be checked against the
* pattern (rather than checking the entire value as a whole). If a
* character is not a match, it will be rejected.
*
* Pasted input will have each character checked individually; if any character
* doesn't match `allowedPattern`, the entire pasted string will be rejected.
* Pasted input will have each character checked individually; if any
* character doesn't match `allowedPattern`, the entire pasted string will
* be rejected.
*
* Note: if you were using `iron-input` in 1.0, you were also required to
* set `prevent-invalid-input`. This is no longer needed as of Polymer 2.0,
* and will be set automatically for you if an `allowedPattern` is provided.
*
*/
allowedPattern: {
type: String
},
allowedPattern: {type: String},

/**
* Set to true to auto-validate the input value as you type.
*/
autoValidate: {
type: Boolean,
value: false
},

/**
* The native input element.
*/
autoValidate: {type: Boolean, value: false},

/**
* The native input element.
*/
_inputElement: Object,
},

observers: [
'_bindValueChanged(bindValue, _inputElement)'
],
observers: ['_bindValueChanged(bindValue, _inputElement)'],

listeners: {
'input': '_onInput',
'keypress': '_onKeypress'
},
listeners: {'input': '_onInput', 'keypress': '_onKeypress'},

created: function() {
Polymer.IronA11yAnnouncer.requestAvailability();
Expand All @@ -202,7 +186,7 @@
/**
* Returns the distributed input element.
*/
get inputElement () {
get inputElement() {
return this._inputElement;
},

Expand Down Expand Up @@ -234,15 +218,15 @@
* @suppress {checkTypes}
*/
_bindValueChanged: function(bindValue, inputElement) {
// The observer could have run before attached() when we have actually initialized
// this property.
// The observer could have run before attached() when we have actually
// initialized this property.
if (!inputElement) {
return;
}

if (bindValue === undefined) {
inputElement.value = null;
} else if (bindValue !== inputElement.value){
} else if (bindValue !== inputElement.value) {
this.inputElement.value = bindValue;
}

Expand All @@ -260,7 +244,8 @@
if (this.allowedPattern && !this._patternAlreadyChecked) {
var valid = this._checkPatternValidity();
if (!valid) {
this._announceInvalidCharacter('Invalid string of characters not entered.');
this._announceInvalidCharacter(
'Invalid string of characters not entered.');
this.inputElement.value = this._previousValidInput;
}
}
Expand All @@ -270,32 +255,33 @@

_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
// - 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
// - in Firefox, printable chars always have a keyCode = 0. In Chrome, the
// keyCode
// always matches the charCode.
// None of this makes any sense.

// For these keys, ASCII code == browser keycode.
var anyNonPrintable =
(event.keyCode == 8) || // backspace
(event.keyCode == 9) || // tab
(event.keyCode == 13) || // enter
(event.keyCode == 27); // escape
var anyNonPrintable = (event.keyCode == 8) || // backspace
(event.keyCode == 9) || // tab
(event.keyCode == 13) || // enter
(event.keyCode == 27); // escape

// For these keys, make sure it's a browser keycode and not an ASCII code.
var mozNonPrintable =
(event.keyCode == 19) || // pause
(event.keyCode == 20) || // caps lock
(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
var mozNonPrintable = (event.keyCode == 19) || // pause
(event.keyCode == 20) || // caps lock
(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 !anyNonPrintable && !(event.charCode == 0 && mozNonPrintable);
},
Expand All @@ -320,7 +306,8 @@
var thisChar = String.fromCharCode(event.charCode);
if (this._isPrintable(event) && !regexp.test(thisChar)) {
event.preventDefault();
this._announceInvalidCharacter('Invalid character ' + thisChar + ' not entered.');
this._announceInvalidCharacter(
'Invalid character ' + thisChar + ' not entered.');
}
},

Expand All @@ -338,8 +325,8 @@
},

/**
* Returns true if `value` is valid. The validator provided in `validator` will be used first,
* then any constraints.
* 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.
*/
validate: function() {
Expand All @@ -349,15 +336,16 @@
}

// Use the nested input's native validity.
var valid = this.inputElement.checkValidity();
var valid = this.inputElement.checkValidity();

// Only do extra checking if the browser thought this was valid.
if (valid) {
// Empty, required input is invalid
if (this.required && this.bindValue === '') {
valid = false;
} else if (this.hasValidator()) {
valid = Polymer.IronValidatableBehavior.validate.call(this, this.bindValue);
valid =
Polymer.IronValidatableBehavior.validate.call(this, this.bindValue);
}
}

Expand All @@ -367,7 +355,7 @@
},

_announceInvalidCharacter: function(message) {
this.fire('iron-announce', { text: message });
this.fire('iron-announce', {text: message});
},

_computeValue: function(bindValue) {
Expand Down
Loading

0 comments on commit 9952b18

Please sign in to comment.