diff options
author | Gareth Rees <gareth@garethrees.co.uk> | 2014-06-09 17:56:05 +0100 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2014-06-24 17:01:40 +0100 |
commit | 124f88eaa22ef5b1851db505af9e639c96d5b1d8 (patch) | |
tree | b5381216ae4b5b2807a40bf2ce27ce46a2657651 /actionview/lib/action_view/helpers | |
parent | a1bd00d5be4f5bef34a259a053a00edede2cd2b5 (diff) | |
download | rails-124f88eaa22ef5b1851db505af9e639c96d5b1d8.tar.gz rails-124f88eaa22ef5b1851db505af9e639c96d5b1d8.tar.bz2 rails-124f88eaa22ef5b1851db505af9e639c96d5b1d8.zip |
Deal with regex match groups in excerpt
Original implementation has bugs if the regex contains a match group.
Example:
excerpt('This is a beautiful? morning', /\b(beau\w*)\b/i, :radius => 5)
Expected: "...is a beautiful? mor..."
Actual: "...is a beautifulbeaut..."
The original phrase was being converted to a regex and returning the text
either side of the phrase as expected:
'This is a beautiful? morning'.split(/beautiful/i, 2)
# => ["This is a ", "? morning"]
When we have a match with groups the match is returned in the array.
Quoting the ruby docs: "If pattern is a Regexp, str is divided where the
pattern matches. [...] If pattern contains groups, the respective matches will
be returned in the array as well."
'This is a beautiful? morning'.split(/\b(beau\w*)\b/iu, 2)
# => ["This is a ", "beautiful", "? morning"]
If we assume we want to split on the first match – this fix makes that
assumption – we can pass the already assigned `phrase` variable as the place
to split (because we already know that a match exists from line 168).
Originally spotted by Louise Crow (@crowbot) at
https://github.com/mysociety/alaveteli/pull/1557
Diffstat (limited to 'actionview/lib/action_view/helpers')
-rw-r--r-- | actionview/lib/action_view/helpers/text_helper.rb | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/actionview/lib/action_view/helpers/text_helper.rb b/actionview/lib/action_view/helpers/text_helper.rb index cf5c1b0e81..b859653bc9 100644 --- a/actionview/lib/action_view/helpers/text_helper.rb +++ b/actionview/lib/action_view/helpers/text_helper.rb @@ -188,7 +188,7 @@ module ActionView end end - first_part, second_part = text.split(regex, 2) + first_part, second_part = text.split(phrase, 2) prefix, first_part = cut_excerpt_part(:first, first_part, separator, options) postfix, second_part = cut_excerpt_part(:second, second_part, separator, options) |