Skip to content

Commit

Permalink
Merge 392c67e into d3fe6cb
Browse files Browse the repository at this point in the history
  • Loading branch information
Olegas committed Apr 25, 2015
2 parents d3fe6cb + 392c67e commit cffb693
Show file tree
Hide file tree
Showing 20 changed files with 547 additions and 293 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
@@ -1,8 +1,6 @@
language: node_js

node_js:
- 0.6
- 0.8
- 0.10
- 0.12
- "iojs"
Expand Down
85 changes: 83 additions & 2 deletions README.md
Expand Up @@ -6,12 +6,16 @@ dom-compare
[![NPM version](https://badge.fury.io/js/dom-compare.png)](http://badge.fury.io/js/dom-compare)
[![Dependency Status](https://gemnasium.com/Olegas/dom-compare.png)](https://gemnasium.com/Olegas/dom-compare)

**Breaking changes. v0.3.0 requires node version >=0.10**

NodeJS module to compare two DOM-trees

* [DOM Comparison](#dom-comparison)
* [Comparison options](#comparison-options)
* [Comments comparison](#comments-comparison)
* [Whitespace comparison](#whitespace-comparison)
* [Custom comparators](#custom-comparators-since-03)
* [Writing custom comparators](#writing-custom-comparators)
* [Cli utility](#cli-utility)
* [DOM Canonic Form](#dom-canonic-form)

Expand Down Expand Up @@ -77,7 +81,7 @@ Diff-object has a following form:
}
```

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

```javascript
{
Expand Down Expand Up @@ -109,6 +113,84 @@ 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.

#### Custom comparators (since 0.3)

**This is experimental feature and may change in future releases**

Sometimes one needs a special rules to compare some DOM elements.

Imagine you have some nodes with `config` attr which contains JSON data (see `samples/json-compare`).
So, if you'd like to compare such documents, you will see something like this:

```
/document/div
Attribute 'config': expected value '{"attr1":10,"attr2":30,"attr3":-1}' instead of '{"attr1":10,"attr2":20}'
```

This makes not much sense... You can use custom comparators option!

```javascript
var domcompare = require('dom-compare');

// create comparator for specified node name
// you can specify multiple node names here - pass multiple arguments
var configComparator = domcompare.comparators.jsonComparator('config')

var res = domcompare.compare(treeA, treeB, {
comparators: {
// for every attrbute difference, run custom comparison routine
// you can pass multiple comparators here using array
ATTRIBUTE_NODE: configComparator
}
});
```

Bundled JSON comparator can parse node's value like JSON, and if it's parsed,
compare using [rfc6902-json-diff](https://www.npmjs.com/package/rfc6902-json-diff) library.
Using comparators, you can get result like this:

```
/document/div
Attribute "config" differs. Expected:
{
"attr1": 10,
"attr2": 20
}
Actual:
{
"attr1": 10,
"attr2": 30,
"attr3": -1
}
Diff:
[
{
"op": "replace",
"path": "/attr2",
"value": 20
},
{
"op": "remove",
"path": "/attr3"
}
]
```

##### Writing custom comparators

The comparator is a function. It receives two arguments: expected node and actual node.

Comparators can return following values:

* any falsy value - skip to next comparator or (if no any) proceed as regular
* `true` - ignore any differences, continue comparison
* non empty string - difference found, string is treated as error message
* object with fields:
* `message` - error message as above,
* `stop` - boolean flag, set to `true` to stop further comparison

See `lib/comparators/jsonComparator.js` for a working example.

### Cli utility

When installed globally with `npm install -g dom-compare` cli utility is available.
Expand All @@ -128,7 +210,6 @@ You can try it on bundled samples:
/document
Expected CDATA value ' cdata node' instead of 'cdata node '
```


DOM Canonic Form
----------------
Expand Down
2 changes: 1 addition & 1 deletion bin/domcompare
Expand Up @@ -9,7 +9,7 @@ var domcompare = require('../'),
fs = require('fs'),
ArgumentParser = require('argparse').ArgumentParser,
version = require('../package.json').version,
xmldom = require('xmldom'),
xmldom = require('tensor-xmldom'),
path = require('path'),
domparser = new (xmldom.DOMParser)(),
args, dom1, dom2, f1ext, f2ext, mimeMap, result, argparser;
Expand Down
5 changes: 4 additions & 1 deletion index.js
Expand Up @@ -14,7 +14,10 @@
compare: require(libPrefix + '/compare'),
XMLSerializer: require(libPrefix + '/canonizer'),
revXPath: require(libPrefix + '/revxpath'),
GroupingReporter: require(libPrefix + '/reporters/groupingReporter.js')
GroupingReporter: require(libPrefix + '/reporters/groupingReporter.js'),
comparators: {
jsonComparator: require(libPrefix + '/comparators/jsonComparator.js')
}
};

})();
2 changes: 1 addition & 1 deletion lib/canonizer.js
@@ -1,6 +1,6 @@
(function(){

"use strict";
'use strict';

var c = require('./node_types'), spaces = ' ';

Expand Down

0 comments on commit cffb693

Please sign in to comment.