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, 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,

    # 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: {},

    # 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, 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, 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
}
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 errors 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.5.2'

Class Method Summary collapse

Class Method Details

.equivalent?(n1, n2, opts = {}) ⇒ 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::Node] n1 left attribute
@param [Nokogiri::XML::Node] n2 right attribute
@param [Hash] opts user-overridden options

@return true if equal, [Array] errors otherwise

Returns:

  • (Boolean)


65
66
67
68
69
# File 'lib/compare-xml.rb', line 65

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