From 7cfbc3d9f81d28b773632d8a4dfbde3d9b0a24e4 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Wed, 13 Feb 2019 09:10:29 +0100 Subject: [PATCH] Allow translation content styling through scrubber --- app/scrubbers/translation_content_scrubber.rb | 13 +++++++++++++ app/views/admin/translations/show.html.erb | 2 +- app/views/problems/show.html.erb | 2 +- test/controllers/admin/dashboard_controller_test.rb | 4 ++-- test/controllers/problems_controller_test.rb | 8 ++++++++ test/fixtures/problems.yml | 4 ++++ test/fixtures/translations.yml | 6 ++++++ test/models/problem_test.rb | 4 ++-- 8 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 app/scrubbers/translation_content_scrubber.rb diff --git a/app/scrubbers/translation_content_scrubber.rb b/app/scrubbers/translation_content_scrubber.rb new file mode 100644 index 0000000..2c17b50 --- /dev/null +++ b/app/scrubbers/translation_content_scrubber.rb @@ -0,0 +1,13 @@ +class TranslationContentScrubber < Rails::Html::PermitScrubber + def initialize + super + self.tags = %w( strong em b i p code pre tt samp kbd var sub + sup dfn cite big small address hr br div span h1 h2 h3 h4 h5 h6 ul ol li dl dt dd abbr + acronym a img blockquote del ins ) + self.attributes = %w( href src width height alt cite datetime title class name xml:lang abbr style ) + end + + def skip_node?(node) + node.text? + end +end diff --git a/app/views/admin/translations/show.html.erb b/app/views/admin/translations/show.html.erb index 0c96aac..efe40c1 100644 --- a/app/views/admin/translations/show.html.erb +++ b/app/views/admin/translations/show.html.erb @@ -23,7 +23,7 @@ <%= panel do %>
- <%= sanitize @translation.content %> + <%= sanitize @translation.content, scrubber: TranslationContentScrubber.new %>
<% end %>
diff --git a/app/views/problems/show.html.erb b/app/views/problems/show.html.erb index 246f0bf..a49fd66 100644 --- a/app/views/problems/show.html.erb +++ b/app/views/problems/show.html.erb @@ -11,7 +11,7 @@ <% end %> <%= panel do %>
- <%= sanitize @problem.content %> + <%= sanitize @problem.content, scrubber: TranslationContentScrubber.new %>
<% end %>
diff --git a/test/controllers/admin/dashboard_controller_test.rb b/test/controllers/admin/dashboard_controller_test.rb index fa89bbd..4a17bee 100644 --- a/test/controllers/admin/dashboard_controller_test.rb +++ b/test/controllers/admin/dashboard_controller_test.rb @@ -27,9 +27,9 @@ class Admin::DashboardControllerTest < ActionDispatch::IntegrationTest test "should fail incorrect problem count" do login_admin - post admin_dashboard_update_problem_count_url(problem_count: 2) + post admin_dashboard_update_problem_count_url(problem_count: 3) assert_redirected_to admin_dashboard_index_url - assert_equal 3, Problem.count + assert_equal 4, Problem.count end end diff --git a/test/controllers/problems_controller_test.rb b/test/controllers/problems_controller_test.rb index 788fe69..8c75e71 100644 --- a/test/controllers/problems_controller_test.rb +++ b/test/controllers/problems_controller_test.rb @@ -22,4 +22,12 @@ class ProblemsControllerTest < ActionDispatch::IntegrationTest assert_redirected_to problem_path(id: 2) end + test "should contain HTML styles in translation content" do + get problem_url(id: 4) + assert_response :success + assert_select ".problem-content" do + assert_select "b", 1 + assert_select 'p[style="text-align:center;"]', 1 + end + end end diff --git a/test/fixtures/problems.yml b/test/fixtures/problems.yml index 48dcf94..a8a370c 100644 --- a/test/fixtures/problems.yml +++ b/test/fixtures/problems.yml @@ -11,3 +11,7 @@ two: three: id: 3 +four: + id: 4 + translation: translation_with_html + diff --git a/test/fixtures/translations.yml b/test/fixtures/translations.yml index 2122ecd..c5e6e33 100644 --- a/test/fixtures/translations.yml +++ b/test/fixtures/translations.yml @@ -17,3 +17,9 @@ translation_two_alternative: title: Second title content: The changed content for the second problem status: 0 + +translation_with_html: + problem_id: 4 + title: Some title + content: 'p>This is some custom text.

This is centered.

' + status: 1 \ No newline at end of file diff --git a/test/models/problem_test.rb b/test/models/problem_test.rb index eff3db1..2f042a8 100644 --- a/test/models/problem_test.rb +++ b/test/models/problem_test.rb @@ -26,7 +26,7 @@ class ProblemTest < ActiveSupport::TestCase end test "should return correct number of translated problems" do - assert_equal 2, Problem.translated_count + assert_equal 3, Problem.translated_count end test "should allow for problem count updating" do @@ -44,7 +44,7 @@ class ProblemTest < ActiveSupport::TestCase assert_raises ArgumentError do Problem.update_count(1) end - assert_equal 3, Problem.count() + assert_equal 4, Problem.count() assert_equal "First title", Problem.find(1).title end end