From 691562746f277aa0453ec9fdc06fad5a59210fb5 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 16 Jun 2005 05:42:47 +0000 Subject: Fixed TextHelper#highlight to return the text, not nil, if the phrase is blank #1409 [patrick@lenz.sh] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1429 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_view/helpers/text_helper.rb | 2 +- actionpack/test/template/text_helper_test.rb | 5 +++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 4ce455bc80..b161b1d552 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed TextHelper#highlight to return the text, not nil, if the phrase is blank #1409 [patrick@lenz.sh] + * Fixed TagHelper such that :name and 'name' keys in the options doesn't result in two attributes #1455 [take_tk] * Ensure that helpers are only available to the controllers where they are defined and their subclasses. #1394 [kdole@tamu.edu] diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 2cc4b68ec0..0abd8e79fa 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -26,7 +26,7 @@ module ActionView # passing +highlighter+ as single-quoted string with \1 where the phrase is supposed to be inserted. # N.B.: The +phrase+ is sanitized to include only letters, digits, and spaces before use. def highlight(text, phrase, highlighter = '\1') - if text.nil? || phrase.nil? then return end + if phrase.blank? then return text end text.gsub(/(#{escape_regexp(phrase)})/i, highlighter) unless text.nil? end diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index 926ebdaf47..81aca6f638 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -35,6 +35,11 @@ class TextHelperTest < Test::Unit::TestCase "This is a beautiful morning, but also a beautiful day", highlight("This is a beautiful morning, but also a beautiful day", "beautiful", '\1') ) + + assert_equal( + "This text is not changed because we supplied an empty phrase", + highlight("This text is not changed because we supplied an empty phrase", nil) + ) end def test_highlighter_with_regexp -- cgit v1.2.3