Skip to content

Commit 551ec7e

Browse files
committed
Merge changes with new fixes (nested links & shrinkwrap)
1 parent c21d2d9 commit 551ec7e

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

lib/custom_render.dart

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
import 'package:collection/collection.dart';
22
import 'package:flutter/gestures.dart';
33
import 'package:flutter/material.dart';
44
import 'package:flutter_html/flutter_html.dart';
@@ -8,7 +8,8 @@ import 'package:flutter_html/src/utils.dart';
88
typedef CustomRenderMatcher = bool Function(RenderContext context);
99

1010
CustomRenderMatcher blockElementMatcher() => (context) {
11-
return context.tree.style.display == Display.BLOCK;
11+
return context.tree.style.display == Display.BLOCK
12+
&& context.tree.children.isNotEmpty;
1213
};
1314

1415
CustomRenderMatcher listElementMatcher() => (context) {
@@ -64,8 +65,22 @@ CustomRender blockElementRender({
6465
newContext: context,
6566
style: style ?? context.tree.style,
6667
shrinkWrap: context.parser.shrinkWrap,
67-
child: child,
68-
children: children ?? buildChildren.call(),
68+
children: children ?? context.tree.children
69+
.expandIndexed((i, childTree) => [
70+
if (context.parser.shrinkWrap &&
71+
childTree.style.display == Display.BLOCK &&
72+
i > 0 &&
73+
context.tree.children[i - 1] is ReplacedElement)
74+
TextSpan(text: "\n"),
75+
context.parser.parseTree(context, childTree),
76+
if (context.parser.shrinkWrap &&
77+
i != context.tree.children.length - 1 &&
78+
childTree.style.display == Display.BLOCK &&
79+
childTree.element?.localName != "html" &&
80+
childTree.element?.localName != "body")
81+
TextSpan(text: "\n"),
82+
])
83+
.toList(),
6984
),
7085
));
7186

@@ -168,8 +183,16 @@ CustomRender verticalAlignRender({
168183

169184
CustomRender fallbackRender({Style? style, List<InlineSpan>? children}) =>
170185
CustomRender.fromInlineSpan(inlineSpan: (context, buildChildren) => TextSpan(
171-
style: style?.generateTextStyle() ?? context.style.generateTextStyle(),
172-
children: children ?? buildChildren.call(),
186+
style: style?.generateTextStyle() ?? context.style.generateTextStyle(),
187+
children: context.tree.children
188+
.expand((tree) => [
189+
context.parser.parseTree(context, tree),
190+
if (tree.style.display == Display.BLOCK &&
191+
tree.element?.localName != "html" &&
192+
tree.element?.localName != "body")
193+
TextSpan(text: "\n"),
194+
])
195+
.toList(),
173196
));
174197

175198
final Map<CustomRenderMatcher, CustomRender> defaultRenders = {
@@ -212,21 +235,17 @@ InlineSpan _getInteractableChildren(RenderContext context, InteractableElement t
212235
);
213236
} else {
214237
return WidgetSpan(
215-
child: RawGestureDetector(
216-
key: context.key,
217-
gestures: {
218-
MultipleTapGestureRecognizer:
219-
GestureRecognizerFactoryWithHandlers<MultipleTapGestureRecognizer>(
220-
() => MultipleTapGestureRecognizer(),
221-
(instance) {
222-
instance
223-
..onTap = context.parser.onAnchorTap != null
224-
? () => context.parser.onAnchorTap!(tree.href, context, tree.attributes, tree.element)
225-
: null;
226-
},
227-
),
228-
},
229-
child: (childSpan as WidgetSpan).child,
238+
child: MultipleTapGestureDetector(
239+
onTap: context.parser.onAnchorTap != null
240+
? () => context.parser.onAnchorTap!(tree.href, context, tree.attributes, tree.element)
241+
: null,
242+
child: GestureDetector(
243+
key: context.key,
244+
onTap: context.parser.onAnchorTap != null
245+
? () => context.parser.onAnchorTap!(tree.href, context, tree.attributes, tree.element)
246+
: null,
247+
child: (childSpan as WidgetSpan).child,
248+
),
230249
),
231250
);
232251
}

lib/flutter_html.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export 'package:flutter_html/html_parser.dart';
2828
export 'package:flutter_html/html_parser.dart';
2929
//export image render api
3030
export 'package:flutter_html/image_render.dart';
31+
export 'package:flutter_html/custom_render.dart';
3132
//export image render api
3233
export 'package:flutter_html/image_render.dart';
3334
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)