Guest User

Untitled

a guest
Jul 23rd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. require 'nokogiri'
  2.  
  3. module Nokogiri
  4. module XML
  5. class Node
  6.  
  7. ##
  8. # Returns true of the argument is semantically equivalent to self
  9. # Equivalent is defined as follows :
  10. # - same name
  11. # - same attributes
  12. # - same same default namespace (different definitions are ok)
  13. # - same number of children
  14. # - each child is equivalent to one child of the argument
  15. # - the order of the children can be different
  16. # - text content can be different by whitespace
  17. def ===(_node)
  18. _node = _node.root if _node.respond_to? :root
  19. return root === _node if respond_to? :root
  20. return false unless self.class == _node.class
  21.  
  22. if _node.name != self.name
  23. false
  24. # whitespace insensitive (not always accurate, but close)
  25. elsif _node.text.gsub(/\s/,'') != self.text.gsub(/\s/,'')
  26. false
  27. elsif (
  28. !_node.is_a?(Nokogiri::XML::Attr) and (
  29. (!_node.namespace.nil? and self.namespace.nil?) or
  30. (_node.namespace.nil? and !self.namespace.nil?) or
  31. (!_node.namespace.nil? and !self.namespace.nil? and
  32. _node.namespace.href != self.namespace.href)
  33. )
  34. )
  35. false
  36. elsif _node.attributes.count != self.attributes.count
  37. false
  38. elsif _node.children.count != self.children.count
  39. false
  40. else
  41. attrs_match = _node.attributes.all? {|n,a|
  42. attributes.any? {|m,b| a === b }
  43. }
  44. children_match = _node.children.all? { |c|
  45. children.any? {|d| d === c }
  46. }
  47.  
  48. attrs_match && children_match
  49. end
  50.  
  51. end
  52. end
  53. end
  54. end
Add Comment
Please, Sign In to add comment