Skip to content

Commit ef13eff

Browse files
content: Handle positive margin-right and margin-left in KaTeX spans
1 parent 92effd0 commit ef13eff

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

lib/model/katex.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ class _KatexParser {
424424
if (stylesheet.topLevels case [css_visitor.RuleSet() && final rule]) {
425425
double? heightEm;
426426
double? verticalAlignEm;
427+
double? marginRightEm;
428+
double? marginLeftEm;
427429

428430
for (final declaration in rule.declarationGroup.declarations) {
429431
if (declaration case css_visitor.Declaration(
@@ -439,6 +441,20 @@ class _KatexParser {
439441
case 'vertical-align':
440442
verticalAlignEm = _getEm(expression);
441443
if (verticalAlignEm != null) continue;
444+
445+
case 'margin-right':
446+
marginRightEm = _getEm(expression);
447+
if (marginRightEm != null) {
448+
if (marginRightEm < 0) throw KatexHtmlParseError();
449+
continue;
450+
}
451+
452+
case 'margin-left':
453+
marginLeftEm = _getEm(expression);
454+
if (marginLeftEm != null) {
455+
if (marginLeftEm < 0) throw KatexHtmlParseError();
456+
continue;
457+
}
442458
}
443459

444460
// TODO handle more CSS properties
@@ -453,6 +469,8 @@ class _KatexParser {
453469
return KatexSpanStyles(
454470
heightEm: heightEm,
455471
verticalAlignEm: verticalAlignEm,
472+
marginRightEm: marginRightEm,
473+
marginLeftEm: marginLeftEm,
456474
);
457475
} else {
458476
throw KatexHtmlParseError();
@@ -491,6 +509,9 @@ class KatexSpanStyles {
491509
final double? heightEm;
492510
final double? verticalAlignEm;
493511

512+
final double? marginRightEm;
513+
final double? marginLeftEm;
514+
494515
final String? fontFamily;
495516
final double? fontSizeEm;
496517
final KatexSpanFontWeight? fontWeight;
@@ -500,6 +521,8 @@ class KatexSpanStyles {
500521
const KatexSpanStyles({
501522
this.heightEm,
502523
this.verticalAlignEm,
524+
this.marginRightEm,
525+
this.marginLeftEm,
503526
this.fontFamily,
504527
this.fontSizeEm,
505528
this.fontWeight,
@@ -512,6 +535,8 @@ class KatexSpanStyles {
512535
'KatexSpanStyles',
513536
heightEm,
514537
verticalAlignEm,
538+
marginRightEm,
539+
marginLeftEm,
515540
fontFamily,
516541
fontSizeEm,
517542
fontWeight,
@@ -524,6 +549,8 @@ class KatexSpanStyles {
524549
return other is KatexSpanStyles &&
525550
other.heightEm == heightEm &&
526551
other.verticalAlignEm == verticalAlignEm &&
552+
other.marginRightEm == marginRightEm &&
553+
other.marginLeftEm == marginLeftEm &&
527554
other.fontFamily == fontFamily &&
528555
other.fontSizeEm == fontSizeEm &&
529556
other.fontWeight == fontWeight &&
@@ -536,6 +563,8 @@ class KatexSpanStyles {
536563
final args = <String>[];
537564
if (heightEm != null) args.add('heightEm: $heightEm');
538565
if (verticalAlignEm != null) args.add('verticalAlignEm: $verticalAlignEm');
566+
if (marginRightEm != null) args.add('marginRightEm: $marginRightEm');
567+
if (marginLeftEm != null) args.add('marginLeftEm: $marginLeftEm');
539568
if (fontFamily != null) args.add('fontFamily: $fontFamily');
540569
if (fontSizeEm != null) args.add('fontSizeEm: $fontSizeEm');
541570
if (fontWeight != null) args.add('fontWeight: $fontWeight');
@@ -548,6 +577,8 @@ class KatexSpanStyles {
548577
return KatexSpanStyles(
549578
heightEm: other.heightEm ?? heightEm,
550579
verticalAlignEm: other.verticalAlignEm ?? verticalAlignEm,
580+
marginRightEm: other.marginRightEm ?? marginRightEm,
581+
marginLeftEm: other.marginLeftEm ?? marginLeftEm,
551582
fontFamily: other.fontFamily ?? fontFamily,
552583
fontSizeEm: other.fontSizeEm ?? fontSizeEm,
553584
fontStyle: other.fontStyle ?? fontStyle,
@@ -559,6 +590,8 @@ class KatexSpanStyles {
559590
KatexSpanStyles filter({
560591
bool heightEm = true,
561592
bool verticalAlignEm = true,
593+
bool marginRightEm = true,
594+
bool marginLeftEm = true,
562595
bool fontFamily = true,
563596
bool fontSizeEm = true,
564597
bool fontWeight = true,
@@ -568,6 +601,8 @@ class KatexSpanStyles {
568601
return KatexSpanStyles(
569602
heightEm: heightEm ? this.heightEm : null,
570603
verticalAlignEm: verticalAlignEm ? this.verticalAlignEm : null,
604+
marginRightEm: marginRightEm ? this.marginRightEm : null,
605+
marginLeftEm: marginLeftEm ? this.marginLeftEm : null,
571606
fontFamily: fontFamily ? this.fontFamily : null,
572607
fontSizeEm: fontSizeEm ? this.fontSizeEm : null,
573608
fontWeight: fontWeight ? this.fontWeight : null,

lib/widgets/content.dart

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,30 @@ class _KatexSpan extends StatelessWidget {
959959
child: widget);
960960
}
961961

962-
return SizedBox(
962+
final marginRight = switch (styles.marginRightEm) {
963+
double marginRightEm => marginRightEm * em,
964+
null => null,
965+
};
966+
final marginLeft = switch (styles.marginLeftEm) {
967+
double marginLeftEm => marginLeftEm * em,
968+
null => null,
969+
};
970+
971+
EdgeInsets? margin;
972+
if (marginRight != null || marginLeft != null) {
973+
margin = EdgeInsets.zero;
974+
if (marginRight != null) {
975+
assert(marginRight >= 0);
976+
margin += EdgeInsets.only(right: marginRight);
977+
}
978+
if (marginLeft != null) {
979+
assert(marginLeft >= 0);
980+
margin += EdgeInsets.only(left: marginLeft);
981+
}
982+
}
983+
984+
return Container(
985+
margin: margin,
963986
height: styles.heightEm != null
964987
? styles.heightEm! * em
965988
: null,

0 commit comments

Comments
 (0)