Skip to content
Closed
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
12 changes: 10 additions & 2 deletions lib/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_html/rich_text_parser.dart';
import 'package:html/dom.dart' as dom;
import 'package:html/parser.dart' as parser;
import 'package:flutter_html/utils.dart';

class HtmlOldParser extends StatelessWidget {
HtmlOldParser({
Expand Down Expand Up @@ -373,8 +374,15 @@ class HtmlOldParser extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.center,
));
case "font":
return Wrap(
children: _parseNodeList(node.nodes),
return DefaultTextStyle.merge(
child: Container(
child: Wrap(
children: _parseNodeList(node.nodes),
),
),
style: TextStyle(
color: colorFromNodeAttribute(node),
),
);
case "footer":
return Container(
Expand Down
7 changes: 7 additions & 0 deletions lib/rich_text_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/utils.dart';
import 'package:html/dom.dart' as dom;
import 'package:html/parser.dart' as parser;

Expand Down Expand Up @@ -218,6 +219,7 @@ class HtmlRichTextParser extends StatelessWidget {
"span",
"big",
"sub",
"font",
];

// specialty elements require unique handling
Expand Down Expand Up @@ -484,6 +486,11 @@ class HtmlRichTextParser extends StatelessWidget {
childStyle =
childStyle.merge(TextStyle(fontWeight: FontWeight.bold));
break;
case "font":
childStyle = childStyle.merge(TextStyle(
color: colorFromNodeAttribute(node),
));
break;
case "i":
case "address":
case "cite":
Expand Down
34 changes: 34 additions & 0 deletions lib/utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:html/dom.dart';
import 'package:flutter/widgets.dart';

//
// Converts a hex string into a color.
//
// Returns Black if the string fails to parse.
class HexToColor extends Color {
static _hexToColor(String code) {
try {
return int.parse(code.substring(1, 7), radix: 16) + 0xFF000000;
} catch (e) {
// Default to black
return 4278190080;
}
}

HexToColor(final String code) : super(_hexToColor(code));
}

//
// Gets a Color from a node attribute.
//
// Returns null if the color is not a hex value.
//
Color colorFromNodeAttribute(Node node) {
Color c;
String color = node.attributes['color'] ?? '';
if (color.startsWith('#')) {
// A hex color`
c = HexToColor(color);
}
return c;
}
94 changes: 93 additions & 1 deletion test/html_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,100 @@ void main() {
expect(find.byType(RichText), findsOneWidget);
});

testWidgets("Check that `font` tag renders", (tester) async {
String html = "<font>font</font>";

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Html(
data: html,
useRichText: false,
),
),
),
);

expect(find.text("font"), findsOneWidget);

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Html(
data: html,
useRichText: true,
),
),
),
);

expect(find.byType(RichText), findsOneWidget);

// Check that font hex color attributes don't cause an exception
html = "<font color='#aabbcc'>font with hex color</font>";

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Html(
data: html,
useRichText: true,
),
),
),
);

expect(find.byType(RichText), findsOneWidget);

html = "<font color='#abc'>font with short hex color</font>";

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Html(
data: html,
useRichText: true,
),
),
),
);

expect(find.byType(RichText), findsOneWidget);

html = "<font color='#axbycz'>font with invalid hex color</font>";

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Html(
data: html,
useRichText: true,
),
),
),
);

expect(find.byType(RichText), findsOneWidget);

// Check that font rgb color attributes don't cause an exception
html = "<font color='rgb(64,127,250'>font</font>";

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Html(
data: html,
useRichText: true,
),
),
),
);

expect(find.byType(RichText), findsOneWidget);
});

testWidgets("Check that `footer` tag renders", (tester) async {
String html = "<b>Footer</b>";
String html = "<footer>Footer</footer>";

await tester.pumpWidget(
MaterialApp(
Expand Down