mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 16:41:17 +08:00
FEATURE: normalize whitespaces in topic title/post content
This commit is contained in:
@ -203,6 +203,8 @@ class PostCreator
|
|||||||
end
|
end
|
||||||
|
|
||||||
def setup_post
|
def setup_post
|
||||||
|
@opts[:raw] = TextCleaner.normalize_whitespaces(@opts[:raw]).strip
|
||||||
|
|
||||||
post = @topic.posts.new(raw: @opts[:raw],
|
post = @topic.posts.new(raw: @opts[:raw],
|
||||||
user: @user,
|
user: @user,
|
||||||
reply_to_post_number: @opts[:reply_to_post_number])
|
reply_to_post_number: @opts[:reply_to_post_number])
|
||||||
|
@ -17,7 +17,10 @@ class PostRevisor
|
|||||||
# :skip_validation ask ActiveRecord to skip validations
|
# :skip_validation ask ActiveRecord to skip validations
|
||||||
#
|
#
|
||||||
def revise!(editor, new_raw, opts = {})
|
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?
|
return false unless should_revise?
|
||||||
@post.acting_user = @editor
|
@post.acting_user = @editor
|
||||||
revise_post
|
revise_post
|
||||||
|
@ -36,10 +36,18 @@ class TextCleaner
|
|||||||
text.sub!(/\s+([!?]\s*)\z/, '\1') if opts[:remove_extraneous_space]
|
text.sub!(/\s+([!?]\s*)\z/, '\1') if opts[:remove_extraneous_space]
|
||||||
# Fixes interior spaces
|
# Fixes interior spaces
|
||||||
text.gsub!(/ +/, ' ') if opts[:fixes_interior_spaces]
|
text.gsub!(/ +/, ' ') if opts[:fixes_interior_spaces]
|
||||||
|
# Normalize whitespaces
|
||||||
|
text = normalize_whitespaces(text)
|
||||||
# Strip whitespaces
|
# Strip whitespaces
|
||||||
text.strip! if opts[:strip_whitespaces]
|
text.strip! if opts[:strip_whitespaces]
|
||||||
|
|
||||||
text
|
text
|
||||||
end
|
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
|
end
|
||||||
|
@ -191,4 +191,12 @@ describe TextCleaner do
|
|||||||
|
|
||||||
end
|
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
|
end
|
||||||
|
Reference in New Issue
Block a user