Skip to content

Commit 8644903

Browse files
content: Support parsing and handling inline styles for KaTeX content
1 parent 99874fa commit 8644903

File tree

3 files changed

+110
-13
lines changed

3 files changed

+110
-13
lines changed

lib/model/katex.dart

+82-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:csslib/parser.dart' as css_parser;
2+
import 'package:csslib/visitor.dart' as css_visitor;
13
import 'package:flutter/foundation.dart';
24
import 'package:html/dom.dart' as dom;
35

@@ -402,11 +404,67 @@ class _KatexParser {
402404
}
403405
if (text == null && spans == null) throw KatexHtmlParseError();
404406

407+
final inlineStyles = _parseSpanInlineStyles(element);
408+
405409
return KatexNode(
406-
styles: styles,
410+
styles: inlineStyles != null
411+
? styles.merge(inlineStyles)
412+
: styles,
407413
text: text,
408414
nodes: spans);
409415
}
416+
417+
KatexSpanStyles? _parseSpanInlineStyles(dom.Element element) {
418+
if (element.attributes case {'style': final styleStr}) {
419+
// `package:csslib` doesn't seem to have a way to parse inline styles:
420+
// https://github.com/dart-lang/tools/issues/1173
421+
// So, workaround that by wrapping it in a universal declaration.
422+
final stylesheet = css_parser.parse('*{$styleStr}');
423+
if (stylesheet.topLevels case [css_visitor.RuleSet() && final rule]) {
424+
double? heightEm;
425+
double? verticalAlignEm;
426+
427+
for (final declaration in rule.declarationGroup.declarations) {
428+
if (declaration case css_visitor.Declaration(
429+
:final property,
430+
expression: css_visitor.Expressions(
431+
expressions: [css_visitor.Expression() && final expression]),
432+
)) {
433+
switch (property) {
434+
case 'height':
435+
heightEm = _getEm(expression);
436+
if (heightEm != null) continue;
437+
438+
case 'vertical-align':
439+
verticalAlignEm = _getEm(expression);
440+
if (verticalAlignEm != null) continue;
441+
}
442+
443+
// TODO handle more CSS properties
444+
_logError('KaTeX: Unsupported CSS property: $property of '
445+
'type ${expression.runtimeType}');
446+
} else {
447+
throw KatexHtmlParseError();
448+
}
449+
}
450+
451+
return KatexSpanStyles(
452+
heightEm: heightEm,
453+
verticalAlignEm: verticalAlignEm,
454+
);
455+
} else {
456+
throw KatexHtmlParseError();
457+
}
458+
}
459+
return null;
460+
}
461+
462+
double? _getEm(css_visitor.Expression expression) {
463+
if (expression is css_visitor.EmTerm && expression.value is num) {
464+
return (expression.value as num).toDouble();
465+
}
466+
return null;
467+
}
410468
}
411469

412470
enum KatexSpanFontWeight {
@@ -425,13 +483,18 @@ enum KatexSpanTextAlign {
425483
}
426484

427485
class KatexSpanStyles {
486+
double? heightEm;
487+
double? verticalAlignEm;
488+
428489
String? fontFamily;
429490
double? fontSizeEm;
430491
KatexSpanFontWeight? fontWeight;
431492
KatexSpanFontStyle? fontStyle;
432493
KatexSpanTextAlign? textAlign;
433494

434495
KatexSpanStyles({
496+
this.heightEm,
497+
this.verticalAlignEm,
435498
this.fontFamily,
436499
this.fontSizeEm,
437500
this.fontWeight,
@@ -442,6 +505,8 @@ class KatexSpanStyles {
442505
@override
443506
int get hashCode => Object.hash(
444507
'KatexSpanStyles',
508+
heightEm,
509+
verticalAlignEm,
445510
fontFamily,
446511
fontSizeEm,
447512
fontWeight,
@@ -452,6 +517,8 @@ class KatexSpanStyles {
452517
@override
453518
bool operator ==(Object other) {
454519
return other is KatexSpanStyles &&
520+
other.heightEm == heightEm &&
521+
other.verticalAlignEm == verticalAlignEm &&
455522
other.fontFamily == fontFamily &&
456523
other.fontSizeEm == fontSizeEm &&
457524
other.fontWeight == fontWeight &&
@@ -462,13 +529,27 @@ class KatexSpanStyles {
462529
@override
463530
String toString() {
464531
final args = <String>[];
532+
if (heightEm != null) args.add('heightEm: $heightEm');
533+
if (verticalAlignEm != null) args.add('verticalAlignEm: $verticalAlignEm');
465534
if (fontFamily != null) args.add('fontFamily: $fontFamily');
466535
if (fontSizeEm != null) args.add('fontSizeEm: $fontSizeEm');
467536
if (fontWeight != null) args.add('fontWeight: $fontWeight');
468537
if (fontStyle != null) args.add('fontStyle: $fontStyle');
469538
if (textAlign != null) args.add('textAlign: $textAlign');
470539
return '${objectRuntimeType(this, 'KatexSpanStyles')}(${args.join(', ')})';
471540
}
541+
542+
KatexSpanStyles merge(KatexSpanStyles other) {
543+
return KatexSpanStyles(
544+
heightEm: other.heightEm ?? heightEm,
545+
verticalAlignEm: other.verticalAlignEm ?? verticalAlignEm,
546+
fontFamily: other.fontFamily ?? fontFamily,
547+
fontSizeEm: other.fontSizeEm ?? fontSizeEm,
548+
fontStyle: other.fontStyle ?? fontStyle,
549+
fontWeight: other.fontWeight ?? fontWeight,
550+
textAlign: other.textAlign ?? textAlign,
551+
);
552+
}
472553
}
473554

474555
class KatexHtmlParseError extends Error {

lib/widgets/content.dart

+15-1
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,21 @@ class _KatexSpan extends StatelessWidget {
933933
textAlign: textAlign,
934934
child: widget);
935935
}
936-
return widget;
936+
937+
if (styles.verticalAlignEm != null) {
938+
final em = DefaultTextStyle.of(context).style.fontSize!;
939+
widget = Baseline(
940+
baseline: styles.verticalAlignEm! * em,
941+
baselineType: TextBaseline.alphabetic,
942+
child: widget);
943+
}
944+
945+
return SizedBox(
946+
height: styles.heightEm != null
947+
? styles.heightEm! * DefaultTextStyle.of(context).style.fontSize!
948+
: null,
949+
child: widget,
950+
);
937951
}
938952
}
939953

test/model/content_test.dart

+13-11
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ class ContentExample {
519519
'<span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">λ</span></span></span></span></p>',
520520
MathInlineNode(texSource: r'\lambda', nodes: [
521521
KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
522-
KatexNode(styles: KatexSpanStyles(), text: null, nodes: []),
522+
KatexNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
523523
KatexNode(
524524
styles: KatexSpanStyles(
525525
fontFamily: 'KaTeX_Math',
@@ -539,7 +539,7 @@ class ContentExample {
539539
'<span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">λ</span></span></span></span></span></p>',
540540
[MathBlockNode(texSource: r'\lambda', nodes: [
541541
KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
542-
KatexNode(styles: KatexSpanStyles(), text: null, nodes: []),
542+
KatexNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
543543
KatexNode(
544544
styles: KatexSpanStyles(
545545
fontFamily: 'KaTeX_Math',
@@ -564,7 +564,7 @@ class ContentExample {
564564
'<span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">b</span></span></span></span></span></p>', [
565565
MathBlockNode(texSource: 'a', nodes: [
566566
KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
567-
KatexNode(styles: KatexSpanStyles(), text: null, nodes: []),
567+
KatexNode(styles: KatexSpanStyles(heightEm: 0.4306), text: null, nodes: []),
568568
KatexNode(
569569
styles: KatexSpanStyles(
570570
fontFamily: 'KaTeX_Math',
@@ -575,7 +575,7 @@ class ContentExample {
575575
]),
576576
MathBlockNode(texSource: 'b', nodes: [
577577
KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
578-
KatexNode(styles: KatexSpanStyles(), text: null, nodes: []),
578+
KatexNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
579579
KatexNode(
580580
styles: KatexSpanStyles(
581581
fontFamily: 'KaTeX_Math',
@@ -603,7 +603,7 @@ class ContentExample {
603603
[QuotationNode([
604604
MathBlockNode(texSource: r'\lambda', nodes: [
605605
KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
606-
KatexNode(styles: KatexSpanStyles(), text: null, nodes: []),
606+
KatexNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
607607
KatexNode(
608608
styles: KatexSpanStyles(
609609
fontFamily: 'KaTeX_Math',
@@ -632,7 +632,7 @@ class ContentExample {
632632
[QuotationNode([
633633
MathBlockNode(texSource: 'a', nodes: [
634634
KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
635-
KatexNode(styles: KatexSpanStyles(), text: null, nodes: []),
635+
KatexNode(styles: KatexSpanStyles(heightEm: 0.4306), text: null, nodes: []),
636636
KatexNode(
637637
styles: KatexSpanStyles(
638638
fontFamily: 'KaTeX_Math',
@@ -643,7 +643,7 @@ class ContentExample {
643643
]),
644644
MathBlockNode(texSource: 'b', nodes: [
645645
KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
646-
KatexNode(styles: KatexSpanStyles(), text: null, nodes: []),
646+
KatexNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
647647
KatexNode(
648648
styles: KatexSpanStyles(
649649
fontFamily: 'KaTeX_Math',
@@ -681,7 +681,7 @@ class ContentExample {
681681
]),
682682
MathBlockNode(texSource: 'a', nodes: [
683683
KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
684-
KatexNode(styles: KatexSpanStyles(),text: null, nodes: []),
684+
KatexNode(styles: KatexSpanStyles(heightEm: 0.4306),text: null, nodes: []),
685685
KatexNode(
686686
styles: KatexSpanStyles(
687687
fontFamily: 'KaTeX_Math',
@@ -732,7 +732,7 @@ class ContentExample {
732732
text: null,
733733
nodes: [
734734
KatexNode(
735-
styles: KatexSpanStyles(),
735+
styles: KatexSpanStyles(heightEm: 1.6034),
736736
text: null,
737737
nodes: []),
738738
KatexNode(
@@ -801,7 +801,7 @@ class ContentExample {
801801
text: null,
802802
nodes: [
803803
KatexNode(
804-
styles: KatexSpanStyles(),
804+
styles: KatexSpanStyles(heightEm: 1.6034),
805805
text: null,
806806
nodes: []),
807807
KatexNode(
@@ -846,7 +846,9 @@ class ContentExample {
846846
text: null,
847847
nodes: [
848848
KatexNode(
849-
styles: KatexSpanStyles(),
849+
styles: KatexSpanStyles(
850+
heightEm: 3.0,
851+
verticalAlignEm: -1.25),
850852
text: null,
851853
nodes: []),
852854
KatexNode(

0 commit comments

Comments
 (0)