FIX: post merging was failing silently (#12566)

https://meta.discourse.org/t/merging-very-long-posts-removes-them/183597
This commit is contained in:
Arpit Jalan
2021-04-01 06:46:18 +05:30
committed by GitHub
parent 28d67b4583
commit c478ffc662
6 changed files with 38 additions and 12 deletions

View File

@ -3,13 +3,7 @@
class StrippedLengthValidator < ActiveModel::EachValidator
def self.validate(record, attribute, value, range)
if !value.nil?
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!
value = get_sanitized_value(value)
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
@ -22,4 +16,13 @@ class StrippedLengthValidator < ActiveModel::EachValidator
range = options[:in].lambda? ? options[:in].call : options[:in]
self.class.validate(record, attribute, value, range)
end
def self.get_sanitized_value(value)
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
end
end