Skip to content

Commit

Permalink
Fix error in markdown parsing image dimensions (#6518)
Browse files Browse the repository at this point in the history
Gracefully handle parsing failures for image width and height in Markdown. Unit tests are provided.

Fixes flutter/flutter#146739
  • Loading branch information
collinjackson committed Apr 14, 2024
1 parent 6841bb1 commit 6698b2d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/flutter_markdown/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.6.23

* Gracefully handle image dimension parsing failures.

## 0.6.22+1

* Removes `_ambiguate` methods from code.
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_markdown/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ class MarkdownBuilder implements md.NodeVisitor {
if (parts.length == 2) {
final List<String> dimensions = parts.last.split('x');
if (dimensions.length == 2) {
width = double.parse(dimensions[0]);
height = double.parse(dimensions[1]);
width = double.tryParse(dimensions[0]);
height = double.tryParse(dimensions[1]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_markdown/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: A Markdown renderer for Flutter. Create rich text output,
formatted with simple Markdown tags.
repository: https://github.com/flutter/packages/tree/main/packages/flutter_markdown
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_markdown%22
version: 0.6.22+1
version: 0.6.23

environment:
sdk: ^3.3.0
Expand Down
36 changes: 36 additions & 0 deletions packages/flutter_markdown/test/image_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,42 @@ void defineTests() {
},
);

testWidgets(
'should gracefully handle width parsing failures',
(WidgetTester tester) async {
const String data = '![alt](https://img#x50)';
await tester.pumpWidget(
boilerplate(
const Markdown(data: data),
),
);

final Image image = tester.widget(find.byType(Image));
final NetworkImage networkImage = image.image as NetworkImage;
expect(networkImage.url, 'https://img');
expect(image.width, null);
expect(image.height, 50);
},
);

testWidgets(
'should gracefully handle height parsing failures',
(WidgetTester tester) async {
const String data = ' ![alt](https://img#50x)';
await tester.pumpWidget(
boilerplate(
const Markdown(data: data),
),
);

final Image image = tester.widget(find.byType(Image));
final NetworkImage networkImage = image.image as NetworkImage;
expect(networkImage.url, 'https://img');
expect(image.width, 50);
expect(image.height, null);
},
);

testWidgets(
'custom image builder',
(WidgetTester tester) async {
Expand Down

0 comments on commit 6698b2d

Please sign in to comment.