image_size.rb 879 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class HtmlSanitizer
  3. module Scrubber
  4. class ImageSize < Base
  5. def scrub(node)
  6. return CONTINUE if node.name != 'img'
  7. if node['src']
  8. update_style(node)
  9. end
  10. STOP
  11. end
  12. private
  13. def update_style(node)
  14. node['style'] = build_style(node['style'])
  15. end
  16. def build_style(input)
  17. style = 'max-width:100%;'
  18. return style if input.blank?
  19. input
  20. .downcase
  21. .gsub(%r{\t|\n|\r}, '')
  22. .split(';')
  23. .each_with_object(style) do |elem, memo|
  24. key, value = elem.split(':')
  25. key.strip!
  26. next if key.blank?
  27. key = 'max-height' if key == 'height'
  28. memo << "#{key}:#{value};"
  29. end
  30. end
  31. end
  32. end
  33. end