diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 1195bfbe040..34c81d6a78c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -110,6 +110,17 @@ module ApplicationHelper text.html_safe end + # circumvents nesting links, which will behave bad in browsers + def link_to_gfm(body, url, html_options = {}) + gfm_body = gfm(body, html_options) + + gfm_body.gsub!(%r{.*?}m) do |match| + "#{match}#{link_to("", url, html_options)[0..-5]}" # "".length +1 + end + + link_to(gfm_body.html_safe, url, html_options) + end + def markdown(text) @__renderer ||= Redcarpet::Markdown.new(Redcarpet::Render::GitlabHTML.new(self, filter_html: true), { no_intra_emphasis: true, diff --git a/spec/helpers/gitlab_flavored_markdown_spec.rb b/spec/helpers/gitlab_flavored_markdown_spec.rb index ec31cd1011e..f64362faae5 100644 --- a/spec/helpers/gitlab_flavored_markdown_spec.rb +++ b/spec/helpers/gitlab_flavored_markdown_spec.rb @@ -163,4 +163,17 @@ describe ApplicationHelper do gfm("fixed in #{@commit.id}", :class => "foo").should have_selector("a.foo") end end + + describe "#link_to_gfm" do + let(:issue1) { Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @project } + let(:issue2) { Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @project } + + it "should handle references nested in links with all the text" do + link_to_gfm("This should finally fix ##{issue1.id} and ##{issue2.id} for real", project_commit_path(@project, :id => @commit.id)).should == "#{link_to "This should finally fix ", project_commit_path(@project, :id => @commit.id)}#{link_to "##{issue1.id}", project_issue_path(@project, issue1), :title => "Issue: #{issue1.title}", :class => "gfm gfm-issue "}#{link_to " and ", project_commit_path(@project, :id => @commit.id)}#{link_to "##{issue2.id}", project_issue_path(@project, issue2), :title => "Issue: #{issue2.title}", :class => "gfm gfm-issue "}#{link_to " for real", project_commit_path(@project, :id => @commit.id)}" + end + + it "should forward HTML options" do + link_to_gfm("This should finally fix ##{issue1.id} for real", project_commit_path(@project, :id => @commit.id), :class => "foo").should have_selector(".foo") + end + end end