Skip to content

Commit

Permalink
Fix bug in inline diffying of html entities
Browse files Browse the repository at this point in the history
Thanks Ward
  • Loading branch information
samg committed Jun 20, 2011
1 parent 6178b96 commit b82934c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
*.gem
*.swp
Gemfile.lock
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@

group :test, :development do
gem 'rspec', "~>2.0"
end
6 changes: 2 additions & 4 deletions lib/diffy/html_formatter.rb
Expand Up @@ -41,8 +41,6 @@ def highlighted_words
if not chunk2
next ERB::Util.h(chunk1)
end
chunk1 = ERB::Util.h(chunk1)
chunk2 = ERB::Util.h(chunk2)

dir1 = chunk1.each_char.first
dir2 = chunk2.each_char.first
Expand All @@ -57,7 +55,7 @@ def highlighted_words
processed << (index + 1)
[hi1, hi2]
else
chunk1
ERB::Util.h(chunk1)
end
end.flatten
lines.map{|line| line.each_line.map(&:chomp).to_a if line }.flatten.compact.
Expand All @@ -66,7 +64,7 @@ def highlighted_words

def split_characters(chunk)
chunk.gsub(/^./, '').each_line.map do |line|
line.chomp.split('') + ['\n']
(line.chomp.split('') + ['\n']).map{|chr| ERB::Util.h(chr) }
end.flatten.join("\n") + "\n"
end

Expand Down
17 changes: 16 additions & 1 deletion spec/diffy_spec.rb
@@ -1,4 +1,4 @@
require 'spec'
require 'rspec'
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'diffy'))

describe Diffy::Diff do
Expand Down Expand Up @@ -245,6 +245,21 @@ def tempfile(string)
@diff.to_s(:html).should == html
end

it "should not double escape html in wierd edge cases" do
@string1 = "preface = (! title .)+ title &{YYACCEPT}\n"
@string2 = "preface = << (! title .)+ title >> &{YYACCEPT}\n"
@diff = Diffy::Diff.new @string1, @string2
html = <<-HTML
<div class="diff">
<ul>
<li class="del"><del>preface = (! title .)+ title &amp;{YYACCEPT}</del></li>
<li class="ins"><ins>preface = <strong>&lt;&lt; </strong>(! title .)+ title <strong>&gt;&gt; </strong>&amp;{YYACCEPT}</ins></li>
</ul>
</div>
HTML
@diff.to_s(:html).should == html
end

it "should highlight the changes within the line with windows style line breaks" do
@string1 = "hahaha\r\ntime flies like an arrow\r\nfoo bar\r\nbang baz\n"
@string2 = "hahaha\r\nfruit flies like a banana\r\nbang baz\n"
Expand Down

0 comments on commit b82934c

Please sign in to comment.