mirror of
https://github.com/discourse/discourse.git
synced 2025-05-29 01:31:35 +08:00
FEATURE: Treat emoji or similar characters as one (#12482)
Long messages consisting only of emojis, dots or commas used to be valid because character-wise they were over the limit.
This commit is contained in:
@ -3,14 +3,15 @@
|
||||
class StrippedLengthValidator < ActiveModel::EachValidator
|
||||
def self.validate(record, attribute, value, range)
|
||||
if !value.nil?
|
||||
html_comments_regexp = /<!--(.*?)-->/
|
||||
stripped_length = value.gsub(html_comments_regexp, '')
|
||||
stripped_length = stripped_length.strip.length
|
||||
value = value.dup
|
||||
value.gsub!(/<!--(.*?)-->/, '') # strip HTML comments
|
||||
value.gsub!(/:\w+(:\w+)?:/, "X") # replace emojis with a single character
|
||||
value.gsub!(/\.{2,}/, '…') # replace multiple ... with …
|
||||
value.gsub!(/\,{2,}/, ',') # replace multiple ,,, with ,
|
||||
value.strip!
|
||||
|
||||
record.errors.add attribute, (I18n.t('errors.messages.too_short', count: range.begin)) unless
|
||||
stripped_length >= range.begin
|
||||
record.errors.add attribute, (I18n.t('errors.messages.too_long_validation', max: range.end, length: stripped_length)) unless
|
||||
stripped_length <= range.end
|
||||
record.errors.add attribute, (I18n.t('errors.messages.too_short', count: range.begin)) if value.length < range.begin
|
||||
record.errors.add attribute, (I18n.t('errors.messages.too_long_validation', max: range.end, length: value.length)) if value.length > range.end
|
||||
else
|
||||
record.errors.add attribute, (I18n.t('errors.messages.blank'))
|
||||
end
|
||||
|
Reference in New Issue
Block a user