Class: Nokogiri::XML::Node

Inherits:
Object
  • Object
show all
Includes:
TDiff, TDiff::Unordered
Defined in:
lib/nokogiri/diff/xml/node.rb

Direct Known Subclasses

Document

Instance Method Summary collapse

Instance Method Details

#diff(other, options = {}) {|change, node| ... } ⇒ Enumerator

Finds the differences between the node and another node.

Parameters:

  • other (Nokogiri::XML::Node)

    The other node to compare against.

  • options (Hash) (defaults to: {})

    Additional options for filtering changes.

Options Hash (options):

  • :added (Boolean)

    Yield nodes that were added.

  • :removed (Boolean)

    Yield nodes that were removed.

Yields:

  • (change, node)

    The given block will be passed each changed node.

Yield Parameters:

  • change (' ', '-', '+')

    Indicates whether the node stayed the same, was removed or added.

  • node (Nokogiri::XML::Node)

    The changed node.

Returns:

  • (Enumerator)

    If no block was given, an Enumerator object will be returned.



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/nokogiri/diff/xml/node.rb', line 86

def diff(other,options={},&block)
  return enum_for(__method__,other,options) unless block

  if (options[:added] || options[:removed])
    tdiff_unordered(other) do |change,node|
      if    (change == '+' && options[:added])   then yield change, node
      elsif (change == '-' && options[:removed]) then yield change, node
      end
    end
  else
    tdiff(other,&block)
  end
end

#tdiff_each_child(node) {|child| ... } ⇒ Object

Enumerates over the children of another XML/HTML node.

Parameters:

Yields:

  • (child)

    The given block will be passed every child of the node.

Yield Parameters:



51
52
53
54
55
56
57
# File 'lib/nokogiri/diff/xml/node.rb', line 51

def tdiff_each_child(node,&block)
  if node.kind_of?(Nokogiri::XML::Element)
    node.attribute_nodes.sort_by(&:name).each(&block)
  end

  node.children.each(&block)
end

#tdiff_equal(node) ⇒ Boolean

Compares the XML/HTML node with another.

Parameters:

Returns:

  • (Boolean)

    Specifies whether the two nodes are equal.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nokogiri/diff/xml/node.rb', line 20

def tdiff_equal(node)
  if (self.class == node.class)
    case node
    when Nokogiri::XML::Attr
      (self.name == node.name && self.value == node.value)
    when Nokogiri::XML::Element, Nokogiri::XML::DTD
      self.name == node.name
    when Nokogiri::XML::Text, Nokogiri::XML::Comment
      self.text == node.text
    when Nokogiri::XML::ProcessingInstruction
      (self.name == node.name && self.content == node.content)
    else
      false
    end
  else
    false
  end
end