Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix application of fontFamily to child nodes #131

Closed
wants to merge 2 commits into from
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
51 changes: 35 additions & 16 deletions lib/rich_text_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,13 @@ class HtmlRichTextParser extends StatelessWidget {

// create a span by default
TextSpan span = TextSpan(
text: finalText,
children: <TextSpan>[],
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>[
TextSpan(text: finalText, style: parseContext.childStyle)
],
);

// in this class, a ParentElement must be a BlockText, LinkTextSpan, Row, Column, TextSpan

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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: <TextSpan>[],
// 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>[TextSpan(style: _linkStyle)],
);
if (parseContext.parentElement is TextSpan) {
nextContext.parentElement.children.add(span);
Expand Down Expand Up @@ -827,9 +833,16 @@ class HtmlRichTextParser extends StatelessWidget {
child: RichText(
textAlign: TextAlign.center,
text: TextSpan(
text: node.attributes['alt'],
style: nextContext.childStyle,
children: <TextSpan>[],
// 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>[
TextSpan(
text: node.attributes['alt'],
style: nextContext.childStyle,
)
],
))));
}
}
Expand All @@ -848,9 +861,12 @@ class HtmlRichTextParser extends StatelessWidget {
left: parseContext.indentLevel * indentSize, top: 3.0),
child: RichText(
text: TextSpan(
text: '',
style: nextContext.childStyle,
children: <TextSpan>[],
// 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>[
TextSpan(text: '', style: nextContext.childStyle)
],
),
),
leadingChar: '$leadingChar ',
Expand Down Expand Up @@ -926,9 +942,12 @@ class HtmlRichTextParser extends StatelessWidget {
child: RichText(
textAlign: textAlign,
text: TextSpan(
text: '',
style: nextContext.childStyle,
children: <TextSpan>[],
// 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>[
TextSpan(text: '', style: nextContext.childStyle)
],
),
),
);
Expand Down
32 changes: 16 additions & 16 deletions test/html_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down