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

Commit ebece0b

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 Conflicts: src/ng/directive/input.js
1 parent 4b7398e commit ebece0b

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
@@ -17,7 +17,9 @@ var inputType = {
1717
* @name input[text]
1818
*
1919
* @description
20-
* Standard HTML text input with angular data binding.
20+
* Standard HTML text input with angular data binding, inherited by most of the `input` elements.
21+
*
22+
* *NOTE* Not every feature offered is available for all input types.
2123
*
2224
* @param {string} ngModel Assignable angular expression to data-bind to.
2325
* @param {string=} name Property name of the form under which the control is published.
@@ -35,6 +37,8 @@ var inputType = {
3537
* @param {string=} ngChange Angular expression to be executed when input changes due to user
3638
* interaction with the input element.
3739
* @param {boolean=} [ngTrim=true] If set to false Angular will not automatically trim the input.
40+
* This parameter is ignored for input[type=password] controls, which will never trim the
41+
* input.
3842
*
3943
* @example
4044
<example name="text-input-directive" module="textInputExample">
@@ -474,6 +478,7 @@ function addNativeHtml5Validators(ctrl, validatorName, badFlags, ignoreFlags, va
474478
function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
475479
var validity = element.prop(VALIDITY_STATE_PROPERTY);
476480
var placeholder = element[0].placeholder, noevent = {};
481+
var type = element[0].type.toLowerCase();
477482
ctrl.$$validityState = validity;
478483

479484
// In composition mode, users are still inputing intermediate text buffer,
@@ -507,8 +512,8 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
507512

508513
// By default we will trim the value
509514
// If the attribute ng-trim exists we will avoid trimming
510-
// e.g. <input ng-model="foo" ng-trim="false">
511-
if (toBoolean(attr.ngTrim || 'T')) {
515+
// If input type is 'password', the value is never trimmed
516+
if (type !== 'password' && (toBoolean(attr.ngTrim || 'T'))) {
512517
value = trim(value);
513518
}
514519

@@ -783,6 +788,8 @@ function checkboxInputType(scope, element, attr, ctrl) {
783788
* HTML input element control with angular data-binding. Input control follows HTML5 input types
784789
* and polyfills the HTML5 validation behavior for older browsers.
785790
*
791+
* *NOTE* Not every feature offered is available for all input types.
792+
*
786793
* @param {string} ngModel Assignable angular expression to data-bind to.
787794
* @param {string=} name Property name of the form under which the control is published.
788795
* @param {string=} required Sets `required` validation error key if the value is not entered.
@@ -796,6 +803,9 @@ function checkboxInputType(scope, element, attr, ctrl) {
796803
* patterns defined as scope expressions.
797804
* @param {string=} ngChange Angular expression to be executed when input changes due to user
798805
* interaction with the input element.
806+
* @param {boolean=} [ngTrim=true] If set to false Angular will not automatically trim the input.
807+
* This parameter is ignored for input[type=password] controls, which will never trim the
808+
* input.
799809
*
800810
* @example
801811
<example name="input-directive" module="inputExample">

test/ng/directive/inputSpec.js

+24
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,30 @@ describe('input', function() {
15461546
expect(scope.items[0].selected).toBe(false);
15471547
});
15481548
});
1549+
1550+
1551+
describe('password', function() {
1552+
// Under no circumstances should input[type=password] trim inputs
1553+
it('should not trim if ngTrim is unspecified', function() {
1554+
compileInput('<input type="password" ng-model="password">');
1555+
changeInputValueTo(' - - untrimmed - - ');
1556+
expect(scope.password.length).toBe(' - - untrimmed - - '.length);
1557+
});
1558+
1559+
1560+
it('should not trim if ngTrim !== false', function() {
1561+
compileInput('<input type="password" ng-model="password" ng-trim="true">');
1562+
changeInputValueTo(' - - untrimmed - - ');
1563+
expect(scope.password.length).toBe(' - - untrimmed - - '.length);
1564+
});
1565+
1566+
1567+
it('should not trim if ngTrim === false', function() {
1568+
compileInput('<input type="password" ng-model="password" ng-trim="false">');
1569+
changeInputValueTo(' - - untrimmed - - ');
1570+
expect(scope.password.length).toBe(' - - untrimmed - - '.length);
1571+
});
1572+
});
15491573
});
15501574

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

0 commit comments

Comments
 (0)