This repository has been archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
137 lines (120 loc) · 4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const type = require('dom-compare-temp/lib/node_types');
const Collector = require('dom-compare-temp/lib/collector');
function Comparator(options, collector) {
this._options = options || {};
if(!collector)
throw new Error('Collector instance must be specified');
this._collector = collector;
}
Comparator.prototype._filterNodes = function(list) {
var ret = [],
i, l, item;
for (i = 0, l = list.length; i < l; i++) {
item = list.item(i);
if (item.nodeType == type.COMMENT_NODE && !this._options.compareComments)
continue;
if (item.nodeType == type.TEXT_NODE && ('' + item.nodeValue).trim() == '')
continue;
ret.push(item);
}
return ret;
};
Comparator.prototype._compareNodeList = function(left, right) {
var lLeft = this._filterNodes(left),
lRight = this._filterNodes(right),
i, l;
for (i = 0, l = Math.max(lLeft.length, lRight.length); i < l; i++) {
if(lLeft[i] && lRight[i]) {
if (!this.compareNode(lLeft[i], lRight[i]))
return false;
}
else {
return this._collector.collectFailure(lLeft[i], lRight[i]);
}
}
return true;
};
Comparator.prototype._compareAttributes = function(expected, actual) {
var aExpected = {}, aActual = {},
i, l;
if (!expected && !actual)
return true;
for(i = 0, l = expected.length; i < l; i++) {
aExpected[expected[i].nodeName] = expected[i];
}
for(i = 0, l = actual.length; i < l; i++) {
aActual[actual[i].nodeName] = actual[i];
}
for(i in aExpected) {
// both nodes has an attribute
if(aExpected.hasOwnProperty(i) && aActual.hasOwnProperty(i)) {
// but values is differ
var vExpected = aExpected[i].nodeValue;
var vActual = aActual[i].nodeValue;
if(this._options.stripSpaces && aExpected[i].nodeType != type.CDATA_SECTION_NODE) {
vExpected = vExpected.trim();
vActual = vActual.trim();
}
if(vExpected !== vActual) {
if(!this._collector.collectFailure(aExpected[i], aActual[i]))
return false;
}
// remove to check for extra/missed attributes;
delete aActual[i];
delete aExpected[i];
}
}
// report all missed attributes
for(i in aExpected) {
if(aExpected.hasOwnProperty(i))
if(!this._collector.collectFailure(aExpected[i], null))
return false;
}
// report all extra attributes
for(i in aActual) {
if(aActual.hasOwnProperty(i))
if(!this._collector.collectFailure(null, aActual[i]))
return false;
}
return true;
};
Comparator.prototype.compareNode = function(left, right) {
var vLeft, vRight, r;
if (left.nodeName === right.nodeName && left.nodeType == right.nodeType) {
switch (left.nodeType) {
case type.DOCUMENT_NODE:
return this.compareNode(left.documentElement, right.documentElement);
case type.ELEMENT_NODE:
return this._compareAttributes(left.attributes, right.attributes) &&
this._compareNodeList(left.childNodes, right.childNodes);
case type.TEXT_NODE:
// fallthrough
case type.CDATA_SECTION_NODE:
// fallthrough
case type.DOCUMENT_FRAGMENT_NODE:
// fallthrough
case type.COMMENT_NODE:
if (left.nodeType == type.COMMENT_NODE && !this._options.compareComments)
return true;
vLeft = '' + left.nodeValue;
vRight = '' + right.nodeValue;
if (this._options.stripSpaces && left.nodeType !== type.CDATA_SECTION_NODE) {
vLeft = vLeft.trim();
vRight = vRight.trim();
}
r = vLeft === vRight;
return !r ? this._collector.collectFailure(left, right) : r;
default:
throw Error('Node type ' + left.nodeType + ' comparison is not implemented');
}
} else
return this._collector.collectFailure(left, right);
};
module.exports = function patch(originalModule) {
originalModule.compare = function(a, b, options) {
var collector = new Collector(options);
var comparator = new Comparator(options, collector);
comparator.compareNode(a, b);
return collector;
};
};