parseStyleAttribute function

Map<String, dynamic> parseStyleAttribute(
  1. String style
)

Parses a CSS style attribute string into Delta attributes.

Converts CSS styles (like 'text-align', 'color', 'font-size', etc.) from style into Quill Delta attributes suitable for rich text formatting.

Parameters:

  • style: The CSS style attribute string to parse.

Returns: A map of Delta attributes derived from the CSS styles.

Example:

final style = 'color: #ff0000; font-size: 16px;';
print(parseStyleAttribute(style)); // Output: {'color': '#ff0000', 'size': '16'}

Implementation

Map<String, dynamic> parseStyleAttribute(String style) {
  Map<String, dynamic> attributes = {};
  if (style.isEmpty) return attributes;

  final styles = style.split(';');
  double? fontSize;

  for (var style in styles) {
    final parts = style.split(':');
    if (parts.length == 2) {
      final key = parts[0].trim();
      final value = parts[1].trim();

      switch (key) {
        case 'text-align':
          attributes['align'] = value;
          break;
        case 'color':
          final color = validateAndGetColor(value);
          if (color != null) {
            attributes['color'] = color;
          }
          break;
        case 'background-color':
          final color = validateAndGetColor(value);
          if (color != null) {
            attributes['background'] = color;
          }
          break;
        case 'padding-left' || 'padding-right':
          final indentation = parseToIndent(value);
          if (indentation != 0) {
            attributes['indent'] = indentation;
          }
          break;
        case 'font-size':
          String? sizeToPass;

          // Handle default values used by [vsc_quill_delta_to_html]
          if (value == '0.75em') {
            fontSize = 10;
            sizeToPass = 'small';
          } else if (value == '1.5em') {
            fontSize = 18;
            sizeToPass = 'large';
          } else if (value == '2.5em') {
            fontSize = 22;
            sizeToPass = 'huge';
          } else {
            try {
              final size = parseSizeToPx(value);
              if (size <= 10) {
                fontSize = 10;
                sizeToPass = 'small';
              } else {
                fontSize = size.floorToDouble();
                sizeToPass = '${size.floor()}';
              }
            } on UnsupportedError {
              //ignore error
              break;
            }
          }
          attributes['size'] = sizeToPass;
          break;
        case 'font-family':
          attributes['font'] = value;
          break;
        case 'line-height':
          final lineHeight = parseLineHeight(value, fontSize: fontSize ?? 16.0);
          attributes['line-height'] = lineHeight;
          break;
        default:
          break;
      }
    } else {
      switch (style) {
        case 'justify' || 'center' || 'left' || 'right':
          attributes['align'] = style;
        case 'rtl':
          attributes['direction'] = 'rtl';
        case 'true' || 'false':
          // Treat as check list
          if (style == 'true') {
            attributes['list'] = 'checked';
          } else {
            attributes['list'] = 'unchecked';
          }
          break;
        default:
          break;
      }
    }
  }

  return attributes;
}