diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index c04a97e4d..a96d40b7b 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -914,7 +914,7 @@ module ApplicationHelper
return '' if text.blank?
project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
- @only_path = only_path = options.delete(:only_path) == false ? false : true
+ @only_path = only_path = options[:only_path] = (options[:only_path] != false)
text = text.dup
macros = catch_macros(text)
@@ -923,7 +923,7 @@ module ApplicationHelper
text = h(text)
else
formatting = Setting.text_formatting
- text = Redmine::WikiFormatting.to_html(formatting, text, :object => obj, :attribute => attr)
+ text = Redmine::WikiFormatting.to_html(formatting, text, options.merge(:object => obj, :attribute => attr, :view => self))
end
@parsed_headings = []
@@ -932,7 +932,7 @@ module ApplicationHelper
parse_sections(text, project, obj, attr, only_path, options)
text = parse_non_pre_blocks(text, obj, macros, options) do |txt|
- [:parse_inline_attachments, :parse_hires_images, :parse_wiki_links, :parse_redmine_links].each do |method_name|
+ [:parse_wiki_links, :parse_redmine_links].each do |method_name|
send method_name, txt, project, obj, attr, only_path, options
end
end
@@ -977,54 +977,6 @@ module ApplicationHelper
parsed
end
- # add srcset attribute to img tags if filename includes @2x, @3x, etc.
- # to support hires displays
- def parse_hires_images(text, project, obj, attr, only_path, options)
- text.gsub!(/src="([^"]+@(\dx)\.(bmp|gif|jpg|jpe|jpeg|png))"/i) do |m|
- filename, dpr = $1, $2
- m + " srcset=\"#{filename} #{dpr}\""
- end
- end
-
- def parse_inline_attachments(text, project, obj, attr, only_path, options)
- return if options[:inline_attachments] == false
-
- # when using an image link, try to use an attachment, if possible
- attachments = options[:attachments] || []
- if obj.is_a?(Journal)
- attachments += obj.journalized.attachments if obj.journalized.respond_to?(:attachments)
- else
- attachments += obj.attachments if obj.respond_to?(:attachments)
- end
- if attachments.present?
- 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('"')
-
- # 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
- end
- end
- end
-
# Wiki links
#
# Examples:
diff --git a/lib/redmine/wiki_formatting.rb b/lib/redmine/wiki_formatting.rb
index ce6097646..3d61df87d 100644
--- a/lib/redmine/wiki_formatting.rb
+++ b/lib/redmine/wiki_formatting.rb
@@ -93,10 +93,10 @@ module Redmine
# Text retrieved from the cache store may be frozen
# We need to dup it so we can do in-place substitutions with gsub!
cache_store.fetch cache_key do
- formatter_for(format).new(text).to_html
+ formatter_for(format).new(text, options).to_html
end.dup
else
- formatter_for(format).new(text).to_html
+ formatter_for(format).new(text, options).to_html
end
text
end
@@ -127,8 +127,9 @@ module Redmine
include ActionView::Helpers::UrlHelper
include Redmine::WikiFormatting::LinksHelper
- def initialize(text)
+ def initialize(text, options = {})
@text = text
+ @options = options
end
def to_html(*args)
diff --git a/lib/redmine/wiki_formatting/common_mark/formatter.rb b/lib/redmine/wiki_formatting/common_mark/formatter.rb
index 668537190..a4941d4fd 100644
--- a/lib/redmine/wiki_formatting/common_mark/formatter.rb
+++ b/lib/redmine/wiki_formatting/common_mark/formatter.rb
@@ -65,8 +65,9 @@ module Redmine
class Formatter
include Redmine::WikiFormatting::SectionHelper
- def initialize(text)
+ def initialize(text, options = {})
@text = text
+ @options = options
end
def to_html(*args)
@@ -75,7 +76,7 @@ module Redmine
SANITIZER.call(fragment)
scrubber = Loofah::Scrubber.new do |node|
- SCRUBBERS.each do |s|
+ (SCRUBBERS + post_processor_scrubbers).each do |s|
result = s.scrub(node)
break result if result == Loofah::Scrubber::STOP
break if node.parent.nil?
@@ -85,6 +86,15 @@ module Redmine
fragment.scrub!(scrubber)
fragment.to_s
end
+
+ private
+
+ def post_processor_scrubbers
+ [
+ Redmine::WikiFormatting::InlineAttachmentsScrubber.new(@options),
+ Redmine::WikiFormatting::HiresImagesScrubber.new
+ ]
+ end
end
end
end
diff --git a/lib/redmine/wiki_formatting/hires_images_scrubber.rb b/lib/redmine/wiki_formatting/hires_images_scrubber.rb
new file mode 100644
index 000000000..b483497d4
--- /dev/null
+++ b/lib/redmine/wiki_formatting/hires_images_scrubber.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+# Redmine - project management software
+# Copyright (C) 2006- Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+module Redmine
+ module WikiFormatting
+ class HiresImagesScrubber < Loofah::Scrubber
+ HIRES_FILENAME_REGEX = /@(?
',
+ 'Inline image:
',
'Inline image: !logo.GIF!' =>
- 'Inline image:
',
+ 'Inline image:
',
'Inline WebP image: !logo.webp!' =>
- 'Inline WebP image:
',
+ 'Inline WebP image:
',
'No match: !ogo.gif!' => 'No match:
',
'No match: !ogo.GIF!' => 'No match: ',
# link image
'!logo.gif!:http://foo.bar/' =>
- '
',
+ '
',
}
attachments = Attachment.all
with_settings :text_formatting => 'textile' do
@@ -194,11 +194,11 @@ class ApplicationHelperTest < Redmine::HelperTest
textilizable('!logo.gif(alt text)!', attachments: attachments)
# When alt text and style are set
- assert_match %r[],
+ assert_match %r[
],
textilizable('!{width:100px}logo.gif(alt text)!', attachments: attachments)
# When alt text is not set
- assert_match %r[
],
+ assert_match %r[
],
textilizable('!logo.gif!', attachments: attachments)
# When alt text is not set and the attachment has no description
@@ -272,7 +272,7 @@ class ApplicationHelperTest < Redmine::HelperTest
with_settings :text_formatting => 'textile' do
assert_equal(
%(
