Skip to content

Instantly share code, notes, and snippets.

@yjbanov
Created May 8, 2024 17:18
Show Gist options
  • Save yjbanov/fca183b9272b0ddb379f686cf095a972 to your computer and use it in GitHub Desktop.
Save yjbanov/fca183b9272b0ddb379f686cf095a972 to your computer and use it in GitHub Desktop.
diff --git a/lib/web_ui/lib/src/engine/semantics/label_and_value.dart b/lib/web_ui/lib/src/engine/semantics/label_and_value.dart
index b1e5cbbfab..8c28be3d08 100644
--- a/lib/web_ui/lib/src/engine/semantics/label_and_value.dart
+++ b/lib/web_ui/lib/src/engine/semantics/label_and_value.dart
@@ -242,12 +242,9 @@ final class SizedSpanRepresentation extends LabelRepresentationBehavior {
// `inline` does not support them.
..display = 'inline-block'
- // Stretch the text full width because if the text is broken up into a
- // multi-line paragraph the width of the paragraph can become smaller than
- // the offsetWidth of the element, and therefore not fully cover the rect
- // of the parent. "justify" will ensure the text is stretched edge to edge
- // covering full offsetWidth.
- ..textAlign = 'justify'
+ // Do not wrap text based on parent constraints. Instead, to fit in the
+ // parent's box the text will be scaled.
+ ..whiteSpace = 'nowrap'
// The origin of the coordinate system is the top-left corner of the
// parent element.
@@ -354,7 +351,14 @@ final class SizedSpanRepresentation extends LabelRepresentationBehavior {
final List<_Measurement> measurements = <_Measurement>[];
- // Step 1: measure all spans in a single batch prior to updating their CSS
+ // Step 1: set `display` to `inline` so that the measurement measures the
+ // true size of the text. Update all spans in a batch so that the
+ // measurement can be done in a single page reflow.
+ for (final _QueuedSizeUpdate update in queue) {
+ update.representation._domText.style.display = 'inline';
+ }
+
+ // Step 2: measure all spans in a single batch prior to updating their CSS
// styles. This way, all measurements are taken with a single reflow.
// Interleaving measurements with updates, will cause the browser to
// reflow the page between measurements.
@@ -377,7 +381,7 @@ final class SizedSpanRepresentation extends LabelRepresentationBehavior {
));
}
- // Step 2: update all spans at a batch without taking any further DOM
+ // Step 3: update all spans at a batch without taking any further DOM
// measurements, which avoids additional reflows.
for (final _Measurement measurement in measurements) {
final SizedSpanRepresentation representation = measurement.representation;
@@ -385,6 +389,9 @@ final class SizedSpanRepresentation extends LabelRepresentationBehavior {
final double domHeight = measurement.domSize.height;
final ui.Size targetSize = measurement.targetSize;
+ // Reset back to `inline-block` (it was set to `inline` in Step 1).
+ representation._domText.style.display = 'inline-block';
+
if (domWidth < 1 && domHeight < 1) {
// Don't bother dealing with divisions by tiny numbers. This probably means
// the label is empty or doesn't contain anything that would be visible to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment