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

Commit

Permalink
feat(select): allow multiline ng-options
Browse files Browse the repository at this point in the history
This patch allows the ng-options value of a <select> element to span
multiple lines, which would previously throw an error when used with filters.

Closes #5602
  • Loading branch information
jrupac authored and jeffbcross committed Jan 10, 2014
1 parent a9cccbe commit 43a2f3d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ng/directive/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ var ngOptionsDirective = valueFn({ terminal: true });
// jshint maxlen: false
var selectDirective = ['$compile', '$parse', function($compile, $parse) {
//0000111110000000000022220000000000000000000000333300000000000000444444444444444000000000555555555555555000000066666666666666600000000000000007777000000000000000000088888
var NG_OPTIONS_REGEXP = /^\s*(.*?)(?:\s+as\s+(.*?))?(?:\s+group\s+by\s+(.*))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+(.*?)(?:\s+track\s+by\s+(.*?))?$/,
var NG_OPTIONS_REGEXP = /^\s*([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+group\s+by\s+([\s\S]+?))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?$/,

This comment has been minimized.

Copy link
@eternalmatt

eternalmatt Jan 13, 2014

Can you explain this change a little bit? What does ([\s\S]+?) add? What was the purpose of (.*?) ? It seems equivalent to (.*).

This comment has been minimized.

Copy link
@sbstp

sbstp May 27, 2014

@eternalmatt . includes everything but new line characters. [\s\S] includes everything that is white-space and everything that isn't, therefore everything.

nullModelCtrl = {$setViewValue: noop};
// jshint maxlen: 100

Expand Down
25 changes: 25 additions & 0 deletions test/ng/directive/selectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,31 @@ describe('select', function() {
expect(sortedHtml(options[0])).toEqual('<option value="regularProperty">visible</option>');
});

it('should allow expressions over multiple lines', function() {
scope.isNotFoo = function(item) {
return item.name !== 'Foo';
};

createSelect({
'ng-options': 'key.id\n' +
'for key in object\n' +
'| filter:isNotFoo',
'ng-model': 'selected'
});

scope.$apply(function() {
scope.object = [{'id': 1, 'name': 'Foo'},
{'id': 2, 'name': 'Bar'},
{'id': 3, 'name': 'Baz'}];
scope.selected = scope.object[0];
});

var options = element.find('option');
expect(options.length).toEqual(3);
expect(sortedHtml(options[1])).toEqual('<option value="0">2</option>');
expect(sortedHtml(options[2])).toEqual('<option value="1">3</option>');
});

describe('binding', function() {

it('should bind to scope value', function() {
Expand Down

0 comments on commit 43a2f3d

Please sign in to comment.