Skip to content
Merged
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
6 changes: 4 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const htmlData = """
<tr><td>fData</td><td>fData</td><td>fData</td></tr>
</tfoot>
</table>
<h3>Custom Element Support:</h3>
<h3>Custom Element Support (inline: <bird></bird> and as block):</h3>
<flutter></flutter>
<flutter horizontal></flutter>
<h3>SVG support:</h3>
Expand Down Expand Up @@ -121,7 +121,6 @@ const htmlData = """
<p>
<img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' />
<a href='https://google.com'><img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' /></a>
<img alt='Alt Text of an intentionally broken image' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30d' />
</p>
<h3>Video support:</h3>
<video controls>
Expand Down Expand Up @@ -172,6 +171,9 @@ class _MyHomePageState extends State<MyHomePage> {
"var": Style(fontFamily: 'serif'),
},
customRender: {
"bird": (RenderContext context, Widget child, attributes, _) {
return TextSpan(text: "🐦");
},
"flutter": (RenderContext context, Widget child, attributes, _) {
return FlutterLogo(
style: (attributes['horizontal'] != null)
Expand Down
26 changes: 14 additions & 12 deletions lib/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'package:html/parser.dart' as htmlparser;
import 'package:webview_flutter/webview_flutter.dart';

typedef OnTap = void Function(String url);
typedef CustomRender = Widget Function(
typedef CustomRender = dynamic Function(
RenderContext context,
Widget parsedChild,
Map<String, String> attributes,
Expand Down Expand Up @@ -243,7 +243,7 @@ class HtmlParser extends StatelessWidget {
);

if (customRender?.containsKey(tree.name) ?? false) {
dynamic customRenderForElement = customRender[tree.name].call(
final render = customRender[tree.name].call(
newContext,
ContainerSpan(
newContext: newContext,
Expand All @@ -257,16 +257,18 @@ class HtmlParser extends StatelessWidget {
tree.attributes,
tree.element,
);

if (customRenderForElement != null) {
return WidgetSpan(
child: ContainerSpan(
newContext: newContext,
style: tree.style,
shrinkWrap: context.parser.shrinkWrap,
child: customRenderForElement,
),
);
if (render != null) {
assert(render is InlineSpan || render is Widget);
return render is InlineSpan
? render
: WidgetSpan(
child: ContainerSpan(
newContext: newContext,
style: tree.style,
shrinkWrap: context.parser.shrinkWrap,
child: render,
),
);
}
}

Expand Down