-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtextrangeperformancetests.js
58 lines (47 loc) · 1.93 KB
/
textrangeperformancetests.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
rangy.config.preferTextRange = true;
xn.test.suite("Range miscellaneous", function(s) {
rangy.init();
var elementCount = 1000;
var testCount = 20;
function setUp(t) {
t.testEl = document.createElement("div");
t.testEl.innerHTML = new Array(elementCount + 1).join("One two<b>three four</b>");
document.body.appendChild(t.testEl);
var textRange = document.body.createTextRange();
textRange.moveToElementText(t.testEl);
var textLength = textRange.text.length;
t.textRanges = [];
for (var i = 0, start, end; i < testCount; ++i) {
textRange = document.body.createTextRange();
textRange.moveToElementText(t.testEl);
start = Math.floor(textLength * Math.random());
end = start + Math.floor((textLength - start) * Math.random());
textRange.collapse(true);
textRange.moveEnd("character", end);
textRange.moveStart("character", start);
if (Math.random() < 0.3) {
textRange.collapse(true);
}
t.textRanges[i] = textRange;
}
}
function tearDown(t) {
t.testEl.parentNode.removeChild(t.testEl);
}
if (document.body.createTextRange) {
s.test("TextRange to Range control", function(t) {
//t.assertEquals(t.testEl.childNodes.length, 2 * elementCount);
for (var i = 0, len = t.textRanges.length, range; i < len; ++i) {
t.textRanges[i].select();
}
}, setUp, tearDown);
s.test("TextRange to Range speed test (binary search)", function(t) {
rangy.init();
for (var i = 0, len = t.textRanges.length, sel; i < len; ++i) {
t.textRanges[i].select();
sel = rangy.getSelection();
t.assertEquals(t.textRanges[i].text, sel.toString());
}
}, setUp, tearDown);
}
}, false);