diff --git a/README.md b/README.md index e471fa7..c4438e2 100644 --- a/README.md +++ b/README.md @@ -277,6 +277,15 @@ combined with the `:context` option. foo bar +### `:ignore_crlf` when doing HTML compares + +You can make the HTML output ignore the CRLF by passing the `:ignore_crlf` option a truthy value. + + >> puts Diffy::Diff.new(" foo\nbar\n", "foo\r\nbar\r\n", ignore_crlf: true).to_s(:html) + "
" + + + Default Diff Options -------------------- diff --git a/lib/diffy/html_formatter.rb b/lib/diffy/html_formatter.rb index a72ee64..9f50668 100644 --- a/lib/diffy/html_formatter.rb +++ b/lib/diffy/html_formatter.rb @@ -90,10 +90,14 @@ def highlighted_words def split_characters(chunk) chunk.gsub(/^./, '').each_line.map do |line| - chars = line.sub(/([\r\n]$)/, '').split('') - # add escaped newlines - chars << '\n' - chars.map{|chr| ERB::Util.h(chr) } + if @options[:ignore_crlf] + (line.chomp.split('') + ['\n']).map{|chr| ERB::Util.h(chr) } + else + chars = line.sub(/([\r\n]$)/, '').split('') + # add escaped newlines + chars << '\n' + chars.map{|chr| ERB::Util.h(chr) } + end end.flatten.join("\n") + "\n" end diff --git a/spec/diffy_spec.rb b/spec/diffy_spec.rb index c84f8ab..551c6c7 100644 --- a/spec/diffy_spec.rb +++ b/spec/diffy_spec.rb @@ -503,6 +503,13 @@ def tempfile(string, fn = 'diffy-spec') expect(@diff.to_s(:html)).to eq(html) end + it "should treat unix vs windows newlines as same if option :ignore_crlf" do + @diff = Diffy::Diff.new("one\ntwo\nthree\n", "one\r\ntwo\r\nthree\r\n", + ignore_crlf: true) + empty_diff = "
" + expect(@diff.to_s(:html)).to eq(empty_diff) + end + describe 'with lines that include \n' do before do string1 = 'a\nb'"\n"