cleanup.rb 999 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright (C) 2012-2022 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. string = cleanup_structure(string, 'pre')
  14. cleanup_structure(string)
  15. end
  16. def clean_string(input)
  17. output = input.gsub(%r{<(|/)[A-z]:[A-z]>}, '')
  18. output = output.delete("\t")
  19. # remove all new lines
  20. output
  21. .gsub(%r{(\n\r|\r\r\n|\r\n|\n)}, "\n")
  22. .gsub(%r{\n\n\n+}, "\n\n")
  23. end
  24. def cleanup_structure(string, type = 'all')
  25. empty_node_scrubber = HtmlSanitizer::Scrubber::RemoveLastEmptyNode.new(type)
  26. string = loop(string, empty_node_scrubber)
  27. Loofah.fragment(string).scrub!(HtmlSanitizer::Scrubber::Cleanup.new).to_html
  28. end
  29. end
  30. end