lib/ab_admin/utils.rb in ab_admin-0.4.0 vs lib/ab_admin/utils.rb in ab_admin-0.5.0

- old
+ new

@@ -12,11 +12,11 @@ end def bm(message = 'Benchmarking', options = {}) result = nil ms = Benchmark.ms { result = yield } - Rails.logger.debug '%s (%.3fms)' % [message, ms] + (options[:logger] || Rails.logger).info '%s (%.3fms)' % [message, ms] result end def pretty(raw_data) data = case raw_data @@ -55,13 +55,13 @@ Rack::Utils.escape_html(raw_html.no_html.squish) end # html like: '<!-- html comment --><script>script content</script><div>div content</div><p>p content</p>' # normalized to: "<p>div content</p><p>p content</p>" - def normalize_html(raw_html) - @@sanitizer ||= Sanitizer.new - @@sanitizer.normalize_html(raw_html) + def normalize_html(raw_html, options = {}, &block) + @@sanitizer ||= Sanitizer.new(options) + @@sanitizer.normalize_html(raw_html, &block) end def url_helpers Rails.application.routes.url_helpers end @@ -84,15 +84,24 @@ end class Sanitizer include ActionView::Helpers::SanitizeHelper + CLEAN_HTML_COMMENTS_REGEXP = /(&lt;|<)\!--.*?--(&gt;|>)/m + CLEAN_LINE_BREAKS_REGEXP = /[^>]\r\n/ + + def initialize(options = {}) + @options = options + end + def normalize_html(raw_html) return '' if raw_html.blank? - html = sanitize(raw_html.gsub(/<!--(.*?)-->[\n]?/m, '')) + cleaned_html = raw_html.gsub(CLEAN_HTML_COMMENTS_REGEXP, '')#.gsub(CLEAN_LINE_BREAKS_REGEXP, '<br/>') + html = sanitize(cleaned_html, @options[:sanitize] || {}) 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 end @@ -101,11 +110,11 @@ @@display_name_methods_cache[resource.class.name] ||= AbAdmin.display_name_methods.find { |method| resource.respond_to? method } end def display_name(resource) return unless resource - resource.send(display_name_method_for(resource)) + resource.send(display_name_method_for(resource)).to_s.no_html end def safe_display_name(resource) return unless display_name_method_for(resource) display_name(resource) @@ -134,6 +143,6 @@ def friendly_token(n=10) SecureRandom.base64(16).tr('+/=', 'xyz').first(n) end end -end \ No newline at end of file +end