diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 1d9e4fe9b8..b1eb6891fa 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -107,7 +107,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 a6200fbdd7..564845779f 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -122,6 +122,29 @@ class TextHelperTest < ActionView::TestCase ) 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)) assert_equal("This is a...", excerpt("This is a beautiful morning", "this", 5))