Skip to content

Commit 0f7044d

Browse files
[AttributedText] - Fix incorrect character lookup by index (Resolves #2531) (#2532)
1 parent ffc5cf6 commit 0f7044d

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

attributed_text/lib/src/attributed_text.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ class AttributedText {
119119
late final String _textWithPlaceholders;
120120

121121
/// Returns the character or placeholder at offset zero.
122-
Object get first => placeholders[0] ?? _text[0];
122+
Object get first => placeholders[0] ?? _textWithPlaceholders[0];
123123

124124
/// Returns the character or placeholder at the given [offset].
125-
Object operator [](int offset) => placeholders[offset] ?? _text[offset];
125+
Object operator [](int offset) => placeholders[offset] ?? _textWithPlaceholders[offset];
126126

127127
/// Returns the character or placeholder at the end of this `AttributedText`.
128-
Object get last => placeholders[length - 1] ?? _text[length - 1];
128+
Object get last => placeholders[length - 1] ?? _textWithPlaceholders[length - 1];
129129

130130
/// Returns a plain-text version of this `AttributedText`.
131131
///

attributed_text/test/attributed_text_placeholders_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,26 @@ void main() {
8181
});
8282
});
8383

84+
test("reports characters and placeholders at indices", () {
85+
final text1 = AttributedText("HelloWorld", null, {
86+
0: const _FakePlaceholder("one"),
87+
6: const _FakePlaceholder("two"),
88+
12: const _FakePlaceholder("three"),
89+
});
90+
91+
expect(text1.first, const _FakePlaceholder("one"));
92+
expect(text1[1], "H");
93+
expect(text1[6], const _FakePlaceholder("two"));
94+
expect(text1[11], "d");
95+
expect(text1.last, const _FakePlaceholder("three"));
96+
97+
final text2 = AttributedText("Hello World", null, {
98+
0: const _FakePlaceholder("one"),
99+
});
100+
expect(text2[11], "d");
101+
expect(text2.last, "d");
102+
});
103+
84104
test("reports plain text value", () {
85105
expect(
86106
AttributedText("", null, {

0 commit comments

Comments
 (0)