Skip to content

Commit ee9d42c

Browse files
content: Handle positive margin-right and margin-left in KaTeX spans
1 parent 284464d commit ee9d42c

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

lib/model/katex.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ class _KatexParser {
392392
if (stylesheet.topLevels case [css_visitor.RuleSet() && final rule]) {
393393
double? heightEm;
394394
double? verticalAlignEm;
395+
double? marginRightEm;
396+
double? marginLeftEm;
395397

396398
for (final declaration in rule.declarationGroup.declarations) {
397399
if (declaration case css_visitor.Declaration(
@@ -407,6 +409,20 @@ class _KatexParser {
407409
case 'vertical-align':
408410
verticalAlignEm = _getEm(expression);
409411
if (verticalAlignEm != null) continue;
412+
413+
case 'margin-right':
414+
marginRightEm = _getEm(expression);
415+
if (marginRightEm != null) {
416+
if (marginRightEm < 0) throw KatexHtmlParseError();
417+
continue;
418+
}
419+
420+
case 'margin-left':
421+
marginLeftEm = _getEm(expression);
422+
if (marginLeftEm != null) {
423+
if (marginLeftEm < 0) throw KatexHtmlParseError();
424+
continue;
425+
}
410426
}
411427

412428
// TODO handle more CSS properties
@@ -421,6 +437,8 @@ class _KatexParser {
421437
return KatexSpanStyles(
422438
heightEm: heightEm,
423439
verticalAlignEm: verticalAlignEm,
440+
marginRightEm: marginRightEm,
441+
marginLeftEm: marginLeftEm,
424442
);
425443
} else {
426444
throw KatexHtmlParseError();
@@ -459,6 +477,9 @@ class KatexSpanStyles {
459477
final double? heightEm;
460478
final double? verticalAlignEm;
461479

480+
final double? marginRightEm;
481+
final double? marginLeftEm;
482+
462483
final String? fontFamily;
463484
final double? fontSizeEm;
464485
final KatexSpanFontWeight? fontWeight;
@@ -468,6 +489,8 @@ class KatexSpanStyles {
468489
const KatexSpanStyles({
469490
this.heightEm,
470491
this.verticalAlignEm,
492+
this.marginRightEm,
493+
this.marginLeftEm,
471494
this.fontFamily,
472495
this.fontSizeEm,
473496
this.fontWeight,
@@ -480,6 +503,8 @@ class KatexSpanStyles {
480503
'KatexSpanStyles',
481504
heightEm,
482505
verticalAlignEm,
506+
marginRightEm,
507+
marginLeftEm,
483508
fontFamily,
484509
fontSizeEm,
485510
fontWeight,
@@ -492,6 +517,8 @@ class KatexSpanStyles {
492517
return other is KatexSpanStyles &&
493518
other.heightEm == heightEm &&
494519
other.verticalAlignEm == verticalAlignEm &&
520+
other.marginRightEm == marginRightEm &&
521+
other.marginLeftEm == marginLeftEm &&
495522
other.fontFamily == fontFamily &&
496523
other.fontSizeEm == fontSizeEm &&
497524
other.fontWeight == fontWeight &&
@@ -504,6 +531,8 @@ class KatexSpanStyles {
504531
final args = <String>[];
505532
if (heightEm != null) args.add('heightEm: $heightEm');
506533
if (verticalAlignEm != null) args.add('verticalAlignEm: $verticalAlignEm');
534+
if (marginRightEm != null) args.add('marginRightEm: $marginRightEm');
535+
if (marginLeftEm != null) args.add('marginLeftEm: $marginLeftEm');
507536
if (fontFamily != null) args.add('fontFamily: $fontFamily');
508537
if (fontSizeEm != null) args.add('fontSizeEm: $fontSizeEm');
509538
if (fontWeight != null) args.add('fontWeight: $fontWeight');
@@ -516,6 +545,8 @@ class KatexSpanStyles {
516545
return KatexSpanStyles(
517546
heightEm: other.heightEm ?? heightEm,
518547
verticalAlignEm: other.verticalAlignEm ?? verticalAlignEm,
548+
marginRightEm: other.marginRightEm ?? marginRightEm,
549+
marginLeftEm: other.marginLeftEm ?? marginLeftEm,
519550
fontFamily: other.fontFamily ?? fontFamily,
520551
fontSizeEm: other.fontSizeEm ?? fontSizeEm,
521552
fontStyle: other.fontStyle ?? fontStyle,

lib/widgets/content.dart

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,30 @@ class _KatexSpan extends StatelessWidget {
965965
child: const Text(''));
966966
}
967967

968-
return SizedBox(
968+
final marginRight = switch (styles.marginRightEm) {
969+
double marginRightEm => marginRightEm * em,
970+
null => null,
971+
};
972+
final marginLeft = switch (styles.marginLeftEm) {
973+
double marginLeftEm => marginLeftEm * em,
974+
null => null,
975+
};
976+
977+
EdgeInsets? margin;
978+
if (marginRight != null || marginLeft != null) {
979+
margin = EdgeInsets.zero;
980+
if (marginRight != null) {
981+
assert(marginRight >= 0);
982+
margin += EdgeInsets.only(right: marginRight);
983+
}
984+
if (marginLeft != null) {
985+
assert(marginLeft >= 0);
986+
margin += EdgeInsets.only(left: marginLeft);
987+
}
988+
}
989+
990+
return Container(
991+
margin: margin,
969992
height: styles.heightEm != null
970993
? styles.heightEm! * em
971994
: null,

0 commit comments

Comments
 (0)