Commit 3ecc4df4b35080d78f83c5155418339aad40dca8

Authored by Dmitriy Zaporozhets
2 parents 85def2d6 d993f666

Merge pull request #1456 from tsigo/issue_1308

Fix HTML entities being parsed in GFM
lib/gitlab/markdown.rb
@@ -26,13 +26,13 @@ module Gitlab @@ -26,13 +26,13 @@ module Gitlab
26 # => "<img alt=\":trollface:\" class=\"emoji\" src=\"/images/trollface.png" title=\":trollface:\" /> 26 # => "<img alt=\":trollface:\" class=\"emoji\" src=\"/images/trollface.png" title=\":trollface:\" />
27 module Markdown 27 module Markdown
28 REFERENCE_PATTERN = %r{ 28 REFERENCE_PATTERN = %r{
29 - ([^\w&;])? # Prefix (1) 29 + (\W)? # Prefix (1)
30 ( # Reference (2) 30 ( # Reference (2)
31 @([\w\._]+) # User name (3) 31 @([\w\._]+) # User name (3)
32 |[#!$](\d+) # Issue/MR/Snippet ID (4) 32 |[#!$](\d+) # Issue/MR/Snippet ID (4)
33 |([\h]{6,40}) # Commit ID (5) 33 |([\h]{6,40}) # Commit ID (5)
34 ) 34 )
35 - ([^\w&;])? # Suffix (6) 35 + (\W)? # Suffix (6)
36 }x.freeze 36 }x.freeze
37 37
38 EMOJI_PATTERN = %r{(:(\S+):)}.freeze 38 EMOJI_PATTERN = %r{(:(\S+):)}.freeze
@@ -84,6 +84,13 @@ module Gitlab @@ -84,6 +84,13 @@ module Gitlab
84 # 84 #
85 # Returns parsed text 85 # Returns parsed text
86 def parse(text) 86 def parse(text)
  87 + parse_references(text) if @project
  88 + parse_emoji(text)
  89 +
  90 + text
  91 + end
  92 +
  93 + def parse_references(text)
87 # parse reference links 94 # parse reference links
88 text.gsub!(REFERENCE_PATTERN) do |match| 95 text.gsub!(REFERENCE_PATTERN) do |match|
89 prefix = $1 || '' 96 prefix = $1 || ''
@@ -91,13 +98,18 @@ module Gitlab @@ -91,13 +98,18 @@ module Gitlab
91 identifier = $3 || $4 || $5 98 identifier = $3 || $4 || $5
92 suffix = $6 || '' 99 suffix = $6 || ''
93 100
94 - if ref_link = reference_link(reference, identifier) 101 + # Avoid HTML entities
  102 + if prefix.ends_with?('&') || suffix.starts_with?(';')
  103 + match
  104 + elsif ref_link = reference_link(reference, identifier)
95 prefix + ref_link + suffix 105 prefix + ref_link + suffix
96 else 106 else
97 match 107 match
98 end 108 end
99 - end if @project 109 + end
  110 + end
100 111
  112 + def parse_emoji(text)
101 # parse emoji 113 # parse emoji
102 text.gsub!(EMOJI_PATTERN) do |match| 114 text.gsub!(EMOJI_PATTERN) do |match|
103 if valid_emoji?($2) 115 if valid_emoji?($2)
@@ -106,8 +118,6 @@ module Gitlab @@ -106,8 +118,6 @@ module Gitlab
106 match 118 match
107 end 119 end
108 end 120 end
109 -  
110 - text  
111 end 121 end
112 122
113 # Private: Checks if an emoji icon exists in the image asset directory 123 # Private: Checks if an emoji icon exists in the image asset directory
spec/helpers/gitlab_markdown_helper_spec.rb
@@ -31,6 +31,7 @@ describe GitlabMarkdownHelper do @@ -31,6 +31,7 @@ describe GitlabMarkdownHelper do
31 end 31 end
32 32
33 it "should not touch HTML entities" do 33 it "should not touch HTML entities" do
  34 + @project.issues.stub(:where).with(id: '39').and_return([issue])
34 actual = expected = "We&#39;ll accept good pull requests." 35 actual = expected = "We&#39;ll accept good pull requests."
35 gfm(actual).should == expected 36 gfm(actual).should == expected
36 end 37 end