mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Have text helpers use built in Regexp.escape rather than home grown alternative
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2350 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
bbfde01b94
commit
b54560600b
2 changed files with 8 additions and 7 deletions
|
@ -35,7 +35,7 @@ module ActionView
|
||||||
# N.B.: The +phrase+ is sanitized to include only letters, digits, and spaces before use.
|
# N.B.: The +phrase+ is sanitized to include only letters, digits, and spaces before use.
|
||||||
def highlight(text, phrase, highlighter = '<strong class="highlight">\1</strong>')
|
def highlight(text, phrase, highlighter = '<strong class="highlight">\1</strong>')
|
||||||
if phrase.blank? then return text end
|
if phrase.blank? then return text end
|
||||||
text.gsub(/(#{escape_regexp(phrase)})/i, highlighter) unless text.nil?
|
text.gsub(/(#{Regexp.escape(phrase)})/i, highlighter) unless text.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
# Extracts an excerpt from the +text+ surrounding the +phrase+ with a number of characters on each side determined
|
# Extracts an excerpt from the +text+ surrounding the +phrase+ with a number of characters on each side determined
|
||||||
|
@ -43,7 +43,7 @@ module ActionView
|
||||||
# excerpt("hello my world", "my", 3) => "...lo my wo..."
|
# excerpt("hello my world", "my", 3) => "...lo my wo..."
|
||||||
def excerpt(text, phrase, radius = 100, excerpt_string = "...")
|
def excerpt(text, phrase, radius = 100, excerpt_string = "...")
|
||||||
if text.nil? || phrase.nil? then return end
|
if text.nil? || phrase.nil? then return end
|
||||||
phrase = escape_regexp(phrase)
|
phrase = Regexp.escape(phrase)
|
||||||
|
|
||||||
if found_pos = text =~ /(#{phrase})/i
|
if found_pos = text =~ /(#{phrase})/i
|
||||||
start_pos = [ found_pos - radius, 0 ].max
|
start_pos = [ found_pos - radius, 0 ].max
|
||||||
|
@ -280,11 +280,6 @@ module ActionView
|
||||||
@_cycles[name] = cycle_object
|
@_cycles[name] = cycle_object
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a version of the text that's safe to use in a regular expression without triggering engine features.
|
|
||||||
def escape_regexp(text)
|
|
||||||
text.gsub(/([\\|?+*\/\)\(])/) { |m| "\\#{$1}" }
|
|
||||||
end
|
|
||||||
|
|
||||||
AUTO_LINK_RE = /
|
AUTO_LINK_RE = /
|
||||||
( # leading text
|
( # leading text
|
||||||
<\w+.*?>| # leading HTML tag, or
|
<\w+.*?>| # leading HTML tag, or
|
||||||
|
|
|
@ -101,6 +101,12 @@ class TextHelperTest < Test::Unit::TestCase
|
||||||
assert_nil excerpt("This is a beautiful morning", "day")
|
assert_nil excerpt("This is a beautiful morning", "day")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_excerpt_with_regex
|
||||||
|
assert_equal('...is a beautiful! morn...', excerpt('This is a beautiful! morning', 'beautiful', 5))
|
||||||
|
assert_equal('...is a beautiful? morn...', excerpt('This is a beautiful? morning', 'beautiful', 5))
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def test_word_wrap
|
def test_word_wrap
|
||||||
assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", 15))
|
assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", 15))
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue