Skip to content

Commit 3a766f1

Browse files
devversiontinayuangao
authored andcommitted
fix(autosize): properly detect line-height in firefox (#6190)
* 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
1 parent 8bb54ca commit 3a766f1

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/lib/input/autosize.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,21 @@ describe('MdTextareaAutosize', () => {
130130
.toBeGreaterThan(previousMinHeight, 'Expected the textarea to grow to two rows.');
131131
});
132132

133+
it('should calculate the proper height based on the specified amount of max rows', () => {
134+
fixture.componentInstance.content = [1, 2, 3, 4, 5, 6, 7, 8].join('\n');
135+
fixture.detectChanges();
136+
autosize.resizeToFitContent();
137+
138+
expect(textarea.clientHeight)
139+
.toBe(textarea.scrollHeight, 'Expected textarea to not have a vertical scrollbar.');
140+
141+
fixture.componentInstance.maxRows = 5;
142+
fixture.detectChanges();
143+
144+
expect(textarea.clientHeight)
145+
.toBeLessThan(textarea.scrollHeight, 'Expected textarea to have a vertical scrollbar.');
146+
});
147+
133148
it('should properly resize to content on init', () => {
134149
// Manually create the test component in this test, because in this test the first change
135150
// detection should be triggered after a multiline content is set.

src/lib/input/autosize.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ export class MdTextareaAutosize implements AfterViewInit {
126126
textareaClone.style.minHeight = '';
127127
textareaClone.style.maxHeight = '';
128128

129+
// In Firefox it happens that textarea elements are always bigger than the specified amount
130+
// of rows. This is because Firefox tries to add extra space for the horizontal scrollbar.
131+
// As a workaround that removes the extra space for the scrollbar, we can just set overflow
132+
// to hidden. This ensures that there is no invalid calculation of the line height.
133+
// See Firefox bug report: https://bugzilla.mozilla.org/show_bug.cgi?id=33654
134+
textareaClone.style.overflow = 'hidden';
135+
129136
textarea.parentNode!.appendChild(textareaClone);
130137
this._cachedLineHeight = textareaClone.clientHeight;
131138
textarea.parentNode!.removeChild(textareaClone);

0 commit comments

Comments
 (0)