mirror of
https://github.com/github/rails.git
synced 2026-01-24 13:58:12 -05:00
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>
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -122,6 +122,29 @@ class TextHelperTest < ActionView::TestCase
|
||||
)
|
||||
end
|
||||
|
||||
def test_highlight_with_html
|
||||
assert_equal(
|
||||
"<p>This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>",
|
||||
highlight("<p>This is a beautiful morning, but also a beautiful day</p>", "beautiful")
|
||||
)
|
||||
assert_equal(
|
||||
"<p>This is a <em><strong class=\"highlight\">beautiful</strong></em> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>",
|
||||
highlight("<p>This is a <em>beautiful</em> morning, but also a beautiful day</p>", "beautiful")
|
||||
)
|
||||
assert_equal(
|
||||
"<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>",
|
||||
highlight("<p>This is a <em class=\"error\">beautiful</em> morning, but also a beautiful <span class=\"last\">day</span></p>", "beautiful")
|
||||
)
|
||||
assert_equal(
|
||||
"<p class=\"beautiful\">This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>",
|
||||
highlight("<p class=\"beautiful\">This is a beautiful morning, but also a beautiful day</p>", "beautiful")
|
||||
)
|
||||
assert_equal(
|
||||
"<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>",
|
||||
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")
|
||||
)
|
||||
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))
|
||||
|
||||
Reference in New Issue
Block a user