提交 956fb811 编写于 作者: J José Valim

Merge pull request #4893 from bcardarella/mark_for_highlight

Highlight defaults to HTML5 `mark` element
...@@ -73,6 +73,9 @@ ...@@ -73,6 +73,9 @@
* `favicon_link_tag` helper will now use the favicon in app/assets by default. *Lucas Caton* * `favicon_link_tag` helper will now use the favicon in app/assets by default. *Lucas Caton*
* `ActionView::Helpers::TextHelper#highlight` now defaults to the
HTML5 `mark` element. *Brian Cardarella*
## Rails 3.2.0 (January 20, 2012) ## ## Rails 3.2.0 (January 20, 2012) ##
* Add `config.action_dispatch.default_charset` to configure default charset for ActionDispatch::Response. *Carlos Antonio da Silva* * Add `config.action_dispatch.default_charset` to configure default charset for ActionDispatch::Response. *Carlos Antonio da Silva*
......
...@@ -90,11 +90,11 @@ def truncate(text, options = {}) ...@@ -90,11 +90,11 @@ def truncate(text, options = {})
# Highlights one or more +phrases+ everywhere in +text+ by inserting it into # Highlights one or more +phrases+ everywhere in +text+ by inserting it into
# a <tt>:highlighter</tt> string. The highlighter can be specialized by passing <tt>:highlighter</tt> # a <tt>:highlighter</tt> string. The highlighter can be specialized by passing <tt>:highlighter</tt>
# as a single-quoted string with \1 where the phrase is to be inserted (defaults to # as a single-quoted string with \1 where the phrase is to be inserted (defaults to
# '<strong class="highlight">\1</strong>') # '<mark>\1</mark>')
# #
# ==== Examples # ==== Examples
# highlight('You searched for: rails', 'rails') # highlight('You searched for: rails', 'rails')
# # => You searched for: <strong class="highlight">rails</strong> # # => You searched for: <mark>rails</mark>
# #
# highlight('You searched for: ruby, rails, dhh', 'actionpack') # highlight('You searched for: ruby, rails, dhh', 'actionpack')
# # => You searched for: ruby, rails, dhh # # => You searched for: ruby, rails, dhh
...@@ -111,9 +111,9 @@ def truncate(text, options = {}) ...@@ -111,9 +111,9 @@ def truncate(text, options = {})
def highlight(text, phrases, *args) def highlight(text, phrases, *args)
options = args.extract_options! options = args.extract_options!
unless args.empty? unless args.empty?
options[:highlighter] = args[0] || '<strong class="highlight">\1</strong>' options[:highlighter] = args[0] || '<mark>\1</mark>'
end end
options.reverse_merge!(:highlighter => '<strong class="highlight">\1</strong>') options.reverse_merge!(:highlighter => '<mark>\1</mark>')
text = sanitize(text) unless options[:sanitize] == false text = sanitize(text) unless options[:sanitize] == false
if text.blank? || phrases.blank? if text.blank? || phrases.blank?
......
...@@ -91,12 +91,12 @@ def test_highlight_should_be_html_safe ...@@ -91,12 +91,12 @@ def test_highlight_should_be_html_safe
def test_highlight def test_highlight
assert_equal( assert_equal(
"This is a <strong class=\"highlight\">beautiful</strong> morning", "This is a <mark>beautiful</mark> morning",
highlight("This is a beautiful morning", "beautiful") highlight("This is a beautiful morning", "beautiful")
) )
assert_equal( assert_equal(
"This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day", "This is a <mark>beautiful</mark> morning, but also a <mark>beautiful</mark> day",
highlight("This is a beautiful morning, but also a beautiful day", "beautiful") highlight("This is a beautiful morning, but also a beautiful day", "beautiful")
) )
...@@ -115,31 +115,31 @@ def test_highlight ...@@ -115,31 +115,31 @@ def test_highlight
def test_highlight_should_sanitize_input def test_highlight_should_sanitize_input
assert_equal( assert_equal(
"This is a <strong class=\"highlight\">beautiful</strong> morning", "This is a <mark>beautiful</mark> morning",
highlight("This is a beautiful morning<script>code!</script>", "beautiful") highlight("This is a beautiful morning<script>code!</script>", "beautiful")
) )
end end
def test_highlight_should_not_sanitize_if_sanitize_option_if_false def test_highlight_should_not_sanitize_if_sanitize_option_if_false
assert_equal( assert_equal(
"This is a <strong class=\"highlight\">beautiful</strong> morning<script>code!</script>", "This is a <mark>beautiful</mark> morning<script>code!</script>",
highlight("This is a beautiful morning<script>code!</script>", "beautiful", :sanitize => false) highlight("This is a beautiful morning<script>code!</script>", "beautiful", :sanitize => false)
) )
end end
def test_highlight_with_regexp def test_highlight_with_regexp
assert_equal( assert_equal(
"This is a <strong class=\"highlight\">beautiful!</strong> morning", "This is a <mark>beautiful!</mark> morning",
highlight("This is a beautiful! morning", "beautiful!") highlight("This is a beautiful! morning", "beautiful!")
) )
assert_equal( assert_equal(
"This is a <strong class=\"highlight\">beautiful! morning</strong>", "This is a <mark>beautiful! morning</mark>",
highlight("This is a beautiful! morning", "beautiful! morning") highlight("This is a beautiful! morning", "beautiful! morning")
) )
assert_equal( assert_equal(
"This is a <strong class=\"highlight\">beautiful? morning</strong>", "This is a <mark>beautiful? morning</mark>",
highlight("This is a beautiful? morning", "beautiful? morning") highlight("This is a beautiful? morning", "beautiful? morning")
) )
end end
...@@ -157,23 +157,23 @@ def test_highlight_with_options_hash ...@@ -157,23 +157,23 @@ def test_highlight_with_options_hash
def test_highlight_with_html def test_highlight_with_html
assert_equal( assert_equal(
"<p>This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>", "<p>This is a <mark>beautiful</mark> morning, but also a <mark>beautiful</mark> day</p>",
highlight("<p>This is a beautiful morning, but also a beautiful day</p>", "beautiful") highlight("<p>This is a beautiful morning, but also a beautiful day</p>", "beautiful")
) )
assert_equal( assert_equal(
"<p>This is a <em><strong class=\"highlight\">beautiful</strong></em> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>", "<p>This is a <em><mark>beautiful</mark></em> morning, but also a <mark>beautiful</mark> day</p>",
highlight("<p>This is a <em>beautiful</em> morning, but also a beautiful day</p>", "beautiful") highlight("<p>This is a <em>beautiful</em> morning, but also a beautiful day</p>", "beautiful")
) )
assert_equal( 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>", "<p>This is a <em class=\"error\"><mark>beautiful</mark></em> morning, but also a <mark>beautiful</mark> <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") highlight("<p>This is a <em class=\"error\">beautiful</em> morning, but also a beautiful <span class=\"last\">day</span></p>", "beautiful")
) )
assert_equal( assert_equal(
"<p class=\"beautiful\">This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>", "<p class=\"beautiful\">This is a <mark>beautiful</mark> morning, but also a <mark>beautiful</mark> day</p>",
highlight("<p class=\"beautiful\">This is a beautiful morning, but also a beautiful day</p>", "beautiful") highlight("<p class=\"beautiful\">This is a beautiful morning, but also a beautiful day</p>", "beautiful")
) )
assert_equal( assert_equal(
"<p>This is a <strong class=\"highlight\">beautiful</strong> <a href=\"http://example.com/beautiful#top?what=beautiful%20morning&amp;when=now+then\">morning</a>, but also a <strong class=\"highlight\">beautiful</strong> day</p>", "<p>This is a <mark>beautiful</mark> <a href=\"http://example.com/beautiful#top?what=beautiful%20morning&amp;when=now+then\">morning</a>, but also a <mark>beautiful</mark> 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") 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")
) )
assert_equal( assert_equal(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册