parseImageStyleAttribute function

Map<String, dynamic> parseImageStyleAttribute(
  1. String style,
  2. String align
)

Parses a CSS <img> style attribute string into Delta attributes.

Converts CSS styles (like 'width', 'height', 'margin') from style into Quill Delta attributes suitable for image 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 = 'width: 50px; height: 250px;';
print(parseStyleAttribute(style)); // Output: {'width': '50px', 'height': '250px'}

Implementation

Map<String, dynamic> parseImageStyleAttribute(String style, String align) {
  Map<String, dynamic> attributes = {};

  final styles = style.split(';');
  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 'width':
          attributes['width'] = value;
          break;
        case 'height':
          attributes['height'] = value;
          break;
        case 'margin':
          attributes['margin'] = value;
          break;
        default:
          // Ignore other styles
          break;
      }
    }
  }

  if (align.isNotEmpty) attributes['alignment'] = align;
  return attributes;
}