Skip to content

Commit 034ff21

Browse files
committed
Merge branch 'feature/upgrade-custom-render' of https://github.com/tneotia/flutter_html into feature/modularization
� Conflicts: � README.md � example/lib/main.dart � lib/custom_render.dart � lib/html_parser.dart
2 parents 6bdf8f9 + 551ec7e commit 034ff21

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

lib/custom_render.dart

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:collection/collection.dart';
12

23
import 'dart:async';
34
import 'dart:convert';
@@ -11,7 +12,8 @@ import 'package:flutter_html/src/utils.dart';
1112
typedef CustomRenderMatcher = bool Function(RenderContext context);
1213

1314
CustomRenderMatcher blockElementMatcher() => (context) {
14-
return context.tree.style.display == Display.BLOCK;
15+
return context.tree.style.display == Display.BLOCK
16+
&& context.tree.children.isNotEmpty;
1517
};
1618

1719
CustomRenderMatcher listElementMatcher() => (context) {
@@ -99,8 +101,22 @@ CustomRender blockElementRender({
99101
newContext: context,
100102
style: style ?? context.tree.style,
101103
shrinkWrap: context.parser.shrinkWrap,
102-
child: child,
103-
children: children ?? buildChildren.call(),
104+
children: children ?? context.tree.children
105+
.expandIndexed((i, childTree) => [
106+
if (context.parser.shrinkWrap &&
107+
childTree.style.display == Display.BLOCK &&
108+
i > 0 &&
109+
context.tree.children[i - 1] is ReplacedElement)
110+
TextSpan(text: "\n"),
111+
context.parser.parseTree(context, childTree),
112+
if (context.parser.shrinkWrap &&
113+
i != context.tree.children.length - 1 &&
114+
childTree.style.display == Display.BLOCK &&
115+
childTree.element?.localName != "html" &&
116+
childTree.element?.localName != "body")
117+
TextSpan(text: "\n"),
118+
])
119+
.toList(),
104120
),
105121
));
106122

@@ -354,8 +370,16 @@ CustomRender verticalAlignRender({
354370

355371
CustomRender fallbackRender({Style? style, List<InlineSpan>? children}) =>
356372
CustomRender.fromInlineSpan(inlineSpan: (context, buildChildren) => TextSpan(
357-
style: style?.generateTextStyle() ?? context.style.generateTextStyle(),
358-
children: children ?? buildChildren.call(),
373+
style: style?.generateTextStyle() ?? context.style.generateTextStyle(),
374+
children: context.tree.children
375+
.expand((tree) => [
376+
context.parser.parseTree(context, tree),
377+
if (tree.style.display == Display.BLOCK &&
378+
tree.element?.localName != "html" &&
379+
tree.element?.localName != "body")
380+
TextSpan(text: "\n"),
381+
])
382+
.toList(),
359383
));
360384

361385
final Map<CustomRenderMatcher, CustomRender> defaultRenders = {
@@ -401,21 +425,17 @@ InlineSpan _getInteractableChildren(RenderContext context, InteractableElement t
401425
);
402426
} else {
403427
return WidgetSpan(
404-
child: RawGestureDetector(
405-
key: context.key,
406-
gestures: {
407-
MultipleTapGestureRecognizer:
408-
GestureRecognizerFactoryWithHandlers<MultipleTapGestureRecognizer>(
409-
() => MultipleTapGestureRecognizer(),
410-
(instance) {
411-
instance
412-
..onTap = context.parser.onAnchorTap != null
413-
? () => context.parser.onAnchorTap!(tree.href, context, tree.attributes, tree.element)
414-
: null;
415-
},
416-
),
417-
},
418-
child: (childSpan as WidgetSpan).child,
428+
child: MultipleTapGestureDetector(
429+
onTap: context.parser.onAnchorTap != null
430+
? () => context.parser.onAnchorTap!(tree.href, context, tree.attributes, tree.element)
431+
: null,
432+
child: GestureDetector(
433+
key: context.key,
434+
onTap: context.parser.onAnchorTap != null
435+
? () => context.parser.onAnchorTap!(tree.href, context, tree.attributes, tree.element)
436+
: null,
437+
child: (childSpan as WidgetSpan).child,
438+
),
419439
),
420440
);
421441
}

lib/flutter_html.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export 'package:flutter_html/html_parser.dart';
2727
export 'package:flutter_html/html_parser.dart';
2828
//export image render api
2929
export 'package:flutter_html/image_render.dart';
30+
export 'package:flutter_html/custom_render.dart';
3031
//export image render api
3132
export 'package:flutter_html/image_render.dart';
3233
export 'package:flutter_html/src/anchor.dart';

lib/html_parser.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'dart:collection';
22
import 'dart:math';
33

4-
import 'package:collection/collection.dart';
54
import 'package:csslib/parser.dart' as cssparser;
65
import 'package:csslib/visitor.dart' as css;
76
import 'package:flutter/material.dart';

0 commit comments

Comments
 (0)