chore: Follow up linebreaks in html rendering
This commit is contained in:
parent
04d4557853
commit
0b8d570c59
1 changed files with 31 additions and 18 deletions
|
|
@ -83,21 +83,25 @@ class HtmlMessage extends StatelessWidget {
|
||||||
/// We add line breaks before these tags:
|
/// We add line breaks before these tags:
|
||||||
static const Set<String> blockHtmlTags = {
|
static const Set<String> blockHtmlTags = {
|
||||||
'p',
|
'p',
|
||||||
|
'ul',
|
||||||
|
'ol',
|
||||||
|
'pre',
|
||||||
|
'br',
|
||||||
|
'div',
|
||||||
|
'table',
|
||||||
|
'details',
|
||||||
|
'blockquote',
|
||||||
|
};
|
||||||
|
|
||||||
|
/// We add line breaks before these tags:
|
||||||
|
static const Set<String> fullLineHtmlTag = {
|
||||||
'h1',
|
'h1',
|
||||||
'h2',
|
'h2',
|
||||||
'h3',
|
'h3',
|
||||||
'h4',
|
'h4',
|
||||||
'h5',
|
'h5',
|
||||||
'h6',
|
'h6',
|
||||||
'ul',
|
|
||||||
'ol',
|
|
||||||
'li',
|
'li',
|
||||||
'pre',
|
|
||||||
'br',
|
|
||||||
'div',
|
|
||||||
'table',
|
|
||||||
'blockquote',
|
|
||||||
'details',
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Adding line breaks before block elements.
|
/// Adding line breaks before block elements.
|
||||||
|
|
@ -105,17 +109,24 @@ class HtmlMessage extends StatelessWidget {
|
||||||
dom.NodeList nodes,
|
dom.NodeList nodes,
|
||||||
BuildContext context, {
|
BuildContext context, {
|
||||||
int depth = 1,
|
int depth = 1,
|
||||||
}) =>
|
}) {
|
||||||
[
|
final onlyElements = nodes.whereType<dom.Element>().toList();
|
||||||
for (var i = 0; i < nodes.length; i++) ...[
|
return [
|
||||||
if (i > 0 &&
|
for (var i = 0; i < nodes.length; i++) ...[
|
||||||
nodes[i] is dom.Element &&
|
// Actually render the node child:
|
||||||
blockHtmlTags.contains((nodes[i] as dom.Element).localName))
|
_renderHtml(nodes[i], context, depth: depth + 1),
|
||||||
const TextSpan(text: '\n'), // Add linebreak
|
// Add linebreaks between blocks:
|
||||||
// Actually render the node child:
|
if (nodes[i] is dom.Element &&
|
||||||
_renderHtml(nodes[i], context, depth: depth + 1),
|
onlyElements.indexOf(nodes[i] as dom.Element) <
|
||||||
|
onlyElements.length - 1) ...[
|
||||||
|
if (blockHtmlTags.contains((nodes[i] as dom.Element).localName))
|
||||||
|
const TextSpan(text: '\n\n'),
|
||||||
|
if (fullLineHtmlTag.contains((nodes[i] as dom.Element).localName))
|
||||||
|
const TextSpan(text: '\n'),
|
||||||
],
|
],
|
||||||
];
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/// Transforms a Node to an InlineSpan.
|
/// Transforms a Node to an InlineSpan.
|
||||||
InlineSpan _renderHtml(
|
InlineSpan _renderHtml(
|
||||||
|
|
@ -144,6 +155,8 @@ class HtmlMessage extends StatelessWidget {
|
||||||
if (!allowedHtmlTags.contains(node.localName)) return const TextSpan();
|
if (!allowedHtmlTags.contains(node.localName)) return const TextSpan();
|
||||||
|
|
||||||
switch (node.localName) {
|
switch (node.localName) {
|
||||||
|
case 'br':
|
||||||
|
return const TextSpan(text: '\n');
|
||||||
case 'a':
|
case 'a':
|
||||||
final href = node.attributes['href'];
|
final href = node.attributes['href'];
|
||||||
if (href == null) continue block;
|
if (href == null) continue block;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue