Files
openfoodnetwork/app/services/content_sanitizer.rb
Neal Chambers 871a8e6f2c Fix Rails/Blank
2023-08-17 16:42:46 +09:00

39 lines
780 B
Ruby

# frozen_string_literal: true
# Sanitizes and cleans up user-provided content that may contain tags, special characters, etc.
class ContentSanitizer
include ActionView::Helpers::SanitizeHelper
FILTERED_CHARACTERS = {
"&" => "&",
"&" => "&",
" " => " "
}.freeze
def strip_content(content)
return if content.blank?
content = strip_tags(content.to_s.strip)
filter_characters(content)
end
def sanitize_content(content)
return if content.blank?
content = sanitize(content.to_s, scrubber: ContentScrubber.new)
filter_characters(content)
end
private
def filter_characters(content)
FILTERED_CHARACTERS.each do |character, sub|
content = content.gsub(character, sub)
end
content
end
end