mirror of
https://github.com/redmine/redmine.git
synced 2025-11-10 23:36:01 +01:00
Fix duplicated alt and title attributes for attached images in Textile formatter (#40650).
Patch by Katsuya HIDAKA (@hidakatsuya). git-svn-id: https://svn.redmine.org/redmine/trunk@22845 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
@@ -944,16 +944,27 @@ module ApplicationHelper
|
||||
attachments += obj.attachments if obj.respond_to?(:attachments)
|
||||
end
|
||||
if attachments.present?
|
||||
text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png|webp))"(\s+alt="([^"]*)")?/i) do |m|
|
||||
filename, ext, alt, alttext = $1, $2, $3, $4
|
||||
title_and_alt_re = /\s+(title|alt)="([^"]*)"/i
|
||||
|
||||
text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png|webp))"([^>]*)/i) do |m|
|
||||
filename, ext, other_attrs = $1, $2, $3
|
||||
|
||||
# search for the picture in attachments
|
||||
if found = Attachment.latest_attach(attachments, CGI.unescape(filename))
|
||||
image_url = download_named_attachment_url(found, found.filename, :only_path => only_path)
|
||||
desc = found.description.to_s.delete('"')
|
||||
if !desc.blank? && alttext.blank?
|
||||
alt = " title=\"#{desc}\" alt=\"#{desc}\""
|
||||
|
||||
# remove title and alt attributes after extracting them
|
||||
title_and_alt = other_attrs.scan(title_and_alt_re).to_h
|
||||
other_attrs.gsub!(title_and_alt_re, '')
|
||||
|
||||
title_and_alt_attrs = if !desc.blank? && title_and_alt['alt'].blank?
|
||||
" title=\"#{desc}\" alt=\"#{desc}\""
|
||||
else
|
||||
# restore original title and alt attributes
|
||||
" #{title_and_alt.map { |k, v| %[#{k}="#{v}"] }.join(' ')}"
|
||||
end
|
||||
"src=\"#{image_url}\"#{alt} loading=\"lazy\""
|
||||
"src=\"#{image_url}\"#{title_and_alt_attrs} loading=\"lazy\"#{other_attrs}"
|
||||
else
|
||||
m
|
||||
end
|
||||
|
||||
@@ -202,11 +202,11 @@ class ApplicationHelperTest < Redmine::HelperTest
|
||||
attachments = Attachment.all
|
||||
with_settings text_formatting: 'textile' do
|
||||
# When alt text is set
|
||||
assert_match %r[<img src=".+?" title="This is a logo" alt="This is a logo" loading=".+?" title="alt text" alt="alt text" />],
|
||||
assert_match %r[<img src=".+?" title="alt text" alt="alt text" loading=".+?" />],
|
||||
textilizable('!logo.gif(alt text)!', attachments: attachments)
|
||||
|
||||
# When alt text and style are set
|
||||
assert_match %r[<img src=".+?" title="This is a logo" alt="This is a logo" loading=".+?" style="width:100px;" title="alt text" alt="alt text" />],
|
||||
assert_match %r[<img src=".+?" title="alt text" alt="alt text" loading=".+?" style="width:100px;" />],
|
||||
textilizable('!{width:100px}logo.gif(alt text)!', attachments: attachments)
|
||||
|
||||
# When alt text is not set
|
||||
|
||||
Reference in New Issue
Block a user