Library to compare two DOM trees


NodeJS module to compare two DOM-trees

Consider two documents. Expected:

    <!-- comment -->
    <element attribute="10" attributeX="100">
        <text>  text content </text>
            <node />
    <![CDATA[  cdata node]]>

and actual one:

    <element attribute="100">
        <text>text content</text>
        <inner />
        <inner2 />
    <![CDATA[cdata node  ]]>

One can compare them, get the result (is them equals, or not), and get extended report (why them are different).

var compare = require('dom-compare').compare,
    reporter = require('dom-compare').GroupingReporter,
    expected = ..., // expected DOM tree 
    actual = ..., // actual one 
    result, diff, groupedDiff;
// compare to DOM trees, get a result object 
result = compare(expected, actual);
// get comparison result 
console.log(result.getResult()); // false cause' trees are different 
// get all differences 
diff = result.getDifferences(); // array of diff-objects 
// differences, grouped by node XPath 
grouped = reporter.getDifferences(result); // object, key - node XPATH, value - array of differences (strings) 
// string representation 

Diff-object has a following form:

    node: "/document/element",
    message: "Attribute 'attribute': expected value '10' instead of '100'";

By using GroupongReporter one can get a result of a following type

    '/document/element': [
        "Attribute 'attribute': expected value '10' instead of '100'",
        "Extra attribute 'attributeX'"

Comparison function can take a third argument with options like this:

var options = {
    stripSpaces: true,
    compareComments: true
result = compare(expected, actual, options);

By default, all comments are ignored. Set compareComments options to true to compare them too.

By default, all text nodes (text, CDATA, comments if enabled as mentioned above) compared with respect to leading and trailing whitespaces. Set stripSpaces option to true to automatically strip spaces in text and comment nodes. This option doesn't change the way CDATA sections is compared, they are always compared with respect to whitespaces.

Implemented as XMLSerializer interface

Simple rules

  1. Every node (text, node, attribute) on a new line
  2. Empty tags - in a short form
  3. Node indent - 4 spaces, attribute indent - 2 spaces
  4. Attributes are sorted alphabetically

Consider the following XML-document...

  <element>DOM Compare</element>
  <element attribute1="value" attribute2="value">
    <element>Text node</element>

...and code snippet...

var canonizingSerializer = new (require('dom-compare').XMLSerializer)();
var doc = ...; // parse above document somehow  

You'll receive the following output

        DOM Compare
    <emptyNode />
            Text node