Skip to content

Commit 5429f07

Browse files
authored
Merge pull request #498 from vrtdev/feature/inlinespan-customrender
Support InlineSpan as customRender
2 parents dea464b + b2467e7 commit 5429f07

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

example/lib/main.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const htmlData = """
8080
<tr><td>fData</td><td>fData</td><td>fData</td></tr>
8181
</tfoot>
8282
</table>
83-
<h3>Custom Element Support:</h3>
83+
<h3>Custom Element Support (inline: <bird></bird> and as block):</h3>
8484
<flutter></flutter>
8585
<flutter horizontal></flutter>
8686
<h3>SVG support:</h3>
@@ -121,7 +121,6 @@ const htmlData = """
121121
<p>
122122
<img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' />
123123
<a href='https://google.com'><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' />
125124
</p>
126125
<h3>Video support:</h3>
127126
<video controls>
@@ -172,6 +171,9 @@ class _MyHomePageState extends State<MyHomePage> {
172171
"var": Style(fontFamily: 'serif'),
173172
},
174173
customRender: {
174+
"bird": (RenderContext context, Widget child, attributes, _) {
175+
return TextSpan(text: "🐦");
176+
},
175177
"flutter": (RenderContext context, Widget child, attributes, _) {
176178
return FlutterLogo(
177179
style: (attributes['horizontal'] != null)

lib/html_parser.dart

Lines changed: 14 additions & 12 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,7 +243,7 @@ class HtmlParser extends StatelessWidget {
243243
);
244244

245245
if (customRender?.containsKey(tree.name) ?? false) {
246-
dynamic customRenderForElement = customRender[tree.name].call(
246+
final render = customRender[tree.name].call(
247247
newContext,
248248
ContainerSpan(
249249
newContext: newContext,
@@ -257,16 +257,18 @@ class HtmlParser extends StatelessWidget {
257257
tree.attributes,
258258
tree.element,
259259
);
260-
261-
if (customRenderForElement != null) {
262-
return WidgetSpan(
263-
child: ContainerSpan(
264-
newContext: newContext,
265-
style: tree.style,
266-
shrinkWrap: context.parser.shrinkWrap,
267-
child: customRenderForElement,
268-
),
269-
);
260+
if (render != null) {
261+
assert(render is InlineSpan || render is Widget);
262+
return render is InlineSpan
263+
? render
264+
: WidgetSpan(
265+
child: ContainerSpan(
266+
newContext: newContext,
267+
style: tree.style,
268+
shrinkWrap: context.parser.shrinkWrap,
269+
child: render,
270+
),
271+
);
270272
}
271273
}
272274

0 commit comments

Comments
 (0)