Class: HTMLDOMDiff::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/html-dom-diff/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rnode, lnode, weight, parent = nil) ⇒ Node



7
8
9
10
11
12
13
# File 'lib/html-dom-diff/node.rb', line 7

def initialize(rnode, lnode, weight, parent=nil)
  @rnode    = rnode
  @lnode    = lnode
  @weight   = weight
  @parent   = parent
  @children = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



3
4
5
# File 'lib/html-dom-diff/node.rb', line 3

def children
  @children
end

#parentObject (readonly)

Returns the value of attribute parent.



3
4
5
# File 'lib/html-dom-diff/node.rb', line 3

def parent
  @parent
end

#rnodeObject (readonly)

Returns the value of attribute rnode.



5
6
7
# File 'lib/html-dom-diff/node.rb', line 5

def rnode
  @rnode
end

#weightObject (readonly)

Returns the value of attribute weight.



3
4
5
# File 'lib/html-dom-diff/node.rb', line 3

def weight
  @weight
end

Instance Method Details

#add_child(child) ⇒ Object



15
16
17
# File 'lib/html-dom-diff/node.rb', line 15

def add_child(child)
  @children << child
end

#as_tree_string(level = 0) ⇒ Object



67
68
69
70
71
# File 'lib/html-dom-diff/node.rb', line 67

def as_tree_string(level=0)
  result  = [(" "*level) + "#{name} - :#{state}"]
  result += children.map { |c| c.as_tree_string(level+1) }
  result.join("\n")
end

#attributesObject

attributes



24
25
26
# File 'lib/html-dom-diff/node.rb', line 24

def attributes
  @rnode.attributes if @rnode
end

#changed?Boolean

states



74
75
76
77
78
79
80
81
# File 'lib/html-dom-diff/node.rb', line 74

def changed?
  return false unless @rnode && @lnode
  if @rnode.text?
    text != original_text
  else
    attributes_changed?
  end
end

#changed_attribute_namesObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/html-dom-diff/node.rb', line 32

def changed_attribute_names
  result = []
  attributes.each do |k, v|
    result << k if original_attributes[k].nil? || (v.value != original_attributes[k].value)
  end
  original_attributes.each do |k, v|
    result << k if attributes[k].nil? || (v.value != attributes[k].value)
  end
  result.uniq
end

#inserted?Boolean



93
94
95
# File 'lib/html-dom-diff/node.rb', line 93

def inserted?
  @lnode.nil?
end

#matched?Boolean



89
90
91
# File 'lib/html-dom-diff/node.rb', line 89

def matched?
  ! (inserted? || removed? || moved?)
end

#moved?Boolean



101
102
103
104
# File 'lib/html-dom-diff/node.rb', line 101

def moved?
  return false if inserted? || removed? || @parent.nil?
  !@parent.parent_of? @lnode, @rnode
end

#nameObject



51
52
53
54
55
56
57
# File 'lib/html-dom-diff/node.rb', line 51

def name
  if @rnode
    @rnode.name
  else
    @lnode.name
  end
end

#original_attributesObject



28
29
30
# File 'lib/html-dom-diff/node.rb', line 28

def original_attributes
  @lnode.attributes if @lnode
end

#original_textObject



47
48
49
# File 'lib/html-dom-diff/node.rb', line 47

def original_text
  @lnode&.text
end

#removed?Boolean



97
98
99
# File 'lib/html-dom-diff/node.rb', line 97

def removed?
  @rnode.nil?
end

#self_and_all_childrenObject



19
20
21
# File 'lib/html-dom-diff/node.rb', line 19

def self_and_all_children
  [self] + @children.map(&:self_and_all_children).flatten
end

#stateObject



83
84
85
86
87
# File 'lib/html-dom-diff/node.rb', line 83

def state
  [:moved, :inserted, :removed, :matched].each do |_state|
    return _state if send("#{_state}?")
  end
end

#textObject



43
44
45
# File 'lib/html-dom-diff/node.rb', line 43

def text
  @rnode.text
end

#text?Boolean



59
60
61
# File 'lib/html-dom-diff/node.rb', line 59

def text?
  (@rnode||@lnode).text?
end

#to_htmlObject



63
64
65
# File 'lib/html-dom-diff/node.rb', line 63

def to_html
  @rnode.to_html
end