Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit a7fb357

Browse files
committed
fix(input): by default, do not trim input[type=password] values
Do not trim input[type=password] values BREAKING CHANGE: Previously, input[type=password] would trim values by default, and would require an explicit ng-trim="false" to disable the trimming behaviour. After this CL, ng-trim no longer effects input[type=password], and will never trim the password value. Closes #8250 Closes #8230
1 parent 09b2987 commit a7fb357

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/ng/directive/input.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ var inputType = {
2525
* @name input[text]
2626
*
2727
* @description
28-
* Standard HTML text input with angular data binding.
28+
* Standard HTML text input with angular data binding, inherited by most of the `input` elements.
29+
*
30+
* *NOTE* Not every feature offered is available for all input types.
2931
*
3032
* @param {string} ngModel Assignable angular expression to data-bind to.
3133
* @param {string=} name Property name of the form under which the control is published.
@@ -43,6 +45,8 @@ var inputType = {
4345
* @param {string=} ngChange Angular expression to be executed when input changes due to user
4446
* interaction with the input element.
4547
* @param {boolean=} [ngTrim=true] If set to false Angular will not automatically trim the input.
48+
* This parameter is ignored for input[type=password] controls, which will never trim the
49+
* input.
4650
*
4751
* @example
4852
<example name="text-input-directive" module="textInputExample">
@@ -908,6 +912,7 @@ function addNativeHtml5Validators(ctrl, validatorName, badFlags, ignoreFlags, va
908912
function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
909913
var validity = element.prop(VALIDITY_STATE_PROPERTY);
910914
var placeholder = element[0].placeholder, noevent = {};
915+
var type = element[0].type.toLowerCase();
911916
ctrl.$$validityState = validity;
912917

913918
// In composition mode, users are still inputing intermediate text buffer,
@@ -942,8 +947,8 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
942947

943948
// By default we will trim the value
944949
// If the attribute ng-trim exists we will avoid trimming
945-
// e.g. <input ng-model="foo" ng-trim="false">
946-
if (!attr.ngTrim || attr.ngTrim !== 'false') {
950+
// If input type is 'password', the value is never trimmed
951+
if (type !== 'password' && (!attr.ngTrim || attr.ngTrim !== 'false')) {
947952
value = trim(value);
948953
}
949954

@@ -1276,6 +1281,8 @@ function checkboxInputType(scope, element, attr, ctrl, $sniffer, $browser, $filt
12761281
* HTML input element control with angular data-binding. Input control follows HTML5 input types
12771282
* and polyfills the HTML5 validation behavior for older browsers.
12781283
*
1284+
* *NOTE* Not every feature offered is available for all input types.
1285+
*
12791286
* @param {string} ngModel Assignable angular expression to data-bind to.
12801287
* @param {string=} name Property name of the form under which the control is published.
12811288
* @param {string=} required Sets `required` validation error key if the value is not entered.
@@ -1289,6 +1296,9 @@ function checkboxInputType(scope, element, attr, ctrl, $sniffer, $browser, $filt
12891296
* patterns defined as scope expressions.
12901297
* @param {string=} ngChange Angular expression to be executed when input changes due to user
12911298
* interaction with the input element.
1299+
* @param {boolean=} [ngTrim=true] If set to false Angular will not automatically trim the input.
1300+
* This parameter is ignored for input[type=password] controls, which will never trim the
1301+
* input.
12921302
*
12931303
* @example
12941304
<example name="input-directive" module="inputExample">

test/ng/directive/inputSpec.js

+24
Original file line numberDiff line numberDiff line change
@@ -2984,6 +2984,30 @@ describe('input', function() {
29842984
expect(scope.items[0].selected).toBe(false);
29852985
});
29862986
});
2987+
2988+
2989+
describe('password', function() {
2990+
// Under no circumstances should input[type=password] trim inputs
2991+
it('should not trim if ngTrim is unspecified', function() {
2992+
compileInput('<input type="password" ng-model="password">');
2993+
changeInputValueTo(' - - untrimmed - - ');
2994+
expect(scope.password.length).toBe(' - - untrimmed - - '.length);
2995+
});
2996+
2997+
2998+
it('should not trim if ngTrim !== false', function() {
2999+
compileInput('<input type="password" ng-model="password" ng-trim="true">');
3000+
changeInputValueTo(' - - untrimmed - - ');
3001+
expect(scope.password.length).toBe(' - - untrimmed - - '.length);
3002+
});
3003+
3004+
3005+
it('should not trim if ngTrim === false', function() {
3006+
compileInput('<input type="password" ng-model="password" ng-trim="false">');
3007+
changeInputValueTo(' - - untrimmed - - ');
3008+
expect(scope.password.length).toBe(' - - untrimmed - - '.length);
3009+
});
3010+
});
29873011
});
29883012

29893013
describe('NgModel animations', function() {

0 commit comments

Comments
 (0)