8000 feat(ngMaxlength): disable maxlength when not set to a non-negative integer by gkalpak · Pull Request #9998 · angular/angular.js · GitHub
[go: up one dir, main page]

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

feat(ngMaxlength): disable maxlength when not set to a non-negative integer #9998

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(ngMaxlength): ignore maxlength when not set to a non-negative int…
…eger

This makes the behaviour of maxlength/ngMaxlength more inline with the
spec.

Closes #9874
  • Loading branch information
gkalpak committed Nov 21, 2014
commit a584255be2c393c7a1fd3061c9b75ffc3c4703e1
5 changes: 3 additions & 2 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
31 changes: 31 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" />');

Expand All @@ -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 />');
Expand Down
0