Class: AbAdmin::Utils::Sanitizer

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::SanitizeHelper
Defined in:
lib/ab_admin/utils.rb

Constant Summary collapse

CLEAN_HTML_COMMENTS_REGEXP =
/(&lt;|<)\!--.*?--(&gt;|>)/m
CLEAN_LINE_BREAKS_REGEXP =
/[^>]\r\n/

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Sanitizer

Returns a new instance of Sanitizer.



92
93
94
# File 'lib/ab_admin/utils.rb', line 92

def initialize(options = {})
  @options = options
end

Instance Method Details

#normalize_html(raw_html, options = {}) {|doc| ... } ⇒ Object

Yields:

  • (doc)


96
97
98
99
100
101
102
103
104
105
# File 'lib/ab_admin/utils.rb', line 96

def normalize_html(raw_html, options = {})
  return '' if raw_html.blank?
  cleaned_html = raw_html.gsub(CLEAN_HTML_COMMENTS_REGEXP, '')#.gsub(CLEAN_LINE_BREAKS_REGEXP, '<br/>')
  html = sanitize(cleaned_html, options || {})
  doc = Nokogiri::HTML.fragment(html)
  #doc.xpath('comment()').each { |c| c.remove }
  yield doc if block_given?
  doc.search('div').each { |el| el.name = 'p' }
  doc.to_html
end