Skip to content

Commit 39fdded

Browse files
committed
katex [nfc]: Simplify extracting from attrs map in _parseInlineStyles
This map pattern syntax looks an awful lot like it's saying that no other keys should be present -- after all, that's what the corresponding syntax in a list pattern would mean. So I initially read this to mean that this code would ignore the inline styles if any other attribute was present on the element; which wouldn't be desirable logic. In fact it's just saying that this one key should be present and match the given pattern. But there are simpler ways to say that; so use one.
1 parent 2a604ba commit 39fdded

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

lib/model/katex.dart

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -653,32 +653,32 @@ class _KatexParser {
653653
///
654654
/// To interpret the resulting map, consider [_takeStyleEm].
655655
static Map<String, css_visitor.Expression>? _parseInlineStyles(dom.Element element) {
656-
if (element.attributes case {'style': final styleStr}) {
657-
// `package:csslib` doesn't seem to have a way to parse inline styles:
658-
// https://github.com/dart-lang/tools/issues/1173
659-
// So, work around that by wrapping it in a universal declaration.
660-
final stylesheet = css_parser.parse('*{$styleStr}');
661-
if (stylesheet.topLevels case [css_visitor.RuleSet() && final ruleSet]) {
662-
final result = <String, css_visitor.Expression>{};
663-
for (final declaration in ruleSet.declarationGroup.declarations) {
664-
if (declaration case css_visitor.Declaration(
665-
:final property,
666-
expression: css_visitor.Expressions(
667-
expressions: [css_visitor.Expression() && final expression]),
668-
)) {
669-
result.update(property, ifAbsent: () => expression,
670-
(_) => throw _KatexHtmlParseError(
671-
'duplicate inline CSS property: $property'));
672-
} else {
673-
throw _KatexHtmlParseError('unexpected shape of inline CSS');
674-
}
656+
final styleStr = element.attributes['style'];
657+
if (styleStr == null) return null;
658+
659+
// `package:csslib` doesn't seem to have a way to parse inline styles:
660+
// https://github.com/dart-lang/tools/issues/1173
661+
// So, work around that by wrapping it in a universal declaration.
662+
final stylesheet = css_parser.parse('*{$styleStr}');
663+
if (stylesheet.topLevels case [css_visitor.RuleSet() && final ruleSet]) {
664+
final result = <String, css_visitor.Expression>{};
665+
for (final declaration in ruleSet.declarationGroup.declarations) {
666+
if (declaration case css_visitor.Declaration(
667+
:final property,
668+
expression: css_visitor.Expressions(
669+
expressions: [css_visitor.Expression() && final expression]),
670+
)) {
671+
result.update(property, ifAbsent: () => expression,
672+
(_) => throw _KatexHtmlParseError(
673+
'duplicate inline CSS property: $property'));
674+
} else {
675+
throw _KatexHtmlParseError('unexpected shape of inline CSS');
675676
}
676-
return result;
677-
} else {
678-
throw _KatexHtmlParseError();
679677
}
678+
return result;
679+
} else {
680+
throw _KatexHtmlParseError();
680681
}
681-
return null;
682682
}
683683

684684
/// Remove the given property from the given style map,

0 commit comments

Comments
 (0)