1234567891011121314151617181920212223242526272829303132333435363738 |
- class HtmlSanitizer
- class Cleanup < Base
- def sanitize(string, timeout: true)
- return run_sanitization(string) if !timeout
- with_timeout(string) do
- run_sanitization(string)
- end
- end
- private
- def run_sanitization(string)
- string = clean_string(string)
- cleanup_structure(string)
- end
- def clean_string(input)
- output = input.gsub(%r{<(|/)[A-z]:[A-z]>}, '')
- output = output.delete("\t")
-
- output
- .gsub(%r{(\n\r|\r\r\n|\r\n|\n)}, "\n")
- .gsub(%r{\n\n\n+}, "\n\n")
- end
- def cleanup_structure(string)
- empty_node_scrubber = HtmlSanitizer::Scrubber::RemoveLastEmptyNode.new
- string = loop_string(string, empty_node_scrubber)
- Loofah.fragment(string).scrub!(HtmlSanitizer::Scrubber::Cleanup.new).to_html
- end
- end
- end
|