[web] Fix text measurement when constraint width is infinite (flutter/engine#14132)

This commit is contained in:
Mouad Debbar
2019-12-04 13:27:14 -08:00
committed by GitHub
parent ce4d1dc06f
commit 51d237a089
2 changed files with 46 additions and 14 deletions

View File

@@ -210,9 +210,25 @@ class TextDimensions {
}
/// Updated element style width.
void updateWidth(String cssWidth) {
void updateConstraintWidth(double width, String ellipsis) {
_invalidateBoundsCache();
_element.style.width = cssWidth;
if (width.isInfinite) {
_element.style
..width = null
..whiteSpace = 'pre';
} else if (ellipsis != null) {
// Width is finite, but we don't want to let the text soft-wrap when
// ellipsis overflow is enabled.
_element.style
..width = '${width}px'
..whiteSpace = 'pre';
} else {
// Width is finite and there's no ellipsis overflow.
_element.style
..width = '${width}px'
..whiteSpace = 'pre-wrap';
}
}
void _invalidateBoundsCache() {
@@ -475,17 +491,8 @@ class ParagraphRuler {
..display = 'block'
..overflowWrap = 'break-word';
// TODO(flutter_web): Implement the ellipsis overflow for multi-line text
// too. As a pre-requisite, we need to be able to programmatically find
// line breaks.
if (style.ellipsis == null) {
elementStyle.whiteSpace = 'pre-wrap';
} else {
// The height measurement is affected by whether the text has the ellipsis
// overflow property or not. This is because when ellipsis is set, we may
// not render all the lines, but stop at the first line that overflows.
if (style.ellipsis != null) {
elementStyle
..whiteSpace = 'pre'
..overflow = 'hidden'
..textOverflow = 'ellipsis';
}
@@ -595,7 +602,10 @@ class ParagraphRuler {
// The extra 0.5 is because sometimes the browser needs slightly more space
// than the size it reports back. When that happens the text may be wrap
// when we thought it didn't.
constrainedDimensions.updateWidth('${constraints.width + 0.5}px');
constrainedDimensions.updateConstraintWidth(
constraints.width + 0.5,
style.ellipsis,
);
}
/// Returns text position in a paragraph that contains multiple
@@ -701,7 +711,7 @@ class ParagraphRuler {
..appendText(before)
..append(rangeSpan)
..appendText(after);
constrainedDimensions.updateWidth('${constraints.width}px');
constrainedDimensions.updateConstraintWidth(constraints.width, null);
// Measure the rects of [rangeSpan].
final List<html.Rectangle<num>> clientRects = rangeSpan.getClientRects();

View File

@@ -406,6 +406,28 @@ void main() async {
}
});
testMeasurements(
'wraps multi-line text correctly when constraint width is infinite',
(TextMeasurementService instance) {
final EngineParagraph paragraph = build(ahemStyle, '123\n456 789');
final MeasurementResult result =
instance.measure(paragraph, infiniteConstraints);
expect(result.isSingleLine, false);
expect(result.maxIntrinsicWidth, 70);
expect(result.minIntrinsicWidth, 30);
expect(result.width, double.infinity);
expect(result.height, 20);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('123', hardBreak: true, width: 30.0, lineNumber: 0),
line('456 789', hardBreak: true, width: 70.0, lineNumber: 1),
]);
}
},
);
testMeasurements(
'takes letter spacing into account',
(TextMeasurementService instance) {