Project

General

Profile

Patch #35104 » 0001-unify-code-blocks.patch

Martin Cizek, 2021-04-15 21:26

View differences:

lib/redmine/wiki_formatting/markdown/formatter.rb
38 38

  
39 39
        def block_code(code, language)
40 40
          if language.present? && Redmine::SyntaxHighlighting.language_supported?(language)
41
            "<pre><code class=\"#{CGI.escapeHTML language} syntaxhl\">" +
42
              Redmine::SyntaxHighlighting.highlight_by_language(code, language) +
43
              "</code></pre>"
41
            html = Redmine::SyntaxHighlighting.highlight_by_language(code, language)
42
            classattr = " class=\"#{CGI.escapeHTML language} syntaxhl\""
44 43
          else
45
            "<pre>" + CGI.escapeHTML(code) + "</pre>"
44
            html = CGI.escapeHTML(code)
46 45
          end
46
          # original language for extension development
47
          langattr = " data-language=\"#{CGI.escapeHTML language}\"" if language.present?
48
          "<pre><code#{classattr}#{langattr}>#{html}</code></pre>"
47 49
        end
48 50

  
49 51
        def image(link, title, alt_text)
lib/redmine/wiki_formatting/textile/formatter.rb
128 128
              if content.match(/<code\s+class=(?:"([^"]+)"|'([^']+)')>\s?(.*)/m)
129 129
                language = $1 || $2
130 130
                text = $3
131
                # original language for extension development
132
                langattr = " data-language=\"#{CGI.escapeHTML language}\"" if language.present?
131 133
                if Redmine::SyntaxHighlighting.language_supported?(language)
132 134
                  text.gsub!(/x%x%/, '&')
133
                  content = "<code class=\"#{language} syntaxhl\">" +
135
                  content = "<code class=\"#{CGI.escapeHTML language} syntaxhl\"#{langattr}>" +
134 136
                    Redmine::SyntaxHighlighting.highlight_by_language(text, language)
135 137
                else
136
                  content = "<code>#{ERB::Util.h(text)}"
138
                  content = "<code#{langattr}>#{ERB::Util.h(text)}"
137 139
                end
138 140
              end
139 141
              content
test/unit/lib/redmine/wiki_formatting/markdown_formatter_test.rb
70 70
    STR
71 71
    assert_select_in @formatter.new(text).to_html, 'pre code.ruby.syntaxhl' do
72 72
      assert_select 'span.k', :text => 'def'
73
      assert_select "[data-language='ruby']"
73 74
    end
74 75
  end
75 76

  
......
79 80
      test
80 81
      ~~~
81 82
    STR
82
    assert_equal "<pre>test\n</pre>", @formatter.new(text).to_html
83
    assert_equal "<pre><code data-language=\"foo\">test\n</code></pre>", @formatter.new(text).to_html
84
  end
85

  
86
  def test_should_preserve_code_block_language_in_data_language
87
    text = <<~STR
88
      ~~~c-k&r
89
      test
90
      ~~~
91
    STR
92
    assert_equal "<pre><code data-language=\"c-k&amp;r\">test\n</code></pre>", @formatter.new(text).to_html
83 93
  end
84 94

  
85 95
  def test_external_links_should_have_external_css_class
test/unit/lib/redmine/wiki_formatting/textile_formatter_test.rb
596 596
  end
597 597

  
598 598
  def test_should_not_allow_arbitrary_class_attribute_on_offtags
599
    %w(code pre kbd).each do |tag|
600
      assert_html_output({"<#{tag} class=\"foo\">test</#{tag}>" => "<#{tag}>test</#{tag}>"}, false)
601
      assert_html_output({"<#{tag} class='foo'>test</#{tag}>" => "<#{tag}>test</#{tag}>"}, false)
602
      assert_html_output({"<#{tag} class=\"ruby foo\">test</#{tag}>" => "<#{tag}>test</#{tag}>"}, false)
603
      assert_html_output({"<#{tag} class='ruby foo'>test</#{tag}>" => "<#{tag}>test</#{tag}>"}, false)
604
      assert_html_output({"<#{tag} class=\"ruby \"foo\" bar\">test</#{tag}>" => "<#{tag}>test</#{tag}>"}, false)
599
    {
600
      "class=\"foo\"" => "data-language=\"foo\"",
601
      "class='foo'" => "data-language=\"foo\"",
602
      "class=\"ruby foo\"" => "data-language=\"ruby foo\"",
603
      "class='ruby foo'" => "data-language=\"ruby foo\"",
604
      "class=\"ruby \"foo\" bar\"" => "data-language=\"ruby \"",
605
    }.each do |classattr, codeattr|
606
      assert_html_output({"<code #{classattr}>test</code>" => "<code #{codeattr}>test</code>"}, false)
607
      assert_html_output({"<pre #{classattr}>test</pre>" => "<pre>test</pre>"}, false)
608
      assert_html_output({"<kbd #{classattr}>test</kbd>" => "<kbd>test</kbd>"}, false)
605 609
    end
606 610

  
607 611
    assert_html_output({"<notextile class=\"foo\">test</notextile>" => "test"}, false)
......
615 619
    # language name is double-quoted
616 620
    assert_html_output(
617 621
      {"<code class=\"ruby\">test</code>" =>
618
         "<code class=\"ruby syntaxhl\"><span class=\"nb\">test</span></code>"},
622
         "<code class=\"ruby syntaxhl\" data-language=\"ruby\"><span class=\"nb\">test</span></code>"},
619 623
      false
620 624
    )
621 625
    # language name is single-quoted
622 626
    assert_html_output(
623 627
      {"<code class='ruby'>test</code>" =>
624
         "<code class=\"ruby syntaxhl\"><span class=\"nb\">test</span></code>"},
628
         "<code class=\"ruby syntaxhl\" data-language=\"ruby\"><span class=\"nb\">test</span></code>"},
629
      false
630
    )
631
  end
632

  
633
  def test_should_preserve_code_language_class_attribute_in_data_language
634
    assert_html_output(
635
      {
636
        "<code class=\"foolang\">unsupported language</code>" =>
637
          "<code data-language=\"foolang\">unsupported language</code>",
638
        "<code class=\"c-k&r\">special-char language</code>" =>
639
          "<code data-language=\"c-k&#38;r\">special-char language</code>",
640
      },
625 641
      false
626 642
    )
627 643
  end
(1-1/2)