Commit d993f666423744d213d7cce063b48e71fa751d43

Authored by Robert Speicher
1 parent 85def2d6

Fix HTML entities being parsed in GFM

Also fixes the spec so that it actually tests the thing it says it's
testing. Hooray!

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