remove_line_breaks.rb 630 B

123456789101112131415161718192021
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class HtmlSanitizer
  3. class RemoveLineBreaks < Loofah::Scrubber
  4. SPAN_LINE_BREAKS = ["\n", "\r", "\r\n"].freeze
  5. DIV_LINE_BREAK_REGEXP = %r{\A([\n\r]+)\z}
  6. def scrub(node)
  7. case node.name
  8. when 'span'
  9. node.children.reject { |t| SPAN_LINE_BREAKS.include?(t.text) }.each { |child| node.before child }
  10. node.remove
  11. when 'div'
  12. node.children.to_a.select { |t| t.text.match?(DIV_LINE_BREAK_REGEXP) }.each(&:remove)
  13. node.remove if node.children.none? && node.classes.none?
  14. end
  15. end
  16. end
  17. end