Project

General

Profile

Defect #37394 » 37394-v2.patch

Go MAEDA, 2025-04-27 10:03

View differences:

Gemfile
gem "html-pipeline", "~> 2.13.2"
gem "sanitize", "~> 6.0"
gem "commonmarker", '~> 2.3.0'
gem 'deckar01-task_list', '2.3.2'
# Optional gem for LDAP authentication
group :ldap do
gem 'net-ldap', '~> 0.17.0'
......
gem 'mini_magick', '~> 5.2.0'
end
# Optional CommonMark support, not for JRuby
group :common_mark do
gem "commonmarker", '~> 2.3.0'
gem 'deckar01-task_list', '2.3.2'
end
# Include database gems for the adapters found in the database
# configuration file
database_file = File.join(File.dirname(__FILE__), "config/database.yml")
lib/redmine.rb
rescue LoadError
# MiniMagick is not available
end
begin
require 'commonmarker' unless Object.const_defined?(:Commonmarker)
rescue LoadError
# CommonMarker is not available
end
module Redmine
end
lib/redmine/preparation.rb
WikiFormatting.map do |format|
format.register :textile
if Object.const_defined?(:Commonmarker)
format.register :common_mark, label: 'CommonMark Markdown (GitHub Flavored)'
end
format.register :common_mark, label: 'CommonMark Markdown (GitHub Flavored)'
end
ActionView::Template.register_template_handler :rsb, Views::ApiTemplateHandler
test/helpers/application_helper_test.rb
end
def test_attached_images_with_markdown_and_non_ascii_filename
skip unless Object.const_defined?(:CommonMarker)
to_test = {
'CAFÉ.JPG' => 'CAF%C3%89.JPG',
'crème.jpg' => 'cr%C3%A8me.jpg',
......
end
end
if Object.const_defined?(:Commonmarker)
def test_toc_with_markdown_formatting_should_be_parsed
with_settings :text_formatting => 'common_mark' do
assert_select_in textilizable("{{toc}}\n\n# Heading"), 'ul.toc li', :text => 'Heading'
assert_select_in textilizable("{{<toc}}\n\n# Heading"), 'ul.toc.left li', :text => 'Heading'
assert_select_in textilizable("{{>toc}}\n\n# Heading"), 'ul.toc.right li', :text => 'Heading'
end
def test_toc_with_markdown_formatting_should_be_parsed
with_settings :text_formatting => 'common_mark' do
assert_select_in textilizable("{{toc}}\n\n# Heading"), 'ul.toc li', :text => 'Heading'
assert_select_in textilizable("{{<toc}}\n\n# Heading"), 'ul.toc.left li', :text => 'Heading'
assert_select_in textilizable("{{>toc}}\n\n# Heading"), 'ul.toc.right li', :text => 'Heading'
end
end
test/unit/lib/redmine/wiki_formatting/common_mark/application_helper_test.rb
require_relative '../../../../../test_helper'
class Redmine::WikiFormatting::CommonMark::ApplicationHelperTest < Redmine::HelperTest
if Object.const_defined?(:Commonmarker)
include ERB::Util
include ERB::Util
def setup
super
set_tmp_attachments_directory
end
def setup
super
set_tmp_attachments_directory
end
def test_attached_images_with_markdown_and_non_ascii_filename
to_test = {
'CAFÉ.JPG' => 'CAF%C3%89.JPG',
'crème.jpg' => 'cr%C3%A8me.jpg',
}
with_settings :text_formatting => 'common_mark' do
to_test.each do |filename, result|
attachment = Attachment.generate!(:filename => filename)
assert_include %(<img src="/attachments/download/#{attachment.id}/#{result}" alt="" loading="lazy">), textilizable("![](#{filename})", :attachments => [attachment])
end
def test_attached_images_with_markdown_and_non_ascii_filename
to_test = {
'CAFÉ.JPG' => 'CAF%C3%89.JPG',
'crème.jpg' => 'cr%C3%A8me.jpg',
}
with_settings :text_formatting => 'common_mark' do
to_test.each do |filename, result|
attachment = Attachment.generate!(:filename => filename)
assert_include %(<img src="/attachments/download/#{attachment.id}/#{result}" alt="" loading="lazy">), textilizable("![](#{filename})", :attachments => [attachment])
end
end
end
def test_toc_with_markdown_formatting_should_be_parsed
with_settings :text_formatting => 'common_mark' do
assert_select_in textilizable("{{toc}}\n\n# Heading"), 'ul.toc li', :text => 'Heading'
assert_select_in textilizable("{{<toc}}\n\n# Heading"), 'ul.toc.left li', :text => 'Heading'
assert_select_in textilizable("{{>toc}}\n\n# Heading"), 'ul.toc.right li', :text => 'Heading'
end
def test_toc_with_markdown_formatting_should_be_parsed
with_settings :text_formatting => 'common_mark' do
assert_select_in textilizable("{{toc}}\n\n# Heading"), 'ul.toc li', :text => 'Heading'
assert_select_in textilizable("{{<toc}}\n\n# Heading"), 'ul.toc.left li', :text => 'Heading'
assert_select_in textilizable("{{>toc}}\n\n# Heading"), 'ul.toc.right li', :text => 'Heading'
end
end
def test_attached_image_alt_attribute_with_madkrown
attachments = Attachment.all
with_settings text_formatting: 'common_mark' do
# When alt text is set
assert_match %r[<img src=".+?" alt="alt text" loading=".+?">],
textilizable('![alt text](logo.gif)', attachments: attachments)
def test_attached_image_alt_attribute_with_madkrown
attachments = Attachment.all
with_settings text_formatting: 'common_mark' do
# When alt text is set
assert_match %r[<img src=".+?" alt="alt text" loading=".+?">],
textilizable('![alt text](logo.gif)', attachments: attachments)
# When alt text is not set
assert_match %r[<img src=".+?" title="This is a logo" alt="This is a logo" loading=".+?">],
textilizable('![](logo.gif)', attachments: attachments)
# When alt text is not set
assert_match %r[<img src=".+?" title="This is a logo" alt="This is a logo" loading=".+?">],
textilizable('![](logo.gif)', attachments: attachments)
# When alt text is not set and the attachment has no description
assert_match %r[<img src=".+?" alt="" loading=".+?">],
textilizable('![](testfile.PNG)', attachments: attachments)
# When alt text is not set and the attachment has no description
assert_match %r[<img src=".+?" alt="" loading=".+?">],
textilizable('![](testfile.PNG)', attachments: attachments)
# When no matching attachments are found
assert_match %r[<img src=".+?" alt="">],
textilizable('![](no-match.jpg)', attachments: attachments)
assert_match %r[<img src=".+?" alt="alt text">],
textilizable('![alt text](no-match.jpg)', attachments: attachments)
# When no matching attachments are found
assert_match %r[<img src=".+?" alt="">],
textilizable('![](no-match.jpg)', attachments: attachments)
assert_match %r[<img src=".+?" alt="alt text">],
textilizable('![alt text](no-match.jpg)', attachments: attachments)
# When no attachment is registered
assert_match %r[<img src=".+?" alt="">],
textilizable('![](logo.gif)', attachments: [])
assert_match %r[<img src=".+?" alt="alt text">],
textilizable('![alt text](logo.gif)', attachments: [])
end
# When no attachment is registered
assert_match %r[<img src=".+?" alt="">],
textilizable('![](logo.gif)', attachments: [])
assert_match %r[<img src=".+?" alt="alt text">],
textilizable('![alt text](logo.gif)', attachments: [])
end
end
end
test/unit/lib/redmine/wiki_formatting/common_mark/external_links_filter_test.rb
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require_relative '../../../../../test_helper'
require 'redmine/wiki_formatting/common_mark/external_links_filter'
if Object.const_defined?(:Commonmarker)
require 'redmine/wiki_formatting/common_mark/external_links_filter'
class Redmine::WikiFormatting::CommonMark::ExternalLinksFilterTest < ActiveSupport::TestCase
def filter(html)
Redmine::WikiFormatting::CommonMark::ExternalLinksFilter.to_html(html, @options)
end
class Redmine::WikiFormatting::CommonMark::ExternalLinksFilterTest < ActiveSupport::TestCase
def filter(html)
Redmine::WikiFormatting::CommonMark::ExternalLinksFilter.to_html(html, @options)
end
def setup
@options = { }
end
def setup
@options = { }
end
def test_external_links_should_have_external_css_class
assert_equal %(<a href="http://example.net/" class="external">link</a>), filter(%(<a href="http://example.net/">link</a>))
end
def test_external_links_should_have_external_css_class
assert_equal %(<a href="http://example.net/" class="external">link</a>), filter(%(<a href="http://example.net/">link</a>))
end
def test_locals_links_should_not_have_external_css_class
assert_equal %(<a href="/">home</a>), filter(%(<a href="/">home</a>))
assert_equal %(<a href="relative">relative</a>), filter(%(<a href="relative">relative</a>))
assert_equal %(<a href="#anchor">anchor</a>), filter(%(<a href="#anchor">anchor</a>))
end
def test_locals_links_should_not_have_external_css_class
assert_equal %(<a href="/">home</a>), filter(%(<a href="/">home</a>))
assert_equal %(<a href="relative">relative</a>), filter(%(<a href="relative">relative</a>))
assert_equal %(<a href="#anchor">anchor</a>), filter(%(<a href="#anchor">anchor</a>))
end
def test_mailto_links_should_have_email_class
assert_equal %(<a href="mailto:user@example.org" class="email">user</a>), filter(%(<a href="mailto:user@example.org">user</a>))
end
def test_mailto_links_should_have_email_class
assert_equal %(<a href="mailto:user@example.org" class="email">user</a>), filter(%(<a href="mailto:user@example.org">user</a>))
end
def test_malformed_uri_should_not_cause_exception
assert_nothing_raised do
filter(%(<a href="http://example.com/foo#bar#">Malformed URI</a>))
end
def test_malformed_uri_should_not_cause_exception
assert_nothing_raised do
filter(%(<a href="http://example.com/foo#bar#">Malformed URI</a>))
end
end
def test_external_links_with_target_get_rel_noopener
assert_equal(
%(<a target="_blank" href="http://example.net/" class="external" rel="noopener">link</a>),
filter(%(<a target="_blank" href="http://example.net/">link</a>))
)
assert_equal(
%(<a target="_blank" href="http://example.net/" rel="nofollow noopener" class="external">link</a>),
filter(%(<a target="_blank" href="http://example.net/" rel="nofollow">link</a>))
)
end
def test_external_links_with_target_get_rel_noopener
assert_equal(
%(<a target="_blank" href="http://example.net/" class="external" rel="noopener">link</a>),
filter(%(<a target="_blank" href="http://example.net/">link</a>))
)
assert_equal(
%(<a target="_blank" href="http://example.net/" rel="nofollow noopener" class="external">link</a>),
filter(%(<a target="_blank" href="http://example.net/" rel="nofollow">link</a>))
)
end
end
test/unit/lib/redmine/wiki_formatting/common_mark/fixup_auto_links_filter_test.rb
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require_relative '../../../../../test_helper'
require 'redmine/wiki_formatting/common_mark/fixup_auto_links_filter'
if Object.const_defined?(:Commonmarker)
require 'redmine/wiki_formatting/common_mark/fixup_auto_links_filter'
class Redmine::WikiFormatting::CommonMark::FixupAutoLinksFilterTest < ActiveSupport::TestCase
def filter(html)
Redmine::WikiFormatting::CommonMark::FixupAutoLinksFilter.to_html(html, @options)
end
class Redmine::WikiFormatting::CommonMark::FixupAutoLinksFilterTest < ActiveSupport::TestCase
def filter(html)
Redmine::WikiFormatting::CommonMark::FixupAutoLinksFilter.to_html(html, @options)
end
def format(markdown)
Redmine::WikiFormatting::CommonMark::MarkdownFilter.to_html(markdown, Redmine::WikiFormatting::CommonMark::PIPELINE_CONFIG)
end
def format(markdown)
Redmine::WikiFormatting::CommonMark::MarkdownFilter.to_html(markdown, Redmine::WikiFormatting::CommonMark::PIPELINE_CONFIG)
end
def setup
@options = { }
end
def setup
@options = { }
end
def test_should_fixup_autolinked_user_references
text = "user:user@example.org"
assert_equal "<p>#{text}</p>", filter(format(text))
text = "@user@example.org"
assert_equal "<p>#{text}</p>", filter(format(text))
end
def test_should_fixup_autolinked_user_references
text = "user:user@example.org"
assert_equal "<p>#{text}</p>", filter(format(text))
text = "@user@example.org"
assert_equal "<p>#{text}</p>", filter(format(text))
end
def test_should_fixup_autolinked_hires_files
text = "printscreen@2x.png"
assert_equal "<p>#{text}</p>", filter(format(text))
end
def test_should_fixup_autolinked_hires_files
text = "printscreen@2x.png"
assert_equal "<p>#{text}</p>", filter(format(text))
end
end
test/unit/lib/redmine/wiki_formatting/common_mark/formatter_test.rb
require_relative '../../../../../test_helper'
class Redmine::WikiFormatting::CommonMark::FormatterTest < ActionView::TestCase
if Object.const_defined?(:Commonmarker)
def setup
@formatter = Redmine::WikiFormatting::CommonMark::Formatter
end
def setup
@formatter = Redmine::WikiFormatting::CommonMark::Formatter
end
def to_html(text)
@formatter.new(text).to_html
end
def to_html(text)
@formatter.new(text).to_html
end
def test_should_render_hard_breaks
html ="<p>foo<br>\nbar</p>"
assert_equal html, to_html("foo\\\nbar")
assert_equal html, to_html("foo \nbar")
end
def test_should_render_hard_breaks
html ="<p>foo<br>\nbar</p>"
assert_equal html, to_html("foo\\\nbar")
assert_equal html, to_html("foo \nbar")
end
def test_should_render_soft_breaks
assert_equal "<p>foo<br>\nbar</p>", to_html("foo\nbar")
end
def test_should_render_soft_breaks
assert_equal "<p>foo<br>\nbar</p>", to_html("foo\nbar")
end
def test_syntax_error_in_image_reference_should_not_raise_exception
assert to_html("!>[](foo.png)")
end
def test_syntax_error_in_image_reference_should_not_raise_exception
assert to_html("!>[](foo.png)")
end
def test_empty_image_should_not_raise_exception
assert to_html("![]()")
end
def test_empty_image_should_not_raise_exception
assert to_html("![]()")
end
def test_inline_style
assert_equal "<p><strong>foo</strong></p>", to_html("**foo**")
end
def test_inline_style
assert_equal "<p><strong>foo</strong></p>", to_html("**foo**")
end
def test_not_set_intra_emphasis
assert_equal "<p>foo_bar_baz</p>", to_html("foo_bar_baz")
end
def test_not_set_intra_emphasis
assert_equal "<p>foo_bar_baz</p>", to_html("foo_bar_baz")
end
def test_wiki_links_should_be_preserved
text = 'This is a wiki link: [[Foo]]'
assert_include '[[Foo]]', to_html(text)
end
def test_wiki_links_should_be_preserved
text = 'This is a wiki link: [[Foo]]'
assert_include '[[Foo]]', to_html(text)
end
def test_redmine_links_with_double_quotes_should_be_preserved
text = 'This is a redmine link: version:"1.0"'
assert_include 'version:"1.0"', to_html(text)
end
def test_redmine_links_with_double_quotes_should_be_preserved
text = 'This is a redmine link: version:"1.0"'
assert_include 'version:"1.0"', to_html(text)
end
def test_links_by_id_should_be_preserved
text = "[project#3]"
assert_equal "<p>#{text}</p>", to_html(text)
end
def test_links_by_id_should_be_preserved
text = "[project#3]"
assert_equal "<p>#{text}</p>", to_html(text)
end
def test_links_to_users_should_be_preserved
text = "[@login]"
assert_equal "<p>#{text}</p>", to_html(text)
text = "[user:login]"
assert_equal "<p>#{text}</p>", to_html(text)
text = "user:user@example.org"
assert_equal "<p>#{text}</p>", to_html(text)
text = "[user:user@example.org]"
assert_equal "<p>#{text}</p>", to_html(text)
text = "@user@example.org"
assert_equal "<p>#{text}</p>", to_html(text)
text = "[@user@example.org]"
assert_equal "<p>#{text}</p>", to_html(text)
end
def test_links_to_users_should_be_preserved
text = "[@login]"
assert_equal "<p>#{text}</p>", to_html(text)
text = "[user:login]"
assert_equal "<p>#{text}</p>", to_html(text)
text = "user:user@example.org"
assert_equal "<p>#{text}</p>", to_html(text)
text = "[user:user@example.org]"
assert_equal "<p>#{text}</p>", to_html(text)
text = "@user@example.org"
assert_equal "<p>#{text}</p>", to_html(text)
text = "[@user@example.org]"
assert_equal "<p>#{text}</p>", to_html(text)
end
def test_files_with_at_should_not_end_up_as_mailto_links
text = "printscreen@2x.png"
assert_equal "<p>#{text}</p>", to_html(text)
text = "[printscreen@2x.png]"
assert_equal "<p>#{text}</p>", to_html(text)
end
def test_files_with_at_should_not_end_up_as_mailto_links
text = "printscreen@2x.png"
assert_equal "<p>#{text}</p>", to_html(text)
text = "[printscreen@2x.png]"
assert_equal "<p>#{text}</p>", to_html(text)
end
def test_should_support_syntax_highlight
text = <<~STR
~~~ruby
def foo
end
~~~
STR
assert_select_in to_html(text), 'pre code.ruby.syntaxhl' do
assert_select 'span.k', :text => 'def'
assert_select "[data-language='ruby']"
def test_should_support_syntax_highlight
text = <<~STR
~~~ruby
def foo
end
~~~
STR
assert_select_in to_html(text), 'pre code.ruby.syntaxhl' do
assert_select 'span.k', :text => 'def'
assert_select "[data-language='ruby']"
end
end
def test_should_support_syntax_highlight_for_language_with_special_chars
text = <<~STR
~~~c++
int main() {
}
~~~
STR
assert_select_in to_html(text), 'pre' do
assert_select 'code[class=?]', "c++ syntaxhl"
assert_select 'span.kt', :text => 'int'
assert_select "[data-language=?]", "c++"
end
def test_should_support_syntax_highlight_for_language_with_special_chars
text = <<~STR
~~~c++
int main() {
}
~~~
STR
assert_select_in to_html(text), 'pre' do
assert_select 'code[class=?]', "c++ syntaxhl"
assert_select 'span.kt', :text => 'int'
assert_select "[data-language=?]", "c++"
end
end
def test_external_links_should_have_external_css_class
text = 'This is a [link](http://example.net/)'
assert_equal '<p>This is a <a href="http://example.net/" class="external">link</a></p>', to_html(text)
end
def test_external_links_should_have_external_css_class
text = 'This is a [link](http://example.net/)'
assert_equal '<p>This is a <a href="http://example.net/" class="external">link</a></p>', to_html(text)
end
def test_locals_links_should_not_have_external_css_class
text = 'This is a [link](/issues)'
assert_equal '<p>This is a <a href="/issues">link</a></p>', to_html(text)
end
def test_locals_links_should_not_have_external_css_class
text = 'This is a [link](/issues)'
assert_equal '<p>This is a <a href="/issues">link</a></p>', to_html(text)
end
def test_markdown_should_not_require_surrounded_empty_line
text = <<-STR
This is a list:
* One
* Two
STR
assert_equal "<p>This is a list:</p>\n<ul>\n<li>One</li>\n<li>Two</li>\n</ul>", to_html(text)
end
def test_markdown_should_not_require_surrounded_empty_line
text = <<~STR
This is a list:
* One
* Two
STR
assert_equal "<p>This is a list:</p>\n<ul>\n<li>One</li>\n<li>Two</li>\n</ul>", to_html(text)
end
def test_footnotes
text = <<~STR
This is some text[^1].
def test_footnotes
text = <<~STR
This is some text[^1].
[^1]: This is the foot note
STR
[^1]: This is the foot note
STR
expected = <<~EXPECTED
<p>This is some text<sup><a href="#fn-1" id="fnref-1">1</a></sup>.</p>
<ol>
<li id="fn-1">
<p>This is the foot note <a href="#fnref-1" aria-label="Back to reference 1">↩</a></p>
</li>
</ol>
EXPECTED
expected = <<~EXPECTED
<p>This is some text<sup><a href="#fn-1" id="fnref-1">1</a></sup>.</p>
<ol>
<li id="fn-1">
<p>This is the foot note <a href="#fnref-1" aria-label="Back to reference 1">↩</a></p>
</li>
</ol>
EXPECTED
assert_equal expected.gsub(%r{[\r\n\t]}, ''), to_html(text).gsub(%r{[\r\n\t]}, '').rstrip
end
assert_equal expected.gsub(%r{[\r\n\t]}, ''), to_html(text).gsub(%r{[\r\n\t]}, '').rstrip
end
STR_WITH_PRE = [
# 0
<<~STR.chomp,
# Title
STR_WITH_PRE = [
# 0
<<~STR.chomp,
# Title
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
STR
# 1
<<~STR.chomp,
## Heading 2
~~~ruby
def foo
end
~~~
Morbi facilisis accumsan orci non pharetra.
~~~ ruby
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
STR
# 1
<<~STR.chomp,
## Heading 2
~~~ruby
def foo
end
~~~
```
Pre Content:
## Inside pre
<tag> inside pre block
Morbi facilisis accumsan orci non pharetra.
```
STR
# 2
<<~STR.chomp,
### Heading 3
Nulla nunc nisi, egestas in ornare vel, posuere ac libero.
STR
]
def test_get_section_should_ignore_pre_content
text = STR_WITH_PRE.join("\n\n")
assert_section_with_hash STR_WITH_PRE[1..2].join("\n\n"), text, 2
assert_section_with_hash STR_WITH_PRE[2], text, 3
end
~~~
def test_get_section_should_not_recognize_double_hash_issue_reference_as_heading
text = <<~STR
## Section A
Morbi facilisis accumsan orci non pharetra.
This text is a part of Section A.
~~~ ruby
def foo
end
~~~
##1 : This is an issue reference, not an ATX heading.
```
Pre Content:
This text is also a part of Section A.
<!-- Section A ends here -->
STR
## Inside pre
assert_section_with_hash text.chomp, text, 1
end
<tag> inside pre block
def test_update_section_should_not_escape_pre_content_outside_section
text = STR_WITH_PRE.join("\n\n")
replacement = "New text"
Morbi facilisis accumsan orci non pharetra.
```
STR
# 2
<<~STR.chomp,
### Heading 3
assert_equal [STR_WITH_PRE[0..1], "New text"].flatten.join("\n\n"),
@formatter.new(text).update_section(3, replacement)
end
Nulla nunc nisi, egestas in ornare vel, posuere ac libero.
STR
]
def test_should_emphasize_text
text = 'This _text_ should be emphasized'
assert_equal '<p>This <em>text</em> should be emphasized</p>', to_html(text)
end
def test_get_section_should_ignore_pre_content
text = STR_WITH_PRE.join("\n\n")
def test_should_strike_through_text
text = 'This ~~text~~ should be striked through'
assert_equal '<p>This <del>text</del> should be striked through</p>', to_html(text)
end
assert_section_with_hash STR_WITH_PRE[1..2].join("\n\n"), text, 2
assert_section_with_hash STR_WITH_PRE[2], text, 3
end
def test_should_autolink_urls_and_emails
[
["http://example.org", '<p><a href="http://example.org" class="external">http://example.org</a></p>'],
["http://www.redmine.org/projects/redmine/issues?utf8=✓",
'<p><a href="http://www.redmine.org/projects/redmine/issues?utf8=%E2%9C%93" class="external">http://www.redmine.org/projects/redmine/issues?utf8=✓</a></p>'],
['[Letters](https://yandex.ru/search/?text=кол-во)', '<p><a href="https://yandex.ru/search/?text=%D0%BA%D0%BE%D0%BB-%D0%B2%D0%BE" class="external">Letters</a></p>'],
["www.example.org", '<p><a href="http://www.example.org" class="external">www.example.org</a></p>'],
["user@example.org", '<p><a href="mailto:user@example.org" class="email">user@example.org</a></p>']
].each do |text, html|
assert_equal html, to_html(text)
end
end
def test_get_section_should_not_recognize_double_hash_issue_reference_as_heading
text = <<~STR
## Section A
This text is a part of Section A.
##1 : This is an issue reference, not an ATX heading.
This text is also a part of Section A.
<!-- Section A ends here -->
STR
assert_section_with_hash text.chomp, text, 1
end
def test_should_support_html_tables
text = '<table style="background: red"><tr><td>Cell</td></tr></table>'
assert_equal '<table><tr><td>Cell</td></tr></table>', to_html(text)
def test_update_section_should_not_escape_pre_content_outside_section
text = STR_WITH_PRE.join("\n\n")
replacement = "New text"
assert_equal [STR_WITH_PRE[0..1], "New text"].flatten.join("\n\n"),
@formatter.new(text).update_section(3, replacement)
end
def test_should_emphasize_text
text = 'This _text_ should be emphasized'
assert_equal '<p>This <em>text</em> should be emphasized</p>', to_html(text)
end
def test_should_strike_through_text
text = 'This ~~text~~ should be striked through'
assert_equal '<p>This <del>text</del> should be striked through</p>', to_html(text)
end
def test_should_autolink_urls_and_emails
[
["http://example.org", '<p><a href="http://example.org" class="external">http://example.org</a></p>'],
["http://www.redmine.org/projects/redmine/issues?utf8=✓",
'<p><a href="http://www.redmine.org/projects/redmine/issues?utf8=%E2%9C%93" class="external">http://www.redmine.org/projects/redmine/issues?utf8=✓</a></p>'],
['[Letters](https://yandex.ru/search/?text=кол-во)', '<p><a href="https://yandex.ru/search/?text=%D0%BA%D0%BE%D0%BB-%D0%B2%D0%BE" class="external">Letters</a></p>'],
["www.example.org", '<p><a href="http://www.example.org" class="external">www.example.org</a></p>'],
["user@example.org", '<p><a href="mailto:user@example.org" class="email">user@example.org</a></p>']
].each do |text, html|
assert_equal html, to_html(text)
end
end
def test_should_remove_unsafe_uris
[
['<img src="data:foobar">', '<img>'],
['<a href="javascript:bla">click me</a>', '<p><a>click me</a></p>'],
].each do |text, html|
assert_equal html, to_html(text)
end
def test_should_support_html_tables
text = '<table style="background: red"><tr><td>Cell</td></tr></table>'
assert_equal '<table><tr><td>Cell</td></tr></table>', to_html(text)
end
def test_should_remove_unsafe_uris
[
['<img src="data:foobar">', '<img>'],
['<a href="javascript:bla">click me</a>', '<p><a>click me</a></p>'],
].each do |text, html|
assert_equal html, to_html(text)
end
end
def test_should_escape_unwanted_tags
def test_should_escape_unwanted_tags
[
[
[
%[<p>sit<br>amet &lt;style&gt;.foo { color: #fff; }&lt;/style&gt; &lt;script&gt;alert("hello world");&lt;/script&gt;</p>],
%[sit<br/>amet <style>.foo { color: #fff; }</style> <script>alert("hello world");</script>]
]
].each do |expected, input|
assert_equal expected, to_html(input)
end
%[<p>sit<br>amet &lt;style&gt;.foo { color: #fff; }&lt;/style&gt; &lt;script&gt;alert("hello world");&lt;/script&gt;</p>],
%[sit<br/>amet <style>.foo { color: #fff; }</style> <script>alert("hello world");</script>]
]
].each do |expected, input|
assert_equal expected, to_html(input)
end
end
def test_should_support_task_list
text = <<~STR
Task list:
* [ ] Task 1
* [x] Task 2
STR
expected = <<~EXPECTED
<p>Task list:</p>
<ul class="task-list">
<li class="task-list-item">
<input type="checkbox" class="task-list-item-checkbox" disabled> Task 1
</li>
<li class="task-list-item">
<input type="checkbox" class="task-list-item-checkbox" checked disabled> Task 2</li>
</ul>
EXPECTED
assert_equal expected.gsub(%r{[\r\n\t]}, ''), to_html(text).gsub(%r{[\r\n\t]}, '').rstrip
end
def test_should_support_task_list
text = <<~STR
Task list:
* [ ] Task 1
* [x] Task 2
STR
expected = <<~EXPECTED
<p>Task list:</p>
<ul class="task-list">
<li class="task-list-item">
<input type="checkbox" class="task-list-item-checkbox" disabled> Task 1
</li>
<li class="task-list-item">
<input type="checkbox" class="task-list-item-checkbox" checked disabled> Task 2</li>
</ul>
EXPECTED
assert_equal expected.gsub(%r{[\r\n\t]}, ''), to_html(text).gsub(%r{[\r\n\t]}, '').rstrip
end
private
private
def assert_section_with_hash(expected, text, index)
result = @formatter.new(text).get_section(index)
def assert_section_with_hash(expected, text, index)
result = @formatter.new(text).get_section(index)
assert_kind_of Array, result
assert_equal 2, result.size
assert_equal expected, result.first, "section content did not match"
assert_equal ActiveSupport::Digest.hexdigest(expected), result.last, "section hash did not match"
end
assert_kind_of Array, result
assert_equal 2, result.size
assert_equal expected, result.first, "section content did not match"
assert_equal ActiveSupport::Digest.hexdigest(expected), result.last, "section hash did not match"
end
end
test/unit/lib/redmine/wiki_formatting/common_mark/markdown_filter_test.rb
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require_relative '../../../../../test_helper'
require 'redmine/wiki_formatting/common_mark/markdown_filter'
if Object.const_defined?(:Commonmarker)
require 'redmine/wiki_formatting/common_mark/markdown_filter'
class Redmine::WikiFormatting::CommonMark::MarkdownFilterTest < ActiveSupport::TestCase
def filter(markdown)
Redmine::WikiFormatting::CommonMark::MarkdownFilter.to_html(markdown)
end
class Redmine::WikiFormatting::CommonMark::MarkdownFilterTest < ActiveSupport::TestCase
def filter(markdown)
Redmine::WikiFormatting::CommonMark::MarkdownFilter.to_html(markdown)
end
# just a basic sanity test. more formatting tests in the formatter_test
def test_should_render_markdown
assert_equal "<p><strong>bold</strong></p>", filter("**bold**")
end
# just a basic sanity test. more formatting tests in the formatter_test
def test_should_render_markdown
assert_equal "<p><strong>bold</strong></p>", filter("**bold**")
end
end
test/unit/lib/redmine/wiki_formatting/common_mark/sanitization_filter_test.rb
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require_relative '../../../../../test_helper'
require 'redmine/wiki_formatting/common_mark/sanitization_filter'
if Object.const_defined?(:Commonmarker)
require 'redmine/wiki_formatting/common_mark/sanitization_filter'
class Redmine::WikiFormatting::CommonMark::SanitizationFilterTest < ActiveSupport::TestCase
def filter(html)
Redmine::WikiFormatting::CommonMark::SanitizationFilter.to_html(html, @options)
end
class Redmine::WikiFormatting::CommonMark::SanitizationFilterTest < ActiveSupport::TestCase
def filter(html)
Redmine::WikiFormatting::CommonMark::SanitizationFilter.to_html(html, @options)
end
def setup
@options = { }
end
def setup
@options = { }
end
def test_should_filter_tags
input = %(<textarea>foo</textarea> <blink>dont blink</blink>)
assert_equal %(foo dont blink), filter(input)
end
def test_should_filter_tags
input = %(<textarea>foo</textarea> <blink>dont blink</blink>)
assert_equal %(foo dont blink), filter(input)
end
def test_should_sanitize_attributes
input = %(<a href="foo" onclick="bar" baz="foo">link</a>)
assert_equal %(<a href="foo">link</a>), filter(input)
end
def test_should_sanitize_attributes
input = %(<a href="foo" onclick="bar" baz="foo">link</a>)
assert_equal %(<a href="foo">link</a>), filter(input)
end
def test_should_allow_relative_links
input = %(<a href="foo/bar">foo/bar</a>)
assert_equal input, filter(input)
end
def test_should_allow_relative_links
input = %(<a href="foo/bar">foo/bar</a>)
assert_equal input, filter(input)
end
def test_should_support_footnotes
input = %(<a href="#fn-1" id="fnref-1">foo</a>)
assert_equal input, filter(input)
input = %(<ol><li id="fn-1">footnote</li></ol>)
assert_equal input, filter(input)
end
def test_should_support_footnotes
input = %(<a href="#fn-1" id="fnref-1">foo</a>)
assert_equal input, filter(input)
input = %(<ol><li id="fn-1">footnote</li></ol>)
assert_equal input, filter(input)
end
def test_should_remove_invalid_ids
input = %(<a href="#fn1" id="foo">foo</a>)
assert_equal %(<a href="#fn1">foo</a>), filter(input)
input = %(<ol><li id="foo">footnote</li></ol>)
assert_equal %(<ol><li>footnote</li></ol>), filter(input)
end
def test_should_remove_invalid_ids
input = %(<a href="#fn1" id="foo">foo</a>)
assert_equal %(<a href="#fn1">foo</a>), filter(input)
input = %(<ol><li id="foo">footnote</li></ol>)
assert_equal %(<ol><li>footnote</li></ol>), filter(input)
end
def test_should_allow_class_on_code_only
input = %(<p class="foo">bar</p>)
assert_equal %(<p>bar</p>), filter(input)
def test_should_allow_class_on_code_only
input = %(<p class="foo">bar</p>)
assert_equal %(<p>bar</p>), filter(input)
input = %(<code class="language-ruby">foo</code>)
assert_equal input, filter(input)
input = %(<code class="language-ruby">foo</code>)
assert_equal input, filter(input)
input = %(<code class="foo">foo</code>)
assert_equal %(<code>foo</code>), filter(input)
end
input = %(<code class="foo">foo</code>)
assert_equal %(<code>foo</code>), filter(input)
def test_should_allow_links_with_safe_url_schemes
%w(http https ftp ssh foo).each do |scheme|
input = %(<a href="#{scheme}://example.org/">foo</a>)
assert_equal input, filter(input)
end
end
def test_should_allow_links_with_safe_url_schemes
%w(http https ftp ssh foo).each do |scheme|
input = %(<a href="#{scheme}://example.org/">foo</a>)
assert_equal input, filter(input)
end
end
def test_should_allow_mailto_links
input = %(<a href="mailto:foo@example.org">bar</a>)
assert_equal input, filter(input)
end
def test_should_allow_mailto_links
input = %(<a href="mailto:foo@example.org">bar</a>)
assert_equal input, filter(input)
end
def test_should_remove_empty_link
input = %(<a href="">bar</a>)
assert_equal %(<a>bar</a>), filter(input)
input = %(<a href=" ">bar</a>)
assert_equal %(<a>bar</a>), filter(input)
end
# samples taken from the Sanitize test suite
# rubocop:disable Layout/LineLength
STRINGS = [
[
'<span style="color: #333; background: url(\'https://example.com/evil.svg\')">hello</span>"',
'<span style="color: #333; ">hello</span>"'
],
[
'<img src="photo.jpg" style="min-width: 100px; max-width: 200px; min-height: 100px; max-height: 200px;">',
'<img src="photo.jpg" style="min-width: 100px; max-width: 200px; min-height: 100px; max-height: 200px;">'
],
[
'<b>Lo<!-- comment -->rem</b> <a href="pants" title="foo" style="text-decoration: underline;">ipsum</a> <a href="http://foo.com/"><strong>dolor</strong></a> sit<br/>amet <style>.foo { color: #fff; }</style> <script>alert("hello world");</script>',
'<b>Lorem</b> <a href="pants" title="foo">ipsum</a> <a href="http://foo.com/"><strong>dolor</strong></a> sit<br>amet .foo { color: #fff; } '
],
[
'Lo<!-- comment -->rem</b> <a href=pants title="foo>ipsum <a href="http://foo.com/"><strong>dolor</a></strong> sit<br/>amet <script>alert("hello world");',
'Lorem <a href="pants" title="foo&gt;ipsum &lt;a href="><strong>dolor</strong></a> sit<br>amet '
],
[
'<p>a</p><blockquote>b',
'<p>a</p><blockquote>b</blockquote>'
],
[
'<b>Lo<!-- comment -->rem</b> <a href="javascript:pants" title="foo">ipsum</a> <a href="http://foo.com/"><strong>dolor</strong></a> sit<br/>amet <<foo>script>alert("hello world");</script>',
'<b>Lorem</b> <a title="foo">ipsum</a> <a href="http://foo.com/"><strong>dolor</strong></a> sit<br>amet &lt;script&gt;alert("hello world");'
]
]
# rubocop:enable Layout/LineLength
def test_should_remove_empty_link
input = %(<a href="">bar</a>)
assert_equal %(<a>bar</a>), filter(input)
input = %(<a href=" ">bar</a>)
assert_equal %(<a>bar</a>), filter(input)
def test_should_sanitize_html_strings
STRINGS.each do |input, expected|
assert_equal expected, filter(input)
end
end
# samples taken from the Sanitize test suite
PROTOCOLS = {
'protocol-based JS injection: simple, no spaces' => [
'<a href="javascript:alert(\'XSS\');">foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: simple, spaces before' => [
'<a href="javascript :alert(\'XSS\');">foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: simple, spaces after' => [
'<a href="javascript: alert(\'XSS\');">foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: simple, spaces before and after' => [
'<a href="javascript : alert(\'XSS\');">foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: preceding colon' => [
'<a href=":javascript:alert(\'XSS\');">foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: UTF-8 encoding' => [
'<a href="javascript&#58;">foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: long UTF-8 encoding' => [
'<a href="javascript&#0058;">foo</a>',
'<a>foo</a>'
],
# samples taken from the Sanitize test suite
# rubocop:disable Layout/LineLength
STRINGS = [
[
'<span style="color: #333; background: url(\'https://example.com/evil.svg\')">hello</span>"',
'<span style="color: #333; ">hello</span>"'
],
[
'<img src="photo.jpg" style="min-width: 100px; max-width: 200px; min-height: 100px; max-height: 200px;">',
'<img src="photo.jpg" style="min-width: 100px; max-width: 200px; min-height: 100px; max-height: 200px;">'
],
[
'<b>Lo<!-- comment -->rem</b> <a href="pants" title="foo" style="text-decoration: underline;">ipsum</a> <a href="http://foo.com/"><strong>dolor</strong></a> sit<br/>amet <style>.foo { color: #fff; }</style> <script>alert("hello world");</script>',
'<b>Lorem</b> <a href="pants" title="foo">ipsum</a> <a href="http://foo.com/"><strong>dolor</strong></a> sit<br>amet .foo { color: #fff; } '
],
[
'Lo<!-- comment -->rem</b> <a href=pants title="foo>ipsum <a href="http://foo.com/"><strong>dolor</a></strong> sit<br/>amet <script>alert("hello world");',
'Lorem <a href="pants" title="foo&gt;ipsum &lt;a href="><strong>dolor</strong></a> sit<br>amet '
],
[
'<p>a</p><blockquote>b',
'<p>a</p><blockquote>b</blockquote>'
],
[
'<b>Lo<!-- comment -->rem</b> <a href="javascript:pants" title="foo">ipsum</a> <a href="http://foo.com/"><strong>dolor</strong></a> sit<br/>amet <<foo>script>alert("hello world");</script>',
'<b>Lorem</b> <a title="foo">ipsum</a> <a href="http://foo.com/"><strong>dolor</strong></a> sit<br>amet &lt;script&gt;alert("hello world");'
]
]
'protocol-based JS injection: long UTF-8 encoding without semicolons' => [
'<a href=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041>foo</a>',
'<a>foo</a>'
],
# rubocop:enable Layout/LineLength
def test_should_sanitize_html_strings
STRINGS.each do |input, expected|
assert_equal expected, filter(input)
end
end
# samples taken from the Sanitize test suite
PROTOCOLS = {
'protocol-based JS injection: simple, no spaces' => [
'<a href="javascript:alert(\'XSS\');">foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: simple, spaces before' => [
'<a href="javascript :alert(\'XSS\');">foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: simple, spaces after' => [
'<a href="javascript: alert(\'XSS\');">foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: simple, spaces before and after' => [
'<a href="javascript : alert(\'XSS\');">foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: preceding colon' => [
'<a href=":javascript:alert(\'XSS\');">foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: UTF-8 encoding' => [
'<a href="javascript&#58;">foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: long UTF-8 encoding' => [
'<a href="javascript&#0058;">foo</a>',
'<a>foo</a>'
],
# rubocop:disable Layout/LineLength
'protocol-based JS injection: long UTF-8 encoding without semicolons' => [
'<a href=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041>foo</a>',
'<a>foo</a>'
],
# rubocop:enable Layout/LineLength
'protocol-based JS injection: hex encoding' => [
'<a href="javascript&#x3A;">foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: long hex encoding' => [
'<a href="javascript&#x003A;">foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: hex encoding without semicolons' => [
'<a href=&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69&#x70&#x74&#x3A&#x61&#x6C&#x65&#x72&#x74&#x28&#x27&#x58&#x53&#x53&#x27&#x29>foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: null char' => [
"<img src=java\0script:alert(\"XSS\")>",
'<img src="java">'
# '<img>'
],
'protocol-based JS injection: invalid URL char' => [
'<img src=java\script:alert("XSS")>',
'<img>'
],
'protocol-based JS injection: spaces and entities' => [
'<img src=" &#14; javascript:alert(\'XSS\');">',
'<img src="">'
# '<img>'
],
'protocol whitespace' => [
'<a href=" http://example.com/"></a>',
'<a href="http://example.com/"></a>'
],
'data images sources' => [
'<img src="data:image/png;base64,foobar">',
'<img>'
],
'data URIs' => [
'<a href="data:text/html;base64,foobar">XSS</a>',
'<a>XSS</a>'
],
'vbscript URIs' => [
'<a href="vbscript:foobar">XSS</a>',
'<a>XSS</a>'
],
}
PROTOCOLS.each do |name, strings|
test "should not allow #{name}" do
input, expected = *strings
assert_equal expected, filter(input)
end
'protocol-based JS injection: hex encoding' => [
'<a href="javascript&#x3A;">foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: long hex encoding' => [
'<a href="javascript&#x003A;">foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: hex encoding without semicolons' => [
'<a href=&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69&#x70&#x74&#x3A&#x61&#x6C&#x65&#x72&#x74&#x28&#x27&#x58&#x53&#x53&#x27&#x29>foo</a>',
'<a>foo</a>'
],
'protocol-based JS injection: null char' => [
"<img src=java\0script:alert(\"XSS\")>",
'<img src="java">'
# '<img>'
],
'protocol-based JS injection: invalid URL char' => [
'<img src=java\script:alert("XSS")>',
'<img>'
],
'protocol-based JS injection: spaces and entities' => [
'<img src=" &#14; javascript:alert(\'XSS\');">',
'<img src="">'
# '<img>'
],
'protocol whitespace' => [
'<a href=" http://example.com/"></a>',
'<a href="http://example.com/"></a>'
],
'data images sources' => [
'<img src="data:image/png;base64,foobar">',
'<img>'
],
'data URIs' => [
'<a href="data:text/html;base64,foobar">XSS</a>',
'<a>XSS</a>'
],
'vbscript URIs' => [
'<a href="vbscript:foobar">XSS</a>',
'<a>XSS</a>'
],
}
PROTOCOLS.each do |name, strings|
test "should not allow #{name}" do
input, expected = *strings
assert_equal expected, filter(input)
end
end
end
test/unit/lib/redmine/wiki_formatting/common_mark/syntax_highlight_filter_test.rb
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require_relative '../../../../../test_helper'
if Object.const_defined?(:Commonmarker)
require 'redmine/wiki_formatting/common_mark/syntax_highlight_filter'
require 'redmine/wiki_formatting/common_mark/syntax_highlight_filter'
class Redmine::WikiFormatting::CommonMark::SyntaxHighlightFilterTest < ActiveSupport::TestCase
def filter(html)
Redmine::WikiFormatting::CommonMark::SyntaxHighlightFilter.to_html(html, @options)
end
class Redmine::WikiFormatting::CommonMark::SyntaxHighlightFilterTest < ActiveSupport::TestCase
def filter(html)
Redmine::WikiFormatting::CommonMark::SyntaxHighlightFilter.to_html(html, @options)
end
def setup
@options = { }
end
def setup
@options = { }
end
def test_should_highlight_supported_language
input = <<~HTML
<pre><code class="language-ruby">
def foo
end
</code></pre>
HTML
expected = <<~HTML
<pre><code class="ruby syntaxhl" data-language="ruby">
<span class="k">def</span> <span class="nf">foo</span>
<span class="k">end</span>
</code></pre>
HTML
assert_equal expected, filter(input)
end
def test_should_highlight_supported_language
input = <<~HTML
<pre><code class="language-ruby">
def foo
end
</code></pre>
HTML
expected = <<~HTML
<pre><code class="ruby syntaxhl" data-language="ruby">
<span class="k">def</span> <span class="nf">foo</span>
<span class="k">end</span>
</code></pre>
HTML
assert_equal expected, filter(input)
end
def test_should_highlight_supported_language_with_special_chars
input = <<~HTML
<pre><code class="language-c-k&amp;r">
int i;
</code></pre>
HTML
expected = <<~HTML
<pre><code data-language="c-k&amp;r">
int i;
</code></pre>
HTML
assert_equal expected, filter(input)
end
def test_should_highlight_supported_language_with_special_chars
input = <<~HTML
<pre><code class="language-c-k&amp;r">
int i;
</code></pre>
HTML
expected = <<~HTML
<pre><code data-language="c-k&amp;r">
int i;
</code></pre>
HTML
assert_equal expected, filter(input)
end
def test_should_strip_code_class_and_preserve_data_language_attr_for_unknown_language
input = <<~HTML
<pre><code class="language-foobar">
def foo
end
</code></pre>
HTML
expected = <<~HTML
<pre><code data-language="foobar">
def foo
end
</code></pre>
HTML
assert_equal expected, filter(input)
end
def test_should_strip_code_class_and_preserve_data_language_attr_for_unknown_language
input = <<~HTML
<pre><code class="language-foobar">
def foo
end
</code></pre>
HTML
expected = <<~HTML
<pre><code data-language="foobar">
def foo
end
</code></pre>
HTML
assert_equal expected, filter(input)
end
def test_should_ignore_code_without_class
input = <<~HTML
<pre><code>
def foo
end
</code></pre>
HTML
assert_equal input, filter(input)
end
def test_should_ignore_code_without_class
input = <<~HTML
<pre><code>
def foo
end
</code></pre>
HTML
assert_equal input, filter(input)
end
end
(2-2/2)