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

feat(ngList): add support for preserving blank tokens #12830

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 13 additions & 9 deletions src/ng/directive/ngList.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
* delimiter is a comma followed by a space - equivalent to `ng-list=", "`. You can specify a custom
* delimiter as the value of the `ngList` attribute - for example, `ng-list=" | "`.
*
* The behaviour of the directive is affected by the use of the `ngTrim` attribute.
* * If `ngTrim` is set to `"false"` then whitespace around both the separator and each
* @param {string=} ngList optional delimiter that should be used to split the value.
* @param {boolean=} ngTrim If set to `"false"`, then whitespace around both the separator and each
* list item is respected. This implies that the user of the directive is responsible for
* dealing with whitespace but also allows you to use whitespace as a delimiter, such as a
* tab or newline character.
* * Otherwise whitespace around the delimiter is ignored when splitting (although it is respected
* when joining the list items back together) and whitespace around each list item is stripped
* before it is added to the model.
*
* ### Example with Validation
* Otherwise whitespace around the delimiter is ignored when splitting
* (although it is respected when joining the list items back together) and whitespace around
* each list item is stripped before it is added to the model.
* @param {boolean=} preserveBlanks If set to true, then empty tokens will be included in resulting
* array
*
* @example
* ### With Validation
*
* <example name="ngList-directive" module="listExample">
* <file name="app.js">
Expand Down Expand Up @@ -66,7 +70,7 @@
* </file>
* </example>
*
* ### Example - splitting on whitespace
* ### Splitting on whitespace
* <example name="ngList-directive-newlines">
* <file name="index.html">
* <textarea ng-model="list" ng-list="&#10;" ng-trim="false"></textarea>
Expand All @@ -83,7 +87,6 @@
* </example>
*
* @element input
* @param {string=} ngList optional delimiter that should be used to split the value.
*/
var ngListDirective = function() {
return {
Expand All @@ -96,6 +99,7 @@ var ngListDirective = function() {
var ngList = element.attr(attr.$attr.ngList) || ', ';
var trimValues = attr.ngTrim !== 'false';
var separator = trimValues ? trim(ngList) : ngList;
var preserveBlanks = attr.preserveBlanks === 'true';

var parse = function(viewValue) {
// If the viewValue is invalid (say required but empty) it will be `undefined`
Expand All @@ -105,7 +109,7 @@ var ngListDirective = function() {

if (viewValue) {
forEach(viewValue.split(separator), function(value) {
if (value) list.push(trimValues ? trim(value) : value);
if (preserveBlanks || value) list.push(trimValues ? trim(value) : value);
});
}

Expand Down
21 changes: 21 additions & 0 deletions test/ng/directive/ngListSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,26 @@ describe('ngList', function() {
expect($rootScope.list).toEqual(['a','b']);
});
});

describe('preserveBlanks', function() {
it('should not preserve blanks when undefined', function() {
helper.compileInput('<input type="text" ng-model="list" ng-list="|" />');
helper.changeInputValueTo('a||b');
expect($rootScope.list).toEqual(['a','b']);
});

it('should not preserve blanks when false', function() {
helper.compileInput('<input type="text" ng-model="list" ng-list="|" preserve-blanks="false" />');
helper.changeInputValueTo('a||b');
expect($rootScope.list).toEqual(['a','b']);
});

it('should preserve blanks when true', function() {
helper.compileInput('<input type="text" ng-model="list" ng-list="|" preserve-blanks="true" />');
helper.changeInputValueTo('a||b');
expect($rootScope.list).toEqual(['a', '', 'b']);
});
});

});