Permalink
Browse files

Merge pull request #8598 from NARKOZ/refactor-highlight

refactor highlight method: return fast if text or phrases are blank
  • Loading branch information...
2 parents 78fd14c + 513331d commit 14a61bba4dda8a48a1949960ee31e28973473916 @spastorino spastorino committed Dec 26, 2012
Showing with 2 additions and 2 deletions.
  1. +2 −2 actionpack/lib/action_view/helpers/text_helper.rb
@@ -112,12 +112,12 @@ def truncate(text, options = {}, &block)
# highlight('You searched for: rails', 'rails', highlighter: '<a href="search?q=\1">\1</a>')
# # => You searched for: <a href="search?q=rails">rails</a>
def highlight(text, phrases, options = {})
- highlighter = options.fetch(:highlighter, '<mark>\1</mark>')
-
text = sanitize(text) if options.fetch(:sanitize, true)
+
if text.blank? || phrases.blank?
text
else
+ highlighter = options.fetch(:highlighter, '<mark>\1</mark>')
match = Array(phrases).map { |p| Regexp.escape(p) }.join('|')
text.gsub(/(#{match})(?![^<]*?>)/i, highlighter)
end.html_safe

0 comments on commit 14a61bb

Please sign in to comment.