From 0f0af19139db71255934e9a7a5b5cd86420b7186 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rub=C3=A9n=20D=C3=A1vila?= <rdavila84@gmail.com>
Date: Wed, 13 Jan 2016 11:39:15 -0500
Subject: [PATCH] Little refactor for usage of html_safe. #3945

---
 app/helpers/diff_helper.rb                        | 9 +++++----
 app/views/projects/blob/diff.html.haml            | 2 +-
 app/views/projects/diffs/_parallel_view.html.haml | 4 ++--
 lib/gitlab/diff/highlight.rb                      | 4 ++--
 4 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/app/helpers/diff_helper.rb b/app/helpers/diff_helper.rb
index 0ec532a9a9..d49e22e8c8 100644
--- a/app/helpers/diff_helper.rb
+++ b/app/helpers/diff_helper.rb
@@ -1,4 +1,6 @@
 module DiffHelper
+  BLANK_SPACE = "&nbsp;".html_safe
+
   def diff_view
     params[:view] == 'parallel' ? 'parallel' : 'inline'
   end
@@ -83,7 +85,7 @@ module DiffHelper
         elsif next_type == 'old' || next_type.nil?
           # Left side has text removed, right side doesn't have any change
           # No next line code, no new line number, no new line text
-          line = [type, line_old, full_line, line_code, next_type, nil, "&nbsp;", nil]
+          line = [type, line_old, full_line, line_code, next_type, nil, BLANK_SPACE, nil]
           lines.push(line)
         end
       elsif type == 'new'
@@ -93,7 +95,7 @@ module DiffHelper
           next
         else
           # Change is only on the right side, left side has no change
-          line = [nil, nil, "&nbsp;", line_code, type, line_new, full_line, line_code]
+          line = [nil, nil, BLANK_SPACE, line_code, type, line_new, full_line, line_code]
           lines.push(line)
         end
       end
@@ -113,8 +115,7 @@ module DiffHelper
     if line.blank?
       " &nbsp;".html_safe
     else
-      # Return line if it isn't a String, it helps when it's Numeric
-      line.is_a?(String) ? line.html_safe : line
+      line.html_safe
     end
   end
 
diff --git a/app/views/projects/blob/diff.html.haml b/app/views/projects/blob/diff.html.haml
index ef23876677..2e913802be 100644
--- a/app/views/projects/blob/diff.html.haml
+++ b/app/views/projects/blob/diff.html.haml
@@ -11,7 +11,7 @@
       %td.old_line.diff-line-num{data: {linenumber: line_old}}
         = link_to raw(line_old), "#"
       %td.new_line= link_to raw(line_new) , "#"
-      %td.line_content.noteable_line= "#{' ' * @form.indent}#{line}".html_safe
+      %td.line_content.noteable_line==#{' ' * @form.indent}#{line}
 
   - if @form.unfold? && @form.bottom? && @form.to < @blob.loc
     %tr.line_holder{ id: @form.to }
diff --git a/app/views/projects/diffs/_parallel_view.html.haml b/app/views/projects/diffs/_parallel_view.html.haml
index 1ad54d1848..e9108c04ce 100644
--- a/app/views/projects/diffs/_parallel_view.html.haml
+++ b/app/views/projects/diffs/_parallel_view.html.haml
@@ -20,7 +20,7 @@
             = link_to raw(line_number_left), "##{line_code_left}", id: line_code_left
             - if @comments_allowed && can?(current_user, :create_note, @project)
               = link_to_new_diff_note(line_code_left, 'old')
-            %td.line_content{class: "parallel noteable_line #{type_left} #{line_code_left}", "line_code" => line_code_left }= line_content_left.html_safe
+            %td.line_content{class: "parallel noteable_line #{type_left} #{line_code_left}", "line_code" => line_code_left }= line_content_left
 
           - if type_right == 'new'
             - new_line_class = 'new'
@@ -33,7 +33,7 @@
             = link_to raw(line_number_right), "##{new_line_code}", id: new_line_code
             - if @comments_allowed && can?(current_user, :create_note, @project)
               = link_to_new_diff_note(line_code_right, 'new')
-            %td.line_content.parallel{class: "noteable_line #{new_line_class} #{new_line_code}", "line_code" => new_line_code}= line_content_right.html_safe
+            %td.line_content.parallel{class: "noteable_line #{new_line_class} #{new_line_code}", "line_code" => new_line_code}= line_content_right
 
       - if @reply_allowed
         - comments_left, comments_right = organize_comments(type_left, type_right, line_code_left, line_code_right)
diff --git a/lib/gitlab/diff/highlight.rb b/lib/gitlab/diff/highlight.rb
index 0b6a348acb..f940b57d59 100644
--- a/lib/gitlab/diff/highlight.rb
+++ b/lib/gitlab/diff/highlight.rb
@@ -22,7 +22,7 @@ module Gitlab
 
         content = blob.data
         lexer = Rouge::Lexer.guess(filename: file_name, source: content).new rescue Rouge::Lexers::PlainText.new
-        formatter.format(lexer.lex(content)).lines
+        formatter.format(lexer.lex(content)).lines.map!(&:html_safe)
       end
 
       def self.formatter
@@ -73,7 +73,7 @@ module Gitlab
 
           # Only update text if line is found. This will prevent
           # issues with submodules given the line only exists in diff content.
-          line.text = highlighted_line.gsub!(/\A\s/, line_prefix) if highlighted_line
+          line.text = highlighted_line.gsub!(/\A\s/, line_prefix).html_safe if highlighted_line
         end
 
         @lines
-- 
2.30.9