From 6c4650b1dac5fc308070c5ffc2ed6e7e42d92e9c Mon Sep 17 00:00:00 2001 From: Pankaj Tyagi Date: Tue, 1 Oct 2019 11:28:16 -0400 Subject: [PATCH] Add option `:ignore_crlf` to fix #105 --- lib/diffy/html_formatter.rb | 12 ++++++++---- spec/diffy_spec.rb | 7 +++++++ 2 files changed, 15 insertions(+), 4 deletions(-) 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"