Skip to content

Commit 228289a

Browse files
content: Handle 'nulldelimiter' KaTeX CSS
1 parent 80dcd47 commit 228289a

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
@@ -198,6 +198,7 @@ class _KatexParser {
198198
// A copy of class definition (where possible) is accompanied in a comment
199199
// with each case statement to keep track of updates.
200200
final spanClasses = List<String>.unmodifiable(element.className.split(' '));
201+
double? widthEm;
201202
String? fontFamily;
202203
double? fontSizeEm;
203204
KatexSpanFontWeight? fontWeight;
@@ -387,7 +388,11 @@ class _KatexParser {
387388
_ => throw _KatexHtmlParseError(),
388389
};
389390

390-
// TODO handle .nulldelimiter and .delimcenter .
391+
case 'nulldelimiter':
392+
// .nulldelimiter { display: inline-block; width: 0.12em; }
393+
widthEm = 0.12;
394+
395+
// TODO .delimcenter .
391396

392397
case 'op-symbol':
393398
// .op-symbol { ... }
@@ -430,6 +435,7 @@ class _KatexParser {
430435
}
431436
}
432437
final styles = KatexSpanStyles(
438+
widthEm: widthEm,
433439
fontFamily: fontFamily,
434440
fontSizeEm: fontSizeEm,
435441
fontWeight: fontWeight,
@@ -523,6 +529,7 @@ enum KatexSpanTextAlign {
523529

524530
@immutable
525531
class KatexSpanStyles {
532+
final double? widthEm;
526533
final double? heightEm;
527534

528535
final String? fontFamily;
@@ -532,6 +539,7 @@ class KatexSpanStyles {
532539
final KatexSpanTextAlign? textAlign;
533540

534541
const KatexSpanStyles({
542+
this.widthEm,
535543
this.heightEm,
536544
this.fontFamily,
537545
this.fontSizeEm,
@@ -543,6 +551,7 @@ class KatexSpanStyles {
543551
@override
544552
int get hashCode => Object.hash(
545553
'KatexSpanStyles',
554+
widthEm,
546555
heightEm,
547556
fontFamily,
548557
fontSizeEm,
@@ -554,6 +563,7 @@ class KatexSpanStyles {
554563
@override
555564
bool operator ==(Object other) {
556565
return other is KatexSpanStyles &&
566+
other.widthEm == widthEm &&
557567
other.heightEm == heightEm &&
558568
other.fontFamily == fontFamily &&
559569
other.fontSizeEm == fontSizeEm &&
@@ -565,6 +575,7 @@ class KatexSpanStyles {
565575
@override
566576
String toString() {
567577
final args = <String>[];
578+
if (widthEm != null) args.add('widthEm: $widthEm');
568579
if (heightEm != null) args.add('heightEm: $heightEm');
569580
if (fontFamily != null) args.add('fontFamily: $fontFamily');
570581
if (fontSizeEm != null) args.add('fontSizeEm: $fontSizeEm');
@@ -583,6 +594,7 @@ class KatexSpanStyles {
583594
/// had `inherit` set to true.
584595
KatexSpanStyles merge(KatexSpanStyles other) {
585596
return KatexSpanStyles(
597+
widthEm: other.widthEm ?? widthEm,
586598
heightEm: other.heightEm ?? heightEm,
587599
fontFamily: other.fontFamily ?? fontFamily,
588600
fontSizeEm: other.fontSizeEm ?? fontSizeEm,

lib/widgets/content.dart

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

949949
return SizedBox(
950+
width: styles.widthEm != null
951+
? styles.widthEm! * (fontSize ?? em)
952+
: null,
950953
height: styles.heightEm != null
951954
? styles.heightEm! * (fontSize ?? em)
952955
: 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)