1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
David Heinemeier Hansson 2005-06-16 05:42:47 +00:00
parent bfe6a759c2
commit 691562746f
3 changed files with 8 additions and 1 deletions

View file

@ -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]

View file

@ -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 = '<strong class="highlight">\1</strong>')
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

View file

@ -35,6 +35,11 @@ class TextHelperTest < Test::Unit::TestCase
"This is a <b>beautiful</b> morning, but also a <b>beautiful</b> day",
highlight("This is a beautiful morning, but also a beautiful day", "beautiful", '<b>\1</b>')
)
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