| 990 | 990 |     end | 
  | 991 | 991 |     if attachments.present? | 
  | 992 | 992 |       title_and_alt_re = /\s+(title|alt)="([^"]*)"/i | 
  |  | 993 |       style_attr_re = /\s+style="([^"]*)"/i | 
  | 993 | 994 |  | 
  | 994 | 995 |       text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png|webp))"([^>]*)/i) do |m| | 
  | 995 | 996 |         filename, ext, other_attrs = $1, $2, $3 | 
  | 996 | 997 |  | 
  |  | 998 |         # Check if style attribute exists | 
  |  | 999 |         has_style = other_attrs =~ style_attr_re | 
  |  | 1000 |  | 
  | 997 | 1001 |         # search for the picture in attachments | 
  | 998 | 1002 |         if found = Attachment.latest_attach(attachments, CGI.unescape(filename)) | 
  | 999 | 1003 |           image_url = download_named_attachment_url(found, found.filename, :only_path => only_path) | 
  | ... | ... |  | 
  | 1003 | 1007 |           title_and_alt = other_attrs.scan(title_and_alt_re).to_h | 
  | 1004 | 1008 |           other_attrs.gsub!(title_and_alt_re, '') | 
  | 1005 | 1009 |  | 
  |  | 1010 |           # Process dimensions and scaling | 
  |  | 1011 |           width, height = nil, nil | 
  |  | 1012 |           image = MiniMagick::Image.open(found.diskfile) | 
  |  | 1013 |           width, height = image.width, image.height | 
  |  | 1014 |  | 
  |  | 1015 |           # Adjust dimensions for @2x, @3x, etc. | 
  |  | 1016 |           if filename =~ /@(\d+)x\./ | 
  |  | 1017 |             scale_factor = $1.to_i | 
  |  | 1018 |             width /= scale_factor | 
  |  | 1019 |             height /= scale_factor | 
  |  | 1020 |           end | 
  |  | 1021 |  | 
  |  | 1022 |           # Add width and height to the img tag only if style attribute is absent | 
  |  | 1023 |           dimension_attrs = (!has_style && width && height) ? " width=\"#{width}\" height=\"#{height}\"" : "" | 
  |  | 1024 |  | 
  | 1006 | 1025 |           title_and_alt_attrs = if !desc.blank? && title_and_alt['alt'].blank? | 
  | 1007 | 1026 |                                   " title=\"#{desc}\" alt=\"#{desc}\"" | 
  | 1008 | 1027 |                                 else | 
  | 1009 | 1028 |                                   # restore original title and alt attributes | 
  | 1010 | 1029 |                                   " #{title_and_alt.map { |k, v| %[#{k}="#{v}"] }.join(' ')}" | 
  | 1011 | 1030 |                                 end | 
  | 1012 |  |           "src=\"#{image_url}\"#{title_and_alt_attrs} loading=\"lazy\"#{other_attrs}" | 
  |  | 1031 |           "src=\"#{image_url}\"#{title_and_alt_attrs}#{dimension_attrs} loading=\"lazy\"#{other_attrs}" | 
  | 1013 | 1032 |         else | 
  | 1014 | 1033 |           m | 
  | 1015 | 1034 |         end |