Skip to content

Commit

Permalink
Fix issue where change to diff options breaks html highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
samg committed Jul 25, 2014
1 parent f0a6887 commit ad94a7a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
20 changes: 11 additions & 9 deletions lib/diffy/diff.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
module Diffy
class Diff
ORIGINAL_DEFAULT_OPTIONS = {
:diff => '-U 10000',
:source => 'strings',
:include_diff_info => false,
:include_plus_and_minus_in_html => false,
:context => nil,
:allow_empty_diff => true,
}

class << self
attr_writer :default_format
def default_format
@default_format || :text
end

attr_writer :default_options
# default options passed to new Diff objects
attr_writer :default_options
def default_options
@default_options ||= {
:diff => '-U 10000',
:source => 'strings',
:include_diff_info => false,
:include_plus_and_minus_in_html => false,
:context => nil,
:allow_empty_diff => true,
}
@default_options ||= ORIGINAL_DEFAULT_OPTIONS.dup
end

end
Expand Down
4 changes: 3 additions & 1 deletion lib/diffy/html_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Diffy
class HtmlFormatter

def initialize(diff, options = {})
@diff = diff
@options = options
Expand Down Expand Up @@ -71,7 +72,8 @@ def highlighted_words
else
line_diff = Diffy::Diff.new(
split_characters(chunk1),
split_characters(chunk2)
split_characters(chunk2),
Diffy::Diff::ORIGINAL_DEFAULT_OPTIONS
)
hi1 = reconstruct_characters(line_diff, '-')
hi2 = reconstruct_characters(line_diff, '+')
Expand Down
20 changes: 20 additions & 0 deletions spec/diffy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,26 @@ def tempfile(string, fn = 'diffy-spec')
</div>
DIFF
end

it "should correctly do inline hightlighting when default diff options are changed" do
original_options = ::Diffy::Diff.default_options
begin
::Diffy::Diff.default_options.merge!(:diff => '-U0')

s1 = "foo\nbar\nbang"
s2 = "foo\nbar\nbangleize"
expect(Diffy::Diff.new(s1,s2).to_s(:html)).to eq <<-DIFF
<div class="diff">
<ul>
<li class="del"><del>bang</del></li>
<li class="ins"><ins>bang<strong>leize</strong></ins></li>
</ul>
</div>
DIFF
ensure
::Diffy::Diff.default_options = original_options
end
end
end

it "should escape diffed html in html output" do
Expand Down

0 comments on commit ad94a7a

Please sign in to comment.