Skip to content

Commit

Permalink
Merge pull request #178 from Hirurg103/master
Browse files Browse the repository at this point in the history
Fix  visit_in_email opens the wrong link #177
  • Loading branch information
Mauro Otonelli committed May 4, 2016
2 parents de8ac15 + 36b83ed commit e17610a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/email_spec/helpers.rb
Expand Up @@ -140,9 +140,11 @@ def parse_email_for_explicit_link(email, regex)
request_uri(url)
end

HrefRegex = %r{href=['"]?([^'"]*)['"]?}
NotClosingARegex = %r{(?:(?!</a>).)*?}
# e.g. Click here in <a href="http://confirm">Click here</a>
def parse_email_for_anchor_text_link(email, link_text)
if textify_images(email.default_part_body) =~ %r{<a[^>]*href=['"]?([^'"]*)['"]?[^>]*?>[^<]*?(.*?)#{link_text}(.*?)[^<]*?</a>}
if textify_images(email.default_part_body) =~ %r{<a[^>]*#{HrefRegex}[^>]*>#{NotClosingARegex}#{link_text}#{NotClosingARegex}</a>}
URI.split($1)[5..-1].compact!.join("?").gsub("&amp;", "&")
# sub correct ampersand after rails switches it (http://dev.rubyonrails.org/ticket/4002)
else
Expand Down
4 changes: 3 additions & 1 deletion spec/email_spec/helpers_spec.rb
Expand Up @@ -31,7 +31,9 @@
end

it "properly finds links with text surrounded by tags" do
email = Mail.new(:body => %(<a href="/path/to/page"><strong>Click Here</strong></a>))
email = Mail.new(:body => %(
<a href="/path/to/app">Welcome</a><a href="/path/to/page"><strong>Click Here</strong></a>))

expect(parse_email_for_link(email, "Click Here")).to eq("/path/to/page")
end

Expand Down

0 comments on commit e17610a

Please sign in to comment.