Skip to content

Commit 6698b2d

Browse files
Fix error in markdown parsing image dimensions (flutter#6518)
Gracefully handle parsing failures for image width and height in Markdown. Unit tests are provided. Fixes flutter/flutter#146739
1 parent 6841bb1 commit 6698b2d

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

packages/flutter_markdown/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.6.23
2+
3+
* Gracefully handle image dimension parsing failures.
4+
15
## 0.6.22+1
26

37
* Removes `_ambiguate` methods from code.

packages/flutter_markdown/lib/src/builder.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,8 @@ class MarkdownBuilder implements md.NodeVisitor {
568568
if (parts.length == 2) {
569569
final List<String> dimensions = parts.last.split('x');
570570
if (dimensions.length == 2) {
571-
width = double.parse(dimensions[0]);
572-
height = double.parse(dimensions[1]);
571+
width = double.tryParse(dimensions[0]);
572+
height = double.tryParse(dimensions[1]);
573573
}
574574
}
575575

packages/flutter_markdown/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: A Markdown renderer for Flutter. Create rich text output,
44
formatted with simple Markdown tags.
55
repository: https://github.com/flutter/packages/tree/main/packages/flutter_markdown
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_markdown%22
7-
version: 0.6.22+1
7+
version: 0.6.23
88

99
environment:
1010
sdk: ^3.3.0

packages/flutter_markdown/test/image_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,42 @@ void defineTests() {
333333
},
334334
);
335335

336+
testWidgets(
337+
'should gracefully handle width parsing failures',
338+
(WidgetTester tester) async {
339+
const String data = '![alt](https://img#x50)';
340+
await tester.pumpWidget(
341+
boilerplate(
342+
const Markdown(data: data),
343+
),
344+
);
345+
346+
final Image image = tester.widget(find.byType(Image));
347+
final NetworkImage networkImage = image.image as NetworkImage;
348+
expect(networkImage.url, 'https://img');
349+
expect(image.width, null);
350+
expect(image.height, 50);
351+
},
352+
);
353+
354+
testWidgets(
355+
'should gracefully handle height parsing failures',
356+
(WidgetTester tester) async {
357+
const String data = ' ![alt](https://img#50x)';
358+
await tester.pumpWidget(
359+
boilerplate(
360+
const Markdown(data: data),
361+
),
362+
);
363+
364+
final Image image = tester.widget(find.byType(Image));
365+
final NetworkImage networkImage = image.image as NetworkImage;
366+
expect(networkImage.url, 'https://img');
367+
expect(image.width, 50);
368+
expect(image.height, null);
369+
},
370+
);
371+
336372
testWidgets(
337373
'custom image builder',
338374
(WidgetTester tester) async {

0 commit comments

Comments
 (0)