Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'nokogiri'
- module Nokogiri
- module XML
- class Node
- ##
- # Returns true of the argument is semantically equivalent to self
- # Equivalent is defined as follows :
- # - same name
- # - same attributes
- # - same same default namespace (different definitions are ok)
- # - same number of children
- # - each child is equivalent to one child of the argument
- # - the order of the children can be different
- # - text content can be different by whitespace
- def ===(_node)
- _node = _node.root if _node.respond_to? :root
- return root === _node if respond_to? :root
- return false unless self.class == _node.class
- if _node.name != self.name
- false
- # whitespace insensitive (not always accurate, but close)
- elsif _node.text.gsub(/\s/,'') != self.text.gsub(/\s/,'')
- false
- elsif (
- !_node.is_a?(Nokogiri::XML::Attr) and (
- (!_node.namespace.nil? and self.namespace.nil?) or
- (_node.namespace.nil? and !self.namespace.nil?) or
- (!_node.namespace.nil? and !self.namespace.nil? and
- _node.namespace.href != self.namespace.href)
- )
- )
- false
- elsif _node.attributes.count != self.attributes.count
- false
- elsif _node.children.count != self.children.count
- false
- else
- attrs_match = _node.attributes.all? {|n,a|
- attributes.any? {|m,b| a === b }
- }
- children_match = _node.children.all? { |c|
- children.any? {|d| d === c }
- }
- attrs_match && children_match
- end
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment