Skip to content

Commit bd3630d

Browse files
committed
content: Make **bold** bolder even in spoiler headers and h1/h2/etc.
Fixes: zulip#705
1 parent 541f185 commit bd3630d

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

lib/widgets/content.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,15 @@ class InlineContent extends StatelessWidget {
722722
required this.style,
723723
required this.nodes,
724724
}) {
725-
assert(style.fontSize != null);
725+
assert(
726+
style.fontSize != null
727+
&& (
728+
style.debugLabel!.contains('weightVariableTextStyle')
729+
// ([ContentTheme.textStylePlainParagraph] applies [weightVariableTextStyle])
730+
|| style.debugLabel!.contains('ContentTheme.textStylePlainParagraph')
731+
|| style.debugLabel!.contains('bolderWghtTextStyle')
732+
)
733+
);
726734
_builder = _InlineContentBuilder(this);
727735
}
728736

@@ -733,6 +741,7 @@ class InlineContent extends StatelessWidget {
733741
///
734742
/// Must set [TextStyle.fontSize]. Some descendant spans will consume it,
735743
/// e.g., to make their content slightly smaller than surrounding text.
744+
/// Similarly with a font weight produced by [weightVariableTextStyle].
736745
final TextStyle style;
737746

738747
final List<InlineContentNode> nodes;
@@ -797,7 +806,7 @@ class _InlineContentBuilder {
797806
// and our parser doesn't. So don't do anything here.
798807
return const TextSpan(text: "");
799808
} else if (node is StrongNode) {
800-
return _buildStrong(node);
809+
return _buildStrong(ambientTextStyle: widget.style, node);
801810
} else if (node is DeletedNode) {
802811
return _buildDeleted(node);
803812
} else if (node is EmphasisNode) {
@@ -831,8 +840,9 @@ class _InlineContentBuilder {
831840
}
832841
}
833842

834-
InlineSpan _buildStrong(StrongNode node) => _buildNodes(node.nodes,
835-
style: weightVariableTextStyle(_context!, wght: 600));
843+
InlineSpan _buildStrong(StrongNode node, {required TextStyle ambientTextStyle}) =>
844+
_buildNodes(style: bolderWghtTextStyle(ambientTextStyle, by: 200),
845+
node.nodes);
836846

837847
InlineSpan _buildDeleted(DeletedNode node) => _buildNodes(node.nodes,
838848
style: const TextStyle(decoration: TextDecoration.lineThrough));

test/widgets/content_test.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,54 @@ void main() {
521521
content: plainContent('<p><strong>bold</strong></p>'),
522522
styleFinder: findWordBold,
523523
);
524+
525+
for (final level in HeadingLevel.values) {
526+
final name = level.name;
527+
assert(RegExp(r'^h[1-6]$').hasMatch(name));
528+
testFontWeight('in $name',
529+
expectedWght: 800,
530+
// **bold**
531+
content: plainContent('<$name><strong>bold</strong></$name>'),
532+
styleFinder: findWordBold,
533+
);
534+
}
535+
536+
testFontWeight('in different kind of span in h1',
537+
expectedWght: 800,
538+
// # ~~**bold**~~
539+
content: plainContent('<h1><del><strong>bold</strong></del></h1>'),
540+
styleFinder: findWordBold,
541+
);
542+
543+
testFontWeight('in spoiler header',
544+
expectedWght: 900,
545+
// ```spoiler regular **bold**
546+
// content
547+
// ```
548+
content: plainContent(
549+
'<div class="spoiler-block"><div class="spoiler-header">\n'
550+
'<p>regular <strong>bold</strong></p>\n'
551+
'</div><div class="spoiler-content" aria-hidden="true">\n'
552+
'<p>content</p>\n'
553+
'</div></div>'
554+
),
555+
styleFinder: findWordBold,
556+
);
557+
558+
testFontWeight('in different kind of span in spoiler header',
559+
expectedWght: 900,
560+
// ```spoiler *italic **bold***
561+
// content
562+
// ```
563+
content: plainContent(
564+
'<div class="spoiler-block"><div class="spoiler-header">\n'
565+
'<p><em>italic <strong>bold</strong></em></p>\n'
566+
'</div><div class="spoiler-content" aria-hidden="true">\n'
567+
'<p>content</p>\n'
568+
'</div></div>'
569+
),
570+
styleFinder: findWordBold,
571+
);
524572
});
525573

526574
testContentSmoke(ContentExample.emphasis);

0 commit comments

Comments
 (0)