Class: XHTMLDiff

Inherits:
Object
  • Object
show all
Includes:
REXML
Defined in:
lib/xhtmldiff.rb

Constant Summary collapse

BLOCK_CONTAINERS =
['div', 'ul', 'li']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ XHTMLDiff

Returns a new instance of XHTMLDiff.



81
82
83
# File 'lib/xhtmldiff.rb', line 81

def initialize(output)
  @output = output
end

Instance Attribute Details

#outputObject

Returns the value of attribute output.



57
58
59
# File 'lib/xhtmldiff.rb', line 57

def output
  @output
end

Class Method Details

.diff(a, b) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/xhtmldiff.rb', line 61

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
  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

Instance Method Details

#change(event) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/xhtmldiff.rb', line 96

def change(event)
begin
  sd = diff(event.old_element, event.new_element)
rescue ArgumentError
  debug { $stderr.puts "Not subdiffable: #{$!.message}" }
  sd = nil
end
if sd and ((rs = Float(sd.to_s.size)) / bs = Math.max(event.old_element.to_s.size, event.new_element.to_s.size)) < 2
  debug { $stderr.puts "Chose recursed: rs = #{rs}, bs = #{bs}" }
  @output << sd
else
  debug { $stderr.puts "Chose block: rs = #{rs}, bs = #{bs}" }
  @output << wrap(event.old_element, 'del')
  @output << wrap(event.new_element, 'ins')
end
end

#choose_event(event, element, tag) ⇒ Object



118
119
# File 'lib/xhtmldiff.rb', line 118

def choose_event(event, element, tag)
end

#diff(a, b) ⇒ Object



77
78
79
# File 'lib/xhtmldiff.rb', line 77

def diff(a, b)
  self.class.diff(a,b)
end

#discard_a(event) ⇒ Object

This will be called when there is an element in A that isn’t in B



92
93
94
# File 'lib/xhtmldiff.rb', line 92

def discard_a(event)
@output << wrap(event.old_element, 'del')
end

#discard_b(event) ⇒ Object

This will be called when there is an element in B that isn’t in A



114
115
116
# File 'lib/xhtmldiff.rb', line 114

def discard_b(event)
  @output << wrap(event.new_element, 'ins')
end

#match(event) ⇒ Object

This will be called with both elements are the same



86
87
88
89
# File 'lib/xhtmldiff.rb', line 86

def match(event)
debug { $stderr.puts event.inspect }
  @output << event.old_element.deep_clone if event.old_element
end

#wrap(element, tag) ⇒ Object



121
122
123
124
125
# File 'lib/xhtmldiff.rb', line 121

def wrap(element, tag)
  el = Element.new tag
  el << element.deep_clone
  el
end