Project

General

Profile

Defect #40650 » 0002-Fix-alt-and-title-attributes-for-attached-images-in-Wiki.patch

Katsuya HIDAKA, 2024-05-01 19:46

View differences:

app/helpers/application_helper.rb
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}\""
end
"src=\"#{image_url}\"#{alt} loading=\"lazy\""
# 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}\"#{title_and_alt_attrs} loading=\"lazy\"#{other_attrs}"
else
m
end
test/helpers/application_helper_test.rb
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
(4-4/4)