diff --git a/app/models/concerns/post_normalizations.rb b/app/models/concerns/post_normalizations.rb new file mode 100644 index 000000000..2533a1a4a --- /dev/null +++ b/app/models/concerns/post_normalizations.rb @@ -0,0 +1,9 @@ +module PostNormalizations + extend ActiveSupport::Concern + + class_methods do + def normalize_newlines(text) + text.encode(text.encoding, universal_newline: true).strip + end + end +end diff --git a/app/models/post.rb b/app/models/post.rb index 1d3012a9b..fa5bb9ba8 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,6 +1,7 @@ class Post < ApplicationRecord include CommunityRelated include Lockable + include PostNormalizations include PostValidations include SoftDeletable include Timestamped diff --git a/app/models/post_history.rb b/app/models/post_history.rb index 2190ac642..5e6d25d33 100644 --- a/app/models/post_history.rb +++ b/app/models/post_history.rb @@ -1,4 +1,5 @@ class PostHistory < ApplicationRecord + include PostNormalizations include PostRelated include EditsValidations @@ -11,11 +12,7 @@ class PostHistory < ApplicationRecord scope :of_type, ->(name) { joins(:post_history_type).where(post_history_types: { name: name }) } scope :on_undeleted, -> { joins(:post).where(posts: { deleted: false }) } - normalize_newlines = lambda { |text| - text.encode(text.encoding, universal_newline: true) - } - normalizes :before_state, with: normalize_newlines - normalizes :after_state, with: normalize_newlines + normalizes :before_state, :after_state, with: ->(text) { normalize_newlines(text) } def before_tags tags.where(post_history_tags: { relationship: 'before' }) diff --git a/app/views/post_history/_diff.html.erb b/app/views/post_history/_diff.html.erb index 77eb63d7b..fa5b7944d 100644 --- a/app/views/post_history/_diff.html.erb +++ b/app/views/post_history/_diff.html.erb @@ -1,7 +1,9 @@
<% if before.present? && after.present? %> <% if before.is_a?(String) && after.is_a?(String) %> - <% diff = Diffy::SplitDiff.new(before, after, format: :html, ignore_crlf: true) %> + <% before = Post.normalize_newlines(before) %> + <% after = Post.normalize_newlines(after) %> + <% diff = Diffy::SplitDiff.new(before, after, format: :html) %>
<%= raw(diff.left) %> diff --git a/test/controllers/posts/update_test.rb b/test/controllers/posts/update_test.rb index 88e512dbb..98c2c3891 100644 --- a/test/controllers/posts/update_test.rb +++ b/test/controllers/posts/update_test.rb @@ -77,7 +77,7 @@ class PostsControllerTest < ActionController::TestCase post = posts(:question_three) before_history = PostHistory.where(post: post).count bm = post.body_markdown - body_markdown_with_crlf = bm.encode(bm.encoding, normalize_newlines: true).split("\n").join("\r\n") + body_markdown_with_crlf = bm.encode(bm.encoding, universal_newline: true).split("\n").join("\r\n") patch :update, params: { id: post.id, post: { title: post.title, body_markdown: body_markdown_with_crlf,