This repository was archived by the owner on May 26, 2025. It is now read-only.
forked from bitovi/jquerypp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrange_test.js
More file actions
205 lines (151 loc) · 5.14 KB
/
range_test.js
File metadata and controls
205 lines (151 loc) · 5.14 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
steal("funcunit/qunit", "jquery/dom/range", "jquery/dom/selection").then(function () {
module("jquery/dom/range");
test("basic range", function () {
$("#qunit-test-area")
.html("<p id='1'>0123456789</p>");
$('#1').selection(1, 5);
var range = $.Range.current();
equals(range.start().offset, 1, "start is 1")
equals(range.end().offset, 5, "end is 5")
});
test("jquery helper, start, select", function () {
var range = $('#qunit-test-area').html("<span>Hello</span> <b>World</b><i>!</i>").range();
range.start("+2");
range.end("-2");
range.select();
equals(range.toString(), "llo Wor")
})
test('jQuery helper', function () {
$("#qunit-test-area").html("<div id='selectMe'>thisTextIsSelected</div>")
var range = $('#selectMe').range();
equals(range.toString(), "thisTextIsSelected")
});
test("constructor with undefined", function () {
var range = $.Range();
equals(document, range.start().container, "start is right");
equals(0, range.start().offset, "start is right");
equals(document, range.end().container, "end is right");
equals(0, range.end().offset, "end is right");
});
test("constructor with element", function () {
$("#qunit-test-area").html("<div id='selectMe'>thisTextIsSelected</div>")
var range = $.Range($('#selectMe')[0]);
equals(range.toString(), "thisTextIsSelected")
});
test('selecting text nodes and parent', function () {
$("#qunit-test-area").html("<div id='selectMe'>this<span>Text</span>Is<span>Sele<span>cted</div>")
var txt = $('#selectMe')[0].childNodes[2]
equals(txt.nodeValue, "Is", "text is right")
var range = $.Range();
range.select(txt);
equals(range.parent(), txt, "right parent node");
})
test('parent', function () {
$("#qunit-test-area").html("<div id='selectMe'>thisTextIsSelected</div>")
var txt = $('#selectMe')[0].childNodes[0]
var range = $.Range(txt);
equals(range.parent(), txt)
});
test("constructor with point", function () {
var floater = $("<div id='floater'>thisTextIsSelected</div>").css({
position : "absolute",
left : "0px",
top : "0px",
border : "solid 1px black"
})
$("#qunit-test-area").html("");
floater.appendTo(document.body);
var range = $.Range({pageX : 5, pageY : 5});
equals(range.start().container.parentNode, floater[0])
floater.remove()
});
test('current', function () {
$("#qunit-test-area").html("<div id='selectMe'>thisTextIsSelected</div>");
$('#selectMe').range().select();
var range = $.Range.current();
equals(range.toString(), "thisTextIsSelected")
})
/* TODO
test('rangeFromPoint', function(){
});
test('overlaps', function(){});
test('collapse', function(){});
test('get start', function(){});
test('set start to object', function(){});
test('set start to number', function(){});
test('set start to string', function(){});
test('get end', function(){});
test('set end to object', function(){});
test('set end to number', function(){});
test('set end to string', function(){});
test('rect', function(){});
test('rects', function(){});
test('compare', function(){});
test('move', function(){});
test('clone', function(){});
*/
// adding brian's tests
test("nested range", function () {
$("#qunit-test-area")
.html("<div id='2'>012<div>3<span>4</span>5</div></div>");
$('#2').selection(1, 5);
var range = $.Range.current();
equals(range.start().container.data, "012", "start is 012")
equals(range.end().container.data, "4", "last char is 4")
});
test("rect", function () {
$("#qunit-test-area")
.html("<p id='1'>0123456789</p>");
$('#1').selection(1, 5);
var range = $.Range.current(),
rect = range.rect();
ok(rect.height, "height non-zero")
ok(rect.width, "width non-zero")
ok(rect.left, "left non-zero")
ok(rect.top, "top non-zero")
});
test("collapsed rect", function () {
$("#qunit-test-area")
.html("<p id='1'>0123456789</p>");
$('#1').selection(1, 1);
var range = $.Range.current(),
start = range.clone(),
rect = start.rect();
var r = start.rect();
ok(rect.height, "height non-zero")
ok(rect.width == 0, "width zero")
ok(rect.left, "left non-zero")
ok(rect.top, "top non-zero")
});
test("rects", function () {
$("#qunit-test-area")
.html("<p id='1'>012<span>34</span>56789</p>");
$('#1').selection(1, 5);
var range = $.Range.current(),
rects = range.rects();
equals(rects.length, 2, "2 rects found")
});
test("multiline rects", function () {
$("#qunit-test-area")
.html("<pre id='1'><code><script type='text/ejs' id='recipes'>\n" +
"<% for(var i=0; i < recipes.length; i++){ %>\n" +
" <li><%=recipes[i].name %></li>\n" +
"<%} %>\n" +
"</script></code></pre>");
$('#1').selection(3, 56);
var range = $.Range.current(),
rects = range.rects();
equals(rects.length, 2, "2 rects found")
ok(rects[1].width, "rect has width")
});
test("compare", function () {
$("#qunit-test-area")
.html("<p id='1'>012<span>34</span>56789</p>");
$('#1').selection(1, 5);
var range1 = $.Range.current();
$('#1').selection(2, 3);
var range2 = $.Range.current();
var pos = range1.compare("START_TO_START", range2)
equals(pos, -1, "pos works")
});
})