mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-26 20:56:48 +00:00
39 lines
780 B
Ruby
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
|