Module: CompareXML

Defined in:
lib/compare-xml.rb,
lib/compare-xml/version.rb

Constant Summary collapse

DEFAULTS_OPTS =

default options used by the module; all of these can be overridden

{
  # when true, trims and collapses whitespace in text nodes and comments to a single space
  # when false, all whitespace is preserved as it is without any changes
  collapse_whitespace: true,

  # when true, attribute order is not important (all attributes are sorted before comparison)
  # when false, attributes are compared in order and comparison stops on the first mismatch
  ignore_attr_order: true,

  # when true, children of elements are always compared
  # when false, children of elements are not compared if the root is different
  force_children: false,

  # when true, children of elements are never compared
  # when false, children of elements are compared if root is different or see force_children
  ignore_children: false,

  # contains an array of user specified strings that is used to ignore any attributes
  # whose content contains a string from this array (e.g. "good automobile" contains "mobile")
  ignore_attr_content: [],

  # contains an array of user-specified CSS rules used to perform attribute exclusions
  # for this to work, a CSS rule MUST contain the attribute to be excluded,
  # i.e. a[href] will exclude all "href" attributes contained in <a> tags.
  ignore_attrs: [],

  # contains an array of user specified strings that is used to ignore any attributes
  # whose name contains a string from this array (e.g. "good automobile" contains "mobile")
  ignore_attrs_by_name: [],

  # when true ignores XML and HTML comments
  # when false, all comments are compared to their counterparts
  ignore_comments: true,

  # contains an array of user-specified CSS rules used to perform node exclusions
  ignore_nodes: [],

  # when true, ignores all text nodes (although blank text nodes are always ignored)
  # when false, all text nodes are compared to their counterparts (except the empty ones)
  ignore_text_nodes: false,

  # when true, provides a list of all error messages encountered in comparisons
  # when false, execution stops when the first error is encountered with no error messages
  verbose: false
}.freeze
EQUIVALENT =

used internally only in order to differentiate equivalence for inequivalence

1
MISSING_ATTRIBUTE =

a list of all possible inequivalence types for nodes these are returned in the differences array to differentiate error types.

2
MISSING_NODE =

attribute is missing its counterpart

3
UNEQUAL_ATTRIBUTES =

node is missing its counterpart

4
UNEQUAL_COMMENTS =

attributes are not equal

5
UNEQUAL_DOCUMENTS =

comment contents are not equal

6
UNEQUAL_ELEMENTS =

document types are not equal

7
UNEQUAL_NODES_TYPES =

nodes have the same type but are not equal

8
UNEQUAL_TEXT_CONTENTS =

nodes do not have the same type

9
VERSION =
'0.66'.freeze

Class Method Summary collapse

Class Method Details

.equivalent?(n1, n2, opts = {}, childopts = {}, diffchildren = false) ⇒ Boolean

Determines whether two XML documents or fragments are equal to each other. The two parameters could be any type of XML documents, or fragments or node sets or even text nodes - any subclass of Nokogiri::XML::Node.

@param [Nokogiri::XML::Element] n1 left node element
@param [Nokogiri::XML::Element] n2 right node element
@param [Hash] opts user-overridden options
@param [Hash] childopts user-overridden options used for the child nodes
@param [Bool] diffchildren use different options for the child nodes

@return true if equal, [Array] differences otherwise

Returns:

  • (Boolean)


80
81
82
83
84
85
86
# File 'lib/compare-xml.rb', line 80

def equivalent?(n1, n2, opts = {}, childopts = {}, diffchildren = false)
  opts = DEFAULTS_OPTS.merge(opts)
  childopts = DEFAULTS_OPTS.merge(childopts)
  differences = []
  result = diffchildren ? compareNodes(n1, n2, opts, differences, childopts, diffchildren) : compareNodes(n1, n2, opts, differences)
  opts[:verbose] ? differences : result == EQUIVALENT
end