diff --git a/lib/redmine/wiki_formatting/markdown/formatter.rb b/lib/redmine/wiki_formatting/markdown/formatter.rb index 418ab8b14..44d65248f 100644 --- a/lib/redmine/wiki_formatting/markdown/formatter.rb +++ b/lib/redmine/wiki_formatting/markdown/formatter.rb @@ -38,12 +38,14 @@ module Redmine def block_code(code, language) if language.present? && Redmine::SyntaxHighlighting.language_supported?(language) - "
" +
- Redmine::SyntaxHighlighting.highlight_by_language(code, language) +
- "
"
+ html = Redmine::SyntaxHighlighting.highlight_by_language(code, language)
+ classattr = " class=\"#{CGI.escapeHTML language} syntaxhl\""
else
- "" + CGI.escapeHTML(code) + "" + html = CGI.escapeHTML(code) end + # original language for extension development + langattr = " data-language=\"#{CGI.escapeHTML language}\"" if language.present? + "
#{html}
"
end
def image(link, title, alt_text)
diff --git a/lib/redmine/wiki_formatting/textile/formatter.rb b/lib/redmine/wiki_formatting/textile/formatter.rb
index 8f0200b33..d42530651 100644
--- a/lib/redmine/wiki_formatting/textile/formatter.rb
+++ b/lib/redmine/wiki_formatting/textile/formatter.rb
@@ -128,12 +128,14 @@ module Redmine
if content.match(/\s?(.*)/m)
language = $1 || $2
text = $3
+ # original language for extension development
+ langattr = " data-language=\"#{CGI.escapeHTML language}\"" if language.present?
if Redmine::SyntaxHighlighting.language_supported?(language)
text.gsub!(/x%x%/, '&')
- content = "" +
+ content = "" +
Redmine::SyntaxHighlighting.highlight_by_language(text, language)
else
- content = "#{ERB::Util.h(text)}"
+ content = "#{ERB::Util.h(text)}"
end
end
content
diff --git a/test/unit/lib/redmine/wiki_formatting/markdown_formatter_test.rb b/test/unit/lib/redmine/wiki_formatting/markdown_formatter_test.rb
index e3bb6ee79..445c7cbbc 100644
--- a/test/unit/lib/redmine/wiki_formatting/markdown_formatter_test.rb
+++ b/test/unit/lib/redmine/wiki_formatting/markdown_formatter_test.rb
@@ -70,6 +70,7 @@ class Redmine::WikiFormatting::MarkdownFormatterTest < ActionView::TestCase
STR
assert_select_in @formatter.new(text).to_html, 'pre code.ruby.syntaxhl' do
assert_select 'span.k', :text => 'def'
+ assert_select "[data-language='ruby']"
end
end
@@ -79,7 +80,16 @@ class Redmine::WikiFormatting::MarkdownFormatterTest < ActionView::TestCase
test
~~~
STR
- assert_equal "test\n
", @formatter.new(text).to_html
+ assert_equal "test\n
", @formatter.new(text).to_html
+ end
+
+ def test_should_preserve_code_block_language_in_data_language
+ text = <<~STR
+ ~~~c-k&r
+ test
+ ~~~
+ STR
+ assert_equal "test\n
", @formatter.new(text).to_html
end
def test_external_links_should_have_external_css_class
diff --git a/test/unit/lib/redmine/wiki_formatting/textile_formatter_test.rb b/test/unit/lib/redmine/wiki_formatting/textile_formatter_test.rb
index 5ee4defe1..3a8e0ad0f 100644
--- a/test/unit/lib/redmine/wiki_formatting/textile_formatter_test.rb
+++ b/test/unit/lib/redmine/wiki_formatting/textile_formatter_test.rb
@@ -596,12 +596,16 @@ class Redmine::WikiFormatting::TextileFormatterTest < ActionView::TestCase
end
def test_should_not_allow_arbitrary_class_attribute_on_offtags
- %w(code pre kbd).each do |tag|
- assert_html_output({"<#{tag} class=\"foo\">test#{tag}>" => "<#{tag}>test#{tag}>"}, false)
- assert_html_output({"<#{tag} class='foo'>test#{tag}>" => "<#{tag}>test#{tag}>"}, false)
- assert_html_output({"<#{tag} class=\"ruby foo\">test#{tag}>" => "<#{tag}>test#{tag}>"}, false)
- assert_html_output({"<#{tag} class='ruby foo'>test#{tag}>" => "<#{tag}>test#{tag}>"}, false)
- assert_html_output({"<#{tag} class=\"ruby \"foo\" bar\">test#{tag}>" => "<#{tag}>test#{tag}>"}, false)
+ {
+ "class=\"foo\"" => "data-language=\"foo\"",
+ "class='foo'" => "data-language=\"foo\"",
+ "class=\"ruby foo\"" => "data-language=\"ruby foo\"",
+ "class='ruby foo'" => "data-language=\"ruby foo\"",
+ "class=\"ruby \"foo\" bar\"" => "data-language=\"ruby \"",
+ }.each do |classattr, codeattr|
+ assert_html_output({"test
" => "test
"}, false)
+ assert_html_output({"test
" => "test
"}, false)
+ assert_html_output({"test" => "test"}, false)
end
assert_html_output({"test " => "test"}, false)
@@ -615,13 +619,25 @@ class Redmine::WikiFormatting::TextileFormatterTest < ActionView::TestCase
# language name is double-quoted
assert_html_output(
{"test
" =>
- "test
"},
+ "test
"},
false
)
# language name is single-quoted
assert_html_output(
{"test
" =>
- "test
"},
+ "test
"},
+ false
+ )
+ end
+
+ def test_should_preserve_code_language_class_attribute_in_data_language
+ assert_html_output(
+ {
+ "unsupported language
" =>
+ "unsupported language
",
+ "special-char language
" =>
+ "special-char language
",
+ },
false
)
end