From b64f8b4de91b68a773bafc46c6cb24b62b10c908 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 8 Apr 2017 07:47:09 +0000 Subject: Merged r16500 to r16503 (#25503). git-svn-id: http://svn.redmine.org/redmine/branches/3.2-stable@16524 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- lib/redcloth3.rb | 14 ++++-- lib/redmine/syntax_highlighting.rb | 16 +++++++ lib/redmine/wiki_formatting/markdown/formatter.rb | 2 +- lib/redmine/wiki_formatting/textile/formatter.rb | 10 +++- public/stylesheets/application.css | 2 +- test/unit/helpers/application_helper_test.rb | 13 +++--- .../wiki_formatting/markdown_formatter_test.rb | 9 ++++ .../wiki_formatting/textile_formatter_test.rb | 54 ++++++++++++++++++++-- 8 files changed, 104 insertions(+), 16 deletions(-) diff --git a/lib/redcloth3.rb b/lib/redcloth3.rb index 31051fa96..d0bd217d3 100644 --- a/lib/redcloth3.rb +++ b/lib/redcloth3.rb @@ -494,7 +494,15 @@ class RedCloth3 < String style << "text-align:#{ h_align( $& ) };" if text =~ A_HLGN cls, id = $1, $2 if cls =~ /^(.*?)#(.*)$/ - + + # add wiki-class- and wiki-id- to classes and ids to prevent setting of + # arbitrary classes and ids + cls = cls.split(/\s+/).map do |c| + c.starts_with?('wiki-class-') ? c : "wiki-class-#{c}" + end.join(' ') if cls + + id = id.starts_with?('wiki-id-') ? id : "wiki-id-#{id}" if id + atts = '' atts << " style=\"#{ style.join }\"" unless style.empty? atts << " class=\"#{ cls }\"" unless cls.to_s.empty? @@ -1097,7 +1105,7 @@ class RedCloth3 < String first.match(/<#{ OFFTAGS }([^>]*)>/) tag = $1 $2.to_s.match(/(class\=("[^"]+"|'[^']+'))/i) - tag << " #{$1}" if $1 + tag << " #{$1}" if $1 && tag == 'code' @pre_list << "<#{ tag }>#{ aftertag }" end elsif $1 and codepre > 0 @@ -1202,8 +1210,8 @@ class RedCloth3 < String end end - ALLOWED_TAGS = %w(redpre pre code notextile) + ALLOWED_TAGS = %w(redpre pre code kbd notextile) def escape_html_tags(text) text.gsub!(%r{<(\/?([!\w]+)[^<>\n]*)(>?)}) {|m| ALLOWED_TAGS.include?($2) ? "<#{$1}#{$3}" : "<#{$1}#{'>' unless $3.blank?}" } end diff --git a/lib/redmine/syntax_highlighting.rb b/lib/redmine/syntax_highlighting.rb index 7480ebd16..7f4334977 100644 --- a/lib/redmine/syntax_highlighting.rb +++ b/lib/redmine/syntax_highlighting.rb @@ -40,6 +40,16 @@ module Redmine rescue ERB::Util.h(text) end + + def language_supported?(language) + if highlighter.respond_to? :language_supported? + highlighter.language_supported? language + else + true + end + rescue + false + end end module CodeRay @@ -58,6 +68,12 @@ module Redmine def highlight_by_language(text, language) ::CodeRay.scan(text, language).html(:wrap => :span) end + + def language_supported?(language) + ::CodeRay::Scanners.list.include?(language.to_s.downcase.to_sym) + rescue + false + end end end end diff --git a/lib/redmine/wiki_formatting/markdown/formatter.rb b/lib/redmine/wiki_formatting/markdown/formatter.rb index 4afbc2fdd..bfb04774c 100644 --- a/lib/redmine/wiki_formatting/markdown/formatter.rb +++ b/lib/redmine/wiki_formatting/markdown/formatter.rb @@ -35,7 +35,7 @@ module Redmine end def block_code(code, language) - if language.present? + if language.present? && Redmine::SyntaxHighlighting.language_supported?(language) "
" +
               Redmine::SyntaxHighlighting.highlight_by_language(code, language) +
               "
" diff --git a/lib/redmine/wiki_formatting/textile/formatter.rb b/lib/redmine/wiki_formatting/textile/formatter.rb index 91ea14960..a698cad45 100644 --- a/lib/redmine/wiki_formatting/textile/formatter.rb +++ b/lib/redmine/wiki_formatting/textile/formatter.rb @@ -121,8 +121,14 @@ module Redmine text.gsub!(//) do content = @pre_list[$1.to_i] if content.match(/\s?(.+)/m) - content = "" + - Redmine::SyntaxHighlighting.highlight_by_language($2, $1) + language = $1 + text = $2 + if Redmine::SyntaxHighlighting.language_supported?(language) + content = "" + + Redmine::SyntaxHighlighting.highlight_by_language(text, language) + else + content = "#{ERB::Util.h(text)}" + end end content end diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index f9a769eea..c49e97c2d 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -941,7 +941,7 @@ div.wiki table, div.wiki td, div.wiki th { padding: 4px; } -div.wiki .noborder, div.wiki .noborder td, div.wiki .noborder th {border:0;} +div.wiki .wiki-class-noborder, div.wiki .wiki-class-noborder td, div.wiki .wiki-class-noborder th {border:0;} div.wiki .external { background-position: 0% 60%; diff --git a/test/unit/helpers/application_helper_test.rb b/test/unit/helpers/application_helper_test.rb index 987668396..cbb4ce8a7 100644 --- a/test/unit/helpers/application_helper_test.rb +++ b/test/unit/helpers/application_helper_test.rb @@ -117,7 +117,8 @@ class ApplicationHelperTest < ActionView::TestCase to_test = { '!http://foo.bar/image.jpg!' => '', 'floating !>http://foo.bar/image.jpg!' => 'floating ', - 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class ', + 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class ', + 'with class !(wiki-class-foo)http://foo.bar/image.jpg!' => 'with class ', 'with style !{width:100px;height:100px}http://foo.bar/image.jpg!' => 'with style ', 'with title !http://foo.bar/image.jpg(This is a title)!' => 'with title This is a title', 'with title !http://foo.bar/image.jpg(This is a double-quoted "title")!' => 'with title This is a double-quoted "title"', @@ -905,11 +906,11 @@ RAW "
content
" => "
<div>content</div>
", "HTML comment: " => "

HTML comment: <!-- no comments -->

", "