summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2015-06-02 08:56:53 +0000
committerDouwe Maan <douwe@gitlab.com>2015-06-02 08:56:53 +0000
commita675bea2c1c1d5d6923cb97b8714eb72d4e4ff9b (patch)
treee69d7a84aa17d7eb2fe64572d062834a85bb6e01
parentd99637bf69bde4fb5717c80a896302c6dda104a8 (diff)
parent5733bdb7c7ccd59f63a3500882d22ebdcf822fe1 (diff)
downloadgitlab-ce-a675bea2c1c1d5d6923cb97b8714eb72d4e4ff9b.tar.gz
Merge branch 'rs-issue-1721' into 'master'
Fix link_to_gfm with only a reference having the incorrect link Before: ```ruby link_to_gfm("#4", "/foo") # => "<a href="/namespace/project/issues/4" class="gfm gfm-issue">#4</a>" link_to_gfm("See #4", "/foo") # => "<a href="/foo">See </a><a href="/namespace/project/issues/4" class="gfm gfm-issue">#4</a>" ``` After: ```ruby link_to_gfm("#4", "/foo") # => "<a href="/foo">#4</a>" link_to_gfm("See #4", "/foo") # => "<a href="/foo">See </a><a href="/namespace/project/issues/4" class="gfm gfm-issue">#4</a>" ``` Closes #192 Closes #1721 Closes [GitHub #7115](https://github.com/gitlabhq/gitlabhq/issues/7115) See merge request !739
-rw-r--r--app/helpers/gitlab_markdown_helper.rb19
-rw-r--r--spec/helpers/gitlab_markdown_helper_spec.rb6
2 files changed, 22 insertions, 3 deletions
diff --git a/app/helpers/gitlab_markdown_helper.rb b/app/helpers/gitlab_markdown_helper.rb
index d89f7b4a28d..3c207619adf 100644
--- a/app/helpers/gitlab_markdown_helper.rb
+++ b/app/helpers/gitlab_markdown_helper.rb
@@ -1,3 +1,5 @@
+require 'nokogiri'
+
module GitlabMarkdownHelper
include Gitlab::Markdown
@@ -21,11 +23,22 @@ module GitlabMarkdownHelper
gfm_body = gfm(escaped_body, {}, html_options)
- gfm_body.gsub!(%r{<a.*?>.*?</a>}m) do |match|
- "</a>#{match}#{link_to("", url, html_options)[0..-5]}" # "</a>".length +1
+ fragment = Nokogiri::XML::DocumentFragment.parse(gfm_body)
+ if fragment.children.size == 1 && fragment.children[0].name == 'a'
+ # Fragment has only one node, and it's a link generated by `gfm`.
+ # Replace it with our requested link.
+ text = fragment.children[0].text
+ fragment.children[0].replace(link_to(text, url, html_options))
+ else
+ # Traverse the fragment's first generation of children looking for pure
+ # text, wrapping anything found in the requested link
+ fragment.children.each do |node|
+ next unless node.text?
+ node.replace(link_to(node.text, url, html_options))
+ end
end
- link_to(gfm_body.html_safe, url, html_options)
+ fragment.to_html.html_safe
end
def markdown(text, options={})
diff --git a/spec/helpers/gitlab_markdown_helper_spec.rb b/spec/helpers/gitlab_markdown_helper_spec.rb
index d0b200a9ff8..bbb434638ce 100644
--- a/spec/helpers/gitlab_markdown_helper_spec.rb
+++ b/spec/helpers/gitlab_markdown_helper_spec.rb
@@ -94,6 +94,12 @@ describe GitlabMarkdownHelper do
expect(link_to_gfm(actual, commit_path)).
to match('&lt;h1&gt;test&lt;/h1&gt;')
end
+
+ it 'ignores reference links when they are the entire body' do
+ text = issues[0].to_reference
+ act = link_to_gfm(text, '/foo')
+ expect(act).to eq %Q(<a href="/foo">#{issues[0].to_reference}</a>)
+ end
end
describe '#render_wiki_content' do