HTMLDiff does not drop endlines anymore
This commit is contained in:
parent
beeed3eee6
commit
d7198af00a
|
@ -397,8 +397,8 @@ module HTMLDiff
|
|||
end
|
||||
|
||||
def self.diff(a, b)
|
||||
a, b = a.split(//), b.split(//) if a.kind_of? String and b.kind_of? String
|
||||
a, b = html2list(a), html2list(b)
|
||||
a = html2list(a)
|
||||
b = html2list(b)
|
||||
|
||||
out = Builder.new(a, b)
|
||||
s = SequenceMatcher.new(a, b)
|
||||
|
@ -410,37 +410,25 @@ module HTMLDiff
|
|||
out.result
|
||||
end
|
||||
|
||||
def self.html2list(x, b=false)
|
||||
mode = 'char'
|
||||
def self.html2list(x)
|
||||
mode = :char
|
||||
cur = ''
|
||||
out = []
|
||||
|
||||
x = x.split(//) if x.kind_of? String
|
||||
|
||||
x.each do |c|
|
||||
if mode == 'tag'
|
||||
x.split('').each do |c|
|
||||
if mode == :tag
|
||||
cur += c
|
||||
if c == '>'
|
||||
if b
|
||||
cur += ']'
|
||||
else
|
||||
cur += c
|
||||
end
|
||||
out.push(cur)
|
||||
cur = ''
|
||||
mode = 'char'
|
||||
else
|
||||
cur += c
|
||||
mode = :char
|
||||
end
|
||||
elsif mode == 'char'
|
||||
elsif mode == :char
|
||||
if c == '<'
|
||||
out.push cur
|
||||
if b
|
||||
cur = '['
|
||||
else
|
||||
cur = c
|
||||
end
|
||||
mode = 'tag'
|
||||
elsif /\s/.match c
|
||||
cur = c
|
||||
mode = :tag
|
||||
elsif c =~ /\s/
|
||||
out.push cur + c
|
||||
cur = ''
|
||||
else
|
||||
|
@ -448,28 +436,9 @@ module HTMLDiff
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
out.push cur
|
||||
# TODO: make something better here
|
||||
out.each{|x| x.chomp! unless is_newline(x)}
|
||||
out.find_all { |x| x != '' }
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
if __FILE__ == $0
|
||||
|
||||
require 'pp'
|
||||
# a = "<p>this is the original string</p>" # \n<p>but around the world</p>"
|
||||
# b = "<p>this is the original </p><p>other parag</p><p>string</p>"
|
||||
a = "<ul>\n\t<li>one</li>\n\t<li>two</li>\n</ul>"
|
||||
b = "<ul>\n\t<li>one</li>\n\t<li>two\n\t<ul><li>abc</li></ul></li>\n</ul>"
|
||||
puts a
|
||||
pp HTMLDiff.html2list(a)
|
||||
puts
|
||||
puts b
|
||||
pp HTMLDiff.html2list(b)
|
||||
puts
|
||||
puts HTMLDiff.diff(a, b)
|
||||
end
|
|
@ -7,63 +7,62 @@ include Diff
|
|||
|
||||
class DiffTest < Test::Unit::TestCase
|
||||
def test_init
|
||||
assert(1 == 1, "tests working")
|
||||
assert_nothing_raised("object created") do
|
||||
s = SequenceMatcher.new "private Thread currentThread;",
|
||||
"private volatile Thread currentThread;",
|
||||
proc { |x| x == ' ' }
|
||||
end
|
||||
assert_nothing_raised {
|
||||
s = SequenceMatcher.new('private Thread currentThread;',
|
||||
'private volatile Thread currentThread;') { |x| x == ' ' }
|
||||
}
|
||||
end
|
||||
|
||||
def test_matching_blocks
|
||||
s = SequenceMatcher.new "abxcd", "abcd"
|
||||
assert(s.get_matching_blocks == [[0, 0, 2], [3, 2, 2], [5, 4, 0]],
|
||||
"get_matching_blocks works")
|
||||
s = SequenceMatcher.new 'abxcd', 'abcd'
|
||||
assert_equal [[0, 0, 2], [3, 2, 2], [5, 4, 0]], s.get_matching_blocks
|
||||
end
|
||||
|
||||
def test_ratio
|
||||
s = SequenceMatcher.new "abcd", "bcde"
|
||||
assert(s.ratio == 0.75, "ratio works")
|
||||
assert(s.quick_ratio == 0.75, "quick_ratio works")
|
||||
assert(s.real_quick_ratio == 1.0, "real_quick_ratio works")
|
||||
s = SequenceMatcher.new 'abcd', 'bcde'
|
||||
assert_equal 0.75, s.ratio, 0.001
|
||||
assert_equal 0.75, s.quick_ratio, 0.001
|
||||
assert_equal 1.0, s.real_quick_ratio, 0.001
|
||||
end
|
||||
|
||||
def test_longest_match
|
||||
s = SequenceMatcher.new(" abcd", "abcd abcd")
|
||||
assert(s.find_longest_match(0, 5, 0, 9) == [0, 4, 5],
|
||||
"find_longest_match works")
|
||||
s = SequenceMatcher.new()
|
||||
s = SequenceMatcher.new(' abcd', 'abcd abcd')
|
||||
assert_equal [0, 4, 5], s.find_longest_match(0, 5, 0, 9)
|
||||
end
|
||||
|
||||
def test_opcodes
|
||||
s = SequenceMatcher.new("qabxcd", "abycdf")
|
||||
assert(s.get_opcodes == [
|
||||
[:delete, 0, 1, 0, 0],
|
||||
[:equal, 1, 3, 0, 2],
|
||||
[:replace, 3, 4, 2, 3],
|
||||
[:equal, 4, 6, 3, 5],
|
||||
[:insert, 6, 6, 5, 6]], "get_opcodes works")
|
||||
s = SequenceMatcher.new('qabxcd', 'abycdf')
|
||||
assert_equal(
|
||||
[
|
||||
[:delete, 0, 1, 0, 0],
|
||||
[:equal, 1, 3, 0, 2],
|
||||
[:replace, 3, 4, 2, 3],
|
||||
[:equal, 4, 6, 3, 5],
|
||||
[:insert, 6, 6, 5, 6]
|
||||
],
|
||||
s.get_opcodes)
|
||||
end
|
||||
|
||||
|
||||
def test_count_leading
|
||||
assert(Diff.count_leading(' abc', ' ') == 3,
|
||||
"count_leading works")
|
||||
assert_equal 3, Diff.count_leading(' abc', ' ')
|
||||
end
|
||||
|
||||
def test_html2list
|
||||
a = "here is the original text"
|
||||
#p HTMLDiff.html2list(a)
|
||||
assert_equal(
|
||||
['here ', 'is ', 'the ', 'original ', 'text'],
|
||||
HTMLDiff.html2list(a))
|
||||
end
|
||||
|
||||
def test_html_diff
|
||||
a = "this was the original string"
|
||||
b = "this is the super string"
|
||||
assert_equal 'this <del class="diffmod">was </del>' +
|
||||
a = 'this was the original string'
|
||||
b = 'this is the super string'
|
||||
assert_equal('this <del class="diffmod">was </del>' +
|
||||
'<ins class="diffmod">is </ins>the ' +
|
||||
'<del class="diffmod">original </del>' +
|
||||
'<ins class="diffmod">super </ins>string',
|
||||
HTMLDiff.diff(a, b)
|
||||
HTMLDiff.diff(a, b))
|
||||
end
|
||||
|
||||
def test_html_diff_with_multiple_paragraphs
|
||||
|
@ -79,4 +78,15 @@ class DiffTest < Test::Unit::TestCase
|
|||
HTMLDiff.diff(a, b)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
# FIXME this test fails (ticket #67, http://dev.instiki.org/ticket/67)
|
||||
def test_html_diff_preserves_endlines_in_pre
|
||||
a = "<pre>\na\nb\nc\n</pre>"
|
||||
b = ''
|
||||
|
||||
assert_equal(
|
||||
"<pre>\n<del class=\"diffdel\">a\nb\nc\n</del></pre>",
|
||||
HTMLDiff.diff(a, b))
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue