class HTMLDOMDiff::Node

Attributes

children[R]
parent[R]
rnode[R]
weight[R]

Public Class Methods

new(rnode, lnode, weight, parent=nil) click to toggle source
# 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

Public Instance Methods

add_child(child) click to toggle source
# File lib/html-dom-diff/node.rb, line 15
def add_child(child)
  @children << child
end
as_tree_string(level=0) click to toggle source
# 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
attributes() click to toggle source

attributes

# File lib/html-dom-diff/node.rb, line 24
def attributes
  @rnode.attributes if @rnode
end
changed?() click to toggle source

states

# 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_names() click to toggle source
# 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?() click to toggle source
# File lib/html-dom-diff/node.rb, line 93
def inserted?
  @lnode.nil?
end
matched?() click to toggle source
# File lib/html-dom-diff/node.rb, line 89
def matched?
  ! (inserted? || removed? || moved?)
end
moved?() click to toggle source
# File lib/html-dom-diff/node.rb, line 101
def moved?
  return false if inserted? || removed? || @parent.nil?
  !@parent.parent_of? @lnode, @rnode
end
name() click to toggle source
# File lib/html-dom-diff/node.rb, line 51
def name
  if @rnode
    @rnode.name
  else
    @lnode.name
  end
end
original_attributes() click to toggle source
# File lib/html-dom-diff/node.rb, line 28
def original_attributes
  @lnode.attributes if @lnode
end
original_text() click to toggle source
# File lib/html-dom-diff/node.rb, line 47
def original_text
  @lnode&.text
end
removed?() click to toggle source
# File lib/html-dom-diff/node.rb, line 97
def removed?
  @rnode.nil?
end
self_and_all_children() click to toggle source
# File lib/html-dom-diff/node.rb, line 19
def self_and_all_children
  [self] + @children.map(&:self_and_all_children).flatten
end
state() click to toggle source
# 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
text() click to toggle source
# File lib/html-dom-diff/node.rb, line 43
def text
  @rnode.text
end
text?() click to toggle source
# File lib/html-dom-diff/node.rb, line 59
def text?
  (@rnode||@lnode).text?
end
to_html() click to toggle source
# File lib/html-dom-diff/node.rb, line 63
def to_html
  @rnode.to_html
end

Protected Instance Methods

attributes_changed?() click to toggle source
# File lib/html-dom-diff/node.rb, line 108
def attributes_changed?
  return true if attributes.size != original_attributes.size
  attributes.each do |k, v|
    if original_attributes[k].nil? || v.value != original_attributes[k].value
      return true
    end
  end
  false
end
parent_of?(lchild, rchild) click to toggle source
# File lib/html-dom-diff/node.rb, line 118
def parent_of?(lchild, rchild)
  return false if @lnode.nil? || @rnode.nil?
  return false unless @lnode.children.include?(lchild)
  @rnode.children.include?(rchild)
end