Skip to content

Commit f8118ec

Browse files
committed
fix(input): make ngList honor custom separators
Remove support for regexp separators, and correctly use custom separator when formatting values into the control. See angular#4008 and angular#2561.
1 parent f031430 commit f8118ec

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/ng/directive/input.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,8 +1314,7 @@ var requiredDirective = function() {
13141314
* can be a fixed string (by default a comma) or a regular expression.
13151315
*
13161316
* @element input
1317-
* @param {string=} ngList optional delimiter that should be used to split the value. If
1318-
* specified in form `/something/` then the value will be converted into a regular expression.
1317+
* @param {string=} ngList optional delimiter that should be used to split the value.
13191318
*
13201319
* @example
13211320
<doc:example>
@@ -1357,8 +1356,7 @@ var ngListDirective = function() {
13571356
return {
13581357
require: 'ngModel',
13591358
link: function(scope, element, attr, ctrl) {
1360-
var match = /\/(.*)\//.exec(attr.ngList),
1361-
separator = match && new RegExp(match[1]) || attr.ngList || ',';
1359+
var separator = attr.ngList || ', ';
13621360

13631361
var parse = function(viewValue) {
13641362
// If the viewValue is invalid (say required but empty) it will be `undefined`
@@ -1367,7 +1365,7 @@ var ngListDirective = function() {
13671365
var list = [];
13681366

13691367
if (viewValue) {
1370-
forEach(viewValue.split(separator), function(value) {
1368+
forEach(viewValue.split(trim(separator)), function(value) {
13711369
if (value) list.push(trim(value));
13721370
});
13731371
}
@@ -1378,7 +1376,7 @@ var ngListDirective = function() {
13781376
ctrl.$parsers.push(parse);
13791377
ctrl.$formatters.push(function(value) {
13801378
if (isArray(value)) {
1381-
return value.join(', ');
1379+
return value.join(separator);
13821380
}
13831381

13841382
return undefined;

test/ng/directive/inputSpec.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ describe('input', function() {
970970
it("should not clobber text if model changes due to itself", function() {
971971
// When the user types 'a,b' the 'a,' stage parses to ['a'] but if the
972972
// $parseModel function runs it will change to 'a', in essence preventing
973-
// the user from ever typying ','.
973+
// the user from ever typing ','.
974974
compileInput('<input type="text" ng-model="list" ng-list />');
975975

976976
changeInputValueTo('a ');
@@ -1012,22 +1012,23 @@ describe('input', function() {
10121012
it('should allow custom separator', function() {
10131013
compileInput('<input type="text" ng-model="list" ng-list=":" />');
10141014

1015+
scope.$apply(function() {
1016+
scope.list = ['x', 'y', 'z'];
1017+
});
1018+
expect(inputElm.val()).toBe('x:y:z');
1019+
10151020
changeInputValueTo('a,a');
10161021
expect(scope.list).toEqual(['a,a']);
10171022

10181023
changeInputValueTo('a:b');
10191024
expect(scope.list).toEqual(['a', 'b']);
10201025
});
10211026

1027+
it('should ignore separator whitespace when splitting', function() {
1028+
compileInput('<input type="text" ng-model="list" ng-list=" | " />');
10221029

1023-
it('should allow regexp as a separator', function() {
1024-
compileInput('<input type="text" ng-model="list" ng-list="/:|,/" />');
1025-
1026-
changeInputValueTo('a,b');
1030+
changeInputValueTo('a|b');
10271031
expect(scope.list).toEqual(['a', 'b']);
1028-
1029-
changeInputValueTo('a,b: c');
1030-
expect(scope.list).toEqual(['a', 'b', 'c']);
10311032
});
10321033
});
10331034

0 commit comments

Comments
 (0)