Merge branch 'toEqualDomNodes' of https://github.com/alexeibs/jasmine into alexeibs-toEqualDomNodes
This commit is contained in:
@@ -2288,6 +2288,29 @@ getJasmineRequireObj().matchersUtil = function(j$) {
|
|||||||
a.ignoreCase == b.ignoreCase;
|
a.ignoreCase == b.ignoreCase;
|
||||||
}
|
}
|
||||||
if (typeof a != 'object' || typeof b != 'object') { return false; }
|
if (typeof a != 'object' || typeof b != 'object') { return false; }
|
||||||
|
|
||||||
|
var aIsDomNode = j$.isDomNode(a);
|
||||||
|
var bIsDomNode = j$.isDomNode(b);
|
||||||
|
if (aIsDomNode && bIsDomNode) {
|
||||||
|
// At first try to use DOM3 method isEqualNode
|
||||||
|
if (a.isEqualNode) {
|
||||||
|
return a.isEqualNode(b);
|
||||||
|
}
|
||||||
|
// IE8 doesn't support isEqualNode, try to use outerHTML && innerText
|
||||||
|
var aIsElement = a instanceof Element;
|
||||||
|
var bIsElement = b instanceof Element;
|
||||||
|
if (aIsElement && bIsElement) {
|
||||||
|
return a.outerHTML == b.outerHTML;
|
||||||
|
}
|
||||||
|
if (aIsElement || bIsElement) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return a.innerText == b.innerText && a.textContent == b.textContent;
|
||||||
|
}
|
||||||
|
if (aIsDomNode || bIsDomNode) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Assume equality for cyclic structures. The algorithm for detecting cyclic
|
// Assume equality for cyclic structures. The algorithm for detecting cyclic
|
||||||
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
|
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
|
||||||
var length = aStack.length;
|
var length = aStack.length;
|
||||||
|
|||||||
@@ -132,6 +132,45 @@ describe("matchersUtil", function() {
|
|||||||
expect(j$.matchersUtil.equals(a,b)).toBe(true);
|
expect(j$.matchersUtil.equals(a,b)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("passes for equivalent DOM nodes", function() {
|
||||||
|
if (typeof document === 'undefined') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var a = document.createElement("div");
|
||||||
|
a.setAttribute("test-attr", "attr-value")
|
||||||
|
a.appendChild(document.createTextNode('test'));
|
||||||
|
|
||||||
|
var b = document.createElement("div");
|
||||||
|
b.setAttribute("test-attr", "attr-value")
|
||||||
|
b.appendChild(document.createTextNode('test'));
|
||||||
|
|
||||||
|
expect(j$.matchersUtil.equals(a,b)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("fails for DOM nodes with different attributes or child nodes", function() {
|
||||||
|
if (typeof document === 'undefined') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var a = document.createElement("div");
|
||||||
|
a.setAttribute("test-attr", "attr-value")
|
||||||
|
a.appendChild(document.createTextNode('test'));
|
||||||
|
|
||||||
|
var b = document.createElement("div");
|
||||||
|
b.setAttribute("test-attr", "attr-value2")
|
||||||
|
b.appendChild(document.createTextNode('test'));
|
||||||
|
|
||||||
|
expect(j$.matchersUtil.equals(a,b)).toBe(false);
|
||||||
|
|
||||||
|
b.setAttribute("test-attr", "attr-value");
|
||||||
|
expect(j$.matchersUtil.equals(a,b)).toBe(true);
|
||||||
|
|
||||||
|
b.appendChild(document.createTextNode('2'));
|
||||||
|
expect(j$.matchersUtil.equals(a,b)).toBe(false);
|
||||||
|
|
||||||
|
a.appendChild(document.createTextNode('2'));
|
||||||
|
expect(j$.matchersUtil.equals(a,b)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
it("passes when Any is used", function() {
|
it("passes when Any is used", function() {
|
||||||
var number = 3,
|
var number = 3,
|
||||||
anyNumber = new j$.Any(Number);
|
anyNumber = new j$.Any(Number);
|
||||||
|
|||||||
@@ -110,6 +110,29 @@ getJasmineRequireObj().matchersUtil = function(j$) {
|
|||||||
a.ignoreCase == b.ignoreCase;
|
a.ignoreCase == b.ignoreCase;
|
||||||
}
|
}
|
||||||
if (typeof a != 'object' || typeof b != 'object') { return false; }
|
if (typeof a != 'object' || typeof b != 'object') { return false; }
|
||||||
|
|
||||||
|
var aIsDomNode = j$.isDomNode(a);
|
||||||
|
var bIsDomNode = j$.isDomNode(b);
|
||||||
|
if (aIsDomNode && bIsDomNode) {
|
||||||
|
// At first try to use DOM3 method isEqualNode
|
||||||
|
if (a.isEqualNode) {
|
||||||
|
return a.isEqualNode(b);
|
||||||
|
}
|
||||||
|
// IE8 doesn't support isEqualNode, try to use outerHTML && innerText
|
||||||
|
var aIsElement = a instanceof Element;
|
||||||
|
var bIsElement = b instanceof Element;
|
||||||
|
if (aIsElement && bIsElement) {
|
||||||
|
return a.outerHTML == b.outerHTML;
|
||||||
|
}
|
||||||
|
if (aIsElement || bIsElement) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return a.innerText == b.innerText && a.textContent == b.textContent;
|
||||||
|
}
|
||||||
|
if (aIsDomNode || bIsDomNode) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Assume equality for cyclic structures. The algorithm for detecting cyclic
|
// Assume equality for cyclic structures. The algorithm for detecting cyclic
|
||||||
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
|
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
|
||||||
var length = aStack.length;
|
var length = aStack.length;
|
||||||
|
|||||||
Reference in New Issue
Block a user