Skip to content

Commit

Permalink
Merge pull request #106 from ptyagi16/issue-105-option-ignore-crlf
Browse files Browse the repository at this point in the history
Add option `:ignore_crlf` to fix #105
  • Loading branch information
samg committed Oct 16, 2019
2 parents fbfc4e0 + a59e6ee commit c4fdc75
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
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

0 comments on commit c4fdc75

Please sign in to comment.