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

fix(input): make ngList honor custom separators #4344

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 4 additions & 6 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1314,8 +1314,7 @@ var requiredDirective = function() {
* can be a fixed string (by default a comma) or a regular expression.
*
* @element input
* @param {string=} ngList optional delimiter that should be used to split the value. If
* specified in form `/something/` then the value will be converted into a regular expression.
* @param {string=} ngList optional delimiter that should be used to split the value.
*
* @example
<doc:example>
Expand Down Expand Up @@ -1357,8 +1356,7 @@ var ngListDirective = function() {
return {
require: 'ngModel',
link: function(scope, element, attr, ctrl) {
var match = /\/(.*)\//.exec(attr.ngList),
separator = match && new RegExp(match[1]) || attr.ngList || ',';
var separator = attr.ngList || ', ';

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

if (viewValue) {
forEach(viewValue.split(separator), function(value) {
forEach(viewValue.split(trim(separator)), function(value) {
if (value) list.push(trim(value));
});
}
Expand All @@ -1378,7 +1376,7 @@ var ngListDirective = function() {
ctrl.$parsers.push(parse);
ctrl.$formatters.push(function(value) {
if (isArray(value)) {
return value.join(', ');
return value.join(separator);
}

return undefined;
Expand Down
17 changes: 9 additions & 8 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ describe('input', function() {
it("should not clobber text if model changes due to itself", function() {
// When the user types 'a,b' the 'a,' stage parses to ['a'] but if the
// $parseModel function runs it will change to 'a', in essence preventing
// the user from ever typying ','.
// the user from ever typing ','.
compileInput('<input type="text" ng-model="list" ng-list />');

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

scope.$apply(function() {
scope.list = ['x', 'y', 'z'];
});
expect(inputElm.val()).toBe('x:y:z');

changeInputValueTo('a,a');
expect(scope.list).toEqual(['a,a']);

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

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

it('should allow regexp as a separator', function() {
compileInput('<input type="text" ng-model="list" ng-list="/:|,/" />');

changeInputValueTo('a,b');
changeInputValueTo('a|b');
expect(scope.list).toEqual(['a', 'b']);

changeInputValueTo('a,b: c');
expect(scope.list).toEqual(['a', 'b', 'c']);
});
});

Expand Down