Skip to content

Commit ed9b92c

Browse files
content: Handle 'position' property in KaTeX span inline style
No changes on the widget side, because all the offsets currently applied (we only support `top`) are relative to widgets original position, using `Container.transform`. Same reason the parser currently only accepts `relative` as the value.
1 parent 7d76da7 commit ed9b92c

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

lib/model/katex.dart

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ class _KatexParser {
514514
double? topEm;
515515
double? marginRightEm;
516516
double? marginLeftEm;
517+
KatexSpanPosition? position;
517518

518519
for (final declaration in rule.declarationGroup.declarations) {
519520
if (declaration case css_visitor.Declaration(
@@ -547,6 +548,13 @@ class _KatexParser {
547548
if (marginLeftEm < 0) throw KatexHtmlParseError();
548549
continue;
549550
}
551+
552+
case 'position':
553+
position = switch (_getLiteral(expression)) {
554+
'relative' => KatexSpanPosition.relative,
555+
_ => null,
556+
};
557+
if (position != null) continue;
550558
}
551559

552560
// TODO handle more CSS properties
@@ -564,6 +572,7 @@ class _KatexParser {
564572
verticalAlignEm: verticalAlignEm,
565573
marginRightEm: marginRightEm,
566574
marginLeftEm: marginLeftEm,
575+
position: position,
567576
);
568577
} else {
569578
throw KatexHtmlParseError();
@@ -580,6 +589,17 @@ class _KatexParser {
580589
}
581590
return null;
582591
}
592+
593+
/// Returns the CSS literal string value if the given [expression] is
594+
/// actually a literal expression, else returns null.
595+
String? _getLiteral(css_visitor.Expression expression) {
596+
if (expression case css_visitor.LiteralTerm(:final value)) {
597+
if (value case css_visitor.Identifier(:final name)) {
598+
return name;
599+
}
600+
}
601+
return null;
602+
}
583603
}
584604

585605
enum KatexSpanFontWeight {
@@ -597,6 +617,10 @@ enum KatexSpanTextAlign {
597617
right,
598618
}
599619

620+
enum KatexSpanPosition {
621+
relative,
622+
}
623+
600624
@immutable
601625
class KatexSpanStyles {
602626
final double? heightEm;
@@ -613,6 +637,8 @@ class KatexSpanStyles {
613637
final KatexSpanFontStyle? fontStyle;
614638
final KatexSpanTextAlign? textAlign;
615639

640+
final KatexSpanPosition? position;
641+
616642
const KatexSpanStyles({
617643
this.heightEm,
618644
this.verticalAlignEm,
@@ -624,6 +650,7 @@ class KatexSpanStyles {
624650
this.fontWeight,
625651
this.fontStyle,
626652
this.textAlign,
653+
this.position,
627654
});
628655

629656
@override
@@ -639,6 +666,7 @@ class KatexSpanStyles {
639666
fontWeight,
640667
fontStyle,
641668
textAlign,
669+
position,
642670
);
643671

644672
@override
@@ -653,7 +681,8 @@ class KatexSpanStyles {
653681
other.fontSizeEm == fontSizeEm &&
654682
other.fontWeight == fontWeight &&
655683
other.fontStyle == fontStyle &&
656-
other.textAlign == textAlign;
684+
other.textAlign == textAlign &&
685+
other.position == position;
657686
}
658687

659688
@override
@@ -669,6 +698,7 @@ class KatexSpanStyles {
669698
if (fontWeight != null) args.add('fontWeight: $fontWeight');
670699
if (fontStyle != null) args.add('fontStyle: $fontStyle');
671700
if (textAlign != null) args.add('textAlign: $textAlign');
701+
if (position != null) args.add('position: $position');
672702
return '${objectRuntimeType(this, 'KatexSpanStyles')}(${args.join(', ')})';
673703
}
674704

@@ -684,6 +714,7 @@ class KatexSpanStyles {
684714
fontStyle: other.fontStyle ?? fontStyle,
685715
fontWeight: other.fontWeight ?? fontWeight,
686716
textAlign: other.textAlign ?? textAlign,
717+
position: other.position ?? position,
687718
);
688719
}
689720

@@ -698,6 +729,7 @@ class KatexSpanStyles {
698729
bool fontWeight = true,
699730
bool fontStyle = true,
700731
bool textAlign = true,
732+
bool position = true,
701733
}) {
702734
return KatexSpanStyles(
703735
heightEm: heightEm ? this.heightEm : null,
@@ -710,6 +742,7 @@ class KatexSpanStyles {
710742
fontWeight: fontWeight ? this.fontWeight : null,
711743
fontStyle: fontStyle ? this.fontStyle : null,
712744
textAlign: textAlign ? this.textAlign : null,
745+
position: position ? this.position : null,
713746
);
714747
}
715748
}

0 commit comments

Comments
 (0)