Skip to content

Commit 9bb0ed0

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 22f09ed commit 9bb0ed0

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
@@ -583,6 +583,7 @@ class _KatexParser {
583583
double? topEm;
584584
double? marginRightEm;
585585
double? marginLeftEm;
586+
KatexSpanPosition? position;
586587

587588
for (final declaration in rule.declarationGroup.declarations) {
588589
if (declaration case css_visitor.Declaration(
@@ -616,6 +617,13 @@ class _KatexParser {
616617
if (marginLeftEm < 0) throw _KatexHtmlParseError();
617618
continue;
618619
}
620+
621+
case 'position':
622+
position = switch (_getLiteral(expression)) {
623+
'relative' => KatexSpanPosition.relative,
624+
_ => null,
625+
};
626+
if (position != null) continue;
619627
}
620628

621629
// TODO handle more CSS properties
@@ -634,6 +642,7 @@ class _KatexParser {
634642
verticalAlignEm: verticalAlignEm,
635643
marginRightEm: marginRightEm,
636644
marginLeftEm: marginLeftEm,
645+
position: position,
637646
);
638647
} else {
639648
throw _KatexHtmlParseError();
@@ -650,6 +659,17 @@ class _KatexParser {
650659
}
651660
return null;
652661
}
662+
663+
/// Returns the CSS literal string value if the given [expression] is
664+
/// actually a literal expression, else returns null.
665+
String? _getLiteral(css_visitor.Expression expression) {
666+
if (expression case css_visitor.LiteralTerm(:final value)) {
667+
if (value case css_visitor.Identifier(:final name)) {
668+
return name;
669+
}
670+
}
671+
return null;
672+
}
653673
}
654674

655675
enum KatexSpanFontWeight {
@@ -667,6 +687,10 @@ enum KatexSpanTextAlign {
667687
right,
668688
}
669689

690+
enum KatexSpanPosition {
691+
relative,
692+
}
693+
670694
@immutable
671695
class KatexSpanStyles {
672696
final double? heightEm;
@@ -683,6 +707,8 @@ class KatexSpanStyles {
683707
final KatexSpanFontStyle? fontStyle;
684708
final KatexSpanTextAlign? textAlign;
685709

710+
final KatexSpanPosition? position;
711+
686712
const KatexSpanStyles({
687713
this.heightEm,
688714
this.verticalAlignEm,
@@ -694,6 +720,7 @@ class KatexSpanStyles {
694720
this.fontWeight,
695721
this.fontStyle,
696722
this.textAlign,
723+
this.position,
697724
});
698725

699726
@override
@@ -709,6 +736,7 @@ class KatexSpanStyles {
709736
fontWeight,
710737
fontStyle,
711738
textAlign,
739+
position,
712740
);
713741

714742
@override
@@ -723,7 +751,8 @@ class KatexSpanStyles {
723751
other.fontSizeEm == fontSizeEm &&
724752
other.fontWeight == fontWeight &&
725753
other.fontStyle == fontStyle &&
726-
other.textAlign == textAlign;
754+
other.textAlign == textAlign &&
755+
other.position == position;
727756
}
728757

729758
@override
@@ -739,6 +768,7 @@ class KatexSpanStyles {
739768
if (fontWeight != null) args.add('fontWeight: $fontWeight');
740769
if (fontStyle != null) args.add('fontStyle: $fontStyle');
741770
if (textAlign != null) args.add('textAlign: $textAlign');
771+
if (position != null) args.add('position: $position');
742772
return '${objectRuntimeType(this, 'KatexSpanStyles')}(${args.join(', ')})';
743773
}
744774

@@ -761,6 +791,7 @@ class KatexSpanStyles {
761791
fontStyle: other.fontStyle ?? fontStyle,
762792
fontWeight: other.fontWeight ?? fontWeight,
763793
textAlign: other.textAlign ?? textAlign,
794+
position: other.position ?? position,
764795
);
765796
}
766797

@@ -775,6 +806,7 @@ class KatexSpanStyles {
775806
bool fontWeight = true,
776807
bool fontStyle = true,
777808
bool textAlign = true,
809+
bool position = true,
778810
}) {
779811
return KatexSpanStyles(
780812
heightEm: heightEm ? this.heightEm : null,
@@ -787,6 +819,7 @@ class KatexSpanStyles {
787819
fontWeight: fontWeight ? this.fontWeight : null,
788820
fontStyle: fontStyle ? this.fontStyle : null,
789821
textAlign: textAlign ? this.textAlign : null,
822+
position: position ? this.position : null,
790823
);
791824
}
792825
}

0 commit comments

Comments
 (0)