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

Commit c6c9d26

Browse files
purcellpetebacondarwin
authored andcommitted
fix(ngList): use custom separators for re-joining list items
The separator string used to split the view value into a list for the model value is now used to join the list items back together again for the view value. BREAKING CHANGE: The `ngList` directive no longer supports splitting the view value via a regular expression. We need to be able to re-join list items back together and doing this when you can split with regular expressions can lead to inconsistent behaviour and would be much more complex to support. If your application relies upon ngList splitting with a regular expression then you should either try to convert the separator to a simple string or you can implement your own version of this directive for you application. Closes #4008 Closes #2561 Closes #4344
1 parent 7c31220 commit c6c9d26

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/ng/directive/input.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -2353,8 +2353,7 @@ var minlengthDirective = function() {
23532353
* can be a fixed string (by default a comma) or a regular expression.
23542354
*
23552355
* @element input
2356-
* @param {string=} ngList optional delimiter that should be used to split the value. If
2357-
* specified in form `/something/` then the value will be converted into a regular expression.
2356+
* @param {string=} ngList optional delimiter that should be used to split the value.
23582357
*
23592358
* @example
23602359
<example name="ngList-directive" module="listExample">
@@ -2403,8 +2402,7 @@ var ngListDirective = function() {
24032402
return {
24042403
require: 'ngModel',
24052404
link: function(scope, element, attr, ctrl) {
2406-
var match = /\/(.*)\//.exec(attr.ngList),
2407-
separator = match && new RegExp(match[1]) || attr.ngList || ',';
2405+
var separator = attr.ngList || ', ';
24082406

24092407
var parse = function(viewValue) {
24102408
// If the viewValue is invalid (say required but empty) it will be `undefined`
@@ -2413,7 +2411,7 @@ var ngListDirective = function() {
24132411
var list = [];
24142412

24152413
if (viewValue) {
2416-
forEach(viewValue.split(separator), function(value) {
2414+
forEach(viewValue.split(trim(separator)), function(value) {
24172415
if (value) list.push(trim(value));
24182416
});
24192417
}
@@ -2424,7 +2422,7 @@ var ngListDirective = function() {
24242422
ctrl.$parsers.push(parse);
24252423
ctrl.$formatters.push(function(value) {
24262424
if (isArray(value)) {
2427-
return value.join(', ');
2425+
return value.join(separator);
24282426
}
24292427

24302428
return undefined;

test/ng/directive/inputSpec.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -2633,7 +2633,7 @@ describe('input', function() {
26332633
it("should not clobber text if model changes due to itself", function() {
26342634
// When the user types 'a,b' the 'a,' stage parses to ['a'] but if the
26352635
// $parseModel function runs it will change to 'a', in essence preventing
2636-
// the user from ever typying ','.
2636+
// the user from ever typing ','.
26372637
compileInput('<input type="text" ng-model="list" ng-list />');
26382638

26392639
changeInputValueTo('a ');
@@ -2675,22 +2675,23 @@ describe('input', function() {
26752675
it('should allow custom separator', function() {
26762676
compileInput('<input type="text" ng-model="list" ng-list=":" />');
26772677

2678+
scope.$apply(function() {
2679+
scope.list = ['x', 'y', 'z'];
2680+
});
2681+
expect(inputElm.val()).toBe('x:y:z');
2682+
26782683
changeInputValueTo('a,a');
26792684
expect(scope.list).toEqual(['a,a']);
26802685

26812686
changeInputValueTo('a:b');
26822687
expect(scope.list).toEqual(['a', 'b']);
26832688
});
26842689

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

2686-
it('should allow regexp as a separator', function() {
2687-
compileInput('<input type="text" ng-model="list" ng-list="/:|,/" />');
2688-
2689-
changeInputValueTo('a,b');
2693+
changeInputValueTo('a|b');
26902694
expect(scope.list).toEqual(['a', 'b']);
2691-
2692-
changeInputValueTo('a,b: c');
2693-
expect(scope.list).toEqual(['a', 'b', 'c']);
26942695
});
26952696
});
26962697

0 commit comments

Comments
 (0)