From ce22beda2cab81206aab60ba5ef1b32f7683bdb5 Mon Sep 17 00:00:00 2001 From: Aaron Madlon-Kay Date: Thu, 18 Jul 2019 23:20:18 +0900 Subject: [PATCH 1/2] Fix application of fontFamily to child nodes Workaround for bug where fontFamily styling is not applied to child spans if the root span is given a style. See: https://github.com/flutter/flutter/issues/35992 --- lib/rich_text_parser.dart | 51 +++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/lib/rich_text_parser.dart b/lib/rich_text_parser.dart index af39f02dea..b250dcdbce 100644 --- a/lib/rich_text_parser.dart +++ b/lib/rich_text_parser.dart @@ -396,9 +396,13 @@ class HtmlRichTextParser extends StatelessWidget { // create a span by default TextSpan span = TextSpan( - text: finalText, - children: [], - style: parseContext.childStyle); + // Workaround for bug where fontFamily styling is not applied to child + // spans if the root span is given a style. + // https://github.com/flutter/flutter/issues/35992 + children: [ + TextSpan(text: finalText, style: parseContext.childStyle) + ], + ); // in this class, a ParentElement must be a BlockText, LinkTextSpan, Row, Column, TextSpan @@ -449,8 +453,8 @@ class HtmlRichTextParser extends StatelessWidget { } else if (parseContext.parentElement is LinkTextSpan) { // add this node to the parent as another LinkTextSpan parseContext.parentElement.children.add(LinkTextSpan( - style: - parseContext.parentElement.style.merge(parseContext.childStyle), + style: parseContext.parentElement.children.first.style + .merge(parseContext.childStyle), url: parseContext.parentElement.url, text: finalText, onLinkTap: onLinkTap, @@ -589,10 +593,12 @@ class HtmlRichTextParser extends StatelessWidget { } else { TextStyle _linkStyle = parseContext.childStyle.merge(linkStyle); LinkTextSpan span = LinkTextSpan( - style: _linkStyle, url: url, onLinkTap: onLinkTap, - children: [], + // Workaround for bug where fontFamily styling is not applied to child + // spans if the root span is given a style. + // https://github.com/flutter/flutter/issues/35992 + children: [TextSpan(style: _linkStyle)], ); if (parseContext.parentElement is TextSpan) { nextContext.parentElement.children.add(span); @@ -827,9 +833,16 @@ class HtmlRichTextParser extends StatelessWidget { child: RichText( textAlign: TextAlign.center, text: TextSpan( - text: node.attributes['alt'], - style: nextContext.childStyle, - children: [], + // Workaround for bug where fontFamily styling is + // not applied to child spans if the root span is + // given a style. + // https://github.com/flutter/flutter/issues/35992 + children: [ + TextSpan( + text: node.attributes['alt'], + style: nextContext.childStyle, + ) + ], )))); } } @@ -848,9 +861,12 @@ class HtmlRichTextParser extends StatelessWidget { left: parseContext.indentLevel * indentSize, top: 3.0), child: RichText( text: TextSpan( - text: '', - style: nextContext.childStyle, - children: [], + // Workaround for bug where fontFamily styling is not applied + // to child spans if the root span is given a style. + // https://github.com/flutter/flutter/issues/35992 + children: [ + TextSpan(text: '', style: nextContext.childStyle) + ], ), ), leadingChar: '$leadingChar ', @@ -926,9 +942,12 @@ class HtmlRichTextParser extends StatelessWidget { child: RichText( textAlign: textAlign, text: TextSpan( - text: '', - style: nextContext.childStyle, - children: [], + // Workaround for bug where fontFamily styling is not applied + // to child spans if the root span is given a style. + // https://github.com/flutter/flutter/issues/35992 + children: [ + TextSpan(text: '', style: nextContext.childStyle) + ], ), ), ); From 3ad3e3b72ce04cd5aa45d98abb54f11c5adae129 Mon Sep 17 00:00:00 2001 From: Aaron Madlon-Kay Date: Fri, 19 Jul 2019 00:07:15 +0900 Subject: [PATCH 2/2] Fix tests for block elements Due to workaround, two widgets are created instead of one --- test/html_parser_test.dart | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/test/html_parser_test.dart b/test/html_parser_test.dart index 44cb709f94..70d1893a53 100644 --- a/test/html_parser_test.dart +++ b/test/html_parser_test.dart @@ -218,7 +218,7 @@ void main() { ), ); - expect(find.byType(BlockText), findsOneWidget); + expect(find.byType(BlockText), findsNWidgets(2)); }); testWidgets("Check that `aside` tag renders", (tester) async { @@ -249,7 +249,7 @@ void main() { ), ); - expect(find.byType(RichText), findsOneWidget); + expect(find.byType(RichText), findsNWidgets(2)); }); testWidgets("Check that `b` tag renders", (tester) async { @@ -615,7 +615,7 @@ void main() { ), ); - expect(find.byType(RichText), findsOneWidget); + expect(find.byType(RichText), findsNWidgets(2)); }); testWidgets("Check that `dfn` tag renders", (tester) async { @@ -645,7 +645,7 @@ void main() { ), ); - expect(find.byType(RichText), findsOneWidget); + expect(find.byType(RichText), findsNWidgets(2)); }); testWidgets("Check that `div` tag renders", (tester) async { @@ -675,7 +675,7 @@ void main() { ), ); - expect(find.byType(BlockText), findsOneWidget); + expect(find.byType(BlockText), findsNWidgets(2)); }); testWidgets("Check that `dl` tag renders", (tester) async { @@ -705,7 +705,7 @@ void main() { ), ); - expect(find.byType(RichText), findsOneWidget); + expect(find.byType(RichText), findsNWidgets(2)); }); testWidgets("Check that `dt` tag renders", (tester) async { @@ -735,7 +735,7 @@ void main() { ), ); - expect(find.byType(RichText), findsOneWidget); + expect(find.byType(RichText), findsNWidgets(2)); }); testWidgets("Check that `em` tag renders", (tester) async { @@ -795,7 +795,7 @@ void main() { ), ); - expect(find.byType(RichText), findsOneWidget); + expect(find.byType(RichText), findsNWidgets(2)); }); testWidgets("Check that `figure` tag renders", (tester) async { @@ -825,7 +825,7 @@ void main() { ), ); - expect(find.byType(RichText), findsOneWidget); + expect(find.byType(RichText), findsNWidgets(2)); }); testWidgets("Check that `footer` tag renders", (tester) async { @@ -885,7 +885,7 @@ void main() { ), ); - expect(find.byType(BlockText), findsOneWidget); + expect(find.byType(BlockText), findsNWidgets(2)); }); testWidgets("Check that `h2` tag renders", (tester) async { @@ -915,7 +915,7 @@ void main() { ), ); - expect(find.byType(BlockText), findsOneWidget); + expect(find.byType(BlockText), findsNWidgets(2)); }); testWidgets("Check that `h3` tag renders", (tester) async { @@ -945,7 +945,7 @@ void main() { ), ); - expect(find.byType(BlockText), findsOneWidget); + expect(find.byType(BlockText), findsNWidgets(2)); }); testWidgets("Check that `h4` tag renders", (tester) async { @@ -975,7 +975,7 @@ void main() { ), ); - expect(find.byType(BlockText), findsOneWidget); + expect(find.byType(BlockText), findsNWidgets(2)); }); testWidgets("Check that `h5` tag renders", (tester) async { @@ -1005,7 +1005,7 @@ void main() { ), ); - expect(find.byType(BlockText), findsOneWidget); + expect(find.byType(BlockText), findsNWidgets(2)); }); testWidgets("Check that `h6` tag renders", (tester) async { @@ -1035,7 +1035,7 @@ void main() { ), ); - expect(find.byType(BlockText), findsOneWidget); + expect(find.byType(BlockText), findsNWidgets(2)); }); testWidgets("Check that `header` tag renders", (tester) async { @@ -1065,7 +1065,7 @@ void main() { ), ); - expect(find.byType(BlockText), findsOneWidget); + expect(find.byType(BlockText), findsNWidgets(2)); }); testWidgets("Check that `hr` tag renders", (tester) async {