Skip to content

Commit 0f634c9

Browse files
committed
katex [nfc]: Inline remaining/main use of _parseSpanInlineStyles
And inline the effect of the `merge` method, eliminating that method too. This way we get to construct just one KatexSpanStyles object, rather than constructing three of them when inline styles are present.
1 parent fe383b8 commit 0f634c9

File tree

1 file changed

+18
-58
lines changed

1 file changed

+18
-58
lines changed

lib/model/katex.dart

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -612,23 +612,32 @@ class _KatexParser {
612612
_hasError = true;
613613
}
614614
}
615-
final classStyles = KatexSpanStyles(
615+
616+
final inlineStyles = _parseInlineStyles(element);
617+
final styles = KatexSpanStyles(
616618
fontFamily: fontFamily,
617619
fontSizeEm: fontSizeEm,
618620
fontWeight: fontWeight,
619621
fontStyle: fontStyle,
620622
textAlign: textAlign,
623+
heightEm: _takeStyleEm(inlineStyles, 'height'),
624+
topEm: _takeStyleEm(inlineStyles, 'top'),
625+
marginLeftEm: _takeStyleEm(inlineStyles, 'margin-left'),
626+
marginRightEm: _takeStyleEm(inlineStyles, 'margin-right'),
627+
// TODO handle more CSS properties
621628
);
622-
final inlineStyles = _parseSpanInlineStyles(element);
623-
if (inlineStyles != null) {
624-
// Currently, we expect `top` to only be inside a vlist, and
625-
// we handle that case separately above.
626-
if (inlineStyles.topEm != null) {
627-
throw _KatexHtmlParseError('unsupported inline CSS property: top');
629+
if (inlineStyles != null && inlineStyles.isNotEmpty) {
630+
for (final property in inlineStyles.keys) {
631+
assert(debugLog('KaTeX: Unexpected inline CSS property: $property'));
632+
unsupportedInlineCssProperties.add(property);
633+
_hasError = true;
628634
}
629635
}
630-
final styles = inlineStyles == null ? classStyles
631-
: classStyles.merge(inlineStyles);
636+
// Currently, we expect `top` to only be inside a vlist, and
637+
// we handle that case separately above.
638+
if (styles.topEm != null) {
639+
throw _KatexHtmlParseError('unsupported inline CSS property: top');
640+
}
632641

633642
String? text;
634643
List<KatexNode>? spans;
@@ -646,34 +655,6 @@ class _KatexParser {
646655
debugHtmlNode: debugHtmlNode);
647656
}
648657

649-
/// Parse the inline CSS styles from the given element,
650-
/// and look for the styles we know how to interpret for a generic KaTeX span.
651-
///
652-
/// TODO: This has a number of call sites that aren't acting on a generic
653-
/// KaTeX span, but instead on spans in particular roles where we have
654-
/// much more specific expectations on the inline styles.
655-
/// For those, switch to [_parseInlineStyles] and inspect the styles directly.
656-
KatexSpanStyles? _parseSpanInlineStyles(dom.Element element) {
657-
final declarations = _parseInlineStyles(element);
658-
if (declarations == null) return null;
659-
660-
final result = KatexSpanStyles(
661-
heightEm: _takeStyleEm(declarations, 'height'),
662-
topEm: _takeStyleEm(declarations, 'top'),
663-
marginRightEm: _takeStyleEm(declarations, 'margin-right'),
664-
marginLeftEm: _takeStyleEm(declarations, 'margin-left'),
665-
// TODO handle more CSS properties
666-
);
667-
668-
for (final property in declarations.keys) {
669-
assert(debugLog('KaTeX: Unexpected inline CSS property: $property'));
670-
unsupportedInlineCssProperties.add(property);
671-
_hasError = true;
672-
}
673-
674-
return result;
675-
}
676-
677658
/// Parse the inline CSS styles from the given element.
678659
///
679660
/// To interpret the resulting map, consider [_takeStyleEm].
@@ -820,27 +801,6 @@ class KatexSpanStyles {
820801
return '${objectRuntimeType(this, 'KatexSpanStyles')}(${args.join(', ')})';
821802
}
822803

823-
/// Creates a new [KatexSpanStyles] with current and [other]'s styles merged.
824-
///
825-
/// The styles in [other] take precedence and any missing styles in [other]
826-
/// are filled in with current styles, if present.
827-
///
828-
/// This similar to the behaviour of [TextStyle.merge], if the given style
829-
/// had `inherit` set to true.
830-
KatexSpanStyles merge(KatexSpanStyles other) {
831-
return KatexSpanStyles(
832-
heightEm: other.heightEm ?? heightEm,
833-
topEm: other.topEm ?? topEm,
834-
marginRightEm: other.marginRightEm ?? marginRightEm,
835-
marginLeftEm: other.marginLeftEm ?? marginLeftEm,
836-
fontFamily: other.fontFamily ?? fontFamily,
837-
fontSizeEm: other.fontSizeEm ?? fontSizeEm,
838-
fontStyle: other.fontStyle ?? fontStyle,
839-
fontWeight: other.fontWeight ?? fontWeight,
840-
textAlign: other.textAlign ?? textAlign,
841-
);
842-
}
843-
844804
KatexSpanStyles filter({
845805
bool heightEm = true,
846806
bool verticalAlignEm = true,

0 commit comments

Comments
 (0)