Skip to content

Commit 4b51709

Browse files
content: Support parsing and handling inline styles for KaTeX content
1 parent d3b21fd commit 4b51709

File tree

3 files changed

+109
-13
lines changed

3 files changed

+109
-13
lines changed

lib/model/katex.dart

Lines changed: 82 additions & 1 deletion
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

@@ -354,11 +356,67 @@ class _KatexParser {
354356
}
355357
if (text == null && spans == null) throw KatexHtmlParseError();
356358

359+
final inlineStyles = _parseSpanInlineStyles(element);
360+
357361
return KatexNode(
358-
styles: styles,
362+
styles: inlineStyles != null
363+
? styles.merge(inlineStyles)
364+
: styles,
359365
text: text,
360366
nodes: spans);
361367
}
368+
369+
KatexSpanStyles? _parseSpanInlineStyles(dom.Element element) {
370+
if (element.attributes case {'style': final styleStr}) {
371+
// `package:csslib` doesn't seem to have a way to parse inline styles:
372+
// https://github.com/dart-lang/tools/issues/1173
373+
// So, workaround that by wrapping it in a universal declaration.
374+
final stylesheet = css_parser.parse('*{$styleStr}');
375+
if (stylesheet.topLevels case [css_visitor.RuleSet() && final rule]) {
376+
double? heightEm;
377+
double? verticalAlignEm;
378+
379+
for (final declaration in rule.declarationGroup.declarations) {
380+
if (declaration case css_visitor.Declaration(
381+
:final property,
382+
expression: css_visitor.Expressions(
383+
expressions: [css_visitor.Expression() && final expression]),
384+
)) {
385+
switch (property) {
386+
case 'height':
387+
heightEm = _getEm(expression);
388+
if (heightEm != null) continue;
389+
390+
case 'vertical-align':
391+
verticalAlignEm = _getEm(expression);
392+
if (verticalAlignEm != null) continue;
393+
}
394+
395+
// TODO handle more CSS properties
396+
_logError('KaTeX: Unsupported CSS property: $property of '
397+
'type ${expression.runtimeType}');
398+
} else {
399+
throw KatexHtmlParseError();
400+
}
401+
}
402+
403+
return KatexSpanStyles(
404+
heightEm: heightEm,
405+
verticalAlignEm: verticalAlignEm,
406+
);
407+
} else {
408+
throw KatexHtmlParseError();
409+
}
410+
}
411+
return null;
412+
}
413+
414+
double? _getEm(css_visitor.Expression expression) {
415+
if (expression is css_visitor.EmTerm && expression.value is num) {
416+
return (expression.value as num).toDouble();
417+
}
418+
return null;
419+
}
362420
}
363421

364422
enum KatexSpanFontWeight {
@@ -378,13 +436,18 @@ enum KatexSpanTextAlign {
378436

379437
@immutable
380438
class KatexSpanStyles {
439+
final double? heightEm;
440+
final double? verticalAlignEm;
441+
381442
final String? fontFamily;
382443
final double? fontSizeEm;
383444
final KatexSpanFontWeight? fontWeight;
384445
final KatexSpanFontStyle? fontStyle;
385446
final KatexSpanTextAlign? textAlign;
386447

387448
const KatexSpanStyles({
449+
this.heightEm,
450+
this.verticalAlignEm,
388451
this.fontFamily,
389452
this.fontSizeEm,
390453
this.fontWeight,
@@ -395,6 +458,8 @@ class KatexSpanStyles {
395458
@override
396459
int get hashCode => Object.hash(
397460
'KatexSpanStyles',
461+
heightEm,
462+
verticalAlignEm,
398463
fontFamily,
399464
fontSizeEm,
400465
fontWeight,
@@ -405,6 +470,8 @@ class KatexSpanStyles {
405470
@override
406471
bool operator ==(Object other) {
407472
return other is KatexSpanStyles &&
473+
other.heightEm == heightEm &&
474+
other.verticalAlignEm == verticalAlignEm &&
408475
other.fontFamily == fontFamily &&
409476
other.fontSizeEm == fontSizeEm &&
410477
other.fontWeight == fontWeight &&
@@ -415,13 +482,27 @@ class KatexSpanStyles {
415482
@override
416483
String toString() {
417484
final args = <String>[];
485+
if (heightEm != null) args.add('heightEm: $heightEm');
486+
if (verticalAlignEm != null) args.add('verticalAlignEm: $verticalAlignEm');
418487
if (fontFamily != null) args.add('fontFamily: $fontFamily');
419488
if (fontSizeEm != null) args.add('fontSizeEm: $fontSizeEm');
420489
if (fontWeight != null) args.add('fontWeight: $fontWeight');
421490
if (fontStyle != null) args.add('fontStyle: $fontStyle');
422491
if (textAlign != null) args.add('textAlign: $textAlign');
423492
return '${objectRuntimeType(this, 'KatexSpanStyles')}(${args.join(', ')})';
424493
}
494+
495+
KatexSpanStyles merge(KatexSpanStyles other) {
496+
return KatexSpanStyles(
497+
heightEm: other.heightEm ?? heightEm,
498+
verticalAlignEm: other.verticalAlignEm ?? verticalAlignEm,
499+
fontFamily: other.fontFamily ?? fontFamily,
500+
fontSizeEm: other.fontSizeEm ?? fontSizeEm,
501+
fontStyle: other.fontStyle ?? fontStyle,
502+
fontWeight: other.fontWeight ?? fontWeight,
503+
textAlign: other.textAlign ?? textAlign,
504+
);
505+
}
425506
}
426507

427508
class KatexHtmlParseError extends Error {

lib/widgets/content.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,20 @@ class _KatexSpan extends StatelessWidget {
939939
textAlign: textAlign,
940940
child: widget);
941941
}
942-
return widget;
942+
943+
if (styles.verticalAlignEm != null) {
944+
widget = Baseline(
945+
baseline: styles.verticalAlignEm! * em,
946+
baselineType: TextBaseline.alphabetic,
947+
child: widget);
948+
}
949+
950+
return SizedBox(
951+
height: styles.heightEm != null
952+
? styles.heightEm! * em
953+
: null,
954+
child: widget,
955+
);
943956
}
944957
}
945958

test/model/content_test.dart

Lines changed: 13 additions & 11 deletions
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)