Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Commit

Permalink
fix(tagsInput): Prevent an empty tag from being added
Browse files Browse the repository at this point in the history
Prevent an empty tag from being added when a certain combination of
validation options allows it, which can also set the input as invalid
in some cases.

Closes #172
  • Loading branch information
mbenford committed Jul 9, 2014
1 parent 57b0825 commit c104c2b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/tags-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ tagsInput.directive('tagsInput', function($timeout, $document, tagsInputConfig)
var self = {}, getTagText, setTagText, tagIsValid;

getTagText = function(tag) {
return tag[options.displayProperty];
return safeToString(tag[options.displayProperty]);
};

setTagText = function(tag, text) {
Expand All @@ -50,7 +50,8 @@ tagsInput.directive('tagsInput', function($timeout, $document, tagsInputConfig)
tagIsValid = function(tag) {
var tagText = getTagText(tag);

return tagText.length >= options.minLength &&
return tagText &&
tagText.length >= options.minLength &&
tagText.length <= options.maxLength &&
options.allowedTagsPattern.test(tagText) &&
!findInObjectArray(self.items, tag, options.displayProperty);
Expand All @@ -65,7 +66,7 @@ tagsInput.directive('tagsInput', function($timeout, $document, tagsInputConfig)
};

self.add = function(tag) {
var tagText = getTagText(tag).trim();
var tagText = getTagText(tag);

if (options.replaceSpacesWithDashes) {
tagText = tagText.replace(/\s/g, '-');
Expand All @@ -77,7 +78,7 @@ tagsInput.directive('tagsInput', function($timeout, $document, tagsInputConfig)
self.items.push(tag);
events.trigger('tag-added', { $tag: tag });
}
else {
else if (tagText) {
events.trigger('invalid-tag', { $tag: tag });
}

Expand Down
12 changes: 12 additions & 0 deletions test/tags-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,18 @@ describe('tags-input directive', function() {
{ text: 'Item3' }
]);
});

it('does not add an empty tag', function() {
// Arrange
compile('min-length="0"', 'allowed-tags-pattern=".*"');

// Act
newTag('');

// Assert
expect($scope.tags).toEqual([]);
expect(isolateScope.newTag.invalid).toBeFalsy();
});
});

describe('focus outline', function() {
Expand Down

0 comments on commit c104c2b

Please sign in to comment.