Skip to content

Commit 963a650

Browse files
content: Handle 'nulldelimiter' KaTeX CSS
Fixes: #1677
1 parent 94037d7 commit 963a650

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

lib/model/katex.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ class _KatexParser {
225225
// A copy of class definition (where possible) is accompanied in a comment
226226
// with each case statement to keep track of updates.
227227
final spanClasses = List<String>.unmodifiable(element.className.split(' '));
228+
double? widthEm;
228229
String? fontFamily;
229230
double? fontSizeEm;
230231
KatexSpanFontWeight? fontWeight;
@@ -415,7 +416,11 @@ class _KatexParser {
415416
_ => throw _KatexHtmlParseError(),
416417
};
417418

418-
// TODO handle .nulldelimiter and .delimcenter .
419+
case 'nulldelimiter':
420+
// .nulldelimiter { display: inline-block; width: 0.12em; }
421+
widthEm = 0.12;
422+
423+
// TODO .delimcenter .
419424

420425
case 'op-symbol':
421426
// .op-symbol { ... }
@@ -458,6 +463,7 @@ class _KatexParser {
458463
}
459464
}
460465
final styles = KatexSpanStyles(
466+
widthEm: widthEm,
461467
fontFamily: fontFamily,
462468
fontSizeEm: fontSizeEm,
463469
fontWeight: fontWeight,
@@ -575,6 +581,7 @@ enum KatexSpanTextAlign {
575581

576582
@immutable
577583
class KatexSpanStyles {
584+
final double? widthEm;
578585
final double? heightEm;
579586
final double? verticalAlignEm;
580587

@@ -588,6 +595,7 @@ class KatexSpanStyles {
588595
final KatexSpanTextAlign? textAlign;
589596

590597
const KatexSpanStyles({
598+
this.widthEm,
591599
this.heightEm,
592600
this.verticalAlignEm,
593601
this.marginRightEm,
@@ -602,6 +610,7 @@ class KatexSpanStyles {
602610
@override
603611
int get hashCode => Object.hash(
604612
'KatexSpanStyles',
613+
widthEm,
605614
heightEm,
606615
verticalAlignEm,
607616
marginRightEm,
@@ -616,6 +625,7 @@ class KatexSpanStyles {
616625
@override
617626
bool operator ==(Object other) {
618627
return other is KatexSpanStyles &&
628+
other.widthEm == widthEm &&
619629
other.heightEm == heightEm &&
620630
other.verticalAlignEm == verticalAlignEm &&
621631
other.marginRightEm == marginRightEm &&
@@ -630,6 +640,7 @@ class KatexSpanStyles {
630640
@override
631641
String toString() {
632642
final args = <String>[];
643+
if (widthEm != null) args.add('widthEm: $widthEm');
633644
if (heightEm != null) args.add('heightEm: $heightEm');
634645
if (verticalAlignEm != null) args.add('verticalAlignEm: $verticalAlignEm');
635646
if (marginRightEm != null) args.add('marginRightEm: $marginRightEm');
@@ -651,6 +662,7 @@ class KatexSpanStyles {
651662
/// had `inherit` set to true.
652663
KatexSpanStyles merge(KatexSpanStyles other) {
653664
return KatexSpanStyles(
665+
widthEm: other.widthEm ?? widthEm,
654666
heightEm: other.heightEm ?? heightEm,
655667
verticalAlignEm: other.verticalAlignEm ?? verticalAlignEm,
656668
marginRightEm: other.marginRightEm ?? marginRightEm,

lib/widgets/content.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,9 @@ class _KatexSpan extends StatelessWidget {
976976
}
977977

978978
widget = SizedBox(
979+
width: styles.widthEm != null
980+
? styles.widthEm! * em
981+
: null,
979982
height: styles.heightEm != null
980983
? styles.heightEm! * em
981984
: null,

test/model/content_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,40 @@ class ContentExample {
943943
]),
944944
]);
945945

946+
static const mathBlockKatexNulldelimiter = ContentExample(
947+
'math block; KaTeX nulldelimiter',
948+
// https://chat.zulip.org/#narrow/channel/7-test-here/topic/Rajesh/near/2205534
949+
'```math\n\\left. a \\middle. b \\right.\n```',
950+
'<p>'
951+
'<span class="katex-display"><span class="katex">'
952+
'<span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>a</mi><mo fence="true" lspace="0.05em" rspace="0.05em">.</mo><mi>b</mi></mrow><annotation encoding="application/x-tex">\\left. a \\middle. b \\right.</annotation></semantics></math></span>'
953+
'<span class="katex-html" aria-hidden="true">'
954+
'<span class="base">'
955+
'<span class="strut" style="height:0.6944em;"></span>'
956+
'<span class="minner">'
957+
'<span class="mopen nulldelimiter"></span>'
958+
'<span class="mord mathnormal">a</span>'
959+
'<span class="nulldelimiter"></span>'
960+
'<span class="mord mathnormal">b</span>'
961+
'<span class="mclose nulldelimiter"></span></span></span></span></span></span></p>', [
962+
MathBlockNode(texSource: '\\left. a \\middle. b \\right.', nodes: [
963+
KatexSpanNode(styles: KatexSpanStyles(), text: null, nodes: [
964+
KatexStrutNode(heightEm: 0.6944, verticalAlignEm: null),
965+
KatexSpanNode(styles: KatexSpanStyles(), text: null, nodes: [
966+
KatexSpanNode(styles: KatexSpanStyles(widthEm: 0.12), text: null, nodes: []),
967+
KatexSpanNode(
968+
styles: KatexSpanStyles(fontFamily: 'KaTeX_Math', fontStyle: KatexSpanFontStyle.italic),
969+
text: 'a', nodes: null),
970+
KatexSpanNode(styles: KatexSpanStyles(widthEm: 0.12), text: null, nodes: []),
971+
KatexSpanNode(
972+
styles: KatexSpanStyles(fontFamily: 'KaTeX_Math', fontStyle: KatexSpanFontStyle.italic),
973+
text: 'b', nodes: null),
974+
KatexSpanNode(styles: KatexSpanStyles(widthEm: 0.12), text: null, nodes: []),
975+
]),
976+
]),
977+
]),
978+
]);
979+
946980
static const imageSingle = ContentExample(
947981
'single image',
948982
// https://chat.zulip.org/#narrow/stream/7-test-here/topic/Thumbnails/near/1900103
@@ -2033,6 +2067,7 @@ void main() async {
20332067
testParseExample(ContentExample.mathBlockKatexNestedSizing);
20342068
testParseExample(ContentExample.mathBlockKatexDelimSizing);
20352069
testParseExample(ContentExample.mathBlockKatexSpace);
2070+
testParseExample(ContentExample.mathBlockKatexNulldelimiter);
20362071

20372072
testParseExample(ContentExample.imageSingle);
20382073
testParseExample(ContentExample.imageSingleNoDimensions);

test/widgets/content_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,10 @@ void main() {
602602
(':', Offset(16.00, 2.24), Size(5.72, 25.00)),
603603
('2', Offset(27.43, 2.24), Size(10.28, 25.00)),
604604
]),
605+
(ContentExample.mathBlockKatexNulldelimiter, skip: false, [
606+
('a', Offset(2.47, 3.36), Size(10.88, 25.00)),
607+
('b', Offset(15.81, 3.36), Size(8.82, 25.00)),
608+
]),
605609
];
606610

607611
for (final testCase in testCases) {

0 commit comments

Comments
 (0)