cleanup.rb 936 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class HtmlSanitizer
  3. class Cleanup < Base
  4. def sanitize(string, timeout: true)
  5. return run_sanitization(string) if !timeout
  6. with_timeout(string) do
  7. run_sanitization(string)
  8. end
  9. end
  10. private
  11. def run_sanitization(string)
  12. string = clean_string(string)
  13. cleanup_structure(string)
  14. end
  15. def clean_string(input)
  16. output = input.gsub(%r{<(|/)[A-z]:[A-z]>}, '')
  17. output = output.delete("\t")
  18. # remove all new lines
  19. output
  20. .gsub(%r{(\n\r|\r\r\n|\r\n|\n)}, "\n")
  21. .gsub(%r{\n\n\n+}, "\n\n")
  22. end
  23. def cleanup_structure(string)
  24. empty_node_scrubber = HtmlSanitizer::Scrubber::RemoveLastEmptyNode.new
  25. string = loop_string(string, empty_node_scrubber)
  26. Loofah.fragment(string).scrub!(HtmlSanitizer::Scrubber::Cleanup.new).to_html
  27. end
  28. end
  29. end