From 525e1a6b8571580a97752d7e83c72a1ffa9f69df Mon Sep 17 00:00:00 2001 From: sudodoki Date: Thu, 10 Oct 2013 01:13:06 +0300 Subject: [PATCH] fix(ngList):Make ngList work with input of type email, url Validity for inputs are checked for single value using static regexp, which won't work with ngList directive on the input, since it's getting us an array of attributes. --- src/ng/directive/input.js | 20 +++-- test/ng/directive/inputSpec.js | 136 +++++++++++++++++++++++++++------ 2 files changed, 127 insertions(+), 29 deletions(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 87afc32844ca..20fd4c94f53c 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -519,10 +519,9 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) { function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) { textInputType(scope, element, attr, ctrl, $sniffer, $browser); - ctrl.$parsers.push(function(value) { var empty = ctrl.$isEmpty(value); - if (empty || NUMBER_REGEXP.test(value)) { + if (empty || NUMBER_REGEXP.test(value)) { ctrl.$setValidity('number', true); return value === '' ? null : (empty ? value : parseFloat(value)); } else { @@ -581,9 +580,14 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) { function urlInputType(scope, element, attr, ctrl, $sniffer, $browser) { textInputType(scope, element, attr, ctrl, $sniffer, $browser); - + var testedValid; var urlValidator = function(value) { - if (ctrl.$isEmpty(value) || URL_REGEXP.test(value)) { + if (isArray(value)) { + testedValid = value.every(function(val){return URL_REGEXP.test(val)}) + } else { + testedValid = URL_REGEXP.test(value) + } + if (ctrl.$isEmpty(value) || testedValid) { ctrl.$setValidity('url', true); return value; } else { @@ -600,7 +604,13 @@ function emailInputType(scope, element, attr, ctrl, $sniffer, $browser) { textInputType(scope, element, attr, ctrl, $sniffer, $browser); var emailValidator = function(value) { - if (ctrl.$isEmpty(value) || EMAIL_REGEXP.test(value)) { + var testedValid; + if (isArray(value)) { + testedValid = value.every(function(val){return EMAIL_REGEXP.test(val)}) + } else { + testedValid = EMAIL_REGEXP.test(value) + } + if (ctrl.$isEmpty(value) || testedValid) { ctrl.$setValidity('email', true); return value; } else { diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index c94eb9b898e1..607149c4d897 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -726,50 +726,138 @@ describe('input', function() { }); describe('email', function() { + describe('should validate e-mail', function(){ + it('when used simply', function() { + compileInput(''); + var widget = scope.form.alias; - it('should validate e-mail', function() { - compileInput(''); + changeInputValueTo('vojta@google.com'); + expect(scope.email).toBe('vojta@google.com'); + expect(inputElm).toBeValid(); + expect(widget.$error.email).toBe(false); - var widget = scope.form.alias; - changeInputValueTo('vojta@google.com'); + changeInputValueTo('invalid@'); + expect(scope.email).toBeUndefined(); + expect(inputElm).toBeInvalid(); + expect(widget.$error.email).toBeTruthy(); + }); + it('when used with ngList (default separator)', function() { + compileInput(''); + var widget = scope.form.alias; - expect(scope.email).toBe('vojta@google.com'); - expect(inputElm).toBeValid(); - expect(widget.$error.email).toBe(false); + changeInputValueTo('vojta@google.com'); + expect(scope.email).toEqual(['vojta@google.com']); + expect(inputElm).toBeValid(); + expect(widget.$error.email).toBe(false); - changeInputValueTo('invalid@'); - expect(scope.email).toBeUndefined(); - expect(inputElm).toBeInvalid(); - expect(widget.$error.email).toBeTruthy(); - }); + changeInputValueTo('vojta@google.com, igor.minar@gmail.com'); + expect(scope.email).toEqual(['vojta@google.com', 'igor.minar@gmail.com']); + expect(inputElm).toBeValid(); + expect(widget.$error.email).toBe(false); + + changeInputValueTo('invalid@'); + expect(scope.email).toBeUndefined(); + expect(inputElm).toBeInvalid(); + expect(widget.$error.email).toBeTruthy(); + }); + it('when used with ngList (custom separator)', function() { + compileInput(''); + var widget = scope.form.alias; + changeInputValueTo('vojta@google.com'); + expect(scope.email).toEqual(['vojta@google.com']); + expect(inputElm).toBeValid(); + expect(widget.$error.email).toBe(false); + + changeInputValueTo('vojta@google.com;igor.minar@gmail.com'); + expect(scope.email).toEqual(['vojta@google.com', 'igor.minar@gmail.com']); + expect(inputElm).toBeValid(); + expect(widget.$error.email).toBe(false); + + changeInputValueTo('vojta@google.com, dorosh@google.com'); + expect(scope.email).toBeUndefined(); + expect(inputElm).toBeInvalid(); + expect(widget.$error.email).toBeTruthy(); + + + changeInputValueTo('invalid@'); + expect(scope.email).toBeUndefined(); + expect(inputElm).toBeInvalid(); + expect(widget.$error.email).toBeTruthy(); + }); + }); - describe('EMAIL_REGEXP', function() { + describe('EMAIL_REGEXP', function(){ it('should validate email', function() { expect(EMAIL_REGEXP.test('a@b.com')).toBe(true); expect(EMAIL_REGEXP.test('a@b.museum')).toBe(true); expect(EMAIL_REGEXP.test('a@B.c')).toBe(false); }); }); + }); describe('url', function() { + describe('should validate url', function(){ + it('when used simply', function() { + compileInput(''); + var widget = scope.form.alias; - it('should validate url', function() { - compileInput(''); - var widget = scope.form.alias; + changeInputValueTo('http://www.something.com'); + expect(scope.url).toBe('http://www.something.com'); + expect(inputElm).toBeValid(); + expect(widget.$error.url).toBe(false); - changeInputValueTo('http://www.something.com'); - expect(scope.url).toBe('http://www.something.com'); - expect(inputElm).toBeValid(); - expect(widget.$error.url).toBe(false); + changeInputValueTo('invalid.com'); + expect(scope.url).toBeUndefined(); + expect(inputElm).toBeInvalid(); + expect(widget.$error.url).toBeTruthy(); + }); + it('when used with ngList (default separator)', function() { + compileInput(''); - changeInputValueTo('invalid.com'); - expect(scope.url).toBeUndefined(); - expect(inputElm).toBeInvalid(); - expect(widget.$error.url).toBeTruthy(); + var widget = scope.form.alias; + changeInputValueTo('http://www.something.com'); + expect(scope.url).toEqual(['http://www.something.com']); + expect(inputElm).toBeValid(); + expect(widget.$error.url).toBe(false); + + changeInputValueTo('http://www.something.com, http://www.somethingelse.com'); + expect(scope.url).toEqual(['http://www.something.com', 'http://www.somethingelse.com']); + expect(inputElm).toBeValid(); + expect(widget.$error.url).toBe(false); + + changeInputValueTo('http:'); + expect(scope.url).toBeUndefined(); + expect(inputElm).toBeInvalid(); + expect(widget.$error.url).toBeTruthy(); + }); + it('when used with ngList (custom separator)', function() { + compileInput(''); + + var widget = scope.form.alias; + changeInputValueTo('http://www.something.com'); + expect(scope.url).toEqual(['http://www.something.com']); + expect(inputElm).toBeValid(); + expect(widget.$error.url).toBe(false); + + changeInputValueTo('http://www.something.com;http://www.somethingelse.com'); + expect(scope.url).toEqual(['http://www.something.com', 'http://www.somethingelse.com']); + expect(inputElm).toBeValid(); + expect(widget.$error.url).toBe(false); + + changeInputValueTo('http://www.something.com, http://www.somethingelse.com'); + expect(scope.url).toBeUndefined(); + expect(inputElm).toBeInvalid(); + expect(widget.$error.url).toBeTruthy(); + + changeInputValueTo('http:'); + expect(scope.url).toBeUndefined(); + expect(inputElm).toBeInvalid(); + expect(widget.$error.url).toBeTruthy(); + }); });