Project

General

Profile

Defect #40610 » 40610.patch

Go MAEDA, 2024-04-25 02:25

View differences:

app/helpers/application_helper.rb
937 937
    return if options[:inline_attachments] == false
938 938

  
939 939
    # when using an image link, try to use an attachment, if possible
940
    attachments = options[:attachments] || []
941
    if obj.is_a?(Journal)
942
      attachments += obj.journalized.attachments if obj.journalized.respond_to?(:attachments)
943
    else
944
      attachments += obj.attachments if obj.respond_to?(:attachments)
945
    end
946
    if attachments.present?
947
      text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png|webp))"(\s+alt="([^"]*)")?/i) do |m|
948
        filename, ext, alt, alttext = $1, $2, $3, $4
949
        # search for the picture in attachments
950
        if found = Attachment.latest_attach(attachments, CGI.unescape(filename))
951
          image_url = download_named_attachment_url(found, found.filename, :only_path => only_path)
952
          desc = found.description.to_s.delete('"')
953
          if !desc.blank? && alttext.blank?
954
            alt = " title=\"#{desc}\" alt=\"#{desc}\""
955
          end
956
          "src=\"#{image_url}\"#{alt} loading=\"lazy\""
957
        else
958
          m
940
    attachments = nil
941
    text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png|webp))"(\s+alt="([^"]*)")?/i) do |m|
942
      # Load attachments only if not already loaded
943
      attachments ||=
944
        options[:attachments].to_a +
945
        (obj.is_a?(Journal) ? obj.journalized.try(:attachments).to_a : obj.try(:attachments).to_a)
946
      # If no attachments, skip processing
947
      next m if attachments.empty?
948

  
949
      filename, ext, alt, alttext = $1, $2, $3, $4
950
      # search for the picture in attachments
951
      if found = Attachment.latest_attach(attachments, CGI.unescape(filename))
952
        image_url = download_named_attachment_url(found, found.filename, :only_path => only_path)
953
        desc = found.description.to_s.delete('"')
954
        if !desc.blank? && alttext.blank?
955
          alt = " title=\"#{desc}\" alt=\"#{desc}\""
959 956
        end
957
        "src=\"#{image_url}\"#{alt} loading=\"lazy\""
958
      else
959
        m
960 960
      end
961 961
    end
962 962
  end
(1-1/2)