From f4208ae83fd43e0cdd663d82a73fabfb65f327bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 12 Aug 2014 00:01:58 +0200 Subject: [PATCH] FEATURE: normalize whitespaces in topic title/post content --- lib/post_creator.rb | 2 ++ lib/post_revisor.rb | 5 ++++- lib/text_cleaner.rb | 8 ++++++++ spec/components/text_cleaner_spec.rb | 8 ++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 4d6433a89e3..a0145bbc778 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -203,6 +203,8 @@ class PostCreator end def setup_post + @opts[:raw] = TextCleaner.normalize_whitespaces(@opts[:raw]).strip + post = @topic.posts.new(raw: @opts[:raw], user: @user, reply_to_post_number: @opts[:reply_to_post_number]) diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index d33f2b36768..dea3b55a4e8 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -17,7 +17,10 @@ class PostRevisor # :skip_validation ask ActiveRecord to skip validations # def revise!(editor, new_raw, opts = {}) - @editor, @new_raw, @opts = editor, new_raw, opts + @editor = editor + @opts = opts + @new_raw = TextCleaner.normalize_whitespaces(new_raw).strip + return false unless should_revise? @post.acting_user = @editor revise_post diff --git a/lib/text_cleaner.rb b/lib/text_cleaner.rb index a74ee4ead69..da12624bc95 100644 --- a/lib/text_cleaner.rb +++ b/lib/text_cleaner.rb @@ -36,10 +36,18 @@ class TextCleaner text.sub!(/\s+([!?]\s*)\z/, '\1') if opts[:remove_extraneous_space] # Fixes interior spaces text.gsub!(/ +/, ' ') if opts[:fixes_interior_spaces] + # Normalize whitespaces + text = normalize_whitespaces(text) # Strip whitespaces text.strip! if opts[:strip_whitespaces] text end + @@whitespaces_regexp = Regexp.new("(\u00A0|\u1680|\u180E|[\u2000-\u200B]|\u2028|\u2029|\u202F|\u205F|\u3000|\uFEFF)", "u").freeze + + def self.normalize_whitespaces(text) + text.gsub(@@whitespaces_regexp, ' ') + end + end diff --git a/spec/components/text_cleaner_spec.rb b/spec/components/text_cleaner_spec.rb index 07d51136541..0603ba3d09c 100644 --- a/spec/components/text_cleaner_spec.rb +++ b/spec/components/text_cleaner_spec.rb @@ -191,4 +191,12 @@ describe TextCleaner do end + describe "#normalize_whitespaces" do + it "normalize whitespaces" do + whitespaces = "\u0020\u00A0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u200B\u2028\u2029\u202F\u205F\u3000\uFEFF" + whitespaces.strip.should_not == "" + TextCleaner.normalize_whitespaces(whitespaces).strip.should == "" + end + end + end