Skip to content

Commit c8558fb

Browse files
content: Handle positive margin-right and margin-left in KaTeX spans
1 parent 832d1b2 commit c8558fb

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
@@ -493,6 +493,8 @@ class _KatexParser {
493493
if (stylesheet.topLevels case [css_visitor.RuleSet() && final rule]) {
494494
double? heightEm;
495495
double? verticalAlignEm;
496+
double? marginRightEm;
497+
double? marginLeftEm;
496498

497499
for (final declaration in rule.declarationGroup.declarations) {
498500
if (declaration case css_visitor.Declaration(
@@ -508,6 +510,20 @@ class _KatexParser {
508510
case 'vertical-align':
509511
verticalAlignEm = _getEm(expression);
510512
if (verticalAlignEm != null) continue;
513+
514+
case 'margin-right':
515+
marginRightEm = _getEm(expression);
516+
if (marginRightEm != null) {
517+
if (marginRightEm < 0) throw _KatexHtmlParseError();
518+
continue;
519+
}
520+
521+
case 'margin-left':
522+
marginLeftEm = _getEm(expression);
523+
if (marginLeftEm != null) {
524+
if (marginLeftEm < 0) throw _KatexHtmlParseError();
525+
continue;
526+
}
511527
}
512528

513529
// TODO handle more CSS properties
@@ -523,6 +539,8 @@ class _KatexParser {
523539
return KatexSpanStyles(
524540
heightEm: heightEm,
525541
verticalAlignEm: verticalAlignEm,
542+
marginRightEm: marginRightEm,
543+
marginLeftEm: marginLeftEm,
526544
);
527545
} else {
528546
throw _KatexHtmlParseError();
@@ -561,6 +579,9 @@ class KatexSpanStyles {
561579
final double? heightEm;
562580
final double? verticalAlignEm;
563581

582+
final double? marginRightEm;
583+
final double? marginLeftEm;
584+
564585
final String? fontFamily;
565586
final double? fontSizeEm;
566587
final KatexSpanFontWeight? fontWeight;
@@ -570,6 +591,8 @@ class KatexSpanStyles {
570591
const KatexSpanStyles({
571592
this.heightEm,
572593
this.verticalAlignEm,
594+
this.marginRightEm,
595+
this.marginLeftEm,
573596
this.fontFamily,
574597
this.fontSizeEm,
575598
this.fontWeight,
@@ -582,6 +605,8 @@ class KatexSpanStyles {
582605
'KatexSpanStyles',
583606
heightEm,
584607
verticalAlignEm,
608+
marginRightEm,
609+
marginLeftEm,
585610
fontFamily,
586611
fontSizeEm,
587612
fontWeight,
@@ -594,6 +619,8 @@ class KatexSpanStyles {
594619
return other is KatexSpanStyles &&
595620
other.heightEm == heightEm &&
596621
other.verticalAlignEm == verticalAlignEm &&
622+
other.marginRightEm == marginRightEm &&
623+
other.marginLeftEm == marginLeftEm &&
597624
other.fontFamily == fontFamily &&
598625
other.fontSizeEm == fontSizeEm &&
599626
other.fontWeight == fontWeight &&
@@ -606,6 +633,8 @@ class KatexSpanStyles {
606633
final args = <String>[];
607634
if (heightEm != null) args.add('heightEm: $heightEm');
608635
if (verticalAlignEm != null) args.add('verticalAlignEm: $verticalAlignEm');
636+
if (marginRightEm != null) args.add('marginRightEm: $marginRightEm');
637+
if (marginLeftEm != null) args.add('marginLeftEm: $marginLeftEm');
609638
if (fontFamily != null) args.add('fontFamily: $fontFamily');
610639
if (fontSizeEm != null) args.add('fontSizeEm: $fontSizeEm');
611640
if (fontWeight != null) args.add('fontWeight: $fontWeight');
@@ -625,6 +654,8 @@ class KatexSpanStyles {
625654
return KatexSpanStyles(
626655
heightEm: other.heightEm ?? heightEm,
627656
verticalAlignEm: other.verticalAlignEm ?? verticalAlignEm,
657+
marginRightEm: other.marginRightEm ?? marginRightEm,
658+
marginLeftEm: other.marginLeftEm ?? marginLeftEm,
628659
fontFamily: other.fontFamily ?? fontFamily,
629660
fontSizeEm: other.fontSizeEm ?? fontSizeEm,
630661
fontStyle: other.fontStyle ?? fontStyle,
@@ -636,6 +667,8 @@ class KatexSpanStyles {
636667
KatexSpanStyles filter({
637668
bool heightEm = true,
638669
bool verticalAlignEm = true,
670+
bool marginRightEm = true,
671+
bool marginLeftEm = true,
639672
bool fontFamily = true,
640673
bool fontSizeEm = true,
641674
bool fontWeight = true,
@@ -645,6 +678,8 @@ class KatexSpanStyles {
645678
return KatexSpanStyles(
646679
heightEm: heightEm ? this.heightEm : null,
647680
verticalAlignEm: verticalAlignEm ? this.verticalAlignEm : null,
681+
marginRightEm: marginRightEm ? this.marginRightEm : null,
682+
marginLeftEm: marginLeftEm ? this.marginLeftEm : null,
648683
fontFamily: fontFamily ? this.fontFamily : null,
649684
fontSizeEm: fontSizeEm ? this.fontSizeEm : null,
650685
fontWeight: fontWeight ? this.fontWeight : null,

lib/widgets/content.dart

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,30 @@ class _KatexSpan extends StatelessWidget {
954954
child: widget);
955955
}
956956

957-
return SizedBox(
957+
final marginRight = switch (styles.marginRightEm) {
958+
double marginRightEm => marginRightEm * em,
959+
null => null,
960+
};
961+
final marginLeft = switch (styles.marginLeftEm) {
962+
double marginLeftEm => marginLeftEm * em,
963+
null => null,
964+
};
965+
966+
EdgeInsets? margin;
967+
if (marginRight != null || marginLeft != null) {
968+
margin = EdgeInsets.zero;
969+
if (marginRight != null) {
970+
assert(marginRight >= 0);
971+
margin += EdgeInsets.only(right: marginRight);
972+
}
973+
if (marginLeft != null) {
974+
assert(marginLeft >= 0);
975+
margin += EdgeInsets.only(left: marginLeft);
976+
}
977+
}
978+
979+
return Container(
980+
margin: margin,
958981
height: styles.heightEm != null
959982
? styles.heightEm! * (fontSize ?? em)
960983
: null,

0 commit comments

Comments
 (0)