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
42 changes: 30 additions & 12 deletions lib/html_parser.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:collection';
import 'dart:math';

import 'package:collection/collection.dart';
import 'package:csslib/parser.dart' as cssparser;
import 'package:csslib/visitor.dart' as css;
import 'package:flutter/gestures.dart';
Expand Down Expand Up @@ -285,7 +286,22 @@ class HtmlParser extends StatelessWidget {
newContext: newContext,
style: tree.style,
shrinkWrap: context.parser.shrinkWrap,
children: tree.children.map((tree) => parseTree(newContext, tree)).toList(),
children: tree.children
.expandIndexed((i, childTree) => [
if (shrinkWrap &&
childTree.style.display == Display.BLOCK &&
i > 0 &&
tree.children[i - 1] is ReplacedElement)
TextSpan(text: "\n"),
parseTree(newContext, childTree),
if (shrinkWrap &&
i != tree.children.length - 1 &&
childTree.style.display == Display.BLOCK &&
childTree.element?.localName != "html" &&
childTree.element?.localName != "body")
TextSpan(text: "\n"),
])
.toList(),
),
);
} else if (tree.style.display == Display.LIST_ITEM) {
Expand Down Expand Up @@ -424,21 +440,26 @@ class HtmlParser extends StatelessWidget {
child: StyledText(
textSpan: TextSpan(
style: newContext.style.generateTextStyle(),
children: tree.children
.map((tree) => parseTree(newContext, tree))
.toList(),
children: tree.children.map((tree) => parseTree(newContext, tree)).toList(),
),
style: newContext.style,
renderContext: context,
renderContext: newContext,
),
),
);
} else {
///[tree] is an inline element.
return TextSpan(
style: newContext.style.generateTextStyle(),
children:
tree.children.map((tree) => parseTree(newContext, tree)).toList(),
children: tree.children
.expand((tree) => [
parseTree(newContext, tree),
if (tree.style.display == Display.BLOCK &&
tree.element?.localName != "html" &&
tree.element?.localName != "body")
TextSpan(text: "\n"),
])
.toList(),
);
}
}
Expand Down Expand Up @@ -827,7 +848,7 @@ class StyledText extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SizedBox(
width: calculateWidth(style.display, renderContext),
width: consumeExpandedBlock(style.display, renderContext),
child: Text.rich(
textSpan,
style: style.generateTextStyle(),
Expand All @@ -840,13 +861,10 @@ class StyledText extends StatelessWidget {
);
}

double? calculateWidth(Display? display, RenderContext context) {
double? consumeExpandedBlock(Display? display, RenderContext context) {
if ((display == Display.BLOCK || display == Display.LIST_ITEM) && !renderContext.parser.shrinkWrap) {
return double.infinity;
}
if (renderContext.parser.shrinkWrap) {
return MediaQuery.of(context.buildContext).size.width;
}
return null;
}
}
4 changes: 2 additions & 2 deletions lib/src/replaced_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,13 @@ class MathElement extends ReplacedElement {
required this.element,
this.texStr,
String name = "math",
}) : super(name: name, alignment: PlaceholderAlignment.middle, style: Style(), elementId: element.id);
}) : super(name: name, alignment: PlaceholderAlignment.middle, style: Style(display: Display.BLOCK), elementId: element.id);

@override
Widget toWidget(RenderContext context) {
texStr = parseMathRecursive(element, r'');
return Container(
width: MediaQuery.of(context.buildContext).size.width,
width: context.parser.shrinkWrap ? null : MediaQuery.of(context.buildContext).size.width,
child: Math.tex(
texStr ?? '',
mathStyle: MathStyle.display,
Expand Down