Module: Workbench::HtmlHelper
- Defined in:
- app/helpers/workbench/html_helper.rb
Overview
Basic, non model dependent helpers that inject HTML, these should all depend on Rails helpers, if they don't they should go into Utilities
Instance Method Summary collapse
-
#mark_tag(string, term, html_safe: true) ⇒ String?
TODO: see also highlight().
Instance Method Details
#mark_tag(string, term, html_safe: true) ⇒ String?
TODO: see also highlight()
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/helpers/workbench/html_helper.rb', line 14 def mark_tag(string, term, html_safe: true) return nil if string.nil? s = if term.blank? string else t = Regexp.escape(term) string.split(/(<[^>]*>)/).map { |piece| next piece if piece.start_with?('<') # We split on the html entities content_tag could produce (though at # time of writing these could also come from user input). piece.split(/(&(?:amp|lt|gt|quot|\#39);)/).map { |chunk| chunk.start_with?('&') ? chunk : chunk.gsub(/(#{t})/i) { content_tag(:mark, $1) } }.join }.join end html_safe ? s.html_safe : String.new(s) end |