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