Files
openfoodnetwork/app/services/content_sanitizer.rb
Jean-Baptiste Bellet 6d68460950 Factorize sanitizer options between edition and displaying
by using the same `app/services/content_scrubber.rb`
2023-01-26 18:21:27 +01:00

39 lines
792 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 unless content.present?
content = strip_tags(content.to_s.strip)
filter_characters(content)
end
def sanitize_content(content)
return unless content.present?
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