mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-22 19:23:38 +08:00
fix(ngMaxlength): ignore maxlength when not set to a non-negative integer
This makes the behaviour of maxlength/ngMaxlength more inline with the spec. Closes #9874
This commit is contained in:
committed by
Pawel Kozlowski
parent
5c1fdff691
commit
92f87b1142
@@ -2690,9 +2690,10 @@ var maxlengthDirective = function() {
|
||||
link: function(scope, elm, attr, ctrl) {
|
||||
if (!ctrl) return;
|
||||
|
||||
var maxlength = 0;
|
||||
var maxlength = -1;
|
||||
attr.$observe('maxlength', function(value) {
|
||||
maxlength = int(value) || 0;
|
||||
var intVal = int(value);
|
||||
maxlength = isNaN(intVal) ? -1 : intVal;
|
||||
ctrl.$validate();
|
||||
});
|
||||
ctrl.$validators.maxlength = function(modelValue, viewValue) {
|
||||
|
||||
@@ -2495,6 +2495,16 @@ describe('input', function() {
|
||||
expect(inputElm).toBeValid();
|
||||
});
|
||||
|
||||
it('should only accept empty values when maxlength is 0', function() {
|
||||
compileInput('<input type="text" ng-model="value" ng-maxlength="0" />');
|
||||
|
||||
changeInputValueTo('');
|
||||
expect(inputElm).toBeValid();
|
||||
|
||||
changeInputValueTo('a');
|
||||
expect(inputElm).toBeInvalid();
|
||||
});
|
||||
|
||||
it('should accept values of any length when maxlength is negative', function() {
|
||||
compileInput('<input type="text" ng-model="value" ng-maxlength="-1" />');
|
||||
|
||||
@@ -2505,6 +2515,27 @@ describe('input', function() {
|
||||
expect(inputElm).toBeValid();
|
||||
});
|
||||
|
||||
it('should accept values of any length when maxlength is non-numeric', function() {
|
||||
compileInput('<input type="text" ng-model="value" ng-maxlength="{{maxlength}}" />');
|
||||
changeInputValueTo('aaaaaaaaaa');
|
||||
|
||||
scope.$apply('maxlength = "5"');
|
||||
expect(inputElm).toBeInvalid();
|
||||
|
||||
scope.$apply('maxlength = "abc"');
|
||||
expect(inputElm).toBeValid();
|
||||
|
||||
scope.$apply('maxlength = ""');
|
||||
expect(inputElm).toBeValid();
|
||||
|
||||
scope.$apply('maxlength = null');
|
||||
expect(inputElm).toBeValid();
|
||||
|
||||
scope.someObj = {};
|
||||
scope.$apply('maxlength = someObj');
|
||||
expect(inputElm).toBeValid();
|
||||
});
|
||||
|
||||
it('should listen on ng-maxlength when maxlength is observed', function() {
|
||||
var value = 0;
|
||||
compileInput('<input type="text" ng-model="value" ng-maxlength="max" attr-capture />');
|
||||
|
||||
Reference in New Issue
Block a user