Skip to content

Commit f9a5b1c

Browse files
content: Handle 'nulldelimiter' KaTeX CSS
1 parent 0bf40b8 commit f9a5b1c

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

lib/model/katex.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class _KatexParser {
146146
// A copy of class definition (where possible) is accompanied in a comment
147147
// with each case statement to keep track of updates.
148148
final spanClasses = List<String>.unmodifiable(element.className.split(' '));
149+
double? widthEm;
149150
String? fontFamily;
150151
double? fontSizeEm;
151152
KatexSpanFontWeight? fontWeight;
@@ -330,7 +331,11 @@ class _KatexParser {
330331
_ => throw KatexHtmlParseError(),
331332
};
332333

333-
// TODO handle .nulldelimiter and .delimcenter .
334+
case 'nulldelimiter':
335+
// .nulldelimiter { display: inline-block; width: 0.12em; }
336+
widthEm = 0.12;
337+
338+
// TODO .delimcenter .
334339

335340
case 'op-symbol':
336341
// .op-symbol { ... }
@@ -361,6 +366,7 @@ class _KatexParser {
361366
}
362367
}
363368
final styles = KatexSpanStyles(
369+
widthEm: widthEm,
364370
fontFamily: fontFamily,
365371
fontSizeEm: fontSizeEm,
366372
fontWeight: fontWeight,
@@ -453,6 +459,7 @@ enum KatexSpanTextAlign {
453459

454460
@immutable
455461
class KatexSpanStyles {
462+
final double? widthEm;
456463
final double? heightEm;
457464

458465
final String? fontFamily;
@@ -462,6 +469,7 @@ class KatexSpanStyles {
462469
final KatexSpanTextAlign? textAlign;
463470

464471
const KatexSpanStyles({
472+
this.widthEm,
465473
this.heightEm,
466474
this.fontFamily,
467475
this.fontSizeEm,
@@ -473,6 +481,7 @@ class KatexSpanStyles {
473481
@override
474482
int get hashCode => Object.hash(
475483
'KatexSpanStyles',
484+
widthEm,
476485
heightEm,
477486
fontFamily,
478487
fontSizeEm,
@@ -484,6 +493,7 @@ class KatexSpanStyles {
484493
@override
485494
bool operator ==(Object other) {
486495
return other is KatexSpanStyles &&
496+
other.widthEm == widthEm &&
487497
other.heightEm == heightEm &&
488498
other.fontFamily == fontFamily &&
489499
other.fontSizeEm == fontSizeEm &&
@@ -495,6 +505,7 @@ class KatexSpanStyles {
495505
@override
496506
String toString() {
497507
final args = <String>[];
508+
if (widthEm != null) args.add('widthEm: $widthEm');
498509
if (heightEm != null) args.add('heightEm: $heightEm');
499510
if (fontFamily != null) args.add('fontFamily: $fontFamily');
500511
if (fontSizeEm != null) args.add('fontSizeEm: $fontSizeEm');
@@ -506,6 +517,7 @@ class KatexSpanStyles {
506517

507518
KatexSpanStyles merge(KatexSpanStyles other) {
508519
return KatexSpanStyles(
520+
widthEm: other.widthEm ?? widthEm,
509521
heightEm: other.heightEm ?? heightEm,
510522
fontFamily: other.fontFamily ?? fontFamily,
511523
fontSizeEm: other.fontSizeEm ?? fontSizeEm,

lib/widgets/content.dart

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

942942
return SizedBox(
943+
width: styles.widthEm != null
944+
? styles.widthEm! * em
945+
: null,
943946
height: styles.heightEm != null
944947
? styles.heightEm! * em
945948
: null,

test/model/content_test.dart

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,67 @@ class ContentExample {
893893
]),
894894
]);
895895

896+
static const mathBlockKatexNulldelimiter = ContentExample(
897+
'math block; KaTeX nulldelimiter',
898+
// https://chat.zulip.org/#narrow/channel/7-test-here/topic/Rajesh/near/2205534
899+
'```math\n\\left. a \\middle. b \\right.\n```',
900+
'<p>'
901+
'<span class="katex-display"><span class="katex">'
902+
'<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>'
903+
'<span class="katex-html" aria-hidden="true">'
904+
'<span class="base">'
905+
'<span class="strut" style="height:0.6944em;"></span>'
906+
'<span class="minner">'
907+
'<span class="mopen nulldelimiter"></span>'
908+
'<span class="mord mathnormal">a</span>'
909+
'<span class="nulldelimiter"></span>'
910+
'<span class="mord mathnormal">b</span>'
911+
'<span class="mclose nulldelimiter"></span></span></span></span></span></span></p>',
912+
[
913+
MathBlockNode(
914+
texSource: '\\left. a \\middle. b \\right.',
915+
nodes: [
916+
KatexSpanNode(
917+
styles: KatexSpanStyles(),
918+
text: null,
919+
nodes: [
920+
KatexSpanNode(
921+
styles: KatexSpanStyles(heightEm: 0.6944),
922+
text: null,
923+
nodes: []),
924+
KatexSpanNode(
925+
styles: KatexSpanStyles(),
926+
text: null,
927+
nodes: [
928+
KatexSpanNode(
929+
styles: KatexSpanStyles(widthEm: 0.12),
930+
text: null,
931+
nodes: []),
932+
KatexSpanNode(
933+
styles: KatexSpanStyles(
934+
fontFamily: 'KaTeX_Math',
935+
fontStyle: KatexSpanFontStyle.italic),
936+
text: 'a',
937+
nodes: null),
938+
KatexSpanNode(
939+
styles: KatexSpanStyles(widthEm: 0.12),
940+
text: null,
941+
nodes: []),
942+
KatexSpanNode(
943+
styles: KatexSpanStyles(
944+
fontFamily: 'KaTeX_Math',
945+
fontStyle: KatexSpanFontStyle.italic),
946+
text: 'b',
947+
nodes: null),
948+
KatexSpanNode(
949+
styles: KatexSpanStyles(widthEm: 0.12),
950+
text: null,
951+
nodes: []),
952+
]),
953+
]),
954+
]),
955+
]);
956+
896957
static const imageSingle = ContentExample(
897958
'single image',
898959
// https://chat.zulip.org/#narrow/stream/7-test-here/topic/Thumbnails/near/1900103
@@ -1967,6 +2028,7 @@ void main() async {
19672028
// `vertical-align` in inline styles. Currently it fails
19682029
// because `strut` span has `vertical-align`.
19692030
testParseExample(ContentExample.mathBlockKatexDelimSizing, skip: true);
2031+
testParseExample(ContentExample.mathBlockKatexNulldelimiter);
19702032

19712033
testParseExample(ContentExample.imageSingle);
19722034
testParseExample(ContentExample.imageSingleNoDimensions);

0 commit comments

Comments
 (0)