Skip to content

Commit

Permalink
update text renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcam-src committed Oct 7, 2024
1 parent 65b753b commit 75bbc64
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions app/renderers/hyrax/renderers/formatted_text_renderer.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
# frozen_string_literal: true
module Hyrax
module Renderers
class FormattedTextRenderer < AttributeRenderer
private
def attribute_value_to_html(value)
# Define allowed tags and attributes
allowed_tags = %w[strong em b i u p br small mark sub sup a ul ol li dl dt dd div span h1 h2 h3 h4 h5 h6]
allowed_attributes = %w[href]

# Sanitize the value, allowing only safe HTML tags and attributes
# Allow for rendering of text as html for the sanitized value
safe_value = sanitize(value, tags: allowed_tags, attributes: allowed_attributes).html_safe

if microdata_value_attributes(field).present?
"<span#{html_attributes(microdata_value_attributes(field))}>#{safe_value}</span>"
else
li_value(value)
end
module Renderers
class FormattedTextRenderer < AttributeRenderer
private
def attribute_value_to_html(value)
sanitized_value = get_sanitized_string(value)
if microdata_value_attributes(field).present?
"<span#{html_attributes(microdata_value_attributes(field))}>#{sanitized_value}</span>"
else
li_value(sanitized_value)
end
end

# Sanitize the value, allowing only safe HTML tags and attributes
def get_sanitized_string(string)
# Define allowed tags and attributes
allowed_tags = %w[strong em b i u p br small mark sub sup a ul ol li dl dt dd div span h1 h2 h3 h4 h5 h6]
allowed_attributes = %w[href]
sanitize(string, tags: allowed_tags, attributes: allowed_attributes)
end

# [hyc-override] Same as attribute renderer override, but without escaping the value
def li_value(value)
field_value = find_language(value) || value
auto_link((field_value))
end
end
end
end
end

0 comments on commit 75bbc64

Please sign in to comment.