aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-03-30 01:19:01 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-03-30 01:19:01 +0000
commit67d5a1abedaae4c2ccc5057bb6780708f04b5f10 (patch)
tree78f884ea378f691f8827b314540324df6f15a86e
parent29d63a04acabf398aa161857b603d99a2bf8e51b (diff)
downloadrails-67d5a1abedaae4c2ccc5057bb6780708f04b5f10.tar.gz
rails-67d5a1abedaae4c2ccc5057bb6780708f04b5f10.tar.bz2
rails-67d5a1abedaae4c2ccc5057bb6780708f04b5f10.zip
Highlight helper highlights one or many terms in a single pass.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6493 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb17
-rw-r--r--actionpack/test/template/text_helper_test.rb6
3 files changed, 20 insertions, 5 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index e90e390397..f68c081e51 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Highlight helper highlights one or many terms in a single pass. [Jeremy Kemper]
+
* Dropped the use of ; as a separator of non-crud actions on resources and went back to the vanilla slash. It was a neat idea, but lots of the non-crud actions turned out not to be RPC (as the ; was primarily intended to discourage), but legitimate sub-resources, like /parties/recent, which didn't deserve the uglification of /parties;recent. Further more, the semicolon caused issues with caching and HTTP authentication in Safari. Just Not Worth It [DHH]
* Added that FormTagHelper#submit_tag will return to its original state if the submit fails and you're using :disable_with [DHH]
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index 1698598edb..d986233796 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -38,15 +38,22 @@ module ActionView
(text.chars.length > length ? text.chars[0...l] + truncate_string : text).to_s
end
- # Highlights +phrase+ everywhere it is found in +text+ by inserting it into
+ # Highlights one or more +phrases+ everywhere in +text+ by inserting it into
# a +highlighter+ string. The highlighter can be specialized by passing +highlighter+
# as a single-quoted string with \1 where the phrase is to be inserted.
#
# highlight('You searched for: rails', 'rails')
- # => You searched for: <strong class="highlight">rails</strong>
- def highlight(text, phrase, highlighter = '<strong class="highlight">\1</strong>')
- if phrase.blank? then return text end
- text.gsub(/(#{Regexp.escape(phrase)})/i, highlighter) unless text.nil?
+ # # => You searched for: <strong class="highlight">rails</strong>
+ #
+ # highlight('You searched for: rails', ['for', 'rails'], '<em>\1</em>')
+ # # => You searched <em>for</em>: <em>rails</em>
+ def highlight(text, phrases, highlighter = '<strong class="highlight">\1</strong>')
+ if text.blank? || phrases.blank?
+ text
+ else
+ match = Array(phrases).map { |p| Regexp.escape(p) }.join('|')
+ text.gsub(/(#{match})/i, highlighter)
+ end
end
# Extracts an excerpt from +text+ that matches the first instance of +phrase+.
diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb
index 2f11547db5..61afbb5136 100644
--- a/actionpack/test/template/text_helper_test.rb
+++ b/actionpack/test/template/text_helper_test.rb
@@ -65,6 +65,8 @@ class TextHelperTest < Test::Unit::TestCase
"This text is not changed because we supplied an empty phrase",
highlight("This text is not changed because we supplied an empty phrase", nil)
)
+
+ assert_equal ' ', highlight(' ', 'blank text is returned verbatim')
end
def test_highlighter_with_regexp
@@ -84,6 +86,10 @@ class TextHelperTest < Test::Unit::TestCase
)
end
+ def test_highlighting_multiple_phrases_in_one_pass
+ assert_equal %(<em>wow</em> <em>em</em>), highlight('wow em', %w(wow em), '<em>\1</em>')
+ end
+
def test_excerpt
assert_equal("...is a beautiful morni...", excerpt("This is a beautiful morning", "beautiful", 5))
assert_equal("This is a...", excerpt("This is a beautiful morning", "this", 5))