parseInlineCssDeclarations function

Map<String, String> parseInlineCssDeclarations(
  1. String? styleAttribute
)

Splits an inline style="" attribute into declarations.

Property names are lower-cased. A later declaration of the same property replaces an earlier one, which is what the CSS cascade does inside one declaration block. A declaration with no colon, an empty property or an empty value is dropped.

Implementation

Map<String, String> parseInlineCssDeclarations(String? styleAttribute) {
  final declarations = <String, String>{};
  if (styleAttribute == null) return declarations;

  for (final part in styleAttribute.split(';')) {
    final colon = part.indexOf(':');
    if (colon <= 0) continue;
    final property = part.substring(0, colon).trim().toLowerCase();
    final value = part.substring(colon + 1).trim();
    if (property.isEmpty || value.isEmpty) continue;
    declarations[property] = value;
  }

  return declarations;
}