diff --git a/src/autosize.js b/src/autosize.js index 0ddca074..ff6c1c46 100644 --- a/src/autosize.js +++ b/src/autosize.js @@ -30,12 +30,14 @@ tagsInput.directive('tiAutosize', function() { value = attrs.placeholder; } - span.text(value); - span.css('display', ''); - width = span.prop('offsetWidth') + THRESHOLD; - span.css('display', 'none'); + if (value) { + span.text(value); + span.css('display', ''); + width = span.prop('offsetWidth'); + span.css('display', 'none'); + } - element.css('width', width + 'px'); + element.css('width', width ? width + THRESHOLD + 'px' : ''); return originalValue; }; diff --git a/test/autosize.spec.js b/test/autosize.spec.js index bbc4b444..5d4965ac 100644 --- a/test/autosize.spec.js +++ b/test/autosize.spec.js @@ -13,7 +13,7 @@ describe('autosize directive', function() { }); style = angular.element('').appendTo('head'); - container = angular.element('
').appendTo('body'); + container = angular.element('
').appendTo('body'); }); afterEach(function() { @@ -82,4 +82,17 @@ describe('autosize directive', function() { // Assert expect(element.css('width')).toBe(getTextWidth('Some placeholder')); }); + + it('clears the input width when it cannot be calculated', function() { + // Arrange + container.hide(); + compile(); + + // Act + element.val('AAAAAAAAAAAAAA'); + element.trigger('input'); + + // Assert + expect(element.prop('style').width).toBe(''); + }); }); \ No newline at end of file diff --git a/test/test-page.html b/test/test-page.html index 7291fa41..8f0f949a 100644 --- a/test/test-page.html +++ b/test/test-page.html @@ -14,6 +14,10 @@ padding-left: 3px; } + .foo .tags .input { + width: 1036px; + } + .foo.ng-pristine .tags { background-color: green; } @@ -33,7 +37,7 @@ -