Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option :ignore_crlf to fix #105 #106

Merged
merged 1 commit into from Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -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)
"<div class=\"diff\"></div>"



Default Diff Options
--------------------

Expand Down
12 changes: 8 additions & 4 deletions lib/diffy/html_formatter.rb
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions spec/diffy_spec.rb
Expand Up @@ -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 = "<div class=\"diff\"></div>"
expect(@diff.to_s(:html)).to eq(empty_diff)
end

describe 'with lines that include \n' do
before do
string1 = 'a\nb'"\n"
Expand Down