Fixed that partial diffs are done against html instead of original code (#9143).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8876 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2012-02-15 17:39:47 +00:00
parent 008557581d
commit 6aae25209a
2 changed files with 48 additions and 24 deletions

View File

@ -112,11 +112,6 @@ module Redmine
private private
# Escape the HTML for the diff
def escapeHTML(line)
CGI.escapeHTML(line)
end
def diff_for_added_line def diff_for_added_line
if @type == 'sbs' && @removed > 0 && @added < @removed if @type == 'sbs' && @removed > 0 && @added < @removed
self[-(@removed - @added)] self[-(@removed - @added)]
@ -130,7 +125,7 @@ module Redmine
def parse_line(line, type="inline") def parse_line(line, type="inline")
if line[0, 1] == "+" if line[0, 1] == "+"
diff = diff_for_added_line diff = diff_for_added_line
diff.line_right = escapeHTML line[1..-1] diff.line_right = line[1..-1]
diff.nb_line_right = @line_num_r diff.nb_line_right = @line_num_r
diff.type_diff_right = 'diff_in' diff.type_diff_right = 'diff_in'
@line_num_r += 1 @line_num_r += 1
@ -138,7 +133,7 @@ module Redmine
true true
elsif line[0, 1] == "-" elsif line[0, 1] == "-"
diff = Diff.new diff = Diff.new
diff.line_left = escapeHTML line[1..-1] diff.line_left = line[1..-1]
diff.nb_line_left = @line_num_l diff.nb_line_left = @line_num_l
diff.type_diff_left = 'diff_out' diff.type_diff_left = 'diff_out'
self << diff self << diff
@ -149,9 +144,9 @@ module Redmine
write_offsets write_offsets
if line[0, 1] =~ /\s/ if line[0, 1] =~ /\s/
diff = Diff.new diff = Diff.new
diff.line_right = escapeHTML line[1..-1] diff.line_right = line[1..-1]
diff.nb_line_right = @line_num_r diff.nb_line_right = @line_num_r
diff.line_left = escapeHTML line[1..-1] diff.line_left = line[1..-1]
diff.nb_line_left = @line_num_l diff.nb_line_left = @line_num_l
self << diff self << diff
@line_num_l += 1 @line_num_l += 1
@ -224,27 +219,15 @@ module Redmine
end end
def html_line_left def html_line_left
if offsets line_to_html(line_left, offsets)
line_left.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>')
else
line_left
end
end end
def html_line_right def html_line_right
if offsets line_to_html(line_right, offsets)
line_right.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>')
else
line_right
end
end end
def html_line def html_line
if offsets line_to_html(line, offsets)
line.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>')
else
line
end
end end
def inspect def inspect
@ -254,5 +237,23 @@ module Redmine
puts self.nb_line_right puts self.nb_line_right
puts self.line_right puts self.line_right
end end
private
def line_to_html(line, offsets)
if offsets
s = ''
unless offsets.first == 0
s << CGI.escapeHTML(line[0..offsets.first-1])
end
s << '<span>' + CGI.escapeHTML(line[offsets.first..offsets.last]) + '</span>'
unless offsets.last == -1
s << CGI.escapeHTML(line[offsets.last+1..-1])
end
s
else
CGI.escapeHTML(line)
end
end
end end
end end

View File

@ -91,6 +91,29 @@ class Redmine::UnifiedDiffTest < ActiveSupport::TestCase
end end
def test_partials_with_html_entities
raw = <<-DIFF
--- test.orig.txt Wed Feb 15 16:10:39 2012
+++ test.new.txt Wed Feb 15 16:11:25 2012
@@ -1,5 +1,5 @@
Semicolons were mysteriously appearing in code diffs in the repository
-void DoSomething(std::auto_ptr<MyClass> myObj)
+void DoSomething(const MyClass& myObj)
DIFF
diff = Redmine::UnifiedDiff.new(raw, :type => 'sbs')
assert_equal 1, diff.size
assert_equal 'void DoSomething(<span>std::auto_ptr&lt;MyClass&gt;</span> myObj)', diff.first[2].html_line_left
assert_equal 'void DoSomething(<span>const MyClass&amp;</span> myObj)', diff.first[2].html_line_right
diff = Redmine::UnifiedDiff.new(raw, :type => 'inline')
assert_equal 1, diff.size
assert_equal 'void DoSomething(<span>std::auto_ptr&lt;MyClass&gt;</span> myObj)', diff.first[2].html_line
assert_equal 'void DoSomething(<span>const MyClass&amp;</span> myObj)', diff.first[3].html_line
end
def test_line_starting_with_dashes def test_line_starting_with_dashes
diff = Redmine::UnifiedDiff.new(<<-DIFF diff = Redmine::UnifiedDiff.new(<<-DIFF
--- old.txt Wed Nov 11 14:24:58 2009 --- old.txt Wed Nov 11 14:24:58 2009