diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index cb85539..8f7eced 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1816,6 +1816,12 @@ module ApplicationHelper @view_layouts_base_sidebar_hook_response ||= call_hook(:view_layouts_base_sidebar) end + # The CSS classes that plugins add to an element through +hook+, as a plain + # (not html_safe) String so that it is escaped in the attribute + def css_classes_from_hook(hook, context={}) + String.new(call_hook(hook, context)).split.join(' ') + end + def view_layouts_base_sidebar_top_hook_response @view_layouts_base_sidebar_top_hook_response ||= call_hook(:view_layouts_base_sidebar_top) end diff --git a/app/views/issues/_list.html.erb b/app/views/issues/_list.html.erb index b181195..7608762 100644 --- a/app/views/issues/_list.html.erb +++ b/app/views/issues/_list.html.erb @@ -35,7 +35,7 @@ <% end %> - "> + <%= css_classes_from_hook(:view_issues_list_row_css_classes, {:issue => issue, :query => query}) %>"> <%= check_box_tag("ids[]", issue.id, false, :id => nil) %> <% query.inline_columns.each do |column| %> <%= content_tag('td', column_content(column, issue), :class => column.css_classes) %> @@ -54,6 +54,7 @@ <% end -%> <% end -%> + <%= call_hook(:view_issues_list_row_after, {:issue => issue, :query => query, :colspan => query.inline_columns.size + 2}) %> <% end -%> diff --git a/app/views/projects/_list.html.erb b/app/views/projects/_list.html.erb index 2f5c83f..c749d47 100644 --- a/app/views/projects/_list.html.erb +++ b/app/views/projects/_list.html.erb @@ -40,7 +40,7 @@ <% end %> <% end %> - "> + <%= css_classes_from_hook(:view_projects_list_row_css_classes, {:project => entry, :query => @query}) %>"> <% if @admin_list %> <% if !entry.scheduled_for_deletion? %> <%= check_box_tag("ids[]", entry.id, false, :id => nil) %> @@ -59,6 +59,7 @@ <% end %> <% end %> + <%= call_hook(:view_projects_list_row_after, {:project => entry, :query => @query, :colspan => @query.inline_columns.size + (@admin_list ? 2 : 0)}) %> <% end -%> diff --git a/app/views/users/_list.html.erb b/app/views/users/_list.html.erb index b5d85c6..64c4b58 100644 --- a/app/views/users/_list.html.erb +++ b/app/views/users/_list.html.erb @@ -31,7 +31,7 @@ <% end %> - <%= user.css_classes %>"> + <%= user.css_classes %> <%= css_classes_from_hook(:view_users_list_row_css_classes, {:user => user, :query => @query}) %>"> <%= check_box_tag("ids[]", user.id, false, id: nil) %> <% @query.inline_columns.each do |column| %> <% if column.name == :login %> @@ -56,6 +56,7 @@ <% end -%> <% end -%> + <%= call_hook(:view_users_list_row_after, {:user => user, :query => @query, :colspan => @query.inline_columns.size + 2}) %> <% end -%> diff --git a/doc/HOOKS b/doc/HOOKS index c5b486e..ca232ff 100644 --- a/doc/HOOKS +++ b/doc/HOOKS @@ -301,6 +301,16 @@ view_issues_index_contextual Context: :project, :query Called in: app/views/issues/index.html.erb +view_issues_list_row_after + Since: 7.1.0 + Context: :issue, :query, :colspan + Called in: app/views/issues/_list.html.erb + +view_issues_list_row_css_classes + Since: 7.1.0 + Context: :issue, :query + Called in: app/views/issues/_list.html.erb + view_issues_new_top Since: - Context: :issue @@ -491,6 +501,16 @@ view_projects_index_contextual Context: :query Called in: app/views/projects/index.html.erb +view_projects_list_row_after + Since: 7.1.0 + Context: :project, :query, :colspan + Called in: app/views/projects/_list.html.erb + +view_projects_list_row_css_classes + Since: 7.1.0 + Context: :project, :query + Called in: app/views/projects/_list.html.erb + view_projects_roadmap_version_bottom Since: - Context: :version @@ -649,6 +669,16 @@ view_users_index_contextual Context: :query Called in: app/views/users/index.html.erb +view_users_list_row_after + Since: 7.1.0 + Context: :user, :query, :colspan + Called in: app/views/users/_list.html.erb + +view_users_list_row_css_classes + Since: 7.1.0 + Context: :user, :query + Called in: app/views/users/_list.html.erb + view_users_memberships_table_header Since: - Context: :user diff --git a/test/integration/lib/redmine/view_hooks_test.rb b/test/integration/lib/redmine/view_hooks_test.rb index 74d2c06..2b9352b 100644 --- a/test/integration/lib/redmine/view_hooks_test.rb +++ b/test/integration/lib/redmine/view_hooks_test.rb @@ -29,7 +29,7 @@ class ViewHooksTest < Redmine::IntegrationTest view_layouts_base_header_top view_layouts_base_header_bottom view_layouts_base_sidebar_top view_layouts_base_content_top view_layouts_base_footer_top view_layouts_base_footer_bottom view_projects_show_contextual view_projects_show_actions_menu), - '/projects/ecookbook/issues' => %w(view_issues_index_contextual view_issues_index_actions_menu), + '/projects/ecookbook/issues' => %w(view_issues_index_contextual view_issues_index_actions_menu view_issues_list_row_after), '/issues/1' => %w( view_issues_show_contextual view_issues_show_actions_menu view_issues_show_top view_issues_show_details_end view_issues_show_bottom), @@ -37,8 +37,8 @@ class ViewHooksTest < Redmine::IntegrationTest view_issues_form_after_subject view_issues_form_after_description view_issues_form_after_attributes view_issues_form_custom_fields_top view_issues_form_custom_fields_bottom), '/projects/ecookbook/settings' => %w(view_projects_form_top view_projects_form_bottom), - '/projects?display_type=list' => %w(view_projects_index_contextual), - '/users' => %w(view_users_index_contextual view_users_index_actions_menu), + '/projects?display_type=list' => %w(view_projects_index_contextual view_projects_list_row_after), + '/users' => %w(view_users_index_contextual view_users_index_actions_menu view_users_list_row_after), '/users/2' => %w(view_users_show_contextual), '/users/2/edit' => %w( view_layouts_base_title_after view_users_form_top view_users_form_after_authentication @@ -58,14 +58,26 @@ class ViewHooksTest < Redmine::IntegrationTest '/trackers/1/edit' => %w(view_trackers_form_top view_trackers_form_bottom), '/admin/plugins' => %w(view_layouts_base_title_after) } + ROW_CLASSES = { + '/projects/ecookbook/issues' => 'view_issues_list_row_css_classes', + '/projects?display_type=list' => 'view_projects_list_row_css_classes', + '/users' => 'view_users_list_row_css_classes' + } # Answers every hook with a marker that is valid where the hook is called class MarkerListener < Redmine::Hook::ViewListener - PAGES.values.flatten.uniq.each do |hook| + (PAGES.values.flatten - ROW_CLASSES.values).uniq.each do |hook| define_method(hook) do |context| - content_tag('span', 'marker', :class => "hook-#{hook}") + if hook.end_with?('_row_after') + content_tag('tr', content_tag('td', 'marker', :colspan => context[:colspan]), :class => "hook-#{hook}") + else + content_tag('span', 'marker', :class => "hook-#{hook}") + end end end + ROW_CLASSES.each_value do |hook| + define_method(hook) {|context| "hook-#{hook}"} + end end class DetailRenderer @@ -102,6 +114,10 @@ class ViewHooksTest < Redmine::IntegrationTest assert_select ".hook-#{hook}", {:minimum => 1}, "#{hook} not called on #{path}" end end + ROW_CLASSES.each do |path, hook| + get path + assert_select "tr.hook-#{hook}", {:minimum => 1}, "#{hook} not called on #{path}" + end end def test_contextual_hook_should_be_before_the_actions_menu_and_the_menu_hook_inside_it @@ -137,6 +153,24 @@ class ViewHooksTest < Redmine::IntegrationTest assert_select 'h2 + span.hook-view_layouts_base_title_after' end + def test_lists_should_have_no_extra_rows_without_listeners + {'/projects/ecookbook/issues' => 'tr.issue', '/projects?display_type=list' => 'tr[id^="project-"]', + '/users' => 'tr[id^="user-"]'}.each do |path, row| + get path + assert_equal css_select("table.list tbody #{row}").size, css_select('table.list tbody > tr').size, path + end + end + + def test_row_css_classes_should_be_escaped + listener = Class.new(Redmine::Hook::ViewListener) do + define_method(:view_issues_list_row_css_classes) {|context| '">'} + end + Redmine::Hook.add_listener(listener) + get '/projects/ecookbook/issues' + assert_response :success + assert_not_includes response.body, '' + end + def test_registered_tabs_should_be_shown # The label is a locale key, as for core's tabs Redmine::Tabs.register :project_settings, :sample, :partial => 'common/preview', :label => :label_preview, :after => :members diff --git a/test/unit/lib/redmine/hook_list_test.rb b/test/unit/lib/redmine/hook_list_test.rb index 87c5c96..80ab103 100644 --- a/test/unit/lib/redmine/hook_list_test.rb +++ b/test/unit/lib/redmine/hook_list_test.rb @@ -27,7 +27,8 @@ class Redmine::HookListTest < ActiveSupport::TestCase def called_hooks Dir.glob(Rails.root.join('{app,lib}/**/*.{erb,rb}')).flat_map do |path| - File.read(path).scan(/call_hook\(?\s*:([a-z0-9_]+)/).flatten + # css_classes_from_hook calls a hook that answers with CSS classes + File.read(path).scan(/(?:call_hook|css_classes_from_hook)\(?\s*:([a-z0-9_]+)/).flatten end.uniq end