mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 10:20:58 -06:00
60e624e768
We have a custom implementation of #blank? in our Onebox helpers. This is likely a legacy from when Onebox was a standalone gem. This change replaces all usages with respective incarnations of #blank?, #present?, and #presence from ActiveSupport. It changes a bunch of "unless blank" to "if present" as well.
52 lines
1.1 KiB
Ruby
52 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Onebox
|
|
class Normalizer
|
|
attr_reader :data
|
|
|
|
def get(attr, *args)
|
|
value = data[attr]
|
|
return if value.blank?
|
|
return value.map { |v| sanitize_value(v, *args) } if value.is_a?(Array)
|
|
sanitize_value(value, *args)
|
|
end
|
|
|
|
def method_missing(attr, *args, &block)
|
|
value = get(attr, *args)
|
|
|
|
return nil if value.blank?
|
|
|
|
method_name = attr.to_s
|
|
if method_name.end_with?(*integer_suffixes)
|
|
value.to_i
|
|
elsif method_name.end_with?(*url_suffixes)
|
|
Onebox::Helpers.normalize_url_for_output(value).presence
|
|
else
|
|
value
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def integer_suffixes
|
|
%w[width height]
|
|
end
|
|
|
|
def url_suffixes
|
|
%w[url image video]
|
|
end
|
|
|
|
def html_entities
|
|
@html_entities ||= HTMLEntities.new
|
|
end
|
|
|
|
def sanitize_value(value, length = nil, sanitize = true)
|
|
value = html_entities.decode(value)
|
|
value = Sanitize.fragment(value) if sanitize
|
|
value.strip!
|
|
value = Onebox::Helpers.truncate(value, length) if length
|
|
value
|
|
end
|
|
end
|
|
end
|