diff --git a/app/controllers/assignments_controller.rb b/app/controllers/assignments_controller.rb index b8d78adb7e..1484770d36 100644 --- a/app/controllers/assignments_controller.rb +++ b/app/controllers/assignments_controller.rb @@ -2,6 +2,7 @@ class AssignmentsController < ApplicationController include ActionView::Helpers::SanitizeHelper + include SanitizeDescription before_action :authenticate_user! before_action :set_assignment, only: %i[show edit update destroy start reopen close] @@ -182,10 +183,6 @@ def check_access end def sanitize_assignment_description - @assignment.description = sanitize( - @assignment.description, - tags: %w[img p strong em a sup sub del u span h1 h2 h3 h4 hr li ol ul blockquot], - attributes: %w[style src href alt title target] - ) + @assignment.description = sanitize_description(@assignment.description) end end diff --git a/app/controllers/concerns/sanitize_description.rb b/app/controllers/concerns/sanitize_description.rb new file mode 100644 index 0000000000..4bb06bb276 --- /dev/null +++ b/app/controllers/concerns/sanitize_description.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module SanitizeDescription + extend ActiveSupport::Concern + + def sanitize_description(description) + sanitize( + description, + tags: %w[img p strong em a sup sub del u span h1 h2 h3 h4 hr li ol ul blockquote br], + attributes: %w[style src href alt title target] + ) + end +end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 4e7a05ae21..40ddfe46d7 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -2,6 +2,7 @@ class ProjectsController < ApplicationController include ActionView::Helpers::SanitizeHelper + include SanitizeDescription before_action :set_project, only: %i[show edit update destroy create_fork change_stars] before_action :authenticate_user!, only: %i[edit update destroy create_fork change_stars] @@ -139,10 +140,6 @@ def sanitize_name # Sanitize description before passing to view def sanitize_project_description - @project.description = sanitize( - @project.description, - tags: %w[img p strong em a sup sub del u span h1 h2 h3 h4 hr li ol ul blockquote], - attributes: %w[style src href alt title target] - ) + @project.description = sanitize_description(@project.description) end end diff --git a/spec/controllers/assignments_controller_spec.rb b/spec/controllers/assignments_controller_spec.rb index 3cdddecd4e..2a8ef5b01e 100644 --- a/spec/controllers/assignments_controller_spec.rb +++ b/spec/controllers/assignments_controller_spec.rb @@ -84,17 +84,17 @@ let(:update_params) do { assignment: { - description: "updated description" + description: "updated description
with line break" } } end context "mentor is signed in" do - it "updates the assignment" do + it "updates the assignment and description contains line breaks" do sign_in @mentor put group_assignment_path(@group, @assignment), params: update_params @assignment.reload - expect(@assignment.description).to eq("updated description") + expect(@assignment.description).to eq("updated description
with line break") end end