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

Commit

Permalink
fix(tagsInput): Fix add-on-paste issue in IE
Browse files Browse the repository at this point in the history
Fix an error when pasting in IE. IE uses window.clipboardData
instead of putting clipboardData on the event itself.

Closes #325.
  • Loading branch information
LoganBarnett authored and mbenford committed Mar 15, 2015
1 parent 18760b2 commit e752682
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/tags-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* @param {expression} onTagRemoving Expression to evaluate that will be invoked before removing a tag. The tag is available as $tag. This method must return either true or false. If false, the tag will not be removed.
* @param {expression} onTagRemoved Expression to evaluate upon removing an existing tag. The removed tag is available as $tag.
*/
tagsInput.directive('tagsInput', function($timeout, $document, tagsInputConfig, tiUtil) {
tagsInput.directive('tagsInput', function($timeout, $document, $window, tagsInputConfig, tiUtil) {
function TagList(options, events, onTagAdding, onTagRemoving) {
var self = {}, getTagText, setTagText, tagIsValid;

Expand Down Expand Up @@ -383,7 +383,13 @@ tagsInput.directive('tagsInput', function($timeout, $document, tagsInputConfig,
})
.on('input-paste', function(event) {
if (options.addOnPaste) {
var data = event.clipboardData.getData('text/plain');
var data;
if(event.clipboardData) {
data = event.clipboardData.getData('text/plain');
}
else {
data = $window.clipboardData.getData('Text');
}
var tags = data.split(options.pasteSplitPattern);
if (tags.length > 1) {
tags.forEach(function(tag) {
Expand Down
24 changes: 22 additions & 2 deletions test/tags-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

describe('tags-input directive', function() {
var $compile, $scope, $timeout, $document,
isolateScope, element;
$window, isolateScope, element;

beforeEach(function() {
jasmine.addMatchers(customMatchers);

module('ngTagsInput');

inject(function(_$compile_, _$rootScope_, _$document_, _$timeout_) {
inject(function(_$compile_, _$rootScope_, _$document_, _$timeout_, _$window_) {
$compile = _$compile_;
$scope = _$rootScope_;
$document = _$document_;
$timeout = _$timeout_;
$window = _$window_;
});
});

Expand Down Expand Up @@ -654,6 +655,25 @@ describe('tags-input directive', function() {
expect(isolateScope.options.addOnPaste).toBe(false);
});

it('works in IE', function() {
// Arrange
compile('add-on-paste="true"');
$window.clipboardData = eventData.clipboardData;
eventData.clipboardData = undefined;
$window.clipboardData.getData.and.returnValue('tag1, tag2, tag3');

// Act
var event = jQuery.Event('paste', eventData);
getInput().trigger(event);

// Assert
expect($scope.tags).toEqual([
{ text: 'tag1' },
{ text: 'tag2' },
{ text: 'tag3' }
]);
});

it('splits the pasted text into tags if there is more than one tag and the option is true', function() {
// Arrange
compile('add-on-paste="true"');
Expand Down

0 comments on commit e752682

Please sign in to comment.