Skip to content

Commit f538853

Browse files
browclaude
andcommitted
fix(mobile): bound preview scanning and honor escaped backticks
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Tom Brow <[email protected]>
1 parent 78ddb56 commit f538853

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

‎mobile/lib/features/channels/message_content/preview_text.dart‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,20 @@ String messagePreviewSource(String content, {required int maxLines}) {
145145
}
146146

147147
/// The first `maxLines + 1` lines that render, trimmed and without block
148-
/// syntax, read lazily so the rest of [content] is never scanned. [code] marks
149-
/// a line inside a fenced block, and [end] is its end offset in [content].
148+
/// syntax, read lazily so the rest of [content] is never scanned. Lines that
149+
/// don't render still cost a scan, so reading also stops after
150+
/// [_maxScanLength] characters. [code] marks a line inside a fenced block, and
151+
/// [end] is its end offset in [content].
150152
Iterable<({String text, bool code, int end})> _keptLines(
151153
String content,
152154
int maxLines,
153155
) sync* {
154156
var kept = 0;
155157
var fenceOpen = false;
156158
var start = 0;
157-
while (start <= content.length && kept <= maxLines) {
159+
while (start <= content.length &&
160+
start <= _maxScanLength &&
161+
kept <= maxLines) {
158162
var end = content.indexOf('\n', start);
159163
if (end < 0) end = content.length;
160164
// A long line wraps past the visible lines well before this length.
@@ -183,6 +187,8 @@ Iterable<({String text, bool code, int end})> _keptLines(
183187
}
184188

185189
const _maxLineLength = 2000;
190+
const _maxScanLength = 8 * _maxLineLength;
191+
const _backslash = 0x5C;
186192
const _backtick = 0x60;
187193

188194
final _rule = RegExp(r'^([-*_])(?:\s*\1){2,}$');
@@ -301,13 +307,18 @@ class _PreviewBuilder {
301307
piece is PreviewText ? text(piece.text) : pieces.add(piece);
302308

303309
/// One line of prose: inline code is literal, everything else is inline
304-
/// Markdown. A code span opens at a run of backticks and closes at the next
305-
/// run of the same length; a run with no match is literal.
310+
/// Markdown. A code span opens at an unescaped run of backticks and closes
311+
/// at the next run of the same length; a run with no match is literal.
306312
void line(String line) {
307313
var prose = 0;
308314
var cursor = 0;
309315
while (cursor < line.length) {
310-
if (line.codeUnitAt(cursor) != _backtick) {
316+
final unit = line.codeUnitAt(cursor);
317+
if (unit == _backslash) {
318+
cursor += 2;
319+
continue;
320+
}
321+
if (unit != _backtick) {
311322
cursor++;
312323
continue;
313324
}

‎mobile/test/features/channels/message_content/preview_text_test.dart‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ void main() {
6262
('escaped emphasis', r'\*literal\* and \_x\_', '*literal* and _x_'),
6363
('escaped backslash', r'a \\ b', r'a \ b'),
6464
('escaped link opener', r'\[docs](https://x)', '[docs](https://x)'),
65+
('escaped backticks', r'\`literal\` and `code`', '`literal` and code'),
6566
]) {
6667
test(name, () => expect(_text(source), expected));
6768
}
@@ -99,6 +100,15 @@ void main() {
99100
expect(_text('# a\n\n---\n- b\nc\nd'), 'a\nb\nc');
100101
});
101102

103+
test('stops reading after a long run of lines that do not render', () {
104+
final content = '\n' * 1000000 + 'late';
105+
expect(_text(content), '');
106+
expect(
107+
messagePreviewSource(content, maxLines: 2).length,
108+
lessThan(20000),
109+
);
110+
});
111+
102112
test('caps a very long line', () {
103113
expect(_text('x' * 5000).length, 2000);
104114
});

0 commit comments

Comments
 (0)