Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81153 views
1
var makeDom = require("../utils").makeDom;
2
var helpers = require("../..");
3
var assert = require("assert");
4
5
describe("helpers", function() {
6
describe("removeSubsets", function() {
7
var removeSubsets = helpers.removeSubsets;
8
var dom = makeDom("<div><p><span></span></p><p></p></div>")[0];
9
10
it("removes identical trees", function() {
11
var matches = removeSubsets([dom, dom]);
12
assert.equal(matches.length, 1);
13
});
14
15
it("Removes subsets found first", function() {
16
var matches = removeSubsets([dom, dom.children[0].children[0]]);
17
assert.equal(matches.length, 1);
18
});
19
20
it("Removes subsets found last", function() {
21
var matches = removeSubsets([dom.children[0], dom]);
22
assert.equal(matches.length, 1);
23
});
24
25
it("Does not remove unique trees", function() {
26
var matches = removeSubsets([dom.children[0], dom.children[1]]);
27
assert.equal(matches.length, 2);
28
});
29
});
30
31
describe("compareDocumentPosition", function() {
32
var compareDocumentPosition = helpers.compareDocumentPosition;
33
var markup = "<div><p><span></span></p><a></a></div>";
34
var dom = makeDom(markup)[0];
35
var p = dom.children[0];
36
var span = p.children[0];
37
var a = dom.children[1];
38
39
it("reports when the first node occurs before the second indirectly", function() {
40
assert.equal(compareDocumentPosition(span, a), 2);
41
});
42
43
it("reports when the first node contains the second", function() {
44
assert.equal(compareDocumentPosition(p, span), 10);
45
});
46
47
it("reports when the first node occurs after the second indirectly", function() {
48
assert.equal(compareDocumentPosition(a, span), 4);
49
});
50
51
it("reports when the first node is contained by the second", function() {
52
assert.equal(compareDocumentPosition(span, p), 20);
53
});
54
55
it("reports when the nodes belong to separate documents", function() {
56
var other = makeDom(markup)[0].children[0].children[0];
57
58
assert.equal(compareDocumentPosition(span, other), 1);
59
});
60
61
it("reports when the nodes are identical", function() {
62
assert.equal(compareDocumentPosition(span, span), 0);
63
});
64
});
65
66
describe("uniqueSort", function() {
67
var uniqueSort = helpers.uniqueSort;
68
var dom, p, span, a;
69
70
beforeEach(function() {
71
dom = makeDom("<div><p><span></span></p><a></a></div>")[0];
72
p = dom.children[0];
73
span = p.children[0];
74
a = dom.children[1];
75
});
76
77
it("leaves unique elements untouched", function() {
78
assert.deepEqual(uniqueSort([p, a]), [p, a]);
79
});
80
81
it("removes duplicate elements", function() {
82
assert.deepEqual(uniqueSort([p, a, p]), [p, a]);
83
});
84
85
it("sorts nodes in document order", function() {
86
assert.deepEqual(uniqueSort([a, dom, span, p]), [dom, p, span, a]);
87
});
88
});
89
});
90
91