Skip to content

Commit

Permalink
fix(autosize): properly detect line-height in firefox (#6190)
Browse files Browse the repository at this point in the history
* fix(autosize): properly detect line-height in firefox

In Firefox it happens that textarea elements are always bigger than the specified amount of rows. This is because Firefox tries to add extra space for the horizontal scrollbar.

As a workaround that removes the extra space for the scrollbar, we can just set overflow to hidden. This ensures that there is no invalid calculation of the line height.

See Firefox bug report: https://bugzilla.mozilla.org/show_bug.cgi?id=33654

Fixes #6179

* Fix test name
  • Loading branch information
devversion authored and tinayuangao committed Aug 1, 2017
1 parent 8bb54ca commit 3a766f1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/lib/input/autosize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ describe('MdTextareaAutosize', () => {
.toBeGreaterThan(previousMinHeight, 'Expected the textarea to grow to two rows.');
});

it('should calculate the proper height based on the specified amount of max rows', () => {
fixture.componentInstance.content = [1, 2, 3, 4, 5, 6, 7, 8].join('\n');
fixture.detectChanges();
autosize.resizeToFitContent();

expect(textarea.clientHeight)
.toBe(textarea.scrollHeight, 'Expected textarea to not have a vertical scrollbar.');

fixture.componentInstance.maxRows = 5;
fixture.detectChanges();

expect(textarea.clientHeight)
.toBeLessThan(textarea.scrollHeight, 'Expected textarea to have a vertical scrollbar.');
});

it('should properly resize to content on init', () => {
// Manually create the test component in this test, because in this test the first change
// detection should be triggered after a multiline content is set.
Expand Down
7 changes: 7 additions & 0 deletions src/lib/input/autosize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ export class MdTextareaAutosize implements AfterViewInit {
textareaClone.style.minHeight = '';
textareaClone.style.maxHeight = '';

// In Firefox it happens that textarea elements are always bigger than the specified amount
// of rows. This is because Firefox tries to add extra space for the horizontal scrollbar.
// As a workaround that removes the extra space for the scrollbar, we can just set overflow
// to hidden. This ensures that there is no invalid calculation of the line height.
// See Firefox bug report: https://bugzilla.mozilla.org/show_bug.cgi?id=33654
textareaClone.style.overflow = 'hidden';

textarea.parentNode!.appendChild(textareaClone);
this._cachedLineHeight = textareaClone.clientHeight;
textarea.parentNode!.removeChild(textareaClone);
Expand Down

0 comments on commit 3a766f1

Please sign in to comment.