##// END OF EJS Templates
Do not require a non-word character after a comma in Redmine links (#3561)....
Jean-Philippe Lang -
r2704:da22a9c8d653
parent child
Show More
@@ -1,654 +1,654
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 'coderay'
19 19 require 'coderay/helpers/file_type'
20 20 require 'forwardable'
21 21 require 'cgi'
22 22
23 23 module ApplicationHelper
24 24 include Redmine::WikiFormatting::Macros::Definitions
25 25 include Redmine::I18n
26 26 include GravatarHelper::PublicMethods
27 27
28 28 extend Forwardable
29 29 def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
30 30
31 31 # Return true if user is authorized for controller/action, otherwise false
32 32 def authorize_for(controller, action)
33 33 User.current.allowed_to?({:controller => controller, :action => action}, @project)
34 34 end
35 35
36 36 # Display a link if user is authorized
37 37 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
38 38 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
39 39 end
40 40
41 41 # Display a link to remote if user is authorized
42 42 def link_to_remote_if_authorized(name, options = {}, html_options = nil)
43 43 url = options[:url] || {}
44 44 link_to_remote(name, options, html_options) if authorize_for(url[:controller] || params[:controller], url[:action])
45 45 end
46 46
47 47 # Display a link to user's account page
48 48 def link_to_user(user, options={})
49 49 (user && !user.anonymous?) ? link_to(user.name(options[:format]), :controller => 'account', :action => 'show', :id => user) : 'Anonymous'
50 50 end
51 51
52 52 def link_to_issue(issue, options={})
53 53 options[:class] ||= issue.css_classes
54 54 link_to "#{issue.tracker.name} ##{issue.id}", {:controller => "issues", :action => "show", :id => issue}, options
55 55 end
56 56
57 57 # Generates a link to an attachment.
58 58 # Options:
59 59 # * :text - Link text (default to attachment filename)
60 60 # * :download - Force download (default: false)
61 61 def link_to_attachment(attachment, options={})
62 62 text = options.delete(:text) || attachment.filename
63 63 action = options.delete(:download) ? 'download' : 'show'
64 64
65 65 link_to(h(text), {:controller => 'attachments', :action => action, :id => attachment, :filename => attachment.filename }, options)
66 66 end
67 67
68 68 def toggle_link(name, id, options={})
69 69 onclick = "Element.toggle('#{id}'); "
70 70 onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
71 71 onclick << "return false;"
72 72 link_to(name, "#", :onclick => onclick)
73 73 end
74 74
75 75 def image_to_function(name, function, html_options = {})
76 76 html_options.symbolize_keys!
77 77 tag(:input, html_options.merge({
78 78 :type => "image", :src => image_path(name),
79 79 :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
80 80 }))
81 81 end
82 82
83 83 def prompt_to_remote(name, text, param, url, html_options = {})
84 84 html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;"
85 85 link_to name, {}, html_options
86 86 end
87 87
88 88 def format_activity_title(text)
89 89 h(truncate_single_line(text, :length => 100))
90 90 end
91 91
92 92 def format_activity_day(date)
93 93 date == Date.today ? l(:label_today).titleize : format_date(date)
94 94 end
95 95
96 96 def format_activity_description(text)
97 97 h(truncate(text.to_s, :length => 120).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...')).gsub(/[\r\n]+/, "<br />")
98 98 end
99 99
100 100 def due_date_distance_in_words(date)
101 101 if date
102 102 l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date))
103 103 end
104 104 end
105 105
106 106 def render_page_hierarchy(pages, node=nil)
107 107 content = ''
108 108 if pages[node]
109 109 content << "<ul class=\"pages-hierarchy\">\n"
110 110 pages[node].each do |page|
111 111 content << "<li>"
112 112 content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'index', :id => page.project, :page => page.title},
113 113 :title => (page.respond_to?(:updated_on) ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil))
114 114 content << "\n" + render_page_hierarchy(pages, page.id) if pages[page.id]
115 115 content << "</li>\n"
116 116 end
117 117 content << "</ul>\n"
118 118 end
119 119 content
120 120 end
121 121
122 122 # Renders flash messages
123 123 def render_flash_messages
124 124 s = ''
125 125 flash.each do |k,v|
126 126 s << content_tag('div', v, :class => "flash #{k}")
127 127 end
128 128 s
129 129 end
130 130
131 131 # Renders the project quick-jump box
132 132 def render_project_jump_box
133 133 # Retrieve them now to avoid a COUNT query
134 134 projects = User.current.projects.all
135 135 if projects.any?
136 136 s = '<select onchange="if (this.value != \'\') { window.location = this.value; }">' +
137 137 "<option selected='selected'>#{ l(:label_jump_to_a_project) }</option>" +
138 138 '<option disabled="disabled">---</option>'
139 139 s << project_tree_options_for_select(projects) do |p|
140 140 { :value => url_for(:controller => 'projects', :action => 'show', :id => p, :jump => current_menu_item) }
141 141 end
142 142 s << '</select>'
143 143 s
144 144 end
145 145 end
146 146
147 147 def project_tree_options_for_select(projects, options = {})
148 148 s = ''
149 149 project_tree(projects) do |project, level|
150 150 name_prefix = (level > 0 ? ('&nbsp;' * 2 * level + '&#187; ') : '')
151 151 tag_options = {:value => project.id, :selected => ((project == options[:selected]) ? 'selected' : nil)}
152 152 tag_options.merge!(yield(project)) if block_given?
153 153 s << content_tag('option', name_prefix + h(project), tag_options)
154 154 end
155 155 s
156 156 end
157 157
158 158 # Yields the given block for each project with its level in the tree
159 159 def project_tree(projects, &block)
160 160 ancestors = []
161 161 projects.sort_by(&:lft).each do |project|
162 162 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
163 163 ancestors.pop
164 164 end
165 165 yield project, ancestors.size
166 166 ancestors << project
167 167 end
168 168 end
169 169
170 170 def project_nested_ul(projects, &block)
171 171 s = ''
172 172 if projects.any?
173 173 ancestors = []
174 174 projects.sort_by(&:lft).each do |project|
175 175 if (ancestors.empty? || project.is_descendant_of?(ancestors.last))
176 176 s << "<ul>\n"
177 177 else
178 178 ancestors.pop
179 179 s << "</li>"
180 180 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
181 181 ancestors.pop
182 182 s << "</ul></li>\n"
183 183 end
184 184 end
185 185 s << "<li>"
186 186 s << yield(project).to_s
187 187 ancestors << project
188 188 end
189 189 s << ("</li></ul>\n" * ancestors.size)
190 190 end
191 191 s
192 192 end
193 193
194 194 # Truncates and returns the string as a single line
195 195 def truncate_single_line(string, *args)
196 196 truncate(string, *args).gsub(%r{[\r\n]+}m, ' ')
197 197 end
198 198
199 199 def html_hours(text)
200 200 text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>')
201 201 end
202 202
203 203 def authoring(created, author, options={})
204 204 author_tag = (author.is_a?(User) && !author.anonymous?) ? link_to(h(author), :controller => 'account', :action => 'show', :id => author) : h(author || 'Anonymous')
205 205 l(options[:label] || :label_added_time_by, :author => author_tag, :age => time_tag(created))
206 206 end
207 207
208 208 def time_tag(time)
209 209 text = distance_of_time_in_words(Time.now, time)
210 210 if @project
211 211 link_to(text, {:controller => 'projects', :action => 'activity', :id => @project, :from => time.to_date}, :title => format_time(time))
212 212 else
213 213 content_tag('acronym', text, :title => format_time(time))
214 214 end
215 215 end
216 216
217 217 def syntax_highlight(name, content)
218 218 type = CodeRay::FileType[name]
219 219 type ? CodeRay.scan(content, type).html : h(content)
220 220 end
221 221
222 222 def to_path_param(path)
223 223 path.to_s.split(%r{[/\\]}).select {|p| !p.blank?}
224 224 end
225 225
226 226 def pagination_links_full(paginator, count=nil, options={})
227 227 page_param = options.delete(:page_param) || :page
228 228 url_param = params.dup
229 229 # don't reuse query params if filters are present
230 230 url_param.merge!(:fields => nil, :values => nil, :operators => nil) if url_param.delete(:set_filter)
231 231
232 232 html = ''
233 233 if paginator.current.previous
234 234 html << link_to_remote_content_update('&#171; ' + l(:label_previous), url_param.merge(page_param => paginator.current.previous)) + ' '
235 235 end
236 236
237 237 html << (pagination_links_each(paginator, options) do |n|
238 238 link_to_remote_content_update(n.to_s, url_param.merge(page_param => n))
239 239 end || '')
240 240
241 241 if paginator.current.next
242 242 html << ' ' + link_to_remote_content_update((l(:label_next) + ' &#187;'), url_param.merge(page_param => paginator.current.next))
243 243 end
244 244
245 245 unless count.nil?
246 246 html << [
247 247 " (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})",
248 248 per_page_links(paginator.items_per_page)
249 249 ].compact.join(' | ')
250 250 end
251 251
252 252 html
253 253 end
254 254
255 255 def per_page_links(selected=nil)
256 256 url_param = params.dup
257 257 url_param.clear if url_param.has_key?(:set_filter)
258 258
259 259 links = Setting.per_page_options_array.collect do |n|
260 260 n == selected ? n : link_to_remote(n, {:update => "content",
261 261 :url => params.dup.merge(:per_page => n),
262 262 :method => :get},
263 263 {:href => url_for(url_param.merge(:per_page => n))})
264 264 end
265 265 links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil
266 266 end
267 267
268 268 def reorder_links(name, url)
269 269 link_to(image_tag('2uparrow.png', :alt => l(:label_sort_highest)), url.merge({"#{name}[move_to]" => 'highest'}), :method => :post, :title => l(:label_sort_highest)) +
270 270 link_to(image_tag('1uparrow.png', :alt => l(:label_sort_higher)), url.merge({"#{name}[move_to]" => 'higher'}), :method => :post, :title => l(:label_sort_higher)) +
271 271 link_to(image_tag('1downarrow.png', :alt => l(:label_sort_lower)), url.merge({"#{name}[move_to]" => 'lower'}), :method => :post, :title => l(:label_sort_lower)) +
272 272 link_to(image_tag('2downarrow.png', :alt => l(:label_sort_lowest)), url.merge({"#{name}[move_to]" => 'lowest'}), :method => :post, :title => l(:label_sort_lowest))
273 273 end
274 274
275 275 def breadcrumb(*args)
276 276 elements = args.flatten
277 277 elements.any? ? content_tag('p', args.join(' &#187; ') + ' &#187; ', :class => 'breadcrumb') : nil
278 278 end
279 279
280 280 def other_formats_links(&block)
281 281 concat('<p class="other-formats">' + l(:label_export_to))
282 282 yield Redmine::Views::OtherFormatsBuilder.new(self)
283 283 concat('</p>')
284 284 end
285 285
286 286 def page_header_title
287 287 if @project.nil? || @project.new_record?
288 288 h(Setting.app_title)
289 289 else
290 290 b = []
291 291 ancestors = (@project.root? ? [] : @project.ancestors.visible)
292 292 if ancestors.any?
293 293 root = ancestors.shift
294 294 b << link_to(h(root), {:controller => 'projects', :action => 'show', :id => root, :jump => current_menu_item}, :class => 'root')
295 295 if ancestors.size > 2
296 296 b << '&#8230;'
297 297 ancestors = ancestors[-2, 2]
298 298 end
299 299 b += ancestors.collect {|p| link_to(h(p), {:controller => 'projects', :action => 'show', :id => p, :jump => current_menu_item}, :class => 'ancestor') }
300 300 end
301 301 b << h(@project)
302 302 b.join(' &#187; ')
303 303 end
304 304 end
305 305
306 306 def html_title(*args)
307 307 if args.empty?
308 308 title = []
309 309 title << @project.name if @project
310 310 title += @html_title if @html_title
311 311 title << Setting.app_title
312 312 title.compact.join(' - ')
313 313 else
314 314 @html_title ||= []
315 315 @html_title += args
316 316 end
317 317 end
318 318
319 319 def accesskey(s)
320 320 Redmine::AccessKeys.key_for s
321 321 end
322 322
323 323 # Formats text according to system settings.
324 324 # 2 ways to call this method:
325 325 # * with a String: textilizable(text, options)
326 326 # * with an object and one of its attribute: textilizable(issue, :description, options)
327 327 def textilizable(*args)
328 328 options = args.last.is_a?(Hash) ? args.pop : {}
329 329 case args.size
330 330 when 1
331 331 obj = options[:object]
332 332 text = args.shift
333 333 when 2
334 334 obj = args.shift
335 335 text = obj.send(args.shift).to_s
336 336 else
337 337 raise ArgumentError, 'invalid arguments to textilizable'
338 338 end
339 339 return '' if text.blank?
340 340
341 341 only_path = options.delete(:only_path) == false ? false : true
342 342
343 343 # when using an image link, try to use an attachment, if possible
344 344 attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil)
345 345
346 346 if attachments
347 347 attachments = attachments.sort_by(&:created_on).reverse
348 348 text = text.gsub(/!((\<|\=|\>)?(\([^\)]+\))?(\[[^\]]+\])?(\{[^\}]+\})?)(\S+\.(bmp|gif|jpg|jpeg|png))!/i) do |m|
349 349 style = $1
350 350 filename = $6.downcase
351 351 # search for the picture in attachments
352 352 if found = attachments.detect { |att| att.filename.downcase == filename }
353 353 image_url = url_for :only_path => only_path, :controller => 'attachments', :action => 'download', :id => found
354 354 desc = found.description.to_s.gsub(/^([^\(\)]*).*$/, "\\1")
355 355 alt = desc.blank? ? nil : "(#{desc})"
356 356 "!#{style}#{image_url}#{alt}!"
357 357 else
358 358 m
359 359 end
360 360 end
361 361 end
362 362
363 363 text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text) { |macro, args| exec_macro(macro, obj, args) }
364 364
365 365 # different methods for formatting wiki links
366 366 case options[:wiki_links]
367 367 when :local
368 368 # used for local links to html files
369 369 format_wiki_link = Proc.new {|project, title, anchor| "#{title}.html" }
370 370 when :anchor
371 371 # used for single-file wiki export
372 372 format_wiki_link = Proc.new {|project, title, anchor| "##{title}" }
373 373 else
374 374 format_wiki_link = Proc.new {|project, title, anchor| url_for(:only_path => only_path, :controller => 'wiki', :action => 'index', :id => project, :page => title, :anchor => anchor) }
375 375 end
376 376
377 377 project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
378 378
379 379 # Wiki links
380 380 #
381 381 # Examples:
382 382 # [[mypage]]
383 383 # [[mypage|mytext]]
384 384 # wiki links can refer other project wikis, using project name or identifier:
385 385 # [[project:]] -> wiki starting page
386 386 # [[project:|mytext]]
387 387 # [[project:mypage]]
388 388 # [[project:mypage|mytext]]
389 389 text = text.gsub(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
390 390 link_project = project
391 391 esc, all, page, title = $1, $2, $3, $5
392 392 if esc.nil?
393 393 if page =~ /^([^\:]+)\:(.*)$/
394 394 link_project = Project.find_by_name($1) || Project.find_by_identifier($1)
395 395 page = $2
396 396 title ||= $1 if page.blank?
397 397 end
398 398
399 399 if link_project && link_project.wiki
400 400 # extract anchor
401 401 anchor = nil
402 402 if page =~ /^(.+?)\#(.+)$/
403 403 page, anchor = $1, $2
404 404 end
405 405 # check if page exists
406 406 wiki_page = link_project.wiki.find_page(page)
407 407 link_to((title || page), format_wiki_link.call(link_project, Wiki.titleize(page), anchor),
408 408 :class => ('wiki-page' + (wiki_page ? '' : ' new')))
409 409 else
410 410 # project or wiki doesn't exist
411 411 all
412 412 end
413 413 else
414 414 all
415 415 end
416 416 end
417 417
418 418 # Redmine links
419 419 #
420 420 # Examples:
421 421 # Issues:
422 422 # #52 -> Link to issue #52
423 423 # Changesets:
424 424 # r52 -> Link to revision 52
425 425 # commit:a85130f -> Link to scmid starting with a85130f
426 426 # Documents:
427 427 # document#17 -> Link to document with id 17
428 428 # document:Greetings -> Link to the document with title "Greetings"
429 429 # document:"Some document" -> Link to the document with title "Some document"
430 430 # Versions:
431 431 # version#3 -> Link to version with id 3
432 432 # version:1.0.0 -> Link to version named "1.0.0"
433 433 # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
434 434 # Attachments:
435 435 # attachment:file.zip -> Link to the attachment of the current object named file.zip
436 436 # Source files:
437 437 # source:some/file -> Link to the file located at /some/file in the project's repository
438 438 # source:some/file@52 -> Link to the file's revision 52
439 439 # source:some/file#L120 -> Link to line 120 of the file
440 440 # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
441 441 # export:some/file -> Force the download of the file
442 442 # Forum messages:
443 443 # message#1218 -> Link to message with id 1218
444 text = text.gsub(%r{([\s\(,\-\>]|^)(!)?(attachment|document|version|commit|source|export|message)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|\s|<|$)}) do |m|
444 text = text.gsub(%r{([\s\(,\-\>]|^)(!)?(attachment|document|version|commit|source|export|message)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|<|$)}) do |m|
445 445 leading, esc, prefix, sep, oid = $1, $2, $3, $5 || $7, $6 || $8
446 446 link = nil
447 447 if esc.nil?
448 448 if prefix.nil? && sep == 'r'
449 449 if project && (changeset = project.changesets.find_by_revision(oid))
450 450 link = link_to("r#{oid}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => oid},
451 451 :class => 'changeset',
452 452 :title => truncate_single_line(changeset.comments, :length => 100))
453 453 end
454 454 elsif sep == '#'
455 455 oid = oid.to_i
456 456 case prefix
457 457 when nil
458 458 if issue = Issue.find_by_id(oid, :include => [:project, :status], :conditions => Project.visible_by(User.current))
459 459 link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid},
460 460 :class => (issue.closed? ? 'issue closed' : 'issue'),
461 461 :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})")
462 462 link = content_tag('del', link) if issue.closed?
463 463 end
464 464 when 'document'
465 465 if document = Document.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
466 466 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
467 467 :class => 'document'
468 468 end
469 469 when 'version'
470 470 if version = Version.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
471 471 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
472 472 :class => 'version'
473 473 end
474 474 when 'message'
475 475 if message = Message.find_by_id(oid, :include => [:parent, {:board => :project}], :conditions => Project.visible_by(User.current))
476 476 link = link_to h(truncate(message.subject, :length => 60)), {:only_path => only_path,
477 477 :controller => 'messages',
478 478 :action => 'show',
479 479 :board_id => message.board,
480 480 :id => message.root,
481 481 :anchor => (message.parent ? "message-#{message.id}" : nil)},
482 482 :class => 'message'
483 483 end
484 484 end
485 485 elsif sep == ':'
486 486 # removes the double quotes if any
487 487 name = oid.gsub(%r{^"(.*)"$}, "\\1")
488 488 case prefix
489 489 when 'document'
490 490 if project && document = project.documents.find_by_title(name)
491 491 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
492 492 :class => 'document'
493 493 end
494 494 when 'version'
495 495 if project && version = project.versions.find_by_name(name)
496 496 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
497 497 :class => 'version'
498 498 end
499 499 when 'commit'
500 500 if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
501 501 link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision},
502 502 :class => 'changeset',
503 503 :title => truncate_single_line(changeset.comments, :length => 100)
504 504 end
505 505 when 'source', 'export'
506 506 if project && project.repository
507 507 name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$}
508 508 path, rev, anchor = $1, $3, $5
509 509 link = link_to h("#{prefix}:#{name}"), {:controller => 'repositories', :action => 'entry', :id => project,
510 510 :path => to_path_param(path),
511 511 :rev => rev,
512 512 :anchor => anchor,
513 513 :format => (prefix == 'export' ? 'raw' : nil)},
514 514 :class => (prefix == 'export' ? 'source download' : 'source')
515 515 end
516 516 when 'attachment'
517 517 if attachments && attachment = attachments.detect {|a| a.filename == name }
518 518 link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment},
519 519 :class => 'attachment'
520 520 end
521 521 end
522 522 end
523 523 end
524 524 leading + (link || "#{prefix}#{sep}#{oid}")
525 525 end
526 526
527 527 text
528 528 end
529 529
530 530 # Same as Rails' simple_format helper without using paragraphs
531 531 def simple_format_without_paragraph(text)
532 532 text.to_s.
533 533 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
534 534 gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
535 535 gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
536 536 end
537 537
538 538 def lang_options_for_select(blank=true)
539 539 (blank ? [["(auto)", ""]] : []) +
540 540 valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last }
541 541 end
542 542
543 543 def label_tag_for(name, option_tags = nil, options = {})
544 544 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
545 545 content_tag("label", label_text)
546 546 end
547 547
548 548 def labelled_tabular_form_for(name, object, options, &proc)
549 549 options[:html] ||= {}
550 550 options[:html][:class] = 'tabular' unless options[:html].has_key?(:class)
551 551 form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc)
552 552 end
553 553
554 554 def back_url_hidden_field_tag
555 555 back_url = params[:back_url] || request.env['HTTP_REFERER']
556 556 back_url = CGI.unescape(back_url.to_s)
557 557 hidden_field_tag('back_url', CGI.escape(back_url)) unless back_url.blank?
558 558 end
559 559
560 560 def check_all_links(form_name)
561 561 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
562 562 " | " +
563 563 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
564 564 end
565 565
566 566 def progress_bar(pcts, options={})
567 567 pcts = [pcts, pcts] unless pcts.is_a?(Array)
568 568 pcts[1] = pcts[1] - pcts[0]
569 569 pcts << (100 - pcts[1] - pcts[0])
570 570 width = options[:width] || '100px;'
571 571 legend = options[:legend] || ''
572 572 content_tag('table',
573 573 content_tag('tr',
574 574 (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0].floor}%;", :class => 'closed') : '') +
575 575 (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1].floor}%;", :class => 'done') : '') +
576 576 (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2].floor}%;", :class => 'todo') : '')
577 577 ), :class => 'progress', :style => "width: #{width};") +
578 578 content_tag('p', legend, :class => 'pourcent')
579 579 end
580 580
581 581 def context_menu_link(name, url, options={})
582 582 options[:class] ||= ''
583 583 if options.delete(:selected)
584 584 options[:class] << ' icon-checked disabled'
585 585 options[:disabled] = true
586 586 end
587 587 if options.delete(:disabled)
588 588 options.delete(:method)
589 589 options.delete(:confirm)
590 590 options.delete(:onclick)
591 591 options[:class] << ' disabled'
592 592 url = '#'
593 593 end
594 594 link_to name, url, options
595 595 end
596 596
597 597 def calendar_for(field_id)
598 598 include_calendar_headers_tags
599 599 image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
600 600 javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
601 601 end
602 602
603 603 def include_calendar_headers_tags
604 604 unless @calendar_headers_tags_included
605 605 @calendar_headers_tags_included = true
606 606 content_for :header_tags do
607 607 javascript_include_tag('calendar/calendar') +
608 608 javascript_include_tag("calendar/lang/calendar-#{current_language.to_s.downcase}.js") +
609 609 javascript_include_tag('calendar/calendar-setup') +
610 610 stylesheet_link_tag('calendar')
611 611 end
612 612 end
613 613 end
614 614
615 615 def content_for(name, content = nil, &block)
616 616 @has_content ||= {}
617 617 @has_content[name] = true
618 618 super(name, content, &block)
619 619 end
620 620
621 621 def has_content?(name)
622 622 (@has_content && @has_content[name]) || false
623 623 end
624 624
625 625 # Returns the avatar image tag for the given +user+ if avatars are enabled
626 626 # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
627 627 def avatar(user, options = { })
628 628 if Setting.gravatar_enabled?
629 629 email = nil
630 630 if user.respond_to?(:mail)
631 631 email = user.mail
632 632 elsif user.to_s =~ %r{<(.+?)>}
633 633 email = $1
634 634 end
635 635 return gravatar(email.to_s.downcase, options) unless email.blank? rescue nil
636 636 end
637 637 end
638 638
639 639 private
640 640
641 641 def wiki_helper
642 642 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
643 643 extend helper
644 644 return self
645 645 end
646 646
647 647 def link_to_remote_content_update(text, url_params)
648 648 link_to_remote(text,
649 649 {:url => url_params, :method => :get, :update => 'content', :complete => 'window.scrollTo(0,0)'},
650 650 {:href => url_for(:params => url_params)}
651 651 )
652 652 end
653 653
654 654 end
@@ -1,427 +1,432
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
32 32 def setup
33 33 super
34 34 end
35 35
36 36 def test_auto_links
37 37 to_test = {
38 38 'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>',
39 39 'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>',
40 40 'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.',
41 41 'https://foo.bar.' => '<a class="external" href="https://foo.bar">https://foo.bar</a>.',
42 42 'This is a link: http://foo.bar.' => 'This is a link: <a class="external" href="http://foo.bar">http://foo.bar</a>.',
43 43 'A link (eg. http://foo.bar).' => 'A link (eg. <a class="external" href="http://foo.bar">http://foo.bar</a>).',
44 44 '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>.',
45 45 'http://www.foo.bar/Test_(foobar)' => '<a class="external" href="http://www.foo.bar/Test_(foobar)">http://www.foo.bar/Test_(foobar)</a>',
46 46 '(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>)',
47 47 '(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>)',
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_(foobar))' => '(see <a href="http://www.foo.bar/Test_(foobar)" class="external">inline link</a>)',
50 50 '(see "inline link":http://www.foo.bar/Test)' => '(see <a href="http://www.foo.bar/Test" 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 'www.foo.bar' => '<a class="external" href="http://www.foo.bar">www.foo.bar</a>',
53 53 '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>',
54 54 'http://foo.bar/page#125' => '<a class="external" href="http://foo.bar/page#125">http://foo.bar/page#125</a>',
55 55 'http://foo@www.bar.com' => '<a class="external" href="http://foo@www.bar.com">http://foo@www.bar.com</a>',
56 56 'http://foo:bar@www.bar.com' => '<a class="external" href="http://foo:bar@www.bar.com">http://foo:bar@www.bar.com</a>',
57 57 'ftp://foo.bar' => '<a class="external" href="ftp://foo.bar">ftp://foo.bar</a>',
58 58 'ftps://foo.bar' => '<a class="external" href="ftps://foo.bar">ftps://foo.bar</a>',
59 59 'sftp://foo.bar' => '<a class="external" href="sftp://foo.bar">sftp://foo.bar</a>',
60 60 # two exclamation marks
61 61 '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 62 }
63 63 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
64 64 end
65 65
66 66 def test_auto_mailto
67 67 assert_equal '<p><a href="mailto:test@foo.bar" class="email">test@foo.bar</a></p>',
68 68 textilizable('test@foo.bar')
69 69 end
70 70
71 71 def test_inline_images
72 72 to_test = {
73 73 '!http://foo.bar/image.jpg!' => '<img src="http://foo.bar/image.jpg" alt="" />',
74 74 'floating !>http://foo.bar/image.jpg!' => 'floating <div style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></div>',
75 75 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="some-class" alt="" />',
76 76 # inline styles should be stripped
77 77 'with style !{width:100px;height100px}http://foo.bar/image.jpg!' => 'with style <img src="http://foo.bar/image.jpg" alt="" />',
78 78 '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" />',
79 79 '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;" />',
80 80 }
81 81 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
82 82 end
83 83
84 84 def test_acronyms
85 85 to_test = {
86 86 'this is an acronym: GPL(General Public License)' => 'this is an acronym: <acronym title="General Public License">GPL</acronym>',
87 87 'GPL(This is a double-quoted "title")' => '<acronym title="This is a double-quoted &quot;title&quot;">GPL</acronym>',
88 88 }
89 89 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
90 90
91 91 end
92 92
93 93 def test_attached_images
94 94 to_test = {
95 95 'Inline image: !logo.gif!' => 'Inline image: <img src="/attachments/download/3" title="This is a logo" alt="This is a logo" />',
96 96 'Inline image: !logo.GIF!' => 'Inline image: <img src="/attachments/download/3" title="This is a logo" alt="This is a logo" />',
97 97 'No match: !ogo.gif!' => 'No match: <img src="ogo.gif" alt="" />',
98 98 'No match: !ogo.GIF!' => 'No match: <img src="ogo.GIF" alt="" />'
99 99 }
100 100 attachments = Attachment.find(:all)
101 101 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
102 102 end
103 103
104 104 def test_textile_external_links
105 105 to_test = {
106 106 'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>',
107 107 'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>',
108 108 '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>',
109 109 '"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>',
110 110 "This is not a \"Link\":\n\nAnother paragraph" => "This is not a \"Link\":</p>\n\n\n\t<p>Another paragraph",
111 111 # no multiline link text
112 112 "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 />\nand another on a second line\":test",
113 113 # mailto link
114 114 "\"system administrator\":mailto:sysadmin@example.com?subject=redmine%20permissions" => "<a href=\"mailto:sysadmin@example.com?subject=redmine%20permissions\">system administrator</a>",
115 115 # two exclamation marks
116 116 '"a link":http://example.net/path!602815048C7B5C20!302.html' => '<a href="http://example.net/path!602815048C7B5C20!302.html" class="external">a link</a>',
117 117 }
118 118 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
119 119 end
120 120
121 121 def test_redmine_links
122 122 issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3},
123 123 :class => 'issue', :title => 'Error 281 when updating a recipe (New)')
124 124
125 125 changeset_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
126 126 :class => 'changeset', :title => 'My very first commit')
127 changeset_link2 = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
128 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
127 129
128 130 document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1},
129 131 :class => 'document')
130 132
131 133 version_link = link_to('1.0', {:controller => 'versions', :action => 'show', :id => 2},
132 134 :class => 'version')
133 135
134 136 message_url = {:controller => 'messages', :action => 'show', :board_id => 1, :id => 4}
135 137
136 138 source_url = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}
137 139 source_url_with_ext = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file.ext']}
138 140
139 141 to_test = {
140 142 # tickets
141 143 '#3, #3 and #3.' => "#{issue_link}, #{issue_link} and #{issue_link}.",
142 144 # changesets
143 145 'r1' => changeset_link,
146 'r1.' => "#{changeset_link}.",
147 'r1, r2' => "#{changeset_link}, #{changeset_link2}",
148 'r1,r2' => "#{changeset_link},#{changeset_link2}",
144 149 # documents
145 150 'document#1' => document_link,
146 151 'document:"Test document"' => document_link,
147 152 # versions
148 153 'version#2' => version_link,
149 154 'version:1.0' => version_link,
150 155 'version:"1.0"' => version_link,
151 156 # source
152 157 'source:/some/file' => link_to('source:/some/file', source_url, :class => 'source'),
153 158 'source:/some/file.' => link_to('source:/some/file', source_url, :class => 'source') + ".",
154 159 'source:/some/file.ext.' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
155 160 'source:/some/file. ' => link_to('source:/some/file', source_url, :class => 'source') + ".",
156 161 'source:/some/file.ext. ' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
157 162 'source:/some/file, ' => link_to('source:/some/file', source_url, :class => 'source') + ",",
158 163 'source:/some/file@52' => link_to('source:/some/file@52', source_url.merge(:rev => 52), :class => 'source'),
159 164 'source:/some/file.ext@52' => link_to('source:/some/file.ext@52', source_url_with_ext.merge(:rev => 52), :class => 'source'),
160 165 'source:/some/file#L110' => link_to('source:/some/file#L110', source_url.merge(:anchor => 'L110'), :class => 'source'),
161 166 'source:/some/file.ext#L110' => link_to('source:/some/file.ext#L110', source_url_with_ext.merge(:anchor => 'L110'), :class => 'source'),
162 167 'source:/some/file@52#L110' => link_to('source:/some/file@52#L110', source_url.merge(:rev => 52, :anchor => 'L110'), :class => 'source'),
163 168 'export:/some/file' => link_to('export:/some/file', source_url.merge(:format => 'raw'), :class => 'source download'),
164 169 # message
165 170 'message#4' => link_to('Post 2', message_url, :class => 'message'),
166 171 'message#5' => link_to('RE: post 2', message_url.merge(:anchor => 'message-5'), :class => 'message'),
167 172 # escaping
168 173 '!#3.' => '#3.',
169 174 '!r1' => 'r1',
170 175 '!document#1' => 'document#1',
171 176 '!document:"Test document"' => 'document:"Test document"',
172 177 '!version#2' => 'version#2',
173 178 '!version:1.0' => 'version:1.0',
174 179 '!version:"1.0"' => 'version:"1.0"',
175 180 '!source:/some/file' => 'source:/some/file',
176 181 # invalid expressions
177 182 'source:' => 'source:',
178 183 # url hash
179 184 "http://foo.bar/FAQ#3" => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
180 185 }
181 186 @project = Project.find(1)
182 187 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
183 188 end
184 189
185 190 def test_wiki_links
186 191 to_test = {
187 192 '[[CookBook documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a>',
188 193 '[[Another page|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a>',
189 194 # link with anchor
190 195 '[[CookBook documentation#One-section]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation#One-section" class="wiki-page">CookBook documentation</a>',
191 196 '[[Another page#anchor|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page#anchor" class="wiki-page">Page</a>',
192 197 # page that doesn't exist
193 198 '[[Unknown page]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
194 199 '[[Unknown page|404]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">404</a>',
195 200 # link to another project wiki
196 201 '[[onlinestore:]]' => '<a href="/projects/onlinestore/wiki/" class="wiki-page">onlinestore</a>',
197 202 '[[onlinestore:|Wiki]]' => '<a href="/projects/onlinestore/wiki/" class="wiki-page">Wiki</a>',
198 203 '[[onlinestore:Start page]]' => '<a href="/projects/onlinestore/wiki/Start_page" class="wiki-page">Start page</a>',
199 204 '[[onlinestore:Start page|Text]]' => '<a href="/projects/onlinestore/wiki/Start_page" class="wiki-page">Text</a>',
200 205 '[[onlinestore:Unknown page]]' => '<a href="/projects/onlinestore/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
201 206 # striked through link
202 207 '-[[Another page|Page]]-' => '<del><a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a></del>',
203 208 '-[[Another page|Page]] link-' => '<del><a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a> link</del>',
204 209 # escaping
205 210 '![[Another page|Page]]' => '[[Another page|Page]]',
206 211 # project does not exist
207 212 '[[unknowproject:Start]]' => '[[unknowproject:Start]]',
208 213 '[[unknowproject:Start|Page title]]' => '[[unknowproject:Start|Page title]]',
209 214 }
210 215 @project = Project.find(1)
211 216 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
212 217 end
213 218
214 219 def test_html_tags
215 220 to_test = {
216 221 "<div>content</div>" => "<p>&lt;div&gt;content&lt;/div&gt;</p>",
217 222 "<div class=\"bold\">content</div>" => "<p>&lt;div class=\"bold\"&gt;content&lt;/div&gt;</p>",
218 223 "<script>some script;</script>" => "<p>&lt;script&gt;some script;&lt;/script&gt;</p>",
219 224 # do not escape pre/code tags
220 225 "<pre>\nline 1\nline2</pre>" => "<pre>\nline 1\nline2</pre>",
221 226 "<pre><code>\nline 1\nline2</code></pre>" => "<pre><code>\nline 1\nline2</code></pre>",
222 227 "<pre><div>content</div></pre>" => "<pre>&lt;div&gt;content&lt;/div&gt;</pre>",
223 228 "HTML comment: <!-- no comments -->" => "<p>HTML comment: &lt;!-- no comments --&gt;</p>",
224 229 "<!-- opening comment" => "<p>&lt;!-- opening comment</p>",
225 230 # remove attributes except class
226 231 "<pre class='foo'>some text</pre>" => "<pre class='foo'>some text</pre>",
227 232 "<pre onmouseover='alert(1)'>some text</pre>" => "<pre>some text</pre>",
228 233 }
229 234 to_test.each { |text, result| assert_equal result, textilizable(text) }
230 235 end
231 236
232 237 def test_allowed_html_tags
233 238 to_test = {
234 239 "<pre>preformatted text</pre>" => "<pre>preformatted text</pre>",
235 240 "<notextile>no *textile* formatting</notextile>" => "no *textile* formatting",
236 241 "<notextile>this is <tag>a tag</tag></notextile>" => "this is &lt;tag&gt;a tag&lt;/tag&gt;"
237 242 }
238 243 to_test.each { |text, result| assert_equal result, textilizable(text) }
239 244 end
240 245
241 246 def syntax_highlight
242 247 raw = <<-RAW
243 248 <pre><code class="ruby">
244 249 # Some ruby code here
245 250 </pre></code>
246 251 RAW
247 252
248 253 expected = <<-EXPECTED
249 254 <pre><code class="ruby CodeRay"><span class="no">1</span> <span class="c"># Some ruby code here</span>
250 255 </pre></code>
251 256 EXPECTED
252 257
253 258 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
254 259 end
255 260
256 261 def test_wiki_links_in_tables
257 262 to_test = {"|[[Page|Link title]]|[[Other Page|Other title]]|\n|Cell 21|[[Last page]]|" =>
258 263 '<tr><td><a href="/projects/ecookbook/wiki/Page" class="wiki-page new">Link title</a></td>' +
259 264 '<td><a href="/projects/ecookbook/wiki/Other_Page" class="wiki-page new">Other title</a></td>' +
260 265 '</tr><tr><td>Cell 21</td><td><a href="/projects/ecookbook/wiki/Last_page" class="wiki-page new">Last page</a></td></tr>'
261 266 }
262 267 @project = Project.find(1)
263 268 to_test.each { |text, result| assert_equal "<table>#{result}</table>", textilizable(text).gsub(/[\t\n]/, '') }
264 269 end
265 270
266 271 def test_text_formatting
267 272 to_test = {'*_+bold, italic and underline+_*' => '<strong><em><ins>bold, italic and underline</ins></em></strong>',
268 273 '(_text within parentheses_)' => '(<em>text within parentheses</em>)',
269 274 'a *Humane Web* Text Generator' => 'a <strong>Humane Web</strong> Text Generator',
270 275 '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>',
271 276 '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',
272 277 }
273 278 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
274 279 end
275 280
276 281 def test_wiki_horizontal_rule
277 282 assert_equal '<hr />', textilizable('---')
278 283 assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---')
279 284 end
280 285
281 286 def test_acronym
282 287 assert_equal '<p>This is an acronym: <acronym title="American Civil Liberties Union">ACLU</acronym>.</p>',
283 288 textilizable('This is an acronym: ACLU(American Civil Liberties Union).')
284 289 end
285 290
286 291 def test_footnotes
287 292 raw = <<-RAW
288 293 This is some text[1].
289 294
290 295 fn1. This is the foot note
291 296 RAW
292 297
293 298 expected = <<-EXPECTED
294 299 <p>This is some text<sup><a href=\"#fn1\">1</a></sup>.</p>
295 300 <p id="fn1" class="footnote"><sup>1</sup> This is the foot note</p>
296 301 EXPECTED
297 302
298 303 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
299 304 end
300 305
301 306 def test_table_of_content
302 307 raw = <<-RAW
303 308 {{toc}}
304 309
305 310 h1. Title
306 311
307 312 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
308 313
309 314 h2. Subtitle with a [[Wiki]] link
310 315
311 316 Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
312 317
313 318 h2. Subtitle with [[Wiki|another Wiki]] link
314 319
315 320 h2. Subtitle with %{color:red}red text%
316 321
317 322 h1. Another title
318 323
319 324 RAW
320 325
321 326 expected = '<ul class="toc">' +
322 327 '<li class="heading1"><a href="#Title">Title</a></li>' +
323 328 '<li class="heading2"><a href="#Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' +
324 329 '<li class="heading2"><a href="#Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' +
325 330 '<li class="heading2"><a href="#Subtitle-with-red-text">Subtitle with red text</a></li>' +
326 331 '<li class="heading1"><a href="#Another-title">Another title</a></li>' +
327 332 '</ul>'
328 333
329 334 assert textilizable(raw).gsub("\n", "").include?(expected)
330 335 end
331 336
332 337 def test_blockquote
333 338 # orig raw text
334 339 raw = <<-RAW
335 340 John said:
336 341 > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
337 342 > Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
338 343 > * Donec odio lorem,
339 344 > * sagittis ac,
340 345 > * malesuada in,
341 346 > * adipiscing eu, dolor.
342 347 >
343 348 > >Nulla varius pulvinar diam. Proin id arcu id lorem scelerisque condimentum. Proin vehicula turpis vitae lacus.
344 349 > Proin a tellus. Nam vel neque.
345 350
346 351 He's right.
347 352 RAW
348 353
349 354 # expected html
350 355 expected = <<-EXPECTED
351 356 <p>John said:</p>
352 357 <blockquote>
353 358 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
354 359 Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
355 360 <ul>
356 361 <li>Donec odio lorem,</li>
357 362 <li>sagittis ac,</li>
358 363 <li>malesuada in,</li>
359 364 <li>adipiscing eu, dolor.</li>
360 365 </ul>
361 366 <blockquote>
362 367 <p>Nulla varius pulvinar diam. Proin id arcu id lorem scelerisque condimentum. Proin vehicula turpis vitae lacus.</p>
363 368 </blockquote>
364 369 <p>Proin a tellus. Nam vel neque.</p>
365 370 </blockquote>
366 371 <p>He's right.</p>
367 372 EXPECTED
368 373
369 374 assert_equal expected.gsub(%r{\s+}, ''), textilizable(raw).gsub(%r{\s+}, '')
370 375 end
371 376
372 377 def test_table
373 378 raw = <<-RAW
374 379 This is a table with empty cells:
375 380
376 381 |cell11|cell12||
377 382 |cell21||cell23|
378 383 |cell31|cell32|cell33|
379 384 RAW
380 385
381 386 expected = <<-EXPECTED
382 387 <p>This is a table with empty cells:</p>
383 388
384 389 <table>
385 390 <tr><td>cell11</td><td>cell12</td><td></td></tr>
386 391 <tr><td>cell21</td><td></td><td>cell23</td></tr>
387 392 <tr><td>cell31</td><td>cell32</td><td>cell33</td></tr>
388 393 </table>
389 394 EXPECTED
390 395
391 396 assert_equal expected.gsub(%r{\s+}, ''), textilizable(raw).gsub(%r{\s+}, '')
392 397 end
393 398
394 399 def test_default_formatter
395 400 Setting.text_formatting = 'unknown'
396 401 text = 'a *link*: http://www.example.net/'
397 402 assert_equal '<p>a *link*: <a href="http://www.example.net/">http://www.example.net/</a></p>', textilizable(text)
398 403 Setting.text_formatting = 'textile'
399 404 end
400 405
401 406 def test_due_date_distance_in_words
402 407 to_test = { Date.today => 'Due in 0 days',
403 408 Date.today + 1 => 'Due in 1 day',
404 409 Date.today + 100 => 'Due in about 3 months',
405 410 Date.today + 20000 => 'Due in over 55 years',
406 411 Date.today - 1 => '1 day late',
407 412 Date.today - 100 => 'about 3 months late',
408 413 Date.today - 20000 => 'over 55 years late',
409 414 }
410 415 to_test.each do |date, expected|
411 416 assert_equal expected, due_date_distance_in_words(date)
412 417 end
413 418 end
414 419
415 420 def test_avatar
416 421 # turn on avatars
417 422 Setting.gravatar_enabled = '1'
418 423 assert avatar(User.find_by_mail('jsmith@somenet.foo')).include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
419 424 assert avatar('jsmith <jsmith@somenet.foo>').include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
420 425 assert_nil avatar('jsmith')
421 426 assert_nil avatar(nil)
422 427
423 428 # turn off avatars
424 429 Setting.gravatar_enabled = '0'
425 430 assert_nil avatar(User.find_by_mail('jsmith@somenet.foo'))
426 431 end
427 432 end
General Comments 0
You need to be logged in to leave comments. Login now