Skip to content

Commit e2a9933

Browse files
authored
Merge pull request #499 from vrtdev/bugfix/link-wrapping
Fix inline rendering of (text) links
2 parents 5429f07 + c050862 commit e2a9933

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

example/lib/main.dart

+3-2
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ const htmlData = """
119119
</p>
120120
<h3>Image support:</h3>
121121
<p>
122-
<img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' />
123-
<a href='https://google.com'><img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' /></a>
122+
<img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' /><br />
123+
<a href='https://google.com'>A linked image: <img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' /></a>
124+
<img alt='Alt Text of an intentionally broken image' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30d' />
124125
</p>
125126
<h3>Video support:</h3>
126127
<video controls>

lib/html_parser.dart

+34-22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:math';
33

44
import 'package:csslib/parser.dart' as cssparser;
55
import 'package:csslib/visitor.dart' as css;
6+
import 'package:flutter/gestures.dart';
67
import 'package:flutter/material.dart';
78
import 'package:flutter_html/flutter_html.dart';
89
import 'package:flutter_html/src/css_parser.dart';
@@ -335,28 +336,39 @@ class HtmlParser extends StatelessWidget {
335336
);
336337
}
337338
} else if (tree is InteractableElement) {
338-
return WidgetSpan(
339-
child: RawGestureDetector(
340-
gestures: {
341-
MultipleTapGestureRecognizer: GestureRecognizerFactoryWithHandlers<
342-
MultipleTapGestureRecognizer>(
343-
() => MultipleTapGestureRecognizer(),
344-
(instance) {
345-
instance..onTap = () => onLinkTap?.call(tree.href);
346-
},
347-
),
348-
},
349-
child: StyledText(
350-
textSpan: TextSpan(
351-
style: newContext.style.generateTextStyle(),
352-
children: tree.children
353-
.map((tree) => parseTree(newContext, tree))
354-
.toList() ??
355-
[],
356-
),
357-
style: newContext.style,
358-
),
359-
),
339+
return TextSpan(
340+
children: tree.children
341+
.map((tree) => parseTree(newContext, tree))
342+
.map((childSpan) {
343+
if (childSpan is TextSpan) {
344+
return TextSpan(
345+
text: childSpan.text,
346+
children: childSpan.children,
347+
style: (childSpan.style ?? TextStyle())
348+
.merge(newContext.style.generateTextStyle()),
349+
semanticsLabel: childSpan.semanticsLabel,
350+
recognizer: TapGestureRecognizer()
351+
..onTap = () => onLinkTap?.call(tree.href),
352+
);
353+
} else {
354+
return WidgetSpan(
355+
child: RawGestureDetector(
356+
gestures: {
357+
MultipleTapGestureRecognizer:
358+
GestureRecognizerFactoryWithHandlers<
359+
MultipleTapGestureRecognizer>(
360+
() => MultipleTapGestureRecognizer(),
361+
(instance) {
362+
instance..onTap = () => onLinkTap?.call(tree.href);
363+
},
364+
),
365+
},
366+
child: (childSpan as WidgetSpan).child,
367+
),
368+
);
369+
}
370+
}).toList() ??
371+
[],
360372
);
361373
} else if (tree is LayoutElement) {
362374
return WidgetSpan(

0 commit comments

Comments
 (0)