5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-06-02 05:34:16 +00:00

feat: limpieza de estilos

esto permite guardar los nuevos estilos sin permitir otras cosas

sutty/editor!1
This commit is contained in:
f 2023-02-01 17:13:00 -03:00
parent 0781bee40a
commit 4e4091f6c9

View file

@ -77,9 +77,40 @@ class MetadataContent < MetadataTemplate
resource['controls'] = true
end
# Elimina los estilos salvo los que asigne el editor
html.css('[style]').each do |element|
if (style = sanitize_style(element['style'])).present?
element['style'] = style
else
element.remove_attribute('style')
end
end
html.to_s.html_safe
end
# Limpia estilos en base a una lista de permitidos
#
# @param style [String]
# @return [String]
def sanitize_style(style)
style.split(';').reduce({}) do |style_hash, style_string|
key, value = style_string.split(':', 2)
style_hash[key] ||= value
style_hash
end.slice(*allowed_styles).map do |style_pair|
style_pair.join(':')
end.join(';')
end
# Estilos permitidos
#
# @return [Array<String>]
def allowed_styles
@allowed_styles ||= %w[text-align color background-color]
end
# Convierte una ubicación local al sitio en una URL de ActiveStorage
#
# XXX: Por qué son tan díficiles de encontrar las rutas de AS