This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[web] Position spans absolutely within paragraph #31907
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
683086f
[web] Position spans absolutely within paragraph
mdebbar b81c990
cleanup
mdebbar 10cb17c
fix dom output tests
mdebbar d2a0927
fix tests in other browsers
mdebbar 9c14f7c
Merge remote-tracking branch 'upstream/main' into spans_absolute
mdebbar 066c10c
isChorme => isBlink
mdebbar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,85 +157,44 @@ class CanvasParagraph implements ui.Paragraph { | |
cssStyle | ||
..position = 'absolute' | ||
// Prevent the browser from doing any line breaks in the paragraph. We want | ||
// to insert our own <BR> breaks based on layout results. | ||
// to have full control of the paragraph layout. | ||
..whiteSpace = 'pre'; | ||
|
||
if (width > longestLine) { | ||
// In this case, we set the width so that the CSS text-align property | ||
// works correctly. | ||
// When `longestLine` is >= `paragraph.width` that means the DOM element | ||
// will automatically size itself to fit the longest line, so there's no | ||
// need to set an explicit width. | ||
cssStyle.width = '${width}px'; | ||
} | ||
|
||
if (paragraphStyle.maxLines != null || paragraphStyle.ellipsis != null) { | ||
cssStyle | ||
..overflowY = 'hidden' | ||
..height = '${height}px'; | ||
} | ||
|
||
// 2. Append all spans to the paragraph. | ||
|
||
FlatTextSpan? span; | ||
|
||
html.HtmlElement element = rootElement; | ||
html.HtmlElement? lastSpanElement; | ||
final List<EngineLineMetrics> lines = computeLineMetrics(); | ||
|
||
for (int i = 0; i < lines.length; i++) { | ||
// Insert a <BR> element before each line except the first line. | ||
if (i > 0) { | ||
element.append(html.document.createElement('br')); | ||
} | ||
|
||
final EngineLineMetrics line = lines[i]; | ||
final List<RangeBox> boxes = line.boxes; | ||
final StringBuffer buffer = StringBuffer(); | ||
|
||
int j = 0; | ||
while (j < boxes.length) { | ||
final RangeBox box = boxes[j++]; | ||
if (box is SpanBox && box.span == span) { | ||
buffer.write(box.toText()); | ||
continue; | ||
} | ||
|
||
if (buffer.isNotEmpty) { | ||
element.appendText(buffer.toString()); | ||
buffer.clear(); | ||
} | ||
|
||
if (box is SpanBox) { | ||
span = box.span; | ||
element = html.document.createElement('span') as html.HtmlElement; | ||
lastSpanElement = html.document.createElement('span') as html.HtmlElement; | ||
applyTextStyleToElement( | ||
element: element, | ||
element: lastSpanElement, | ||
style: box.span.style, | ||
isSpan: true, | ||
); | ||
rootElement.append(element); | ||
_positionSpanElement(lastSpanElement, line, box); | ||
lastSpanElement.appendText(box.toText()); | ||
rootElement.append(lastSpanElement); | ||
buffer.write(box.toText()); | ||
} else if (box is PlaceholderBox) { | ||
span = null; | ||
// If there's a line-end after this placeholder, we want the <BR> to | ||
// be inserted in the root paragraph element. | ||
element = rootElement; | ||
rootElement.append( | ||
createPlaceholderElement(placeholder: box.placeholder), | ||
); | ||
lastSpanElement = null; | ||
} else { | ||
throw UnimplementedError('Unknown box type: ${box.runtimeType}'); | ||
} | ||
} | ||
|
||
if (buffer.isNotEmpty) { | ||
element.appendText(buffer.toString()); | ||
buffer.clear(); | ||
} | ||
|
||
final String? ellipsis = line.ellipsis; | ||
if (ellipsis != null) { | ||
element.appendText(ellipsis); | ||
(lastSpanElement ?? rootElement).appendText(ellipsis); | ||
} | ||
} | ||
|
||
|
@@ -307,6 +266,9 @@ void _applyNecessaryParagraphStyles({ | |
}) { | ||
final html.CssStyleDeclaration cssStyle = element.style; | ||
|
||
// TODO(mdebbar): Now that we absolutely position each span inside the | ||
// paragraph, do we still need these style on <p>? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we cascade the styles down to span level? In Flutter, paragraph styles and parent text styles are meant to be "inherited" to descendants. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed offline, the classes |
||
|
||
if (style.textAlign != null) { | ||
cssStyle.textAlign = textAlignToCssValue( | ||
style.textAlign, style.textDirection ?? ui.TextDirection.ltr); | ||
|
@@ -352,6 +314,14 @@ void _applySpanStylesToParagraph({ | |
} | ||
} | ||
|
||
void _positionSpanElement(html.Element element, EngineLineMetrics line, RangeBox box) { | ||
final ui.TextBox textBox = box.toTextBox(line); | ||
element.style | ||
..position = 'absolute' | ||
..top = '${textBox.top}px' | ||
..left = '${textBox.left}px'; | ||
} | ||
|
||
/// A common interface for all types of spans that make up a paragraph. | ||
/// | ||
/// These spans are stored as a flat list in the paragraph object. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
html.SpanElement()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No reason. Is one different from the other?
I'll keep it as
html.document.createElement('span')
for now so I can easily change it toflt-span
next.