parseInlineStyle function

List<CssDeclaration> parseInlineStyle(
  1. String style
)

Splits an inline style="" attribute into declarations, in source order.

Keeps duplicates: the CSS cascade resolves two declarations of the same property by source order, and the whole point of this package is to resolve that cascade before flutter_html sees it. Splits on ; only outside parentheses, so an rgb(0, 0, 0) value survives intact.

Implementation

List<CssDeclaration> parseInlineStyle(String style) {
  final out = <CssDeclaration>[];
  var depth = 0;
  var start = 0;

  void take(int end) {
    final part = style.substring(start, end);
    final colon = part.indexOf(':');
    if (colon <= 0) return;
    final property = part.substring(0, colon).trim().toLowerCase();
    var value = part.substring(colon + 1).trim();
    if (property.isEmpty || value.isEmpty) return;

    var important = false;
    final bang = value.lastIndexOf('!');
    if (bang >= 0 && value.substring(bang + 1).trim().toLowerCase() == 'important') {
      important = true;
      value = value.substring(0, bang).trim();
    }
    if (value.isEmpty) return;

    out.add(CssDeclaration(property, value, important: important));
  }

  for (var i = 0; i < style.length; i++) {
    final c = style[i];
    if (c == '(') {
      depth++;
    } else if (c == ')') {
      if (depth > 0) depth--;
    } else if (c == ';' && depth == 0) {
      take(i);
      start = i + 1;
    }
  }
  take(style.length);
  return out;
}