Project

General

Profile

Patch #5249 » 0001-Add-rel-nofollow-on-external-links.patch

Dennis Møllegaard Pedersen, 2010-04-06 20:07

View differences:

app/helpers/application_helper.rb
478 478
          end
479 479
          # check if page exists
480 480
          wiki_page = link_project.wiki.find_page(page)
481
          link_to((title || page), format_wiki_link.call(link_project, Wiki.titleize(page), anchor),
482
                                   :class => ('wiki-page' + (wiki_page ? '' : ' new')))
481
          url = format_wiki_link.call(link_project, Wiki.titleize(page), anchor)
482
          url_params = { :class => ('wiki-page' + (wiki_page ? '' : ' new')) }
483
          url_params[:rel] = "nofollow" if (url =~ /^https?:\/\//)
484
          link_to((title || page), url, url_params)
483 485
        else
484 486
          # project or wiki doesn't exist
485 487
          all
lib/redcloth3.rb
810 810
          else
811 811
            url, url_title = check_refs( url )
812 812
            title ||= url_title
813
            external_link = (url =~ /^https?:\/\//);
813 814
            
814 815
            # Idea below : an URL with unbalanced parethesis and
815 816
            # ending by ')' is put into external parenthesis
......
819 820
            end
820 821
            atts = pba( atts )
821 822
            atts = " href=\"#{ url }#{ slash }\"#{ atts }"
823
            atts << " rel=\"nofollow\"" if external_link
822 824
            atts << " title=\"#{ htmlesc title }\"" if title
823 825
            atts = shelve( atts ) if atts
824 826
            
825
            external = (url =~ /^https?:\/\//) ? ' class="external"' : ''
827
            external = ' class="external"' if external_link
826 828
            
827 829
            "#{ pre }<a#{ atts }#{ external }>#{ text }</a>#{ post }"
828 830
          end
lib/redmine/wiki_formatting/textile/formatter.rb
135 135
                url=url[0..-2] # discard closing parenth from url
136 136
                post = ")"+post # add closing parenth to post
137 137
              end
138
              %(#{leading}<a class="external" href="#{proto=="www."?"http://www.":proto}#{url}">#{proto + url}</a>#{post})
138
              %(#{leading}<a class="external" rel="nofollow" href="#{proto=="www."?"http://www.":proto}#{url}">#{proto + url}</a>#{post})
139 139
            end
140 140
          end
141 141
        end
test/unit/helpers/application_helper_test.rb
36 36
  
37 37
  def test_auto_links
38 38
    to_test = {
39
      'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>',
40
      'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>',
41
      'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.',
42
      'https://foo.bar.' => '<a class="external" href="https://foo.bar">https://foo.bar</a>.',
43
      'This is a link: http://foo.bar.' => 'This is a link: <a class="external" href="http://foo.bar">http://foo.bar</a>.',
44
      'A link (eg. http://foo.bar).' => 'A link (eg. <a class="external" href="http://foo.bar">http://foo.bar</a>).',
45
      'http://foo.bar/foo.bar#foo.bar.' => '<a class="external" href="http://foo.bar/foo.bar#foo.bar">http://foo.bar/foo.bar#foo.bar</a>.',
46
      'http://www.foo.bar/Test_(foobar)' => '<a class="external" href="http://www.foo.bar/Test_(foobar)">http://www.foo.bar/Test_(foobar)</a>',
47
      '(see inline link : http://www.foo.bar/Test_(foobar))' => '(see inline link : <a class="external" href="http://www.foo.bar/Test_(foobar)">http://www.foo.bar/Test_(foobar)</a>)',
48
      '(see inline link : http://www.foo.bar/Test)' => '(see inline link : <a class="external" href="http://www.foo.bar/Test">http://www.foo.bar/Test</a>)',
49
      '(see inline link : http://www.foo.bar/Test).' => '(see inline link : <a class="external" href="http://www.foo.bar/Test">http://www.foo.bar/Test</a>).',
50
      '(see "inline link":http://www.foo.bar/Test_(foobar))' => '(see <a href="http://www.foo.bar/Test_(foobar)" class="external">inline link</a>)',
51
      '(see "inline link":http://www.foo.bar/Test)' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>)',
52
      '(see "inline link":http://www.foo.bar/Test).' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>).',
53
      'www.foo.bar' => '<a class="external" href="http://www.foo.bar">www.foo.bar</a>',
54
      'http://foo.bar/page?p=1&t=z&s=' => '<a class="external" href="http://foo.bar/page?p=1&#38;t=z&#38;s=">http://foo.bar/page?p=1&#38;t=z&#38;s=</a>',
55
      'http://foo.bar/page#125' => '<a class="external" href="http://foo.bar/page#125">http://foo.bar/page#125</a>',
56
      'http://foo@www.bar.com' => '<a class="external" href="http://foo@www.bar.com">http://foo@www.bar.com</a>',
57
      'http://foo:bar@www.bar.com' => '<a class="external" href="http://foo:bar@www.bar.com">http://foo:bar@www.bar.com</a>',
58
      'ftp://foo.bar' => '<a class="external" href="ftp://foo.bar">ftp://foo.bar</a>',
59
      'ftps://foo.bar' => '<a class="external" href="ftps://foo.bar">ftps://foo.bar</a>',
60
      'sftp://foo.bar' => '<a class="external" href="sftp://foo.bar">sftp://foo.bar</a>',
39
      'http://foo.bar' => '<a class="external" rel="nofollow" href="http://foo.bar">http://foo.bar</a>',
40
      'http://foo.bar/~user' => '<a class="external" rel="nofollow" href="http://foo.bar/~user">http://foo.bar/~user</a>',
41
      'http://foo.bar.' => '<a class="external" rel="nofollow" href="http://foo.bar">http://foo.bar</a>.',
42
      'https://foo.bar.' => '<a class="external" rel="nofollow" href="https://foo.bar">https://foo.bar</a>.',
43
      'This is a link: http://foo.bar.' => 'This is a link: <a class="external" rel="nofollow" href="http://foo.bar">http://foo.bar</a>.',
44
      'A link (eg. http://foo.bar).' => 'A link (eg. <a class="external" rel="nofollow" href="http://foo.bar">http://foo.bar</a>).',
45
      'http://foo.bar/foo.bar#foo.bar.' => '<a class="external" rel="nofollow" href="http://foo.bar/foo.bar#foo.bar">http://foo.bar/foo.bar#foo.bar</a>.',
46
      'http://www.foo.bar/Test_(foobar)' => '<a class="external" rel="nofollow" href="http://www.foo.bar/Test_(foobar)">http://www.foo.bar/Test_(foobar)</a>',
47
      '(see inline link : http://www.foo.bar/Test_(foobar))' => '(see inline link : <a class="external" rel="nofollow" href="http://www.foo.bar/Test_(foobar)">http://www.foo.bar/Test_(foobar)</a>)',
48
      '(see inline link : http://www.foo.bar/Test)' => '(see inline link : <a class="external" rel="nofollow" href="http://www.foo.bar/Test">http://www.foo.bar/Test</a>)',
49
      '(see inline link : http://www.foo.bar/Test).' => '(see inline link : <a class="external" rel="nofollow" href="http://www.foo.bar/Test">http://www.foo.bar/Test</a>).',
50
      '(see "inline link":http://www.foo.bar/Test_(foobar))' => '(see <a href="http://www.foo.bar/Test_(foobar)" rel="nofollow" class="external">inline link</a>)',
51
      '(see "inline link":http://www.foo.bar/Test)' => '(see <a href="http://www.foo.bar/Test" rel="nofollow" class="external">inline link</a>)',
52
      '(see "inline link":http://www.foo.bar/Test).' => '(see <a href="http://www.foo.bar/Test" rel="nofollow" class="external">inline link</a>).',
53
      'www.foo.bar' => '<a class="external" rel="nofollow" href="http://www.foo.bar">www.foo.bar</a>',
54
      'http://foo.bar/page?p=1&t=z&s=' => '<a class="external" rel="nofollow" href="http://foo.bar/page?p=1&#38;t=z&#38;s=">http://foo.bar/page?p=1&#38;t=z&#38;s=</a>',
55
      'http://foo.bar/page#125' => '<a class="external" rel="nofollow" href="http://foo.bar/page#125">http://foo.bar/page#125</a>',
56
      'http://foo@www.bar.com' => '<a class="external" rel="nofollow" href="http://foo@www.bar.com">http://foo@www.bar.com</a>',
57
      'http://foo:bar@www.bar.com' => '<a class="external" rel="nofollow" href="http://foo:bar@www.bar.com">http://foo:bar@www.bar.com</a>',
58
      'ftp://foo.bar' => '<a class="external" rel="nofollow" href="ftp://foo.bar">ftp://foo.bar</a>',
59
      'ftps://foo.bar' => '<a class="external" rel="nofollow" href="ftps://foo.bar">ftps://foo.bar</a>',
60
      'sftp://foo.bar' => '<a class="external" rel="nofollow" href="sftp://foo.bar">sftp://foo.bar</a>',
61 61
      # two exclamation marks
62
      'http://example.net/path!602815048C7B5C20!302.html' => '<a class="external" href="http://example.net/path!602815048C7B5C20!302.html">http://example.net/path!602815048C7B5C20!302.html</a>',
62
      'http://example.net/path!602815048C7B5C20!302.html' => '<a class="external" rel="nofollow" href="http://example.net/path!602815048C7B5C20!302.html">http://example.net/path!602815048C7B5C20!302.html</a>',
63 63
    }
64 64
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
65 65
  end
......
119 119
  
120 120
  def test_textile_external_links
121 121
    to_test = {
122
      'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>',
122
      'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" rel="nofollow" class="external">link</a>',
123 123
      'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>',
124
      '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>',
125
      '"link (Link title with "double-quotes")":http://foo.bar' => '<a href="http://foo.bar" title="Link title with &quot;double-quotes&quot;" class="external">link</a>',
124
      '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" rel="nofollow" title="Link title" class="external">link</a>',
125
      '"link (Link title with "double-quotes")":http://foo.bar' => '<a href="http://foo.bar" rel="nofollow" title="Link title with &quot;double-quotes&quot;" class="external">link</a>',
126 126
      "This is not a \"Link\":\n\nAnother paragraph" => "This is not a \"Link\":</p>\n\n\n\t<p>Another paragraph",
127 127
      # no multiline link text
128 128
      "This is a double quote \"on the first line\nand another on a second line\":test" => "This is a double quote \"on the first line<br />and another on a second line\":test",
129 129
      # mailto link
130 130
      "\"system administrator\":mailto:sysadmin@example.com?subject=redmine%20permissions" => "<a href=\"mailto:sysadmin@example.com?subject=redmine%20permissions\">system administrator</a>",
131 131
      # two exclamation marks
132
      '"a link":http://example.net/path!602815048C7B5C20!302.html' => '<a href="http://example.net/path!602815048C7B5C20!302.html" class="external">a link</a>',
132
      '"a link":http://example.net/path!602815048C7B5C20!302.html' => '<a href="http://example.net/path!602815048C7B5C20!302.html" rel="nofollow" class="external">a link</a>',
133 133
    }
134 134
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
135 135
  end
......
205 205
      # invalid expressions
206 206
      'source:'                     => 'source:',
207 207
      # url hash
208
      "http://foo.bar/FAQ#3"       => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
208
      "http://foo.bar/FAQ#3"       => '<a class="external" rel="nofollow" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
209 209
    }
210 210
    @project = Project.find(1)
211 211
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
    (1-1/1)