diff --git a/lib/redmine/wiki_formatting/common_mark/sanitization_filter.rb b/lib/redmine/wiki_formatting/common_mark/sanitization_filter.rb index 94e4b8f5e..69805491a 100644 --- a/lib/redmine/wiki_formatting/common_mark/sanitization_filter.rb +++ b/lib/redmine/wiki_formatting/common_mark/sanitization_filter.rb @@ -48,7 +48,7 @@ module Redmine return unless node.name == "code" return unless node.has_attribute?("class") - unless /\Alanguage-(\w+)\z/.match?(node["class"]) + unless /\Alanguage-(\S+)\z/.match?(node["class"]) node.remove_attribute("class") end } diff --git a/lib/redmine/wiki_formatting/common_mark/syntax_highlight_filter.rb b/lib/redmine/wiki_formatting/common_mark/syntax_highlight_filter.rb index 316a7b8fd..f561488e5 100644 --- a/lib/redmine/wiki_formatting/common_mark/syntax_highlight_filter.rb +++ b/lib/redmine/wiki_formatting/common_mark/syntax_highlight_filter.rb @@ -26,11 +26,14 @@ module Redmine def call doc.search("pre > code").each do |node| next unless lang = node["class"].presence - next unless lang =~ /\Alanguage-(\w+)\z/ + next unless lang =~ /\Alanguage-(\S+)\z/ lang = $1 text = node.inner_text + # original language for extension development + node["data-language"] = lang unless node["data-language"] + if Redmine::SyntaxHighlighting.language_supported?(lang) html = Redmine::SyntaxHighlighting.highlight_by_language(text, lang) next if html.nil? diff --git a/test/unit/lib/redmine/wiki_formatting/common_mark/formatter_test.rb b/test/unit/lib/redmine/wiki_formatting/common_mark/formatter_test.rb index d121d5425..cd5e89931 100644 --- a/test/unit/lib/redmine/wiki_formatting/common_mark/formatter_test.rb +++ b/test/unit/lib/redmine/wiki_formatting/common_mark/formatter_test.rb @@ -94,24 +94,31 @@ class Redmine::WikiFormatting::CommonMark::FormatterTest < ActionView::TestCase end def test_should_support_syntax_highlight - text = <<-STR - ~~~ruby - def foo - end - ~~~ + text = <<~STR + ~~~ruby + def foo + end + ~~~ STR assert_select_in format(text), 'pre code.ruby.syntaxhl' do assert_select 'span.k', :text => 'def' + assert_select "[data-language='ruby']" end end - def test_should_not_allow_invalid_language_for_code_blocks - text = <<-STR - ~~~foo - test - ~~~ + def test_should_support_syntax_highlight_for_language_with_special_chars + text = <<~STR + ~~~c++ + int main() { + } + ~~~ STR - assert_equal "
test\n", format(text) + + assert_select_in format(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 diff --git a/test/unit/lib/redmine/wiki_formatting/common_mark/syntax_highlight_filter_test.rb b/test/unit/lib/redmine/wiki_formatting/common_mark/syntax_highlight_filter_test.rb index e7e782d53..586dbbe1e 100644 --- a/test/unit/lib/redmine/wiki_formatting/common_mark/syntax_highlight_filter_test.rb +++ b/test/unit/lib/redmine/wiki_formatting/common_mark/syntax_highlight_filter_test.rb @@ -38,7 +38,7 @@ if Object.const_defined?(:CommonMarker) HTML expected = <<~HTML -
+
def foo
end
@@ -46,7 +46,21 @@ if Object.const_defined?(:CommonMarker)
assert_equal expected, filter(input)
end
- def test_should_strip_code_for_unknown_lang
+ def test_should_highlight_supported_language_with_special_chars
+ input = <<~HTML
+
+ int i;
+
+ HTML
+ expected = <<~HTML
+
+ int i;
+
+ HTML
+ assert_equal expected, filter(input)
+ end
+
+ def test_should_strip_code_class_and_preserve_data_language_attr_for_unknown_language
input = <<~HTML
def foo
@@ -54,10 +68,10 @@ if Object.const_defined?(:CommonMarker)
HTML
expected = <<~HTML
-
+
def foo
end
-
+
HTML
assert_equal expected, filter(input)
end