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


100
101
102
# File 'lib/ab_admin/utils.rb', line 100

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

Instance Method Details

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

Yields:

  • (doc)

104
105
106
107
108
109
110
111
112
113
# File 'lib/ab_admin/utils.rb', line 104

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