From e752682d067cf48a2969820fb4f383bcbf807fa9 Mon Sep 17 00:00:00 2001 From: Logan Barnett Date: Wed, 31 Dec 2014 14:06:56 -0800 Subject: [PATCH] fix(tagsInput): Fix add-on-paste issue in IE Fix an error when pasting in IE. IE uses window.clipboardData instead of putting clipboardData on the event itself. Closes #325. --- src/tags-input.js | 10 ++++++++-- test/tags-input.spec.js | 24 ++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/tags-input.js b/src/tags-input.js index daf70d10..7b9d33ad 100644 --- a/src/tags-input.js +++ b/src/tags-input.js @@ -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; @@ -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) { diff --git a/test/tags-input.spec.js b/test/tags-input.spec.js index 1b2fa8f8..19bc991d 100644 --- a/test/tags-input.spec.js +++ b/test/tags-input.spec.js @@ -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_; }); }); @@ -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"');