Skip to content

Commit fbaa7ba

Browse files
committed
Support InlineSpan as customRender
Same as #298 but only the minimal required changes to make this work
1 parent f9638ad commit fbaa7ba

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

example/lib/main.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const htmlData = """
8181
<tr><td>fData</td><td>fData</td><td>fData</td></tr>
8282
</tfoot>
8383
</table>
84-
<h3>Custom Element Support:</h3>
84+
<h3>Custom Element Support (inline: <bird></bird> and as block):</h3>
8585
<flutter></flutter>
8686
<flutter horizontal></flutter>
8787
<h3>SVG support:</h3>
@@ -122,7 +122,6 @@ const htmlData = """
122122
<p>
123123
<img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' />
124124
<a href='https://google.com'><img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' /></a>
125-
<img alt='Alt Text of an intentionally broken image' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30d' />
126125
</p>
127126
<h3>Video support:</h3>
128127
<video controls>
@@ -173,6 +172,9 @@ class _MyHomePageState extends State<MyHomePage> {
173172
"var": Style(fontFamily: 'serif'),
174173
},
175174
customRender: {
175+
"bird": (RenderContext context, Widget child, attributes, _) {
176+
return TextSpan(text: "🐦");
177+
},
176178
"flutter": (RenderContext context, Widget child, attributes, _) {
177179
return FlutterLogo(
178180
style: (attributes['horizontal'] != null)

lib/html_parser.dart

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import 'package:html/parser.dart' as htmlparser;
1515
import 'package:webview_flutter/webview_flutter.dart';
1616

1717
typedef OnTap = void Function(String url);
18-
typedef CustomRender = Widget Function(
18+
typedef CustomRender = dynamic Function(
1919
RenderContext context,
2020
Widget parsedChild,
2121
Map<String, String> attributes,
@@ -243,27 +243,31 @@ class HtmlParser extends StatelessWidget {
243243
);
244244

245245
if (customRender?.containsKey(tree.name) ?? false) {
246-
return WidgetSpan(
247-
child: ContainerSpan(
246+
final render = customRender[tree.name].call(
247+
newContext,
248+
ContainerSpan(
248249
newContext: newContext,
249250
style: tree.style,
250251
shrinkWrap: context.parser.shrinkWrap,
251-
child: customRender[tree.name].call(
252-
newContext,
253-
ContainerSpan(
254-
newContext: newContext,
255-
style: tree.style,
256-
shrinkWrap: context.parser.shrinkWrap,
257-
children: tree.children
258-
?.map((tree) => parseTree(newContext, tree))
259-
?.toList() ??
260-
[],
261-
),
262-
tree.attributes,
263-
tree.element,
264-
),
252+
children: tree.children
253+
?.map((tree) => parseTree(newContext, tree))
254+
?.toList() ??
255+
[],
265256
),
257+
tree.attributes,
258+
tree.element,
266259
);
260+
assert(render is InlineSpan || render is Widget);
261+
return render is InlineSpan
262+
? render
263+
: WidgetSpan(
264+
child: ContainerSpan(
265+
newContext: newContext,
266+
style: tree.style,
267+
shrinkWrap: context.parser.shrinkWrap,
268+
child: render,
269+
),
270+
);
267271
}
268272

269273
//Return the correct InlineSpan based on the element type.

0 commit comments

Comments
 (0)