[web] Render ellipsis for overflowing text in DOM mode (flutter/engine#25556)
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
repository: https://github.com/flutter/goldens.git
|
||||
revision: 6d0f8e73356fe57e7a87eaae9f7257435fda3489
|
||||
revision: dbb1b70d0e6766dac4a4ecea00d9de59322a7076
|
||||
|
||||
@@ -157,18 +157,11 @@ class CanvasParagraph implements EngineParagraph {
|
||||
..height = '${height}px';
|
||||
}
|
||||
|
||||
if (paragraphStyle._ellipsis != null &&
|
||||
(paragraphStyle._maxLines == null || paragraphStyle._maxLines == 1)) {
|
||||
cssStyle
|
||||
..width = '${width}px'
|
||||
..overflowX = 'hidden'
|
||||
..textOverflow = 'ellipsis';
|
||||
}
|
||||
|
||||
// 2. Append all spans to the paragraph.
|
||||
|
||||
ParagraphSpan? span;
|
||||
late html.HtmlElement element;
|
||||
|
||||
html.HtmlElement element = rootElement;
|
||||
final List<EngineLineMetrics> lines = computeLineMetrics();
|
||||
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
@@ -177,7 +170,8 @@ class CanvasParagraph implements EngineParagraph {
|
||||
domRenderer.append(element, domRenderer.createElement('br'));
|
||||
}
|
||||
|
||||
for (final RangeBox box in lines[i].boxes!) {
|
||||
final EngineLineMetrics line = lines[i];
|
||||
for (final RangeBox box in line.boxes!) {
|
||||
if (box is SpanBox) {
|
||||
if (box.span != span) {
|
||||
span = box.span;
|
||||
@@ -203,6 +197,11 @@ class CanvasParagraph implements EngineParagraph {
|
||||
throw UnimplementedError('Unknown box type: ${box.runtimeType}');
|
||||
}
|
||||
}
|
||||
|
||||
final String? ellipsis = line.ellipsis;
|
||||
if (ellipsis != null) {
|
||||
domRenderer.appendText(element, ellipsis);
|
||||
}
|
||||
}
|
||||
|
||||
return rootElement;
|
||||
|
||||
@@ -15,8 +15,6 @@ import 'helper.dart';
|
||||
|
||||
typedef CanvasTest = FutureOr<void> Function(EngineCanvas canvas);
|
||||
|
||||
const Rect bounds = Rect.fromLTWH(0, 0, 800, 600);
|
||||
|
||||
void main() {
|
||||
internalBootstrapBrowserTest(() => testMain);
|
||||
}
|
||||
@@ -24,26 +22,28 @@ void main() {
|
||||
void testMain() async {
|
||||
setUpStableTestFonts();
|
||||
|
||||
test('ellipsis', () {
|
||||
final canvas = BitmapCanvas(bounds, RenderStrategy());
|
||||
|
||||
void testEllipsis(EngineCanvas canvas) {
|
||||
Offset offset = Offset.zero;
|
||||
CanvasParagraph paragraph;
|
||||
|
||||
final double fontSize = 22.0;
|
||||
final double width = 126.0;
|
||||
final double padding = 20.0;
|
||||
|
||||
paragraph = rich(
|
||||
ParagraphStyle(fontFamily: 'Roboto', ellipsis: '...'),
|
||||
ParagraphStyle(fontFamily: 'Roboto', fontSize: fontSize, ellipsis: '...'),
|
||||
(builder) {
|
||||
builder.pushStyle(EngineTextStyle.only(color: blue));
|
||||
builder.addText('Lorem ');
|
||||
builder.pushStyle(EngineTextStyle.only(color: green));
|
||||
builder.addText('ipsum');
|
||||
},
|
||||
)..layout(constrain(80.0));
|
||||
)..layout(constrain(width));
|
||||
canvas.drawParagraph(paragraph, offset);
|
||||
offset = offset.translate(0, paragraph.height + 10);
|
||||
offset = offset.translate(0, paragraph.height + padding);
|
||||
|
||||
paragraph = rich(
|
||||
ParagraphStyle(fontFamily: 'Roboto', ellipsis: '...'),
|
||||
ParagraphStyle(fontFamily: 'Roboto', fontSize: fontSize, ellipsis: '...'),
|
||||
(builder) {
|
||||
builder.pushStyle(EngineTextStyle.only(color: blue));
|
||||
builder.addText('Lorem\n');
|
||||
@@ -52,12 +52,12 @@ void testMain() async {
|
||||
builder.pushStyle(EngineTextStyle.only(color: red));
|
||||
builder.addText('dolor sit');
|
||||
},
|
||||
)..layout(constrain(80.0));
|
||||
)..layout(constrain(width));
|
||||
canvas.drawParagraph(paragraph, offset);
|
||||
offset = offset.translate(0, paragraph.height + 10);
|
||||
offset = offset.translate(0, paragraph.height + padding);
|
||||
|
||||
paragraph = rich(
|
||||
ParagraphStyle(fontFamily: 'Roboto', ellipsis: '...'),
|
||||
ParagraphStyle(fontFamily: 'Roboto', fontSize: fontSize, ellipsis: '...'),
|
||||
(builder) {
|
||||
builder.pushStyle(EngineTextStyle.only(color: blue));
|
||||
builder.addText('Lorem\n');
|
||||
@@ -82,12 +82,12 @@ void testMain() async {
|
||||
builder.pushStyle(EngineTextStyle.only(color: red));
|
||||
builder.addText('t');
|
||||
},
|
||||
)..layout(constrain(80.0));
|
||||
)..layout(constrain(width));
|
||||
canvas.drawParagraph(paragraph, offset);
|
||||
offset = offset.translate(0, paragraph.height + 10);
|
||||
offset = offset.translate(0, paragraph.height + padding);
|
||||
|
||||
paragraph = rich(
|
||||
ParagraphStyle(fontFamily: 'Roboto', maxLines: 2, ellipsis: '...'),
|
||||
ParagraphStyle(fontFamily: 'Roboto', fontSize: fontSize, maxLines: 2, ellipsis: '...'),
|
||||
(builder) {
|
||||
builder.pushStyle(EngineTextStyle.only(color: blue));
|
||||
builder.addText('Lorem');
|
||||
@@ -102,10 +102,22 @@ void testMain() async {
|
||||
builder.pushStyle(EngineTextStyle.only(color: blue));
|
||||
builder.addText('consectetur');
|
||||
},
|
||||
)..layout(constrain(80.0));
|
||||
)..layout(constrain(width));
|
||||
canvas.drawParagraph(paragraph, offset);
|
||||
offset = offset.translate(0, paragraph.height + 10);
|
||||
offset = offset.translate(0, paragraph.height + padding);
|
||||
}
|
||||
|
||||
test('ellipsis', () {
|
||||
const Rect bounds = Rect.fromLTWH(0, 0, 300, 300);
|
||||
final EngineCanvas canvas = BitmapCanvas(bounds, RenderStrategy());
|
||||
testEllipsis(canvas);
|
||||
return takeScreenshot(canvas, bounds, 'canvas_paragraph_ellipsis');
|
||||
});
|
||||
|
||||
test('ellipsis (dom)', () {
|
||||
const Rect bounds = Rect.fromLTWH(0, 0, 300, 300);
|
||||
final EngineCanvas canvas = DomCanvas(domRenderer.createElement('flt-picture'));
|
||||
testEllipsis(canvas);
|
||||
return takeScreenshot(canvas, bounds, 'canvas_paragraph_ellipsis_dom');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -134,23 +134,23 @@ void testMain() async {
|
||||
final EngineParagraphStyle style = EngineParagraphStyle(ellipsis: '...');
|
||||
final CanvasParagraphBuilder builder = CanvasParagraphBuilder(style);
|
||||
|
||||
builder.addText('Hello');
|
||||
builder.addText('HelloWorld');
|
||||
|
||||
final CanvasParagraph paragraph = builder.build();
|
||||
expect(paragraph.paragraphStyle, style);
|
||||
expect(paragraph.toPlainText(), 'Hello');
|
||||
expect(paragraph.toPlainText(), 'HelloWorld');
|
||||
|
||||
double expectedHeight = 14.0;
|
||||
if (isIosSafari) {
|
||||
// On iOS Safari, the height measurement is one extra pixel.
|
||||
expectedHeight++;
|
||||
}
|
||||
paragraph.layout(ParagraphConstraints(width: double.infinity));
|
||||
paragraph.layout(ParagraphConstraints(width: 100.0));
|
||||
expect(
|
||||
paragraph.toDomElement().outerHtml,
|
||||
'<p style="$defaultFontSize $defaultFontFamily $paragraphStyle overflow: hidden; height: ${expectedHeight}px; text-overflow: ellipsis;">'
|
||||
'<p style="$defaultFontSize $defaultFontFamily $paragraphStyle width: 100px; overflow-y: hidden; height: ${expectedHeight}px;">'
|
||||
'<span style="$defaultColor $defaultFontSize $defaultFontFamily">'
|
||||
'Hello'
|
||||
'Hell...'
|
||||
'</span>'
|
||||
'</p>',
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user