Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class _MyHomePageState extends State<MyHomePage> {
decorationColor: Colors.redAccent,
decoration: TextDecoration.underline,
),
onLinkTap: (url) {
onLinkTap: (text, url) {
print("Opening $url...");
},
onImageTap: (src) {
Expand Down
3 changes: 2 additions & 1 deletion lib/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ class HtmlOldParser extends StatelessWidget {
),
onTap: () {
if (node.attributes.containsKey('href') && onLinkTap != null) {
final String text = node.text;
String url = node.attributes['href'];
onLinkTap(url);
onLinkTap(text, url);
}
});
case "abbr":
Expand Down
7 changes: 4 additions & 3 deletions lib/rich_text_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ typedef CustomTextStyle = TextStyle Function(
);
typedef CustomTextAlign = TextAlign Function(dom.Element elem);
typedef CustomEdgeInsets = EdgeInsets Function(dom.Node node);
typedef OnLinkTap = void Function(String url);
typedef OnLinkTap = void Function(String text, String url);
typedef OnImageTap = void Function(String source);

const OFFSET_TAGS_FONT_SIZE_FACTOR =
Expand Down Expand Up @@ -47,7 +47,7 @@ class LinkTextSpan extends TextSpan {
children: children ?? <TextSpan>[],
recognizer: TapGestureRecognizer()
..onTap = () {
onLinkTap?.call(url);
onLinkTap?.call(text, url);
},
);
}
Expand All @@ -60,6 +60,7 @@ class LinkBlock extends Container {
final List<Widget> children;

LinkBlock({
String text,
String url,
EdgeInsets padding,
EdgeInsets margin,
Expand All @@ -70,7 +71,7 @@ class LinkBlock extends Container {
margin: margin,
child: GestureDetector(
onTap: () {
onLinkTap(url);
onLinkTap(text, url);
},
child: Column(
children: children,
Expand Down
17 changes: 10 additions & 7 deletions test/html_parser_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_html/rich_text_parser.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_html/flutter_html.dart';

void main() {
testWidgets("Check that default parser does not fail on empty data",
Expand Down Expand Up @@ -65,14 +65,16 @@ void main() {
testWidgets("Check that tapping on the `a` tag calls the callback",
(tester) async {
String html = "<a href='https://github.com'>Test link</a>";
String textTapped;
String urlTapped;

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Html(
data: html,
onLinkTap: (url) {
onLinkTap: (text, url) {
textTapped = text;
urlTapped = url;
},
useRichText: false,
Expand All @@ -81,21 +83,24 @@ void main() {
),
);
await tester.tap(find.text("Test link"));
expect(textTapped, "Test link");
expect(urlTapped, "https://github.com");
});

testWidgets(
"Check that tapping on the `a` tag calls the callback `RichText` parser",
(tester) async {
String html = "<a href='https://github.com'>Test link</a>";
String textTapped;
String urlTapped;

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Html(
data: html,
onLinkTap: (url) {
onLinkTap: (text, url) {
textTapped = text;
urlTapped = url;
},
useRichText: true,
Expand All @@ -104,6 +109,7 @@ void main() {
),
);
await tester.tap(find.byType(RichText));
expect(textTapped, "Test link");
expect(urlTapped, "https://github.com");
});

Expand Down Expand Up @@ -168,10 +174,7 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Html(
data: "<address>Address</address>",
useRichText: false
),
body: Html(data: "<address>Address</address>", useRichText: false),
),
),
);
Expand Down