image_size.rb 924 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class HtmlSanitizer
  3. module Scrubber
  4. module Outgoing
  5. class ImageSize < Base
  6. def scrub(node)
  7. return CONTINUE if node.name != 'img'
  8. return CONTINUE if node['style'].blank?
  9. adjust(node, 'height')
  10. adjust(node, 'width')
  11. STOP
  12. end
  13. private
  14. def split_style(node)
  15. node['style'].downcase.gsub(%r{\t|\n|\r}, '').split(';')
  16. end
  17. def adjust(node, key)
  18. return if node[key].present?
  19. split_style(node).each do |elem|
  20. attr, value = elem.split(':')
  21. attr.strip!
  22. value.strip!
  23. next if attr != key
  24. next if !value.downcase.ends_with?('px')
  25. node[key] = value.include?('.') ? value.to_f : value.to_i
  26. end
  27. end
  28. end
  29. end
  30. end
  31. end