##// END OF EJS Templates
Sanitize image links and handle nils in the toc formatter. #5445...
Eric Davis -
r3697:6eea3300f88a
parent child
Show More
@@ -1,161 +1,163
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'redcloth3'
19 19
20 20 module Redmine
21 21 module WikiFormatting
22 22 module Textile
23 23 class Formatter < RedCloth3
24 24 include ActionView::Helpers::TagHelper
25 25
26 26 # auto_link rule after textile rules so that it doesn't break !image_url! tags
27 27 RULES = [:textile, :block_markdown_rule, :inline_auto_link, :inline_auto_mailto, :inline_toc]
28 28
29 29 def initialize(*args)
30 30 super
31 31 self.hard_breaks=true
32 32 self.no_span_caps=true
33 33 self.filter_styles=true
34 34 end
35 35
36 36 def to_html(*rules)
37 37 @toc = []
38 38 super(*RULES).to_s
39 39 end
40 40
41 41 private
42 42
43 43 # Patch for RedCloth. Fixed in RedCloth r128 but _why hasn't released it yet.
44 44 # <a href="http://code.whytheluckystiff.net/redcloth/changeset/128">http://code.whytheluckystiff.net/redcloth/changeset/128</a>
45 45 def hard_break( text )
46 46 text.gsub!( /(.)\n(?!\n|\Z|>| *([#*=]+(\s|$)|[{|]))/, "\\1<br />" ) if hard_breaks
47 47 end
48 48
49 49 # Patch to add code highlighting support to RedCloth
50 50 def smooth_offtags( text )
51 51 unless @pre_list.empty?
52 52 ## replace <pre> content
53 53 text.gsub!(/<redpre#(\d+)>/) do
54 54 content = @pre_list[$1.to_i]
55 55 if content.match(/<code\s+class="(\w+)">\s?(.+)/m)
56 56 content = "<code class=\"#{$1} syntaxhl\">" +
57 57 Redmine::SyntaxHighlighting.highlight_by_language($2, $1)
58 58 end
59 59 content
60 60 end
61 61 end
62 62 end
63 63
64 64 # Patch to add 'table of content' support to RedCloth
65 65 def textile_p_withtoc(tag, atts, cite, content)
66 66 # removes wiki links from the item
67 67 toc_item = content.gsub(/(\[\[([^\]\|]*)(\|([^\]]*))?\]\])/) { $4 || $2 }
68 68 # sanitizes titles from links
69 69 # see redcloth3.rb, same as "#{pre}#{text}#{post}"
70 toc_item.gsub!(LINK_RE) { $2+$4+$9 }
70 toc_item.gsub!(LINK_RE) { [$2, $4, $9].join }
71 # sanitizes image links from titles
72 toc_item.gsub!(IMAGE_RE) { [$5].join }
71 73 # removes styles
72 74 # eg. %{color:red}Triggers% => Triggers
73 75 toc_item.gsub! %r[%\{[^\}]*\}([^%]+)%], '\\1'
74 76
75 77 # replaces non word caracters by dashes
76 78 anchor = toc_item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
77 79
78 80 unless anchor.blank?
79 81 if tag =~ /^h(\d)$/
80 82 @toc << [$1.to_i, anchor, toc_item]
81 83 end
82 84 atts << " id=\"#{anchor}\""
83 85 content = content + "<a href=\"##{anchor}\" class=\"wiki-anchor\">&para;</a>"
84 86 end
85 87 textile_p(tag, atts, cite, content)
86 88 end
87 89
88 90 alias :textile_h1 :textile_p_withtoc
89 91 alias :textile_h2 :textile_p_withtoc
90 92 alias :textile_h3 :textile_p_withtoc
91 93
92 94 def inline_toc(text)
93 95 text.gsub!(/<p>\{\{([<>]?)toc\}\}<\/p>/i) do
94 96 div_class = 'toc'
95 97 div_class << ' right' if $1 == '>'
96 98 div_class << ' left' if $1 == '<'
97 99 out = "<ul class=\"#{div_class}\">"
98 100 @toc.each do |heading|
99 101 level, anchor, toc_item = heading
100 102 out << "<li class=\"heading#{level}\"><a href=\"##{anchor}\">#{toc_item}</a></li>\n"
101 103 end
102 104 out << '</ul>'
103 105 out
104 106 end
105 107 end
106 108
107 109 AUTO_LINK_RE = %r{
108 110 ( # leading text
109 111 <\w+.*?>| # leading HTML tag, or
110 112 [^=<>!:'"/]| # leading punctuation, or
111 113 ^ # beginning of line
112 114 )
113 115 (
114 116 (?:https?://)| # protocol spec, or
115 117 (?:s?ftps?://)|
116 118 (?:www\.) # www.*
117 119 )
118 120 (
119 121 (\S+?) # url
120 122 (\/)? # slash
121 123 )
122 124 ([^\w\=\/;\(\)]*?) # post
123 125 (?=<|\s|$)
124 126 }x unless const_defined?(:AUTO_LINK_RE)
125 127
126 128 # Turns all urls into clickable links (code from Rails).
127 129 def inline_auto_link(text)
128 130 text.gsub!(AUTO_LINK_RE) do
129 131 all, leading, proto, url, post = $&, $1, $2, $3, $6
130 132 if leading =~ /<a\s/i || leading =~ /![<>=]?/
131 133 # don't replace URL's that are already linked
132 134 # and URL's prefixed with ! !> !< != (textile images)
133 135 all
134 136 else
135 137 # Idea below : an URL with unbalanced parethesis and
136 138 # ending by ')' is put into external parenthesis
137 139 if ( url[-1]==?) and ((url.count("(") - url.count(")")) < 0 ) )
138 140 url=url[0..-2] # discard closing parenth from url
139 141 post = ")"+post # add closing parenth to post
140 142 end
141 143 tag = content_tag('a', proto + url, :href => "#{proto=="www."?"http://www.":proto}#{url}", :class => 'external')
142 144 %(#{leading}#{tag}#{post})
143 145 end
144 146 end
145 147 end
146 148
147 149 # Turns all email addresses into clickable links (code from Rails).
148 150 def inline_auto_mailto(text)
149 151 text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
150 152 mail = $1
151 153 if text.match(/<a\b[^>]*>(.*)(#{Regexp.escape(mail)})(.*)<\/a>/)
152 154 mail
153 155 else
154 156 content_tag('a', mail, :href => "mailto:#{mail}", :class => "email")
155 157 end
156 158 end
157 159 end
158 160 end
159 161 end
160 162 end
161 163 end
@@ -1,596 +1,600
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2009 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.dirname(__FILE__) + '/../../test_helper'
19 19
20 20 class ApplicationHelperTest < HelperTestCase
21 21 include ApplicationHelper
22 22 include ActionView::Helpers::TextHelper
23 23 include ActionView::Helpers::DateHelper
24 24
25 25 fixtures :projects, :roles, :enabled_modules, :users,
26 26 :repositories, :changesets,
27 27 :trackers, :issue_statuses, :issues, :versions, :documents,
28 28 :wikis, :wiki_pages, :wiki_contents,
29 29 :boards, :messages,
30 30 :attachments,
31 31 :enumerations
32 32
33 33 def setup
34 34 super
35 35 end
36 36
37 37 def test_auto_links
38 38 to_test = {
39 39 'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>',
40 40 'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>',
41 41 'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.',
42 42 'https://foo.bar.' => '<a class="external" href="https://foo.bar">https://foo.bar</a>.',
43 43 'This is a link: http://foo.bar.' => 'This is a link: <a class="external" href="http://foo.bar">http://foo.bar</a>.',
44 44 'A link (eg. http://foo.bar).' => 'A link (eg. <a class="external" href="http://foo.bar">http://foo.bar</a>).',
45 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 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 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 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 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 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 51 '(see "inline link":http://www.foo.bar/Test)' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>)',
52 52 '(see "inline link":http://www.foo.bar/Test).' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>).',
53 53 'www.foo.bar' => '<a class="external" href="http://www.foo.bar">www.foo.bar</a>',
54 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 55 'http://foo.bar/page#125' => '<a class="external" href="http://foo.bar/page#125">http://foo.bar/page#125</a>',
56 56 'http://foo@www.bar.com' => '<a class="external" href="http://foo@www.bar.com">http://foo@www.bar.com</a>',
57 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 58 'ftp://foo.bar' => '<a class="external" href="ftp://foo.bar">ftp://foo.bar</a>',
59 59 'ftps://foo.bar' => '<a class="external" href="ftps://foo.bar">ftps://foo.bar</a>',
60 60 'sftp://foo.bar' => '<a class="external" href="sftp://foo.bar">sftp://foo.bar</a>',
61 61 # two exclamation marks
62 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>',
63 63 # escaping
64 64 'http://foo"bar' => '<a class="external" href="http://foo&quot;bar">http://foo"bar</a>',
65 65 }
66 66 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
67 67 end
68 68
69 69 def test_auto_mailto
70 70 assert_equal '<p><a class="email" href="mailto:test@foo.bar">test@foo.bar</a></p>',
71 71 textilizable('test@foo.bar')
72 72 end
73 73
74 74 def test_inline_images
75 75 to_test = {
76 76 '!http://foo.bar/image.jpg!' => '<img src="http://foo.bar/image.jpg" alt="" />',
77 77 'floating !>http://foo.bar/image.jpg!' => 'floating <div style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></div>',
78 78 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="some-class" alt="" />',
79 79 # inline styles should be stripped
80 80 'with style !{width:100px;height100px}http://foo.bar/image.jpg!' => 'with style <img src="http://foo.bar/image.jpg" alt="" />',
81 81 'with title !http://foo.bar/image.jpg(This is a title)!' => 'with title <img src="http://foo.bar/image.jpg" title="This is a title" alt="This is a title" />',
82 82 'with title !http://foo.bar/image.jpg(This is a double-quoted "title")!' => 'with title <img src="http://foo.bar/image.jpg" title="This is a double-quoted &quot;title&quot;" alt="This is a double-quoted &quot;title&quot;" />',
83 83 }
84 84 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
85 85 end
86 86
87 87 def test_inline_images_inside_tags
88 88 raw = <<-RAW
89 89 h1. !foo.png! Heading
90 90
91 91 Centered image:
92 92
93 93 p=. !bar.gif!
94 94 RAW
95 95
96 96 assert textilizable(raw).include?('<img src="foo.png" alt="" />')
97 97 assert textilizable(raw).include?('<img src="bar.gif" alt="" />')
98 98 end
99 99
100 100 def test_acronyms
101 101 to_test = {
102 102 'this is an acronym: GPL(General Public License)' => 'this is an acronym: <acronym title="General Public License">GPL</acronym>',
103 103 'GPL(This is a double-quoted "title")' => '<acronym title="This is a double-quoted &quot;title&quot;">GPL</acronym>',
104 104 }
105 105 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
106 106
107 107 end
108 108
109 109 def test_attached_images
110 110 to_test = {
111 111 'Inline image: !logo.gif!' => 'Inline image: <img src="/attachments/download/3" title="This is a logo" alt="This is a logo" />',
112 112 'Inline image: !logo.GIF!' => 'Inline image: <img src="/attachments/download/3" title="This is a logo" alt="This is a logo" />',
113 113 'No match: !ogo.gif!' => 'No match: <img src="ogo.gif" alt="" />',
114 114 'No match: !ogo.GIF!' => 'No match: <img src="ogo.GIF" alt="" />',
115 115 # link image
116 116 '!logo.gif!:http://foo.bar/' => '<a href="http://foo.bar/"><img src="/attachments/download/3" title="This is a logo" alt="This is a logo" /></a>',
117 117 }
118 118 attachments = Attachment.find(:all)
119 119 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
120 120 end
121 121
122 122 def test_textile_external_links
123 123 to_test = {
124 124 'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>',
125 125 'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>',
126 126 '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>',
127 127 '"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>',
128 128 "This is not a \"Link\":\n\nAnother paragraph" => "This is not a \"Link\":</p>\n\n\n\t<p>Another paragraph",
129 129 # no multiline link text
130 130 "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",
131 131 # mailto link
132 132 "\"system administrator\":mailto:sysadmin@example.com?subject=redmine%20permissions" => "<a href=\"mailto:sysadmin@example.com?subject=redmine%20permissions\">system administrator</a>",
133 133 # two exclamation marks
134 134 '"a link":http://example.net/path!602815048C7B5C20!302.html' => '<a href="http://example.net/path!602815048C7B5C20!302.html" class="external">a link</a>',
135 135 # escaping
136 136 '"test":http://foo"bar' => '<a href="http://foo&quot;bar" class="external">test</a>',
137 137 }
138 138 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
139 139 end
140 140
141 141 def test_redmine_links
142 142 issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3},
143 143 :class => 'issue status-1 priority-1 overdue', :title => 'Error 281 when updating a recipe (New)')
144 144
145 145 changeset_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
146 146 :class => 'changeset', :title => 'My very first commit')
147 147 changeset_link2 = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
148 148 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
149 149
150 150 document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1},
151 151 :class => 'document')
152 152
153 153 version_link = link_to('1.0', {:controller => 'versions', :action => 'show', :id => 2},
154 154 :class => 'version')
155 155
156 156 message_url = {:controller => 'messages', :action => 'show', :board_id => 1, :id => 4}
157 157
158 158 project_url = {:controller => 'projects', :action => 'show', :id => 'subproject1'}
159 159
160 160 source_url = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}
161 161 source_url_with_ext = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file.ext']}
162 162
163 163 to_test = {
164 164 # tickets
165 165 '#3, [#3], (#3) and #3.' => "#{issue_link}, [#{issue_link}], (#{issue_link}) and #{issue_link}.",
166 166 # changesets
167 167 'r1' => changeset_link,
168 168 'r1.' => "#{changeset_link}.",
169 169 'r1, r2' => "#{changeset_link}, #{changeset_link2}",
170 170 'r1,r2' => "#{changeset_link},#{changeset_link2}",
171 171 # documents
172 172 'document#1' => document_link,
173 173 'document:"Test document"' => document_link,
174 174 # versions
175 175 'version#2' => version_link,
176 176 'version:1.0' => version_link,
177 177 'version:"1.0"' => version_link,
178 178 # source
179 179 'source:/some/file' => link_to('source:/some/file', source_url, :class => 'source'),
180 180 'source:/some/file.' => link_to('source:/some/file', source_url, :class => 'source') + ".",
181 181 'source:/some/file.ext.' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
182 182 'source:/some/file. ' => link_to('source:/some/file', source_url, :class => 'source') + ".",
183 183 'source:/some/file.ext. ' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
184 184 'source:/some/file, ' => link_to('source:/some/file', source_url, :class => 'source') + ",",
185 185 'source:/some/file@52' => link_to('source:/some/file@52', source_url.merge(:rev => 52), :class => 'source'),
186 186 'source:/some/file.ext@52' => link_to('source:/some/file.ext@52', source_url_with_ext.merge(:rev => 52), :class => 'source'),
187 187 'source:/some/file#L110' => link_to('source:/some/file#L110', source_url.merge(:anchor => 'L110'), :class => 'source'),
188 188 'source:/some/file.ext#L110' => link_to('source:/some/file.ext#L110', source_url_with_ext.merge(:anchor => 'L110'), :class => 'source'),
189 189 'source:/some/file@52#L110' => link_to('source:/some/file@52#L110', source_url.merge(:rev => 52, :anchor => 'L110'), :class => 'source'),
190 190 'export:/some/file' => link_to('export:/some/file', source_url.merge(:format => 'raw'), :class => 'source download'),
191 191 # message
192 192 'message#4' => link_to('Post 2', message_url, :class => 'message'),
193 193 'message#5' => link_to('RE: post 2', message_url.merge(:anchor => 'message-5'), :class => 'message'),
194 194 # project
195 195 'project#3' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
196 196 'project:subproject1' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
197 197 'project:"eCookbook subProject 1"' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
198 198 # escaping
199 199 '!#3.' => '#3.',
200 200 '!r1' => 'r1',
201 201 '!document#1' => 'document#1',
202 202 '!document:"Test document"' => 'document:"Test document"',
203 203 '!version#2' => 'version#2',
204 204 '!version:1.0' => 'version:1.0',
205 205 '!version:"1.0"' => 'version:"1.0"',
206 206 '!source:/some/file' => 'source:/some/file',
207 207 # not found
208 208 '#0123456789' => '#0123456789',
209 209 # invalid expressions
210 210 'source:' => 'source:',
211 211 # url hash
212 212 "http://foo.bar/FAQ#3" => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
213 213 }
214 214 @project = Project.find(1)
215 215 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
216 216 end
217 217
218 218 def test_attachment_links
219 219 attachment_link = link_to('error281.txt', {:controller => 'attachments', :action => 'download', :id => '1'}, :class => 'attachment')
220 220 to_test = {
221 221 'attachment:error281.txt' => attachment_link
222 222 }
223 223 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => Issue.find(3).attachments), "#{text} failed" }
224 224 end
225 225
226 226 def test_wiki_links
227 227 to_test = {
228 228 '[[CookBook documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a>',
229 229 '[[Another page|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a>',
230 230 # link with anchor
231 231 '[[CookBook documentation#One-section]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation#One-section" class="wiki-page">CookBook documentation</a>',
232 232 '[[Another page#anchor|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page#anchor" class="wiki-page">Page</a>',
233 233 # page that doesn't exist
234 234 '[[Unknown page]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
235 235 '[[Unknown page|404]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">404</a>',
236 236 # link to another project wiki
237 237 '[[onlinestore:]]' => '<a href="/projects/onlinestore/wiki/" class="wiki-page">onlinestore</a>',
238 238 '[[onlinestore:|Wiki]]' => '<a href="/projects/onlinestore/wiki/" class="wiki-page">Wiki</a>',
239 239 '[[onlinestore:Start page]]' => '<a href="/projects/onlinestore/wiki/Start_page" class="wiki-page">Start page</a>',
240 240 '[[onlinestore:Start page|Text]]' => '<a href="/projects/onlinestore/wiki/Start_page" class="wiki-page">Text</a>',
241 241 '[[onlinestore:Unknown page]]' => '<a href="/projects/onlinestore/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
242 242 # striked through link
243 243 '-[[Another page|Page]]-' => '<del><a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a></del>',
244 244 '-[[Another page|Page]] link-' => '<del><a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a> link</del>',
245 245 # escaping
246 246 '![[Another page|Page]]' => '[[Another page|Page]]',
247 247 # project does not exist
248 248 '[[unknowproject:Start]]' => '[[unknowproject:Start]]',
249 249 '[[unknowproject:Start|Page title]]' => '[[unknowproject:Start|Page title]]',
250 250 }
251 251 @project = Project.find(1)
252 252 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
253 253 end
254 254
255 255 def test_html_tags
256 256 to_test = {
257 257 "<div>content</div>" => "<p>&lt;div&gt;content&lt;/div&gt;</p>",
258 258 "<div class=\"bold\">content</div>" => "<p>&lt;div class=\"bold\"&gt;content&lt;/div&gt;</p>",
259 259 "<script>some script;</script>" => "<p>&lt;script&gt;some script;&lt;/script&gt;</p>",
260 260 # do not escape pre/code tags
261 261 "<pre>\nline 1\nline2</pre>" => "<pre>\nline 1\nline2</pre>",
262 262 "<pre><code>\nline 1\nline2</code></pre>" => "<pre><code>\nline 1\nline2</code></pre>",
263 263 "<pre><div>content</div></pre>" => "<pre>&lt;div&gt;content&lt;/div&gt;</pre>",
264 264 "HTML comment: <!-- no comments -->" => "<p>HTML comment: &lt;!-- no comments --&gt;</p>",
265 265 "<!-- opening comment" => "<p>&lt;!-- opening comment</p>",
266 266 # remove attributes except class
267 267 "<pre class='foo'>some text</pre>" => "<pre class='foo'>some text</pre>",
268 268 "<pre onmouseover='alert(1)'>some text</pre>" => "<pre>some text</pre>",
269 269 }
270 270 to_test.each { |text, result| assert_equal result, textilizable(text) }
271 271 end
272 272
273 273 def test_allowed_html_tags
274 274 to_test = {
275 275 "<pre>preformatted text</pre>" => "<pre>preformatted text</pre>",
276 276 "<notextile>no *textile* formatting</notextile>" => "no *textile* formatting",
277 277 "<notextile>this is <tag>a tag</tag></notextile>" => "this is &lt;tag&gt;a tag&lt;/tag&gt;"
278 278 }
279 279 to_test.each { |text, result| assert_equal result, textilizable(text) }
280 280 end
281 281
282 282 def test_pre_tags
283 283 raw = <<-RAW
284 284 Before
285 285
286 286 <pre>
287 287 <prepared-statement-cache-size>32</prepared-statement-cache-size>
288 288 </pre>
289 289
290 290 After
291 291 RAW
292 292
293 293 expected = <<-EXPECTED
294 294 <p>Before</p>
295 295 <pre>
296 296 &lt;prepared-statement-cache-size&gt;32&lt;/prepared-statement-cache-size&gt;
297 297 </pre>
298 298 <p>After</p>
299 299 EXPECTED
300 300
301 301 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
302 302 end
303 303
304 304 def test_pre_content_should_not_parse_wiki_and_redmine_links
305 305 raw = <<-RAW
306 306 [[CookBook documentation]]
307 307
308 308 #1
309 309
310 310 <pre>
311 311 [[CookBook documentation]]
312 312
313 313 #1
314 314 </pre>
315 315 RAW
316 316
317 317 expected = <<-EXPECTED
318 318 <p><a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a></p>
319 319 <p><a href="/issues/1" class="issue status-1 priority-1" title="Can't print recipes (New)">#1</a></p>
320 320 <pre>
321 321 [[CookBook documentation]]
322 322
323 323 #1
324 324 </pre>
325 325 EXPECTED
326 326
327 327 @project = Project.find(1)
328 328 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
329 329 end
330 330
331 331 def test_non_closing_pre_blocks_should_be_closed
332 332 raw = <<-RAW
333 333 <pre><code>
334 334 RAW
335 335
336 336 expected = <<-EXPECTED
337 337 <pre><code>
338 338 </code></pre>
339 339 EXPECTED
340 340
341 341 @project = Project.find(1)
342 342 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
343 343 end
344 344
345 345 def test_syntax_highlight
346 346 raw = <<-RAW
347 347 <pre><code class="ruby">
348 348 # Some ruby code here
349 349 </code></pre>
350 350 RAW
351 351
352 352 expected = <<-EXPECTED
353 353 <pre><code class="ruby syntaxhl"><span class=\"CodeRay\"><span class="no">1</span> <span class="c"># Some ruby code here</span></span>
354 354 </code></pre>
355 355 EXPECTED
356 356
357 357 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
358 358 end
359 359
360 360 def test_wiki_links_in_tables
361 361 to_test = {"|[[Page|Link title]]|[[Other Page|Other title]]|\n|Cell 21|[[Last page]]|" =>
362 362 '<tr><td><a href="/projects/ecookbook/wiki/Page" class="wiki-page new">Link title</a></td>' +
363 363 '<td><a href="/projects/ecookbook/wiki/Other_Page" class="wiki-page new">Other title</a></td>' +
364 364 '</tr><tr><td>Cell 21</td><td><a href="/projects/ecookbook/wiki/Last_page" class="wiki-page new">Last page</a></td></tr>'
365 365 }
366 366 @project = Project.find(1)
367 367 to_test.each { |text, result| assert_equal "<table>#{result}</table>", textilizable(text).gsub(/[\t\n]/, '') }
368 368 end
369 369
370 370 def test_text_formatting
371 371 to_test = {'*_+bold, italic and underline+_*' => '<strong><em><ins>bold, italic and underline</ins></em></strong>',
372 372 '(_text within parentheses_)' => '(<em>text within parentheses</em>)',
373 373 'a *Humane Web* Text Generator' => 'a <strong>Humane Web</strong> Text Generator',
374 374 'a H *umane* W *eb* T *ext* G *enerator*' => 'a H <strong>umane</strong> W <strong>eb</strong> T <strong>ext</strong> G <strong>enerator</strong>',
375 375 'a *H* umane *W* eb *T* ext *G* enerator' => 'a <strong>H</strong> umane <strong>W</strong> eb <strong>T</strong> ext <strong>G</strong> enerator',
376 376 }
377 377 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
378 378 end
379 379
380 380 def test_wiki_horizontal_rule
381 381 assert_equal '<hr />', textilizable('---')
382 382 assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---')
383 383 end
384 384
385 385 def test_acronym
386 386 assert_equal '<p>This is an acronym: <acronym title="American Civil Liberties Union">ACLU</acronym>.</p>',
387 387 textilizable('This is an acronym: ACLU(American Civil Liberties Union).')
388 388 end
389 389
390 390 def test_footnotes
391 391 raw = <<-RAW
392 392 This is some text[1].
393 393
394 394 fn1. This is the foot note
395 395 RAW
396 396
397 397 expected = <<-EXPECTED
398 398 <p>This is some text<sup><a href=\"#fn1\">1</a></sup>.</p>
399 399 <p id="fn1" class="footnote"><sup>1</sup> This is the foot note</p>
400 400 EXPECTED
401 401
402 402 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
403 403 end
404 404
405 405 def test_table_of_content
406 406 raw = <<-RAW
407 407 {{toc}}
408 408
409 409 h1. Title
410 410
411 411 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
412 412
413 413 h2. Subtitle with a [[Wiki]] link
414 414
415 415 Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
416 416
417 417 h2. Subtitle with [[Wiki|another Wiki]] link
418 418
419 419 h2. Subtitle with %{color:red}red text%
420 420
421 421 h1. Another title
422 422
423 423 h2. An "Internet link":http://www.redmine.org/ inside subtitle
424
425 h2. "Project Name !/attachments/1234/logo_small.gif! !/attachments/5678/logo_2.png!":/projects/projectname/issues
426
424 427 RAW
425 428
426 429 expected = '<ul class="toc">' +
427 430 '<li class="heading1"><a href="#Title">Title</a></li>' +
428 431 '<li class="heading2"><a href="#Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' +
429 432 '<li class="heading2"><a href="#Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' +
430 433 '<li class="heading2"><a href="#Subtitle-with-red-text">Subtitle with red text</a></li>' +
431 434 '<li class="heading1"><a href="#Another-title">Another title</a></li>' +
432 435 '<li class="heading2"><a href="#An-Internet-link-inside-subtitle">An Internet link inside subtitle</a></li>' +
436 '<li class="heading2"><a href="#Project-Name">Project Name</a></li>' +
433 437 '</ul>'
434
438
435 439 assert textilizable(raw).gsub("\n", "").include?(expected)
436 440 end
437 441
438 442 def test_blockquote
439 443 # orig raw text
440 444 raw = <<-RAW
441 445 John said:
442 446 > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
443 447 > Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
444 448 > * Donec odio lorem,
445 449 > * sagittis ac,
446 450 > * malesuada in,
447 451 > * adipiscing eu, dolor.
448 452 >
449 453 > >Nulla varius pulvinar diam. Proin id arcu id lorem scelerisque condimentum. Proin vehicula turpis vitae lacus.
450 454 > Proin a tellus. Nam vel neque.
451 455
452 456 He's right.
453 457 RAW
454 458
455 459 # expected html
456 460 expected = <<-EXPECTED
457 461 <p>John said:</p>
458 462 <blockquote>
459 463 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
460 464 Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
461 465 <ul>
462 466 <li>Donec odio lorem,</li>
463 467 <li>sagittis ac,</li>
464 468 <li>malesuada in,</li>
465 469 <li>adipiscing eu, dolor.</li>
466 470 </ul>
467 471 <blockquote>
468 472 <p>Nulla varius pulvinar diam. Proin id arcu id lorem scelerisque condimentum. Proin vehicula turpis vitae lacus.</p>
469 473 </blockquote>
470 474 <p>Proin a tellus. Nam vel neque.</p>
471 475 </blockquote>
472 476 <p>He's right.</p>
473 477 EXPECTED
474 478
475 479 assert_equal expected.gsub(%r{\s+}, ''), textilizable(raw).gsub(%r{\s+}, '')
476 480 end
477 481
478 482 def test_table
479 483 raw = <<-RAW
480 484 This is a table with empty cells:
481 485
482 486 |cell11|cell12||
483 487 |cell21||cell23|
484 488 |cell31|cell32|cell33|
485 489 RAW
486 490
487 491 expected = <<-EXPECTED
488 492 <p>This is a table with empty cells:</p>
489 493
490 494 <table>
491 495 <tr><td>cell11</td><td>cell12</td><td></td></tr>
492 496 <tr><td>cell21</td><td></td><td>cell23</td></tr>
493 497 <tr><td>cell31</td><td>cell32</td><td>cell33</td></tr>
494 498 </table>
495 499 EXPECTED
496 500
497 501 assert_equal expected.gsub(%r{\s+}, ''), textilizable(raw).gsub(%r{\s+}, '')
498 502 end
499 503
500 504 def test_table_with_line_breaks
501 505 raw = <<-RAW
502 506 This is a table with line breaks:
503 507
504 508 |cell11
505 509 continued|cell12||
506 510 |-cell21-||cell23
507 511 cell23 line2
508 512 cell23 *line3*|
509 513 |cell31|cell32
510 514 cell32 line2|cell33|
511 515
512 516 RAW
513 517
514 518 expected = <<-EXPECTED
515 519 <p>This is a table with line breaks:</p>
516 520
517 521 <table>
518 522 <tr>
519 523 <td>cell11<br />continued</td>
520 524 <td>cell12</td>
521 525 <td></td>
522 526 </tr>
523 527 <tr>
524 528 <td><del>cell21</del></td>
525 529 <td></td>
526 530 <td>cell23<br/>cell23 line2<br/>cell23 <strong>line3</strong></td>
527 531 </tr>
528 532 <tr>
529 533 <td>cell31</td>
530 534 <td>cell32<br/>cell32 line2</td>
531 535 <td>cell33</td>
532 536 </tr>
533 537 </table>
534 538 EXPECTED
535 539
536 540 assert_equal expected.gsub(%r{\s+}, ''), textilizable(raw).gsub(%r{\s+}, '')
537 541 end
538 542
539 543 def test_textile_should_not_mangle_brackets
540 544 assert_equal '<p>[msg1][msg2]</p>', textilizable('[msg1][msg2]')
541 545 end
542 546
543 547 def test_default_formatter
544 548 Setting.text_formatting = 'unknown'
545 549 text = 'a *link*: http://www.example.net/'
546 550 assert_equal '<p>a *link*: <a href="http://www.example.net/">http://www.example.net/</a></p>', textilizable(text)
547 551 Setting.text_formatting = 'textile'
548 552 end
549 553
550 554 def test_due_date_distance_in_words
551 555 to_test = { Date.today => 'Due in 0 days',
552 556 Date.today + 1 => 'Due in 1 day',
553 557 Date.today + 100 => 'Due in about 3 months',
554 558 Date.today + 20000 => 'Due in over 54 years',
555 559 Date.today - 1 => '1 day late',
556 560 Date.today - 100 => 'about 3 months late',
557 561 Date.today - 20000 => 'over 54 years late',
558 562 }
559 563 to_test.each do |date, expected|
560 564 assert_equal expected, due_date_distance_in_words(date)
561 565 end
562 566 end
563 567
564 568 def test_avatar
565 569 # turn on avatars
566 570 Setting.gravatar_enabled = '1'
567 571 assert avatar(User.find_by_mail('jsmith@somenet.foo')).include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
568 572 assert avatar('jsmith <jsmith@somenet.foo>').include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
569 573 assert_nil avatar('jsmith')
570 574 assert_nil avatar(nil)
571 575
572 576 # turn off avatars
573 577 Setting.gravatar_enabled = '0'
574 578 assert_nil avatar(User.find_by_mail('jsmith@somenet.foo'))
575 579 end
576 580
577 581 def test_link_to_user
578 582 user = User.find(2)
579 583 t = link_to_user(user)
580 584 assert_equal "<a href=\"/users/2\">#{ user.name }</a>", t
581 585 end
582 586
583 587 def test_link_to_user_should_not_link_to_locked_user
584 588 user = User.find(5)
585 589 assert user.locked?
586 590 t = link_to_user(user)
587 591 assert_equal user.name, t
588 592 end
589 593
590 594 def test_link_to_user_should_not_link_to_anonymous
591 595 user = User.anonymous
592 596 assert user.anonymous?
593 597 t = link_to_user(user)
594 598 assert_equal ::I18n.t(:label_user_anonymous), t
595 599 end
596 600 end
General Comments 0
You need to be logged in to leave comments. Login now