Skip to content

fix(autosize): properly detect line-height in firefox #6190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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