Patch #43745 » 0001-Moves-parse-hires-images-and-parse-inline-attachment.patch
| app/helpers/application_helper.rb | ||
|---|---|---|
|
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 = options.delete(:only_path) == false ? false : true
|
||
|
options[:only_path] = only_path
|
||
|
@only_path = only_path
|
||
|
text = text.dup
|
||
|
macros = catch_macros(text)
|
||
| ... | ... | |
|
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 = []
|
||
| ... | ... | |
|
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
|
||
| ... | ... | |
|
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:
|
||
| lib/redmine/wiki_formatting.rb | ||
|---|---|---|
|
# 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
|
||
| ... | ... | |
|
include ActionView::Helpers::UrlHelper
|
||
|
include Redmine::WikiFormatting::LinksHelper
|
||
|
def initialize(text)
|
||
|
def initialize(text, options = {})
|
||
|
@text = text
|
||
|
@options = options
|
||
|
end
|
||
|
def to_html(*args)
|
||
| lib/redmine/wiki_formatting/common_mark/formatter.rb | ||
|---|---|---|
|
class Formatter
|
||
|
include Redmine::WikiFormatting::SectionHelper
|
||
|
def initialize(text)
|
||
|
def initialize(text, options = {})
|
||
|
@text = text
|
||
|
@options = options
|
||
|
end
|
||
|
def to_html(*args)
|
||
|
html = MarkdownFilter.new(@text, PIPELINE_CONFIG).call
|
||
|
fragment = Redmine::WikiFormatting::HtmlParser.parse(html)
|
||
|
SANITIZER.call(fragment)
|
||
|
SCRUBBERS.each do |scrubber|
|
||
|
(SCRUBBERS + post_processor_scrubbers).each do |scrubber|
|
||
|
fragment.scrub!(scrubber)
|
||
|
end
|
||
|
fragment.to_s
|
||
|
end
|
||
|
private
|
||
|
def post_processor_scrubbers
|
||
|
[
|
||
|
Redmine::WikiFormatting::InlineAttachmentsScrubber.new(@options),
|
||
|
Redmine::WikiFormatting::HiresImagesScrubber.new
|
||
|
]
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
| lib/redmine/wiki_formatting/hires_images_scrubber.rb | ||
|---|---|---|
|
# 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 = /@(?<dpr>\dx)\.(?:bmp|gif|jpg|jpe|jpeg|png)\z/i
|
||
|
def scrub(node)
|
||
|
return unless node.name == 'img' && node['src'].present?
|
||
|
src = node['src']
|
||
|
return unless src.include?('@')
|
||
|
match = src.match(HIRES_FILENAME_REGEX)
|
||
|
return unless match
|
||
|
# Set the srcset attribute.
|
||
|
node['srcset'] = "#{src} #{match[:dpr]}"
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
| lib/redmine/wiki_formatting/html_sanitizer.rb | ||
|---|---|---|
|
# Combination of SanitizationFilter and ExternalLinksScrubber
|
||
|
class HtmlSanitizer
|
||
|
SANITIZER = Redmine::WikiFormatting::CommonMark::SanitizationFilter.new
|
||
|
SCRUBBERS = [Redmine::WikiFormatting::CommonMark::ExternalLinksScrubber.new]
|
||
|
SCRUBBERS = [
|
||
|
Redmine::WikiFormatting::CommonMark::ExternalLinksScrubber.new,
|
||
|
Redmine::WikiFormatting::HiresImagesScrubber.new
|
||
|
]
|
||
|
def self.call(html)
|
||
|
fragment = HtmlParser.parse(html)
|
||
| lib/redmine/wiki_formatting/inline_attachments_scrubber.rb | ||
|---|---|---|
|
# 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 InlineAttachmentsScrubber < Loofah::Scrubber
|
||
|
def initialize(options = {})
|
||
|
super()
|
||
|
@options = options
|
||
|
@obj = options[:object]
|
||
|
@view = options[:view]
|
||
|
@only_path = options[:only_path]
|
||
|
@attachments = options[:attachments] || []
|
||
|
if @obj.is_a?(Journal)
|
||
|
@attachments += @obj.journalized.attachments if @obj.journalized.respond_to?(:attachments)
|
||
|
elsif @obj.respond_to?(:attachments)
|
||
|
@attachments += @obj.attachments
|
||
|
end
|
||
|
if @attachments.present?
|
||
|
@attachments = @attachments.sort_by{|attachment| [attachment.created_on, attachment.id]}.reverse
|
||
|
end
|
||
|
end
|
||
|
def scrub(node)
|
||
|
return unless node.name == 'img' && node['src'].present?
|
||
|
parse_inline_attachments(node)
|
||
|
end
|
||
|
private
|
||
|
def parse_inline_attachments(node)
|
||
|
return if @attachments.blank?
|
||
|
src = node['src']
|
||
|
if src =~ %r{\A(?<filename>[^/"]+?\.(?:bmp|gif|jpg|jpeg|jpe|png|webp))\z}i
|
||
|
filename = $~[:filename]
|
||
|
if found = find_attachment(CGI.unescape(filename))
|
||
|
image_url = @view.download_named_attachment_url(found, found.filename, :only_path => @only_path)
|
||
|
node['src'] = image_url
|
||
|
desc = found.description.to_s.delete('"')
|
||
|
if !desc.blank? && node['alt'].blank?
|
||
|
node['title'] = desc
|
||
|
node['alt'] = desc
|
||
|
end
|
||
|
node['loading'] = 'lazy'
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
def find_attachment(filename)
|
||
|
return unless filename.valid_encoding?
|
||
|
@attachments.detect do |att|
|
||
|
filename.casecmp?(att.filename)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
| lib/redmine/wiki_formatting/textile/formatter.rb | ||
|---|---|---|
|
extend Forwardable
|
||
|
def_delegators :@filter, :extract_sections, :rip_offtags
|
||
|
def initialize(args)
|
||
|
@filter = Filter.new(args)
|
||
|
def initialize(text, options = {})
|
||
|
@filter = Filter.new(text)
|
||
|
@options = options
|
||
|
end
|
||
|
def to_html(*rules)
|
||
|
html = @filter.to_html(rules)
|
||
|
fragment = Loofah.html5_fragment(html)
|
||
|
SCRUBBERS.each do |scrubber|
|
||
|
(SCRUBBERS + post_processor_scrubbers).each do |scrubber|
|
||
|
fragment.scrub!(scrubber)
|
||
|
end
|
||
|
fragment.to_s
|
||
|
end
|
||
|
private
|
||
|
def post_processor_scrubbers
|
||
|
[
|
||
|
Redmine::WikiFormatting::InlineAttachmentsScrubber.new(@options),
|
||
|
Redmine::WikiFormatting::HiresImagesScrubber.new
|
||
|
]
|
||
|
end
|
||
|
end
|
||
|
class Filter < RedCloth3
|
||
| test/helpers/application_helper_test.rb | ||
|---|---|---|
|
def test_attached_images
|
||
|
to_test = {
|
||
|
'Inline image: !logo.gif!' =>
|
||
|
'Inline image: <img src="/attachments/download/3/logo.gif" title="This is a logo" alt="This is a logo" loading="lazy">',
|
||
|
'Inline image: <img src="/attachments/download/3/logo.gif" alt="This is a logo" title="This is a logo" loading="lazy">',
|
||
|
'Inline image: !logo.GIF!' =>
|
||
|
'Inline image: <img src="/attachments/download/3/logo.gif" title="This is a logo" alt="This is a logo" loading="lazy">',
|
||
|
'Inline image: <img src="/attachments/download/3/logo.gif" alt="This is a logo" title="This is a logo" loading="lazy">',
|
||
|
'Inline WebP image: !logo.webp!' =>
|
||
|
'Inline WebP image: <img src="/attachments/download/24/logo.webp" title="WebP image" alt="WebP image" loading="lazy">',
|
||
|
'Inline WebP image: <img src="/attachments/download/24/logo.webp" alt="WebP image" title="WebP image" loading="lazy">',
|
||
|
'No match: !ogo.gif!' => 'No match: <img src="ogo.gif" alt="">',
|
||
|
'No match: !ogo.GIF!' => 'No match: <img src="ogo.GIF" alt="">',
|
||
|
# link image
|
||
|
'!logo.gif!:http://foo.bar/' =>
|
||
|
'<a href="http://foo.bar/"><img src="/attachments/download/3/logo.gif" title="This is a logo" alt="This is a logo" loading="lazy"></a>',
|
||
|
'<a href="http://foo.bar/"><img src="/attachments/download/3/logo.gif" alt="This is a logo" title="This is a logo" loading="lazy"></a>',
|
||
|
}
|
||
|
attachments = Attachment.all
|
||
|
with_settings :text_formatting => 'textile' do
|
||
| ... | ... | |
|
textilizable('!logo.gif(alt text)!', attachments: attachments)
|
||
|
# When alt text and style are set
|
||
|
assert_match %r[<img src=".+?" title="alt text" alt="alt text" loading=".+?" style="width:100px;">],
|
||
|
assert_match %r[<img src=".+?" style="width:100px;" title="alt text" alt="alt text" loading=".+?">],
|
||
|
textilizable('!{width:100px}logo.gif(alt text)!', attachments: attachments)
|
||
|
# When alt text is not set
|
||
|
assert_match %r[<img src=".+?" title="This is a logo" alt="This is a logo" loading=".+?">],
|
||
|
assert_match %r[<img src=".+?" alt="This is a logo" title="This is a logo" loading=".+?">],
|
||
|
textilizable('!logo.gif!', attachments: attachments)
|
||
|
# When alt text is not set and the attachment has no description
|
||
| ... | ... | |
|
with_settings :text_formatting => 'textile' do
|
||
|
assert_equal(
|
||
|
%(<p><img src="/attachments/download/#{attachment.id}/image@2x.png" ) +
|
||
|
%(srcset="/attachments/download/#{attachment.id}/image@2x.png 2x" alt="" loading="lazy"></p>),
|
||
|
%(alt="" loading="lazy" srcset="/attachments/download/#{attachment.id}/image@2x.png 2x"></p>),
|
||
|
textilizable("!image@2x.png!", :attachments => [attachment])
|
||
|
)
|
||
|
end
|
||
| test/unit/lib/redmine/wiki_formatting/common_mark/application_helper_test.rb | ||
|---|---|---|
|
textilizable('', attachments: attachments)
|
||
|
# When alt text is not set
|
||
|
assert_match %r[<img src=".+?" title="This is a logo" alt="This is a logo" loading=".+?">],
|
||
|
assert_match %r[<img src=".+?" alt="This is a logo" title="This is a logo" loading=".+?">],
|
||
|
textilizable('', attachments: attachments)
|
||
|
# When alt text is not set and the attachment has no description
|
||