Skip to content
This repository
Browse code

Using the highlight helper on text with html shouldn't highlight text…

… inside html attributes. [#1302 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit 91eeb0ff119d34d0fcdb44d3d7fcbb7924208e05 1 parent a0d8202
Dan Weinand authored October 30, 2008 lifo committed January 28, 2009
2  actionpack/lib/action_view/helpers/text_helper.rb
@@ -107,7 +107,7 @@ def highlight(text, phrases, *args)
107 107
           text
108 108
         else
109 109
           match = Array(phrases).map { |p| Regexp.escape(p) }.join('|')
110  
-          text.gsub(/(#{match})/i, options[:highlighter])
  110
+          text.gsub(/(#{match})(?!(?:[^<]*?)?(?:["'])[^<>]*>)/i, options[:highlighter])
111 111
         end
112 112
       end
113 113
 
23  actionpack/test/template/text_helper_test.rb
@@ -122,6 +122,29 @@ def test_highlight_with_options_hash
122 122
     )
123 123
   end
124 124
 
  125
+  def test_highlight_with_html
  126
+    assert_equal(
  127
+      "<p>This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>",
  128
+      highlight("<p>This is a beautiful morning, but also a beautiful day</p>", "beautiful")
  129
+    )
  130
+    assert_equal(
  131
+      "<p>This is a <em><strong class=\"highlight\">beautiful</strong></em> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>",
  132
+      highlight("<p>This is a <em>beautiful</em> morning, but also a beautiful day</p>", "beautiful")
  133
+    )
  134
+    assert_equal(
  135
+      "<p>This is a <em class=\"error\"><strong class=\"highlight\">beautiful</strong></em> morning, but also a <strong class=\"highlight\">beautiful</strong> <span class=\"last\">day</span></p>",
  136
+      highlight("<p>This is a <em class=\"error\">beautiful</em> morning, but also a beautiful <span class=\"last\">day</span></p>", "beautiful")
  137
+    )
  138
+    assert_equal(
  139
+      "<p class=\"beautiful\">This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>",
  140
+      highlight("<p class=\"beautiful\">This is a beautiful morning, but also a beautiful day</p>", "beautiful")
  141
+    )
  142
+    assert_equal(
  143
+      "<p>This is a <strong class=\"highlight\">beautiful</strong> <a href=\"http://example.com/beautiful\#top?what=beautiful%20morning&when=now+then\">morning</a>, but also a <strong class=\"highlight\">beautiful</strong> day</p>",
  144
+      highlight("<p>This is a beautiful <a href=\"http://example.com/beautiful\#top?what=beautiful%20morning&when=now+then\">morning</a>, but also a beautiful day</p>", "beautiful")
  145
+    )
  146
+  end
  147
+
125 148
   def test_excerpt
126 149
     assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", 5))
127 150
     assert_equal("This is a...", excerpt("This is a beautiful morning", "this", 5))

0 notes on commit 91eeb0f

Please sign in to comment.
Something went wrong with that request. Please try again.