# File lib/xhtmldiff.rb, line 55 def diff(a, b) if a == b return a.deep_clone end if REXML::HashableElementDelegator === a and REXML::HashableElementDelegator === b o = REXML::Element.new(a.name) o.add_attributes a.attributes hd = self.new(o) Diff::LCS.traverse_balanced(a, b, hd) o elsif REXML::Text === a and REXML::Text === b o = REXML::Element.new('span') aa = a.value.split(/\s/) ba = b.value.split(/\s/) hd = XHTMLTextDiff.new(o) Diff::LCS.traverse_balanced(aa, ba, hd) o else raise ArgumentError.new("both arguments must be equal or both be elements. a is #{a.class.name} and b is #{b.class.name}") end end
# File lib/xhtmldiff.rb, line 82 def initialize(output) @output = output end
# File lib/xhtmldiff.rb, line 96 def change(event) begin sd = diff(event.old_element, event.new_element) rescue ArgumentError sd = nil end if sd and (ratio = (Float(rs = sd.to_s.gsub(%r{<(ins|del)>.*</\1>}, '').size) / bs = Math.max(event.old_element.to_s.size, event.new_element.to_s.size))) > 0.5 @output << sd else @output << wrap(event.old_element, 'del') @output << wrap(event.new_element, 'ins') end end
# File lib/xhtmldiff.rb, line 115 def choose_event(event, element, tag) end
# File lib/xhtmldiff.rb, line 78 def diff(a, b) self.class.diff(a,b) end
This will be called when there is an element in A that isn't in B
# File lib/xhtmldiff.rb, line 92 def discard_a(event) @output << wrap(event.old_element, 'del') end
This will be called when there is an element in B that isn't in A
# File lib/xhtmldiff.rb, line 111 def discard_b(event) @output << wrap(event.new_element, 'ins') end
This will be called with both elements are the same
# File lib/xhtmldiff.rb, line 87 def match(event) @output << event.old_element.deep_clone if event.old_element end
# File lib/xhtmldiff.rb, line 118 def wrap(element, tag = nil) if tag el = Element.new tag el << element.deep_clone else el = element.deep_clone end el end