Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit a396dc1

Browse files
[skwasm] Use text position affinity when calculating word boundaries. (#51753)
I had failed to take position affinity into account when calculating word boundaries on skwasm. This brings skwasm's behavior in line with canvaskit's.
1 parent 4e6692c commit a396dc1

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paragraph.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,11 @@ class SkwasmParagraph extends SkwasmObjectWrapper<RawParagraph> implements ui.Pa
233233
@override
234234
ui.TextRange getWordBoundary(ui.TextPosition position) => withStackScope((StackScope scope) {
235235
final Pointer<Int32> outRange = scope.allocInt32Array(2);
236-
paragraphGetWordBoundary(handle, position.offset, outRange);
236+
final int characterPosition = switch (position.affinity) {
237+
ui.TextAffinity.upstream => position.offset - 1,
238+
ui.TextAffinity.downstream => position.offset,
239+
};
240+
paragraphGetWordBoundary(handle, characterPosition, outRange);
237241
return ui.TextRange(start: outRange[0], end: outRange[1]);
238242
});
239243

lib/web_ui/test/ui/paragraph_builder_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ Future<void> testMain() async {
4040
expect(() => builder.build(), returnsNormally);
4141
});
4242

43+
test('getWordBoundary respects position affinity', () {
44+
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle());
45+
builder.addText('hello world');
46+
47+
final Paragraph paragraph = builder.build();
48+
paragraph.layout(const ParagraphConstraints(width: double.infinity));
49+
50+
final TextRange downstreamWordBoundary = paragraph.getWordBoundary(const TextPosition(
51+
offset: 5,
52+
));
53+
expect(downstreamWordBoundary, const TextRange(start: 5, end: 6));
54+
55+
final TextRange upstreamWordBoundary = paragraph.getWordBoundary(const TextPosition(
56+
offset: 5,
57+
affinity: TextAffinity.upstream,
58+
));
59+
expect(upstreamWordBoundary, const TextRange(start: 0, end: 5));
60+
});
61+
4362
test('build and layout a paragraph with an empty addText', () {
4463
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle());
4564
builder.addText('');

0 commit comments

Comments
 (0)