From 80917cd01b8b8faede5e8189036e54c3bd3d02ac Mon Sep 17 00:00:00 2001 From: Dan Weinand Date: Thu, 30 Oct 2008 12:52:12 -0500 Subject: [PATCH] Using the highlight helper on text with html shouldn't highlight text inside html attributes. --- actionpack/lib/action_view/helpers/text_helper.rb | 2 +- actionpack/test/template/text_helper_test.rb | 23 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletions(-) diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 3af7440..bee9473 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -117,7 +117,7 @@ module ActionView text else match = Array(phrases).map { |p| Regexp.escape(p) }.join('|') - text.gsub(/(#{match})/i, options[:highlighter]) + text.gsub(/(#{match})(?!(?:[^<]*?)?(?:["'])[^<>]*>)/i, options[:highlighter]) end end diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index 5f9f715..126cc93 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -121,6 +121,29 @@ class TextHelperTest < ActionView::TestCase highlight("This is a beautiful morning, but also a beautiful day", "beautiful", :highlighter => '\1') ) end + + def test_highlight_with_html + assert_equal( + "

This is a beautiful morning, but also a beautiful day

", + highlight("

This is a beautiful morning, but also a beautiful day

", "beautiful") + ) + assert_equal( + "

This is a beautiful morning, but also a beautiful day

", + highlight("

This is a beautiful morning, but also a beautiful day

", "beautiful") + ) + assert_equal( + "

This is a beautiful morning, but also a beautiful day

", + highlight("

This is a beautiful morning, but also a beautiful day

", "beautiful") + ) + assert_equal( + "

This is a beautiful morning, but also a beautiful day

", + highlight("

This is a beautiful morning, but also a beautiful day

", "beautiful") + ) + assert_equal( + "

This is a beautiful morning, but also a beautiful day

", + highlight("

This is a beautiful morning, but also a beautiful day

", "beautiful") + ) + end def test_excerpt assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", 5)) -- 1.5.6.4