Skip to content

Commit f24f461

Browse files
authored
Merge pull request #517 from vrtdev/bugfix/515-link-inner-html-linking-and-styles
Recursively applying linking and styling inside <a> tags
2 parents 17735ce + ee3eb73 commit f24f461

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

lib/html_parser.dart

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -336,37 +336,46 @@ class HtmlParser extends StatelessWidget {
336336
);
337337
}
338338
} else if (tree is InteractableElement) {
339+
InlineSpan addTaps(InlineSpan childSpan, TextStyle childStyle) {
340+
if (childSpan is TextSpan) {
341+
return TextSpan(
342+
text: childSpan.text,
343+
children: childSpan.children
344+
?.map((e) => addTaps(e, childStyle.merge(childSpan.style)))
345+
?.toList(),
346+
style: newContext.style.generateTextStyle().merge(
347+
childSpan.style == null
348+
? childStyle
349+
: childStyle.merge(childSpan.style)),
350+
semanticsLabel: childSpan.semanticsLabel,
351+
recognizer: TapGestureRecognizer()
352+
..onTap = () => onLinkTap?.call(tree.href),
353+
);
354+
} else {
355+
return WidgetSpan(
356+
child: RawGestureDetector(
357+
gestures: {
358+
MultipleTapGestureRecognizer:
359+
GestureRecognizerFactoryWithHandlers<
360+
MultipleTapGestureRecognizer>(
361+
() => MultipleTapGestureRecognizer(),
362+
(instance) {
363+
instance..onTap = () => onLinkTap?.call(tree.href);
364+
},
365+
),
366+
},
367+
child: (childSpan as WidgetSpan).child,
368+
),
369+
);
370+
}
371+
}
372+
339373
return TextSpan(
340374
children: tree.children
341375
.map((tree) => parseTree(newContext, tree))
342376
.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-
}
377+
return addTaps(childSpan,
378+
newContext.style.generateTextStyle().merge(childSpan.style));
370379
}).toList() ??
371380
[],
372381
);

0 commit comments

Comments
 (0)