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