Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
malwaredllc
GitHub Repository: malwaredllc/byob
Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/test/test.js
1293 views
1
var Pos = CodeMirror.Pos;
2
3
CodeMirror.defaults.rtlMoveVisually = true;
4
5
function forEach(arr, f) {
6
for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
7
}
8
9
function addDoc(cm, width, height) {
10
var content = [], line = "";
11
for (var i = 0; i < width; ++i) line += "x";
12
for (var i = 0; i < height; ++i) content.push(line);
13
cm.setValue(content.join("\n"));
14
}
15
16
function byClassName(elt, cls) {
17
if (elt.getElementsByClassName) return elt.getElementsByClassName(cls);
18
var found = [], re = new RegExp("\\b" + cls + "\\b");
19
function search(elt) {
20
if (elt.nodeType == 3) return;
21
if (re.test(elt.className)) found.push(elt);
22
for (var i = 0, e = elt.childNodes.length; i < e; ++i)
23
search(elt.childNodes[i]);
24
}
25
search(elt);
26
return found;
27
}
28
29
var ie_lt8 = /MSIE [1-7]\b/.test(navigator.userAgent);
30
var mac = /Mac/.test(navigator.platform);
31
var phantom = /PhantomJS/.test(navigator.userAgent);
32
var opera = /Opera\/\./.test(navigator.userAgent);
33
var opera_version = opera && navigator.userAgent.match(/Version\/(\d+\.\d+)/);
34
if (opera_version) opera_version = Number(opera_version);
35
var opera_lt10 = opera && (!opera_version || opera_version < 10);
36
37
namespace = "core_";
38
39
test("core_fromTextArea", function() {
40
var te = document.getElementById("code");
41
te.value = "CONTENT";
42
var cm = CodeMirror.fromTextArea(te);
43
is(!te.offsetHeight);
44
eq(cm.getValue(), "CONTENT");
45
cm.setValue("foo\nbar");
46
eq(cm.getValue(), "foo\nbar");
47
cm.save();
48
is(/^foo\r?\nbar$/.test(te.value));
49
cm.setValue("xxx");
50
cm.toTextArea();
51
is(te.offsetHeight);
52
eq(te.value, "xxx");
53
});
54
55
testCM("getRange", function(cm) {
56
eq(cm.getLine(0), "1234");
57
eq(cm.getLine(1), "5678");
58
eq(cm.getLine(2), null);
59
eq(cm.getLine(-1), null);
60
eq(cm.getRange(Pos(0, 0), Pos(0, 3)), "123");
61
eq(cm.getRange(Pos(0, -1), Pos(0, 200)), "1234");
62
eq(cm.getRange(Pos(0, 2), Pos(1, 2)), "34\n56");
63
eq(cm.getRange(Pos(1, 2), Pos(100, 0)), "78");
64
}, {value: "1234\n5678"});
65
66
testCM("replaceRange", function(cm) {
67
eq(cm.getValue(), "");
68
cm.replaceRange("foo\n", Pos(0, 0));
69
eq(cm.getValue(), "foo\n");
70
cm.replaceRange("a\nb", Pos(0, 1));
71
eq(cm.getValue(), "fa\nboo\n");
72
eq(cm.lineCount(), 3);
73
cm.replaceRange("xyzzy", Pos(0, 0), Pos(1, 1));
74
eq(cm.getValue(), "xyzzyoo\n");
75
cm.replaceRange("abc", Pos(0, 0), Pos(10, 0));
76
eq(cm.getValue(), "abc");
77
eq(cm.lineCount(), 1);
78
});
79
80
testCM("selection", function(cm) {
81
cm.setSelection(Pos(0, 4), Pos(2, 2));
82
is(cm.somethingSelected());
83
eq(cm.getSelection(), "11\n222222\n33");
84
eqPos(cm.getCursor(false), Pos(2, 2));
85
eqPos(cm.getCursor(true), Pos(0, 4));
86
cm.setSelection(Pos(1, 0));
87
is(!cm.somethingSelected());
88
eq(cm.getSelection(), "");
89
eqPos(cm.getCursor(true), Pos(1, 0));
90
cm.replaceSelection("abc");
91
eq(cm.getSelection(), "abc");
92
eq(cm.getValue(), "111111\nabc222222\n333333");
93
cm.replaceSelection("def", "end");
94
eq(cm.getSelection(), "");
95
eqPos(cm.getCursor(true), Pos(1, 3));
96
cm.setCursor(Pos(2, 1));
97
eqPos(cm.getCursor(true), Pos(2, 1));
98
cm.setCursor(1, 2);
99
eqPos(cm.getCursor(true), Pos(1, 2));
100
}, {value: "111111\n222222\n333333"});
101
102
testCM("extendSelection", function(cm) {
103
cm.setExtending(true);
104
addDoc(cm, 10, 10);
105
cm.setSelection(Pos(3, 5));
106
eqPos(cm.getCursor("head"), Pos(3, 5));
107
eqPos(cm.getCursor("anchor"), Pos(3, 5));
108
cm.setSelection(Pos(2, 5), Pos(5, 5));
109
eqPos(cm.getCursor("head"), Pos(5, 5));
110
eqPos(cm.getCursor("anchor"), Pos(2, 5));
111
eqPos(cm.getCursor("start"), Pos(2, 5));
112
eqPos(cm.getCursor("end"), Pos(5, 5));
113
cm.setSelection(Pos(5, 5), Pos(2, 5));
114
eqPos(cm.getCursor("head"), Pos(2, 5));
115
eqPos(cm.getCursor("anchor"), Pos(5, 5));
116
eqPos(cm.getCursor("start"), Pos(2, 5));
117
eqPos(cm.getCursor("end"), Pos(5, 5));
118
cm.extendSelection(Pos(3, 2));
119
eqPos(cm.getCursor("head"), Pos(3, 2));
120
eqPos(cm.getCursor("anchor"), Pos(5, 5));
121
cm.extendSelection(Pos(6, 2));
122
eqPos(cm.getCursor("head"), Pos(6, 2));
123
eqPos(cm.getCursor("anchor"), Pos(5, 5));
124
cm.extendSelection(Pos(6, 3), Pos(6, 4));
125
eqPos(cm.getCursor("head"), Pos(6, 4));
126
eqPos(cm.getCursor("anchor"), Pos(5, 5));
127
cm.extendSelection(Pos(0, 3), Pos(0, 4));
128
eqPos(cm.getCursor("head"), Pos(0, 3));
129
eqPos(cm.getCursor("anchor"), Pos(5, 5));
130
cm.extendSelection(Pos(4, 5), Pos(6, 5));
131
eqPos(cm.getCursor("head"), Pos(6, 5));
132
eqPos(cm.getCursor("anchor"), Pos(4, 5));
133
cm.setExtending(false);
134
cm.extendSelection(Pos(0, 3), Pos(0, 4));
135
eqPos(cm.getCursor("head"), Pos(0, 4));
136
eqPos(cm.getCursor("anchor"), Pos(0, 3));
137
});
138
139
testCM("lines", function(cm) {
140
eq(cm.getLine(0), "111111");
141
eq(cm.getLine(1), "222222");
142
eq(cm.getLine(-1), null);
143
cm.removeLine(1);
144
cm.setLine(1, "abc");
145
eq(cm.getValue(), "111111\nabc");
146
}, {value: "111111\n222222\n333333"});
147
148
testCM("indent", function(cm) {
149
cm.indentLine(1);
150
eq(cm.getLine(1), " blah();");
151
cm.setOption("indentUnit", 8);
152
cm.indentLine(1);
153
eq(cm.getLine(1), "\tblah();");
154
cm.setOption("indentUnit", 10);
155
cm.setOption("tabSize", 4);
156
cm.indentLine(1);
157
eq(cm.getLine(1), "\t\t blah();");
158
}, {value: "if (x) {\nblah();\n}", indentUnit: 3, indentWithTabs: true, tabSize: 8});
159
160
testCM("indentByNumber", function(cm) {
161
cm.indentLine(0, 2);
162
eq(cm.getLine(0), " foo");
163
cm.indentLine(0, -200);
164
eq(cm.getLine(0), "foo");
165
cm.setSelection(Pos(0, 0), Pos(1, 2));
166
cm.indentSelection(3);
167
eq(cm.getValue(), " foo\n bar\nbaz");
168
}, {value: "foo\nbar\nbaz"});
169
170
test("core_defaults", function() {
171
var defsCopy = {}, defs = CodeMirror.defaults;
172
for (var opt in defs) defsCopy[opt] = defs[opt];
173
defs.indentUnit = 5;
174
defs.value = "uu";
175
defs.enterMode = "keep";
176
defs.tabindex = 55;
177
var place = document.getElementById("testground"), cm = CodeMirror(place);
178
try {
179
eq(cm.getOption("indentUnit"), 5);
180
cm.setOption("indentUnit", 10);
181
eq(defs.indentUnit, 5);
182
eq(cm.getValue(), "uu");
183
eq(cm.getOption("enterMode"), "keep");
184
eq(cm.getInputField().tabIndex, 55);
185
}
186
finally {
187
for (var opt in defsCopy) defs[opt] = defsCopy[opt];
188
place.removeChild(cm.getWrapperElement());
189
}
190
});
191
192
testCM("lineInfo", function(cm) {
193
eq(cm.lineInfo(-1), null);
194
var mark = document.createElement("span");
195
var lh = cm.setGutterMarker(1, "FOO", mark);
196
var info = cm.lineInfo(1);
197
eq(info.text, "222222");
198
eq(info.gutterMarkers.FOO, mark);
199
eq(info.line, 1);
200
eq(cm.lineInfo(2).gutterMarkers, null);
201
cm.setGutterMarker(lh, "FOO", null);
202
eq(cm.lineInfo(1).gutterMarkers, null);
203
cm.setGutterMarker(1, "FOO", mark);
204
cm.setGutterMarker(0, "FOO", mark);
205
cm.clearGutter("FOO");
206
eq(cm.lineInfo(0).gutterMarkers, null);
207
eq(cm.lineInfo(1).gutterMarkers, null);
208
}, {value: "111111\n222222\n333333"});
209
210
testCM("coords", function(cm) {
211
cm.setSize(null, 100);
212
addDoc(cm, 32, 200);
213
var top = cm.charCoords(Pos(0, 0));
214
var bot = cm.charCoords(Pos(200, 30));
215
is(top.left < bot.left);
216
is(top.top < bot.top);
217
is(top.top < top.bottom);
218
cm.scrollTo(null, 100);
219
var top2 = cm.charCoords(Pos(0, 0));
220
is(top.top > top2.top);
221
eq(top.left, top2.left);
222
});
223
224
testCM("coordsChar", function(cm) {
225
addDoc(cm, 35, 70);
226
for (var i = 0; i < 2; ++i) {
227
var sys = i ? "local" : "page";
228
for (var ch = 0; ch <= 35; ch += 5) {
229
for (var line = 0; line < 70; line += 5) {
230
cm.setCursor(line, ch);
231
var coords = cm.charCoords(Pos(line, ch), sys);
232
var pos = cm.coordsChar({left: coords.left + 1, top: coords.top + 1}, sys);
233
eqPos(pos, Pos(line, ch));
234
}
235
}
236
}
237
}, {lineNumbers: true});
238
239
testCM("posFromIndex", function(cm) {
240
cm.setValue(
241
"This function should\n" +
242
"convert a zero based index\n" +
243
"to line and ch."
244
);
245
246
var examples = [
247
{ index: -1, line: 0, ch: 0 }, // <- Tests clipping
248
{ index: 0, line: 0, ch: 0 },
249
{ index: 10, line: 0, ch: 10 },
250
{ index: 39, line: 1, ch: 18 },
251
{ index: 55, line: 2, ch: 7 },
252
{ index: 63, line: 2, ch: 15 },
253
{ index: 64, line: 2, ch: 15 } // <- Tests clipping
254
];
255
256
for (var i = 0; i < examples.length; i++) {
257
var example = examples[i];
258
var pos = cm.posFromIndex(example.index);
259
eq(pos.line, example.line);
260
eq(pos.ch, example.ch);
261
if (example.index >= 0 && example.index < 64)
262
eq(cm.indexFromPos(pos), example.index);
263
}
264
});
265
266
testCM("undo", function(cm) {
267
cm.setLine(0, "def");
268
eq(cm.historySize().undo, 1);
269
cm.undo();
270
eq(cm.getValue(), "abc");
271
eq(cm.historySize().undo, 0);
272
eq(cm.historySize().redo, 1);
273
cm.redo();
274
eq(cm.getValue(), "def");
275
eq(cm.historySize().undo, 1);
276
eq(cm.historySize().redo, 0);
277
cm.setValue("1\n\n\n2");
278
cm.clearHistory();
279
eq(cm.historySize().undo, 0);
280
for (var i = 0; i < 20; ++i) {
281
cm.replaceRange("a", Pos(0, 0));
282
cm.replaceRange("b", Pos(3, 0));
283
}
284
eq(cm.historySize().undo, 40);
285
for (var i = 0; i < 40; ++i)
286
cm.undo();
287
eq(cm.historySize().redo, 40);
288
eq(cm.getValue(), "1\n\n\n2");
289
}, {value: "abc"});
290
291
testCM("undoDepth", function(cm) {
292
cm.replaceRange("d", Pos(0));
293
cm.replaceRange("e", Pos(0));
294
cm.replaceRange("f", Pos(0));
295
cm.undo(); cm.undo(); cm.undo();
296
eq(cm.getValue(), "abcd");
297
}, {value: "abc", undoDepth: 2});
298
299
testCM("undoDoesntClearValue", function(cm) {
300
cm.undo();
301
eq(cm.getValue(), "x");
302
}, {value: "x"});
303
304
testCM("undoMultiLine", function(cm) {
305
cm.operation(function() {
306
cm.replaceRange("x", Pos(0, 0));
307
cm.replaceRange("y", Pos(1, 0));
308
});
309
cm.undo();
310
eq(cm.getValue(), "abc\ndef\nghi");
311
cm.operation(function() {
312
cm.replaceRange("y", Pos(1, 0));
313
cm.replaceRange("x", Pos(0, 0));
314
});
315
cm.undo();
316
eq(cm.getValue(), "abc\ndef\nghi");
317
cm.operation(function() {
318
cm.replaceRange("y", Pos(2, 0));
319
cm.replaceRange("x", Pos(1, 0));
320
cm.replaceRange("z", Pos(2, 0));
321
});
322
cm.undo();
323
eq(cm.getValue(), "abc\ndef\nghi", 3);
324
}, {value: "abc\ndef\nghi"});
325
326
testCM("undoComposite", function(cm) {
327
cm.replaceRange("y", Pos(1));
328
cm.operation(function() {
329
cm.replaceRange("x", Pos(0));
330
cm.replaceRange("z", Pos(2));
331
});
332
eq(cm.getValue(), "ax\nby\ncz\n");
333
cm.undo();
334
eq(cm.getValue(), "a\nby\nc\n");
335
cm.undo();
336
eq(cm.getValue(), "a\nb\nc\n");
337
cm.redo(); cm.redo();
338
eq(cm.getValue(), "ax\nby\ncz\n");
339
}, {value: "a\nb\nc\n"});
340
341
testCM("undoSelection", function(cm) {
342
cm.setSelection(Pos(0, 2), Pos(0, 4));
343
cm.replaceSelection("");
344
cm.setCursor(Pos(1, 0));
345
cm.undo();
346
eqPos(cm.getCursor(true), Pos(0, 2));
347
eqPos(cm.getCursor(false), Pos(0, 4));
348
cm.setCursor(Pos(1, 0));
349
cm.redo();
350
eqPos(cm.getCursor(true), Pos(0, 2));
351
eqPos(cm.getCursor(false), Pos(0, 2));
352
}, {value: "abcdefgh\n"});
353
354
testCM("markTextSingleLine", function(cm) {
355
forEach([{a: 0, b: 1, c: "", f: 2, t: 5},
356
{a: 0, b: 4, c: "", f: 0, t: 2},
357
{a: 1, b: 2, c: "x", f: 3, t: 6},
358
{a: 4, b: 5, c: "", f: 3, t: 5},
359
{a: 4, b: 5, c: "xx", f: 3, t: 7},
360
{a: 2, b: 5, c: "", f: 2, t: 3},
361
{a: 2, b: 5, c: "abcd", f: 6, t: 7},
362
{a: 2, b: 6, c: "x", f: null, t: null},
363
{a: 3, b: 6, c: "", f: null, t: null},
364
{a: 0, b: 9, c: "hallo", f: null, t: null},
365
{a: 4, b: 6, c: "x", f: 3, t: 4},
366
{a: 4, b: 8, c: "", f: 3, t: 4},
367
{a: 6, b: 6, c: "a", f: 3, t: 6},
368
{a: 8, b: 9, c: "", f: 3, t: 6}], function(test) {
369
cm.setValue("1234567890");
370
var r = cm.markText(Pos(0, 3), Pos(0, 6), {className: "foo"});
371
cm.replaceRange(test.c, Pos(0, test.a), Pos(0, test.b));
372
var f = r.find();
373
eq(f && f.from.ch, test.f); eq(f && f.to.ch, test.t);
374
});
375
});
376
377
testCM("markTextMultiLine", function(cm) {
378
function p(v) { return v && Pos(v[0], v[1]); }
379
forEach([{a: [0, 0], b: [0, 5], c: "", f: [0, 0], t: [2, 5]},
380
{a: [0, 0], b: [0, 5], c: "foo\n", f: [1, 0], t: [3, 5]},
381
{a: [0, 1], b: [0, 10], c: "", f: [0, 1], t: [2, 5]},
382
{a: [0, 5], b: [0, 6], c: "x", f: [0, 6], t: [2, 5]},
383
{a: [0, 0], b: [1, 0], c: "", f: [0, 0], t: [1, 5]},
384
{a: [0, 6], b: [2, 4], c: "", f: [0, 5], t: [0, 7]},
385
{a: [0, 6], b: [2, 4], c: "aa", f: [0, 5], t: [0, 9]},
386
{a: [1, 2], b: [1, 8], c: "", f: [0, 5], t: [2, 5]},
387
{a: [0, 5], b: [2, 5], c: "xx", f: null, t: null},
388
{a: [0, 0], b: [2, 10], c: "x", f: null, t: null},
389
{a: [1, 5], b: [2, 5], c: "", f: [0, 5], t: [1, 5]},
390
{a: [2, 0], b: [2, 3], c: "", f: [0, 5], t: [2, 2]},
391
{a: [2, 5], b: [3, 0], c: "a\nb", f: [0, 5], t: [2, 5]},
392
{a: [2, 3], b: [3, 0], c: "x", f: [0, 5], t: [2, 3]},
393
{a: [1, 1], b: [1, 9], c: "1\n2\n3", f: [0, 5], t: [4, 5]}], function(test) {
394
cm.setValue("aaaaaaaaaa\nbbbbbbbbbb\ncccccccccc\ndddddddd\n");
395
var r = cm.markText(Pos(0, 5), Pos(2, 5),
396
{className: "CodeMirror-matchingbracket"});
397
cm.replaceRange(test.c, p(test.a), p(test.b));
398
var f = r.find();
399
eqPos(f && f.from, p(test.f)); eqPos(f && f.to, p(test.t));
400
});
401
});
402
403
testCM("markTextUndo", function(cm) {
404
var marker1, marker2, bookmark;
405
marker1 = cm.markText(Pos(0, 1), Pos(0, 3),
406
{className: "CodeMirror-matchingbracket"});
407
marker2 = cm.markText(Pos(0, 0), Pos(2, 1),
408
{className: "CodeMirror-matchingbracket"});
409
bookmark = cm.setBookmark(Pos(1, 5));
410
cm.operation(function(){
411
cm.replaceRange("foo", Pos(0, 2));
412
cm.replaceRange("bar\nbaz\nbug\n", Pos(2, 0), Pos(3, 0));
413
});
414
var v1 = cm.getValue();
415
cm.setValue("");
416
eq(marker1.find(), null); eq(marker2.find(), null); eq(bookmark.find(), null);
417
cm.undo();
418
eqPos(bookmark.find(), Pos(1, 5), "still there");
419
cm.undo();
420
var m1Pos = marker1.find(), m2Pos = marker2.find();
421
eqPos(m1Pos.from, Pos(0, 1)); eqPos(m1Pos.to, Pos(0, 3));
422
eqPos(m2Pos.from, Pos(0, 0)); eqPos(m2Pos.to, Pos(2, 1));
423
eqPos(bookmark.find(), Pos(1, 5));
424
cm.redo(); cm.redo();
425
eq(bookmark.find(), null);
426
cm.undo();
427
eqPos(bookmark.find(), Pos(1, 5));
428
eq(cm.getValue(), v1);
429
}, {value: "1234\n56789\n00\n"});
430
431
testCM("markTextStayGone", function(cm) {
432
var m1 = cm.markText(Pos(0, 0), Pos(0, 1));
433
cm.replaceRange("hi", Pos(0, 2));
434
m1.clear();
435
cm.undo();
436
eq(m1.find(), null);
437
}, {value: "hello"});
438
439
testCM("markTextAllowEmpty", function(cm) {
440
var m1 = cm.markText(Pos(0, 1), Pos(0, 2), {clearWhenEmpty: false});
441
is(m1.find());
442
cm.replaceRange("x", Pos(0, 0));
443
is(m1.find());
444
cm.replaceRange("y", Pos(0, 2));
445
is(m1.find());
446
cm.replaceRange("z", Pos(0, 3), Pos(0, 4));
447
is(!m1.find());
448
var m2 = cm.markText(Pos(0, 1), Pos(0, 2), {clearWhenEmpty: false,
449
inclusiveLeft: true,
450
inclusiveRight: true});
451
cm.replaceRange("q", Pos(0, 1), Pos(0, 2));
452
is(m2.find());
453
cm.replaceRange("", Pos(0, 0), Pos(0, 3));
454
is(!m2.find());
455
var m3 = cm.markText(Pos(0, 1), Pos(0, 1), {clearWhenEmpty: false});
456
cm.replaceRange("a", Pos(0, 3));
457
is(m3.find());
458
cm.replaceRange("b", Pos(0, 1));
459
is(!m3.find());
460
}, {value: "abcde"});
461
462
testCM("markTextStacked", function(cm) {
463
var m1 = cm.markText(Pos(0, 0), Pos(0, 0), {clearWhenEmpty: false});
464
var m2 = cm.markText(Pos(0, 0), Pos(0, 0), {clearWhenEmpty: false});
465
cm.replaceRange("B", Pos(0, 1));
466
is(m1.find() && m2.find());
467
}, {value: "A"});
468
469
testCM("undoPreservesNewMarks", function(cm) {
470
cm.markText(Pos(0, 3), Pos(0, 4));
471
cm.markText(Pos(1, 1), Pos(1, 3));
472
cm.replaceRange("", Pos(0, 3), Pos(3, 1));
473
var mBefore = cm.markText(Pos(0, 0), Pos(0, 1));
474
var mAfter = cm.markText(Pos(0, 5), Pos(0, 6));
475
var mAround = cm.markText(Pos(0, 2), Pos(0, 4));
476
cm.undo();
477
eqPos(mBefore.find().from, Pos(0, 0));
478
eqPos(mBefore.find().to, Pos(0, 1));
479
eqPos(mAfter.find().from, Pos(3, 3));
480
eqPos(mAfter.find().to, Pos(3, 4));
481
eqPos(mAround.find().from, Pos(0, 2));
482
eqPos(mAround.find().to, Pos(3, 2));
483
var found = cm.findMarksAt(Pos(2, 2));
484
eq(found.length, 1);
485
eq(found[0], mAround);
486
}, {value: "aaaa\nbbbb\ncccc\ndddd"});
487
488
testCM("markClearBetween", function(cm) {
489
cm.setValue("aaa\nbbb\nccc\nddd\n");
490
cm.markText(Pos(0, 0), Pos(2));
491
cm.replaceRange("aaa\nbbb\nccc", Pos(0, 0), Pos(2));
492
eq(cm.findMarksAt(Pos(1, 1)).length, 0);
493
});
494
495
testCM("deleteSpanCollapsedInclusiveLeft", function(cm) {
496
var from = Pos(1, 0), to = Pos(1, 1);
497
var m = cm.markText(from, to, {collapsed: true, inclusiveLeft: true});
498
// Delete collapsed span.
499
cm.replaceRange("", from, to);
500
}, {value: "abc\nX\ndef"});
501
502
testCM("bookmark", function(cm) {
503
function p(v) { return v && Pos(v[0], v[1]); }
504
forEach([{a: [1, 0], b: [1, 1], c: "", d: [1, 4]},
505
{a: [1, 1], b: [1, 1], c: "xx", d: [1, 7]},
506
{a: [1, 4], b: [1, 5], c: "ab", d: [1, 6]},
507
{a: [1, 4], b: [1, 6], c: "", d: null},
508
{a: [1, 5], b: [1, 6], c: "abc", d: [1, 5]},
509
{a: [1, 6], b: [1, 8], c: "", d: [1, 5]},
510
{a: [1, 4], b: [1, 4], c: "\n\n", d: [3, 1]},
511
{bm: [1, 9], a: [1, 1], b: [1, 1], c: "\n", d: [2, 8]}], function(test) {
512
cm.setValue("1234567890\n1234567890\n1234567890");
513
var b = cm.setBookmark(p(test.bm) || Pos(1, 5));
514
cm.replaceRange(test.c, p(test.a), p(test.b));
515
eqPos(b.find(), p(test.d));
516
});
517
});
518
519
testCM("bookmarkInsertLeft", function(cm) {
520
var br = cm.setBookmark(Pos(0, 2), {insertLeft: false});
521
var bl = cm.setBookmark(Pos(0, 2), {insertLeft: true});
522
cm.setCursor(Pos(0, 2));
523
cm.replaceSelection("hi");
524
eqPos(br.find(), Pos(0, 2));
525
eqPos(bl.find(), Pos(0, 4));
526
cm.replaceRange("", Pos(0, 4), Pos(0, 5));
527
cm.replaceRange("", Pos(0, 2), Pos(0, 4));
528
cm.replaceRange("", Pos(0, 1), Pos(0, 2));
529
// Verify that deleting next to bookmarks doesn't kill them
530
eqPos(br.find(), Pos(0, 1));
531
eqPos(bl.find(), Pos(0, 1));
532
}, {value: "abcdef"});
533
534
testCM("bookmarkCursor", function(cm) {
535
var pos01 = cm.cursorCoords(Pos(0, 1)), pos11 = cm.cursorCoords(Pos(1, 1)),
536
pos20 = cm.cursorCoords(Pos(2, 0)), pos30 = cm.cursorCoords(Pos(3, 0)),
537
pos41 = cm.cursorCoords(Pos(4, 1));
538
cm.setBookmark(Pos(0, 1), {widget: document.createTextNode("←"), insertLeft: true});
539
cm.setBookmark(Pos(2, 0), {widget: document.createTextNode("←"), insertLeft: true});
540
cm.setBookmark(Pos(1, 1), {widget: document.createTextNode("→")});
541
cm.setBookmark(Pos(3, 0), {widget: document.createTextNode("→")});
542
var new01 = cm.cursorCoords(Pos(0, 1)), new11 = cm.cursorCoords(Pos(1, 1)),
543
new20 = cm.cursorCoords(Pos(2, 0)), new30 = cm.cursorCoords(Pos(3, 0));
544
is(new01.left == pos01.left && new01.top == pos01.top, "at left, middle of line");
545
is(new11.left > pos11.left && new11.top == pos11.top, "at right, middle of line");
546
is(new20.left == pos20.left && new20.top == pos20.top, "at left, empty line");
547
is(new30.left > pos30.left && new30.top == pos30.top, "at right, empty line");
548
cm.setBookmark(Pos(4, 0), {widget: document.createTextNode("→")});
549
is(cm.cursorCoords(Pos(4, 1)).left > pos41.left, "single-char bug");
550
}, {value: "foo\nbar\n\n\nx\ny"});
551
552
testCM("multiBookmarkCursor", function(cm) {
553
if (phantom) return;
554
var ms = [], m;
555
function add(insertLeft) {
556
for (var i = 0; i < 3; ++i) {
557
var node = document.createElement("span");
558
node.innerHTML = "X";
559
ms.push(cm.setBookmark(Pos(0, 1), {widget: node, insertLeft: insertLeft}));
560
}
561
}
562
var base1 = cm.cursorCoords(Pos(0, 1)).left, base4 = cm.cursorCoords(Pos(0, 4)).left;
563
add(true);
564
is(Math.abs(base1 - cm.cursorCoords(Pos(0, 1)).left) < .1);
565
while (m = ms.pop()) m.clear();
566
add(false);
567
is(Math.abs(base4 - cm.cursorCoords(Pos(0, 1)).left) < .1);
568
}, {value: "abcdefg"});
569
570
testCM("getAllMarks", function(cm) {
571
addDoc(cm, 10, 10);
572
var m1 = cm.setBookmark(Pos(0, 2));
573
var m2 = cm.markText(Pos(0, 2), Pos(3, 2));
574
var m3 = cm.markText(Pos(1, 2), Pos(1, 8));
575
var m4 = cm.markText(Pos(8, 0), Pos(9, 0));
576
eq(cm.getAllMarks().length, 4);
577
m1.clear();
578
m3.clear();
579
eq(cm.getAllMarks().length, 2);
580
});
581
582
testCM("bug577", function(cm) {
583
cm.setValue("a\nb");
584
cm.clearHistory();
585
cm.setValue("fooooo");
586
cm.undo();
587
});
588
589
testCM("scrollSnap", function(cm) {
590
cm.setSize(100, 100);
591
addDoc(cm, 200, 200);
592
cm.setCursor(Pos(100, 180));
593
var info = cm.getScrollInfo();
594
is(info.left > 0 && info.top > 0);
595
cm.setCursor(Pos(0, 0));
596
info = cm.getScrollInfo();
597
is(info.left == 0 && info.top == 0, "scrolled clean to top");
598
cm.setCursor(Pos(100, 180));
599
cm.setCursor(Pos(199, 0));
600
info = cm.getScrollInfo();
601
is(info.left == 0 && info.top + 2 > info.height - cm.getScrollerElement().clientHeight, "scrolled clean to bottom");
602
});
603
604
testCM("scrollIntoView", function(cm) {
605
if (phantom) return;
606
var outer = cm.getWrapperElement().getBoundingClientRect();
607
function test(line, ch) {
608
var pos = Pos(line, ch);
609
cm.scrollIntoView(pos);
610
var box = cm.charCoords(pos, "window");
611
is(box.left >= outer.left && box.right <= outer.right &&
612
box.top >= outer.top && box.bottom <= outer.bottom);
613
}
614
addDoc(cm, 200, 200);
615
test(199, 199);
616
test(0, 0);
617
test(100, 100);
618
test(199, 0);
619
test(0, 199);
620
test(100, 100);
621
});
622
623
testCM("selectionPos", function(cm) {
624
if (phantom) return;
625
cm.setSize(100, 100);
626
addDoc(cm, 200, 100);
627
cm.setSelection(Pos(1, 100), Pos(98, 100));
628
var lineWidth = cm.charCoords(Pos(0, 200), "local").left;
629
var lineHeight = (cm.charCoords(Pos(99)).top - cm.charCoords(Pos(0)).top) / 100;
630
cm.scrollTo(0, 0);
631
var selElt = byClassName(cm.getWrapperElement(), "CodeMirror-selected");
632
var outer = cm.getWrapperElement().getBoundingClientRect();
633
var sawMiddle, sawTop, sawBottom;
634
for (var i = 0, e = selElt.length; i < e; ++i) {
635
var box = selElt[i].getBoundingClientRect();
636
var atLeft = box.left - outer.left < 30;
637
var width = box.right - box.left;
638
var atRight = box.right - outer.left > .8 * lineWidth;
639
if (atLeft && atRight) {
640
sawMiddle = true;
641
is(box.bottom - box.top > 90 * lineHeight, "middle high");
642
is(width > .9 * lineWidth, "middle wide");
643
} else {
644
is(width > .4 * lineWidth, "top/bot wide enough");
645
is(width < .6 * lineWidth, "top/bot slim enough");
646
if (atLeft) {
647
sawBottom = true;
648
is(box.top - outer.top > 96 * lineHeight, "bot below");
649
} else if (atRight) {
650
sawTop = true;
651
is(box.top - outer.top < 2.1 * lineHeight, "top above");
652
}
653
}
654
}
655
is(sawTop && sawBottom && sawMiddle, "all parts");
656
}, null);
657
658
testCM("restoreHistory", function(cm) {
659
cm.setValue("abc\ndef");
660
cm.setLine(1, "hello");
661
cm.setLine(0, "goop");
662
cm.undo();
663
var storedVal = cm.getValue(), storedHist = cm.getHistory();
664
if (window.JSON) storedHist = JSON.parse(JSON.stringify(storedHist));
665
eq(storedVal, "abc\nhello");
666
cm.setValue("");
667
cm.clearHistory();
668
eq(cm.historySize().undo, 0);
669
cm.setValue(storedVal);
670
cm.setHistory(storedHist);
671
cm.redo();
672
eq(cm.getValue(), "goop\nhello");
673
cm.undo(); cm.undo();
674
eq(cm.getValue(), "abc\ndef");
675
});
676
677
testCM("doubleScrollbar", function(cm) {
678
var dummy = document.body.appendChild(document.createElement("p"));
679
dummy.style.cssText = "height: 50px; overflow: scroll; width: 50px";
680
var scrollbarWidth = dummy.offsetWidth + 1 - dummy.clientWidth;
681
document.body.removeChild(dummy);
682
if (scrollbarWidth < 2) return;
683
cm.setSize(null, 100);
684
addDoc(cm, 1, 300);
685
var wrap = cm.getWrapperElement();
686
is(wrap.offsetWidth - byClassName(wrap, "CodeMirror-lines")[0].offsetWidth <= scrollbarWidth * 1.5);
687
});
688
689
testCM("weirdLinebreaks", function(cm) {
690
cm.setValue("foo\nbar\rbaz\r\nquux\n\rplop");
691
is(cm.getValue(), "foo\nbar\nbaz\nquux\n\nplop");
692
is(cm.lineCount(), 6);
693
cm.setValue("\n\n");
694
is(cm.lineCount(), 3);
695
});
696
697
testCM("setSize", function(cm) {
698
cm.setSize(100, 100);
699
var wrap = cm.getWrapperElement();
700
is(wrap.offsetWidth, 100);
701
is(wrap.offsetHeight, 100);
702
cm.setSize("100%", "3em");
703
is(wrap.style.width, "100%");
704
is(wrap.style.height, "3em");
705
cm.setSize(null, 40);
706
is(wrap.style.width, "100%");
707
is(wrap.style.height, "40px");
708
});
709
710
function foldLines(cm, start, end, autoClear) {
711
return cm.markText(Pos(start, 0), Pos(end - 1), {
712
inclusiveLeft: true,
713
inclusiveRight: true,
714
collapsed: true,
715
clearOnEnter: autoClear
716
});
717
}
718
719
testCM("collapsedLines", function(cm) {
720
addDoc(cm, 4, 10);
721
var range = foldLines(cm, 4, 5), cleared = 0;
722
CodeMirror.on(range, "clear", function() {cleared++;});
723
cm.setCursor(Pos(3, 0));
724
CodeMirror.commands.goLineDown(cm);
725
eqPos(cm.getCursor(), Pos(5, 0));
726
cm.setLine(3, "abcdefg");
727
cm.setCursor(Pos(3, 6));
728
CodeMirror.commands.goLineDown(cm);
729
eqPos(cm.getCursor(), Pos(5, 4));
730
cm.setLine(3, "ab");
731
cm.setCursor(Pos(3, 2));
732
CodeMirror.commands.goLineDown(cm);
733
eqPos(cm.getCursor(), Pos(5, 2));
734
cm.operation(function() {range.clear(); range.clear();});
735
eq(cleared, 1);
736
});
737
738
testCM("collapsedRangeCoordsChar", function(cm) {
739
var pos_1_3 = cm.charCoords(Pos(1, 3));
740
pos_1_3.left += 2; pos_1_3.top += 2;
741
var opts = {collapsed: true, inclusiveLeft: true, inclusiveRight: true};
742
var m1 = cm.markText(Pos(0, 0), Pos(2, 0), opts);
743
eqPos(cm.coordsChar(pos_1_3), Pos(3, 3));
744
m1.clear();
745
var m1 = cm.markText(Pos(0, 0), Pos(1, 1), {collapsed: true, inclusiveLeft: true});
746
var m2 = cm.markText(Pos(1, 1), Pos(2, 0), {collapsed: true, inclusiveRight: true});
747
eqPos(cm.coordsChar(pos_1_3), Pos(3, 3));
748
m1.clear(); m2.clear();
749
var m1 = cm.markText(Pos(0, 0), Pos(1, 6), opts);
750
eqPos(cm.coordsChar(pos_1_3), Pos(3, 3));
751
}, {value: "123456\nabcdef\nghijkl\nmnopqr\n"});
752
753
testCM("hiddenLinesAutoUnfold", function(cm) {
754
var range = foldLines(cm, 1, 3, true), cleared = 0;
755
CodeMirror.on(range, "clear", function() {cleared++;});
756
cm.setCursor(Pos(3, 0));
757
eq(cleared, 0);
758
cm.execCommand("goCharLeft");
759
eq(cleared, 1);
760
range = foldLines(cm, 1, 3, true);
761
CodeMirror.on(range, "clear", function() {cleared++;});
762
eqPos(cm.getCursor(), Pos(3, 0));
763
cm.setCursor(Pos(0, 3));
764
cm.execCommand("goCharRight");
765
eq(cleared, 2);
766
}, {value: "abc\ndef\nghi\njkl"});
767
768
testCM("hiddenLinesSelectAll", function(cm) { // Issue #484
769
addDoc(cm, 4, 20);
770
foldLines(cm, 0, 10);
771
foldLines(cm, 11, 20);
772
CodeMirror.commands.selectAll(cm);
773
eqPos(cm.getCursor(true), Pos(10, 0));
774
eqPos(cm.getCursor(false), Pos(10, 4));
775
});
776
777
778
testCM("everythingFolded", function(cm) {
779
addDoc(cm, 2, 2);
780
function enterPress() {
781
cm.triggerOnKeyDown({type: "keydown", keyCode: 13, preventDefault: function(){}, stopPropagation: function(){}});
782
}
783
var fold = foldLines(cm, 0, 2);
784
enterPress();
785
eq(cm.getValue(), "xx\nxx");
786
fold.clear();
787
fold = foldLines(cm, 0, 2, true);
788
eq(fold.find(), null);
789
enterPress();
790
eq(cm.getValue(), "\nxx\nxx");
791
});
792
793
testCM("structuredFold", function(cm) {
794
if (phantom) return;
795
addDoc(cm, 4, 8);
796
var range = cm.markText(Pos(1, 2), Pos(6, 2), {
797
replacedWith: document.createTextNode("Q")
798
});
799
cm.setCursor(0, 3);
800
CodeMirror.commands.goLineDown(cm);
801
eqPos(cm.getCursor(), Pos(6, 2));
802
CodeMirror.commands.goCharLeft(cm);
803
eqPos(cm.getCursor(), Pos(1, 2));
804
CodeMirror.commands.delCharAfter(cm);
805
eq(cm.getValue(), "xxxx\nxxxx\nxxxx");
806
addDoc(cm, 4, 8);
807
range = cm.markText(Pos(1, 2), Pos(6, 2), {
808
replacedWith: document.createTextNode("M"),
809
clearOnEnter: true
810
});
811
var cleared = 0;
812
CodeMirror.on(range, "clear", function(){++cleared;});
813
cm.setCursor(0, 3);
814
CodeMirror.commands.goLineDown(cm);
815
eqPos(cm.getCursor(), Pos(6, 2));
816
CodeMirror.commands.goCharLeft(cm);
817
eqPos(cm.getCursor(), Pos(6, 1));
818
eq(cleared, 1);
819
range.clear();
820
eq(cleared, 1);
821
range = cm.markText(Pos(1, 2), Pos(6, 2), {
822
replacedWith: document.createTextNode("Q"),
823
clearOnEnter: true
824
});
825
range.clear();
826
cm.setCursor(1, 2);
827
CodeMirror.commands.goCharRight(cm);
828
eqPos(cm.getCursor(), Pos(1, 3));
829
range = cm.markText(Pos(2, 0), Pos(4, 4), {
830
replacedWith: document.createTextNode("M")
831
});
832
cm.setCursor(1, 0);
833
CodeMirror.commands.goLineDown(cm);
834
eqPos(cm.getCursor(), Pos(2, 0));
835
}, null);
836
837
testCM("nestedFold", function(cm) {
838
addDoc(cm, 10, 3);
839
function fold(ll, cl, lr, cr) {
840
return cm.markText(Pos(ll, cl), Pos(lr, cr), {collapsed: true});
841
}
842
var inner1 = fold(0, 6, 1, 3), inner2 = fold(0, 2, 1, 8), outer = fold(0, 1, 2, 3), inner0 = fold(0, 5, 0, 6);
843
cm.setCursor(0, 1);
844
CodeMirror.commands.goCharRight(cm);
845
eqPos(cm.getCursor(), Pos(2, 3));
846
inner0.clear();
847
CodeMirror.commands.goCharLeft(cm);
848
eqPos(cm.getCursor(), Pos(0, 1));
849
outer.clear();
850
CodeMirror.commands.goCharRight(cm);
851
eqPos(cm.getCursor(), Pos(0, 2));
852
CodeMirror.commands.goCharRight(cm);
853
eqPos(cm.getCursor(), Pos(1, 8));
854
inner2.clear();
855
CodeMirror.commands.goCharLeft(cm);
856
eqPos(cm.getCursor(), Pos(1, 7));
857
cm.setCursor(0, 5);
858
CodeMirror.commands.goCharRight(cm);
859
eqPos(cm.getCursor(), Pos(0, 6));
860
CodeMirror.commands.goCharRight(cm);
861
eqPos(cm.getCursor(), Pos(1, 3));
862
});
863
864
testCM("badNestedFold", function(cm) {
865
addDoc(cm, 4, 4);
866
cm.markText(Pos(0, 2), Pos(3, 2), {collapsed: true});
867
var caught;
868
try {cm.markText(Pos(0, 1), Pos(0, 3), {collapsed: true});}
869
catch(e) {caught = e;}
870
is(caught instanceof Error, "no error");
871
is(/overlap/i.test(caught.message), "wrong error");
872
});
873
874
testCM("nestedFoldOnSide", function(cm) {
875
var m1 = cm.markText(Pos(0, 1), Pos(2, 1), {collapsed: true, inclusiveRight: true});
876
var m2 = cm.markText(Pos(0, 1), Pos(0, 2), {collapsed: true});
877
cm.markText(Pos(0, 1), Pos(0, 2), {collapsed: true}).clear();
878
try { cm.markText(Pos(0, 1), Pos(0, 2), {collapsed: true, inclusiveLeft: true}); }
879
catch(e) { var caught = e; }
880
is(caught && /overlap/i.test(caught.message));
881
var m3 = cm.markText(Pos(2, 0), Pos(2, 1), {collapsed: true});
882
var m4 = cm.markText(Pos(2, 0), Pos(2, 1), {collapse: true, inclusiveRight: true});
883
m1.clear(); m4.clear();
884
m1 = cm.markText(Pos(0, 1), Pos(2, 1), {collapsed: true});
885
cm.markText(Pos(2, 0), Pos(2, 1), {collapsed: true}).clear();
886
try { cm.markText(Pos(2, 0), Pos(2, 1), {collapsed: true, inclusiveRight: true}); }
887
catch(e) { var caught = e; }
888
is(caught && /overlap/i.test(caught.message));
889
}, {value: "ab\ncd\ef"});
890
891
testCM("wrappingInlineWidget", function(cm) {
892
cm.setSize("11em");
893
var w = document.createElement("span");
894
w.style.color = "red";
895
w.innerHTML = "one two three four";
896
cm.markText(Pos(0, 6), Pos(0, 9), {replacedWith: w});
897
var cur0 = cm.cursorCoords(Pos(0, 0)), cur1 = cm.cursorCoords(Pos(0, 10));
898
is(cur0.top < cur1.top);
899
is(cur0.bottom < cur1.bottom);
900
var curL = cm.cursorCoords(Pos(0, 6)), curR = cm.cursorCoords(Pos(0, 9));
901
eq(curL.top, cur0.top);
902
eq(curL.bottom, cur0.bottom);
903
eq(curR.top, cur1.top);
904
eq(curR.bottom, cur1.bottom);
905
cm.replaceRange("", Pos(0, 9), Pos(0));
906
curR = cm.cursorCoords(Pos(0, 9));
907
eq(curR.top, cur1.top);
908
eq(curR.bottom, cur1.bottom);
909
}, {value: "1 2 3 xxx 4", lineWrapping: true});
910
911
testCM("changedInlineWidget", function(cm) {
912
cm.setSize("10em");
913
var w = document.createElement("span");
914
w.innerHTML = "x";
915
var m = cm.markText(Pos(0, 4), Pos(0, 5), {replacedWith: w});
916
w.innerHTML = "and now the widget is really really long all of a sudden and a scrollbar is needed";
917
m.changed();
918
var hScroll = byClassName(cm.getWrapperElement(), "CodeMirror-hscrollbar")[0];
919
is(hScroll.scrollWidth > hScroll.clientWidth);
920
}, {value: "hello there"});
921
922
testCM("changedBookmark", function(cm) {
923
cm.setSize("10em");
924
var w = document.createElement("span");
925
w.innerHTML = "x";
926
var m = cm.setBookmark(Pos(0, 4), {widget: w});
927
w.innerHTML = "and now the widget is really really long all of a sudden and a scrollbar is needed";
928
m.changed();
929
var hScroll = byClassName(cm.getWrapperElement(), "CodeMirror-hscrollbar")[0];
930
is(hScroll.scrollWidth > hScroll.clientWidth);
931
}, {value: "abcdefg"});
932
933
testCM("inlineWidget", function(cm) {
934
var w = cm.setBookmark(Pos(0, 2), {widget: document.createTextNode("uu")});
935
cm.setCursor(0, 2);
936
CodeMirror.commands.goLineDown(cm);
937
eqPos(cm.getCursor(), Pos(1, 4));
938
cm.setCursor(0, 2);
939
cm.replaceSelection("hi");
940
eqPos(w.find(), Pos(0, 2));
941
cm.setCursor(0, 1);
942
cm.replaceSelection("ay");
943
eqPos(w.find(), Pos(0, 4));
944
eq(cm.getLine(0), "uayuhiuu");
945
}, {value: "uuuu\nuuuuuu"});
946
947
testCM("wrappingAndResizing", function(cm) {
948
cm.setSize(null, "auto");
949
cm.setOption("lineWrapping", true);
950
var wrap = cm.getWrapperElement(), h0 = wrap.offsetHeight;
951
var doc = "xxx xxx xxx xxx xxx";
952
cm.setValue(doc);
953
for (var step = 10, w = cm.charCoords(Pos(0, 18), "div").right;; w += step) {
954
cm.setSize(w);
955
if (wrap.offsetHeight <= h0 * (opera_lt10 ? 1.2 : 1.5)) {
956
if (step == 10) { w -= 10; step = 1; }
957
else break;
958
}
959
}
960
// Ensure that putting the cursor at the end of the maximally long
961
// line doesn't cause wrapping to happen.
962
cm.setCursor(Pos(0, doc.length));
963
eq(wrap.offsetHeight, h0);
964
cm.replaceSelection("x");
965
is(wrap.offsetHeight > h0, "wrapping happens");
966
// Now add a max-height and, in a document consisting of
967
// almost-wrapped lines, go over it so that a scrollbar appears.
968
cm.setValue(doc + "\n" + doc + "\n");
969
cm.getScrollerElement().style.maxHeight = "100px";
970
cm.replaceRange("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n!\n", Pos(2, 0));
971
forEach([Pos(0, doc.length), Pos(0, doc.length - 1),
972
Pos(0, 0), Pos(1, doc.length), Pos(1, doc.length - 1)],
973
function(pos) {
974
var coords = cm.charCoords(pos);
975
eqPos(pos, cm.coordsChar({left: coords.left + 2, top: coords.top + 5}));
976
});
977
}, null, ie_lt8);
978
979
testCM("measureEndOfLine", function(cm) {
980
cm.setSize(null, "auto");
981
var inner = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild;
982
var lh = inner.offsetHeight;
983
for (var step = 10, w = cm.charCoords(Pos(0, 7), "div").right;; w += step) {
984
cm.setSize(w);
985
if (inner.offsetHeight < 2.5 * lh) {
986
if (step == 10) { w -= 10; step = 1; }
987
else break;
988
}
989
}
990
cm.setValue(cm.getValue() + "\n\n");
991
var endPos = cm.charCoords(Pos(0, 18), "local");
992
is(endPos.top > lh * .8, "not at top");
993
is(endPos.left > w - 20, "not at right");
994
endPos = cm.charCoords(Pos(0, 18));
995
eqPos(cm.coordsChar({left: endPos.left, top: endPos.top + 5}), Pos(0, 18));
996
}, {mode: "text/html", value: "<!-- foo barrr -->", lineWrapping: true}, ie_lt8 || opera_lt10);
997
998
testCM("scrollVerticallyAndHorizontally", function(cm) {
999
cm.setSize(100, 100);
1000
addDoc(cm, 40, 40);
1001
cm.setCursor(39);
1002
var wrap = cm.getWrapperElement(), bar = byClassName(wrap, "CodeMirror-vscrollbar")[0];
1003
is(bar.offsetHeight < wrap.offsetHeight, "vertical scrollbar limited by horizontal one");
1004
var cursorBox = byClassName(wrap, "CodeMirror-cursor")[0].getBoundingClientRect();
1005
var editorBox = wrap.getBoundingClientRect();
1006
is(cursorBox.bottom < editorBox.top + cm.getScrollerElement().clientHeight,
1007
"bottom line visible");
1008
}, {lineNumbers: true});
1009
1010
testCM("moveVstuck", function(cm) {
1011
var lines = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild, h0 = lines.offsetHeight;
1012
var val = "fooooooooooooooooooooooooo baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar\n";
1013
cm.setValue(val);
1014
for (var w = cm.charCoords(Pos(0, 26), "div").right * 2.8;; w += 5) {
1015
cm.setSize(w);
1016
if (lines.offsetHeight <= 3.5 * h0) break;
1017
}
1018
cm.setCursor(Pos(0, val.length - 1));
1019
cm.moveV(-1, "line");
1020
eqPos(cm.getCursor(), Pos(0, 26));
1021
}, {lineWrapping: true}, ie_lt8 || opera_lt10);
1022
1023
testCM("collapseOnMove", function(cm) {
1024
cm.setSelection(Pos(0, 1), Pos(2, 4));
1025
cm.execCommand("goLineUp");
1026
is(!cm.somethingSelected());
1027
eqPos(cm.getCursor(), Pos(0, 1));
1028
cm.setSelection(Pos(0, 1), Pos(2, 4));
1029
cm.execCommand("goPageDown");
1030
is(!cm.somethingSelected());
1031
eqPos(cm.getCursor(), Pos(2, 4));
1032
cm.execCommand("goLineUp");
1033
cm.execCommand("goLineUp");
1034
eqPos(cm.getCursor(), Pos(0, 4));
1035
cm.setSelection(Pos(0, 1), Pos(2, 4));
1036
cm.execCommand("goCharLeft");
1037
is(!cm.somethingSelected());
1038
eqPos(cm.getCursor(), Pos(0, 1));
1039
}, {value: "aaaaa\nb\nccccc"});
1040
1041
testCM("clickTab", function(cm) {
1042
var p0 = cm.charCoords(Pos(0, 0));
1043
eqPos(cm.coordsChar({left: p0.left + 5, top: p0.top + 5}), Pos(0, 0));
1044
eqPos(cm.coordsChar({left: p0.right - 5, top: p0.top + 5}), Pos(0, 1));
1045
}, {value: "\t\n\n", lineWrapping: true, tabSize: 8});
1046
1047
testCM("verticalScroll", function(cm) {
1048
cm.setSize(100, 200);
1049
cm.setValue("foo\nbar\nbaz\n");
1050
var sc = cm.getScrollerElement(), baseWidth = sc.scrollWidth;
1051
cm.setLine(0, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah");
1052
is(sc.scrollWidth > baseWidth, "scrollbar present");
1053
cm.setLine(0, "foo");
1054
if (!phantom) eq(sc.scrollWidth, baseWidth, "scrollbar gone");
1055
cm.setLine(0, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah");
1056
cm.setLine(1, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbh");
1057
is(sc.scrollWidth > baseWidth, "present again");
1058
var curWidth = sc.scrollWidth;
1059
cm.setLine(0, "foo");
1060
is(sc.scrollWidth < curWidth, "scrollbar smaller");
1061
is(sc.scrollWidth > baseWidth, "but still present");
1062
});
1063
1064
testCM("extraKeys", function(cm) {
1065
var outcome;
1066
function fakeKey(expected, code, props) {
1067
if (typeof code == "string") code = code.charCodeAt(0);
1068
var e = {type: "keydown", keyCode: code, preventDefault: function(){}, stopPropagation: function(){}};
1069
if (props) for (var n in props) e[n] = props[n];
1070
outcome = null;
1071
cm.triggerOnKeyDown(e);
1072
eq(outcome, expected);
1073
}
1074
CodeMirror.commands.testCommand = function() {outcome = "tc";};
1075
CodeMirror.commands.goTestCommand = function() {outcome = "gtc";};
1076
cm.setOption("extraKeys", {"Shift-X": function() {outcome = "sx";},
1077
"X": function() {outcome = "x";},
1078
"Ctrl-Alt-U": function() {outcome = "cau";},
1079
"End": "testCommand",
1080
"Home": "goTestCommand",
1081
"Tab": false});
1082
fakeKey(null, "U");
1083
fakeKey("cau", "U", {ctrlKey: true, altKey: true});
1084
fakeKey(null, "U", {shiftKey: true, ctrlKey: true, altKey: true});
1085
fakeKey("x", "X");
1086
fakeKey("sx", "X", {shiftKey: true});
1087
fakeKey("tc", 35);
1088
fakeKey(null, 35, {shiftKey: true});
1089
fakeKey("gtc", 36);
1090
fakeKey("gtc", 36, {shiftKey: true});
1091
fakeKey(null, 9);
1092
}, null, window.opera && mac);
1093
1094
testCM("wordMovementCommands", function(cm) {
1095
cm.execCommand("goWordLeft");
1096
eqPos(cm.getCursor(), Pos(0, 0));
1097
cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
1098
eqPos(cm.getCursor(), Pos(0, 7));
1099
cm.execCommand("goWordLeft");
1100
eqPos(cm.getCursor(), Pos(0, 5));
1101
cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
1102
eqPos(cm.getCursor(), Pos(0, 12));
1103
cm.execCommand("goWordLeft");
1104
eqPos(cm.getCursor(), Pos(0, 9));
1105
cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
1106
eqPos(cm.getCursor(), Pos(0, 24));
1107
cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
1108
eqPos(cm.getCursor(), Pos(1, 9));
1109
cm.execCommand("goWordRight");
1110
eqPos(cm.getCursor(), Pos(1, 13));
1111
cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
1112
eqPos(cm.getCursor(), Pos(2, 0));
1113
}, {value: "this is (the) firstline.\na foo12\u00e9\u00f8\u00d7bar\n"});
1114
1115
testCM("groupMovementCommands", function(cm) {
1116
cm.execCommand("goGroupLeft");
1117
eqPos(cm.getCursor(), Pos(0, 0));
1118
cm.execCommand("goGroupRight");
1119
eqPos(cm.getCursor(), Pos(0, 4));
1120
cm.execCommand("goGroupRight");
1121
eqPos(cm.getCursor(), Pos(0, 7));
1122
cm.execCommand("goGroupRight");
1123
eqPos(cm.getCursor(), Pos(0, 10));
1124
cm.execCommand("goGroupLeft");
1125
eqPos(cm.getCursor(), Pos(0, 7));
1126
cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight");
1127
eqPos(cm.getCursor(), Pos(0, 15));
1128
cm.setCursor(Pos(0, 17));
1129
cm.execCommand("goGroupLeft");
1130
eqPos(cm.getCursor(), Pos(0, 16));
1131
cm.execCommand("goGroupLeft");
1132
eqPos(cm.getCursor(), Pos(0, 14));
1133
cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight");
1134
eqPos(cm.getCursor(), Pos(0, 20));
1135
cm.execCommand("goGroupRight");
1136
eqPos(cm.getCursor(), Pos(1, 5));
1137
cm.execCommand("goGroupLeft"); cm.execCommand("goGroupLeft");
1138
eqPos(cm.getCursor(), Pos(1, 0));
1139
cm.execCommand("goGroupLeft");
1140
eqPos(cm.getCursor(), Pos(0, 16));
1141
}, {value: "booo ba---quux. ffff\n abc d"});
1142
1143
testCM("charMovementCommands", function(cm) {
1144
cm.execCommand("goCharLeft"); cm.execCommand("goColumnLeft");
1145
eqPos(cm.getCursor(), Pos(0, 0));
1146
cm.execCommand("goCharRight"); cm.execCommand("goCharRight");
1147
eqPos(cm.getCursor(), Pos(0, 2));
1148
cm.setCursor(Pos(1, 0));
1149
cm.execCommand("goColumnLeft");
1150
eqPos(cm.getCursor(), Pos(1, 0));
1151
cm.execCommand("goCharLeft");
1152
eqPos(cm.getCursor(), Pos(0, 5));
1153
cm.execCommand("goColumnRight");
1154
eqPos(cm.getCursor(), Pos(0, 5));
1155
cm.execCommand("goCharRight");
1156
eqPos(cm.getCursor(), Pos(1, 0));
1157
cm.execCommand("goLineEnd");
1158
eqPos(cm.getCursor(), Pos(1, 5));
1159
cm.execCommand("goLineStartSmart");
1160
eqPos(cm.getCursor(), Pos(1, 1));
1161
cm.execCommand("goLineStartSmart");
1162
eqPos(cm.getCursor(), Pos(1, 0));
1163
cm.setCursor(Pos(2, 0));
1164
cm.execCommand("goCharRight"); cm.execCommand("goColumnRight");
1165
eqPos(cm.getCursor(), Pos(2, 0));
1166
}, {value: "line1\n ine2\n"});
1167
1168
testCM("verticalMovementCommands", function(cm) {
1169
cm.execCommand("goLineUp");
1170
eqPos(cm.getCursor(), Pos(0, 0));
1171
cm.execCommand("goLineDown");
1172
if (!phantom) // This fails in PhantomJS, though not in a real Webkit
1173
eqPos(cm.getCursor(), Pos(1, 0));
1174
cm.setCursor(Pos(1, 12));
1175
cm.execCommand("goLineDown");
1176
eqPos(cm.getCursor(), Pos(2, 5));
1177
cm.execCommand("goLineDown");
1178
eqPos(cm.getCursor(), Pos(3, 0));
1179
cm.execCommand("goLineUp");
1180
eqPos(cm.getCursor(), Pos(2, 5));
1181
cm.execCommand("goLineUp");
1182
eqPos(cm.getCursor(), Pos(1, 12));
1183
cm.execCommand("goPageDown");
1184
eqPos(cm.getCursor(), Pos(5, 0));
1185
cm.execCommand("goPageDown"); cm.execCommand("goLineDown");
1186
eqPos(cm.getCursor(), Pos(5, 0));
1187
cm.execCommand("goPageUp");
1188
eqPos(cm.getCursor(), Pos(0, 0));
1189
}, {value: "line1\nlong long line2\nline3\n\nline5\n"});
1190
1191
testCM("verticalMovementCommandsWrapping", function(cm) {
1192
cm.setSize(120);
1193
cm.setCursor(Pos(0, 5));
1194
cm.execCommand("goLineDown");
1195
eq(cm.getCursor().line, 0);
1196
is(cm.getCursor().ch > 5, "moved beyond wrap");
1197
for (var i = 0; ; ++i) {
1198
is(i < 20, "no endless loop");
1199
cm.execCommand("goLineDown");
1200
var cur = cm.getCursor();
1201
if (cur.line == 1) eq(cur.ch, 5);
1202
if (cur.line == 2) { eq(cur.ch, 1); break; }
1203
}
1204
}, {value: "a very long line that wraps around somehow so that we can test cursor movement\nshortone\nk",
1205
lineWrapping: true});
1206
1207
testCM("rtlMovement", function(cm) {
1208
forEach(["خحج", "خحabcخحج", "abخحخحجcd", "abخde", "abخح2342خ1حج", "خ1ح2خح3حxج",
1209
"خحcd", "1خحcd", "abcdeح1ج", "خمرحبها مها!", "foobarر", "خ ة ق",
1210
"<img src=\"/בדיקה3.jpg\">"], function(line) {
1211
var inv = line.charAt(0) == "خ";
1212
cm.setValue(line + "\n"); cm.execCommand(inv ? "goLineEnd" : "goLineStart");
1213
var cursor = byClassName(cm.getWrapperElement(), "CodeMirror-cursor")[0];
1214
var prevX = cursor.offsetLeft, prevY = cursor.offsetTop;
1215
for (var i = 0; i <= line.length; ++i) {
1216
cm.execCommand("goCharRight");
1217
if (i == line.length) is(cursor.offsetTop > prevY, "next line");
1218
else is(cursor.offsetLeft > prevX, "moved right");
1219
prevX = cursor.offsetLeft; prevY = cursor.offsetTop;
1220
}
1221
cm.setCursor(0, 0); cm.execCommand(inv ? "goLineStart" : "goLineEnd");
1222
prevX = cursor.offsetLeft;
1223
for (var i = 0; i < line.length; ++i) {
1224
cm.execCommand("goCharLeft");
1225
is(cursor.offsetLeft < prevX, "moved left");
1226
prevX = cursor.offsetLeft;
1227
}
1228
});
1229
});
1230
1231
// Verify that updating a line clears its bidi ordering
1232
testCM("bidiUpdate", function(cm) {
1233
cm.setCursor(Pos(0, 2));
1234
cm.replaceSelection("خحج", "start");
1235
cm.execCommand("goCharRight");
1236
eqPos(cm.getCursor(), Pos(0, 4));
1237
}, {value: "abcd\n"});
1238
1239
testCM("movebyTextUnit", function(cm) {
1240
cm.setValue("בְּרֵאשִ\ńéée\n");
1241
cm.execCommand("goLineEnd");
1242
for (var i = 0; i < 4; ++i) cm.execCommand("goCharRight");
1243
eqPos(cm.getCursor(), Pos(0, 0));
1244
cm.execCommand("goCharRight");
1245
eqPos(cm.getCursor(), Pos(1, 0));
1246
cm.execCommand("goCharRight");
1247
cm.execCommand("goCharRight");
1248
eqPos(cm.getCursor(), Pos(1, 3));
1249
cm.execCommand("goCharRight");
1250
cm.execCommand("goCharRight");
1251
eqPos(cm.getCursor(), Pos(1, 6));
1252
});
1253
1254
testCM("lineChangeEvents", function(cm) {
1255
addDoc(cm, 3, 5);
1256
var log = [], want = ["ch 0", "ch 1", "del 2", "ch 0", "ch 0", "del 1", "del 3", "del 4"];
1257
for (var i = 0; i < 5; ++i) {
1258
CodeMirror.on(cm.getLineHandle(i), "delete", function(i) {
1259
return function() {log.push("del " + i);};
1260
}(i));
1261
CodeMirror.on(cm.getLineHandle(i), "change", function(i) {
1262
return function() {log.push("ch " + i);};
1263
}(i));
1264
}
1265
cm.replaceRange("x", Pos(0, 1));
1266
cm.replaceRange("xy", Pos(1, 1), Pos(2));
1267
cm.replaceRange("foo\nbar", Pos(0, 1));
1268
cm.replaceRange("", Pos(0, 0), Pos(cm.lineCount()));
1269
eq(log.length, want.length, "same length");
1270
for (var i = 0; i < log.length; ++i)
1271
eq(log[i], want[i]);
1272
});
1273
1274
testCM("scrollEntirelyToRight", function(cm) {
1275
if (phantom) return;
1276
addDoc(cm, 500, 2);
1277
cm.setCursor(Pos(0, 500));
1278
var wrap = cm.getWrapperElement(), cur = byClassName(wrap, "CodeMirror-cursor")[0];
1279
is(wrap.getBoundingClientRect().right > cur.getBoundingClientRect().left);
1280
});
1281
1282
testCM("lineWidgets", function(cm) {
1283
addDoc(cm, 500, 3);
1284
var last = cm.charCoords(Pos(2, 0));
1285
var node = document.createElement("div");
1286
node.innerHTML = "hi";
1287
var widget = cm.addLineWidget(1, node);
1288
is(last.top < cm.charCoords(Pos(2, 0)).top, "took up space");
1289
cm.setCursor(Pos(1, 1));
1290
cm.execCommand("goLineDown");
1291
eqPos(cm.getCursor(), Pos(2, 1));
1292
cm.execCommand("goLineUp");
1293
eqPos(cm.getCursor(), Pos(1, 1));
1294
});
1295
1296
testCM("lineWidgetFocus", function(cm) {
1297
var place = document.getElementById("testground");
1298
place.className = "offscreen";
1299
try {
1300
addDoc(cm, 500, 10);
1301
var node = document.createElement("input");
1302
var widget = cm.addLineWidget(1, node);
1303
node.focus();
1304
eq(document.activeElement, node);
1305
cm.replaceRange("new stuff", Pos(1, 0));
1306
eq(document.activeElement, node);
1307
} finally {
1308
place.className = "";
1309
}
1310
});
1311
1312
testCM("getLineNumber", function(cm) {
1313
addDoc(cm, 2, 20);
1314
var h1 = cm.getLineHandle(1);
1315
eq(cm.getLineNumber(h1), 1);
1316
cm.replaceRange("hi\nbye\n", Pos(0, 0));
1317
eq(cm.getLineNumber(h1), 3);
1318
cm.setValue("");
1319
eq(cm.getLineNumber(h1), null);
1320
});
1321
1322
testCM("jumpTheGap", function(cm) {
1323
var longLine = "abcdef ghiklmnop qrstuvw xyz ";
1324
longLine += longLine; longLine += longLine; longLine += longLine;
1325
cm.setLine(2, longLine);
1326
cm.setSize("200px", null);
1327
cm.getWrapperElement().style.lineHeight = 2;
1328
cm.refresh();
1329
cm.setCursor(Pos(0, 1));
1330
cm.execCommand("goLineDown");
1331
eqPos(cm.getCursor(), Pos(1, 1));
1332
cm.execCommand("goLineDown");
1333
eqPos(cm.getCursor(), Pos(2, 1));
1334
cm.execCommand("goLineDown");
1335
eq(cm.getCursor().line, 2);
1336
is(cm.getCursor().ch > 1);
1337
cm.execCommand("goLineUp");
1338
eqPos(cm.getCursor(), Pos(2, 1));
1339
cm.execCommand("goLineUp");
1340
eqPos(cm.getCursor(), Pos(1, 1));
1341
var node = document.createElement("div");
1342
node.innerHTML = "hi"; node.style.height = "30px";
1343
cm.addLineWidget(0, node);
1344
cm.addLineWidget(1, node.cloneNode(true), {above: true});
1345
cm.setCursor(Pos(0, 2));
1346
cm.execCommand("goLineDown");
1347
eqPos(cm.getCursor(), Pos(1, 2));
1348
cm.execCommand("goLineUp");
1349
eqPos(cm.getCursor(), Pos(0, 2));
1350
}, {lineWrapping: true, value: "abc\ndef\nghi\njkl\n"});
1351
1352
testCM("addLineClass", function(cm) {
1353
function cls(line, text, bg, wrap) {
1354
var i = cm.lineInfo(line);
1355
eq(i.textClass, text);
1356
eq(i.bgClass, bg);
1357
eq(i.wrapClass, wrap);
1358
}
1359
cm.addLineClass(0, "text", "foo");
1360
cm.addLineClass(0, "text", "bar");
1361
cm.addLineClass(1, "background", "baz");
1362
cm.addLineClass(1, "wrap", "foo");
1363
cls(0, "foo bar", null, null);
1364
cls(1, null, "baz", "foo");
1365
var lines = cm.display.lineDiv;
1366
eq(byClassName(lines, "foo").length, 2);
1367
eq(byClassName(lines, "bar").length, 1);
1368
eq(byClassName(lines, "baz").length, 1);
1369
cm.removeLineClass(0, "text", "foo");
1370
cls(0, "bar", null, null);
1371
cm.removeLineClass(0, "text", "foo");
1372
cls(0, "bar", null, null);
1373
cm.removeLineClass(0, "text", "bar");
1374
cls(0, null, null, null);
1375
cm.addLineClass(1, "wrap", "quux");
1376
cls(1, null, "baz", "foo quux");
1377
cm.removeLineClass(1, "wrap");
1378
cls(1, null, "baz", null);
1379
}, {value: "hohoho\n"});
1380
1381
testCM("atomicMarker", function(cm) {
1382
addDoc(cm, 10, 10);
1383
function atom(ll, cl, lr, cr, li, ri) {
1384
return cm.markText(Pos(ll, cl), Pos(lr, cr),
1385
{atomic: true, inclusiveLeft: li, inclusiveRight: ri});
1386
}
1387
var m = atom(0, 1, 0, 5);
1388
cm.setCursor(Pos(0, 1));
1389
cm.execCommand("goCharRight");
1390
eqPos(cm.getCursor(), Pos(0, 5));
1391
cm.execCommand("goCharLeft");
1392
eqPos(cm.getCursor(), Pos(0, 1));
1393
m.clear();
1394
m = atom(0, 0, 0, 5, true);
1395
eqPos(cm.getCursor(), Pos(0, 5), "pushed out");
1396
cm.execCommand("goCharLeft");
1397
eqPos(cm.getCursor(), Pos(0, 5));
1398
m.clear();
1399
m = atom(8, 4, 9, 10, false, true);
1400
cm.setCursor(Pos(9, 8));
1401
eqPos(cm.getCursor(), Pos(8, 4), "set");
1402
cm.execCommand("goCharRight");
1403
eqPos(cm.getCursor(), Pos(8, 4), "char right");
1404
cm.execCommand("goLineDown");
1405
eqPos(cm.getCursor(), Pos(8, 4), "line down");
1406
cm.execCommand("goCharLeft");
1407
eqPos(cm.getCursor(), Pos(8, 3));
1408
m.clear();
1409
m = atom(1, 1, 3, 8);
1410
cm.setCursor(Pos(0, 0));
1411
cm.setCursor(Pos(2, 0));
1412
eqPos(cm.getCursor(), Pos(3, 8));
1413
cm.execCommand("goCharLeft");
1414
eqPos(cm.getCursor(), Pos(1, 1));
1415
cm.execCommand("goCharRight");
1416
eqPos(cm.getCursor(), Pos(3, 8));
1417
cm.execCommand("goLineUp");
1418
eqPos(cm.getCursor(), Pos(1, 1));
1419
cm.execCommand("goLineDown");
1420
eqPos(cm.getCursor(), Pos(3, 8));
1421
cm.execCommand("delCharBefore");
1422
eq(cm.getValue().length, 80, "del chunk");
1423
m = atom(3, 0, 5, 5);
1424
cm.setCursor(Pos(3, 0));
1425
cm.execCommand("delWordAfter");
1426
eq(cm.getValue().length, 53, "del chunk");
1427
});
1428
1429
testCM("readOnlyMarker", function(cm) {
1430
function mark(ll, cl, lr, cr, at) {
1431
return cm.markText(Pos(ll, cl), Pos(lr, cr),
1432
{readOnly: true, atomic: at});
1433
}
1434
var m = mark(0, 1, 0, 4);
1435
cm.setCursor(Pos(0, 2));
1436
cm.replaceSelection("hi", "end");
1437
eqPos(cm.getCursor(), Pos(0, 2));
1438
eq(cm.getLine(0), "abcde");
1439
cm.execCommand("selectAll");
1440
cm.replaceSelection("oops");
1441
eq(cm.getValue(), "oopsbcd");
1442
cm.undo();
1443
eqPos(m.find().from, Pos(0, 1));
1444
eqPos(m.find().to, Pos(0, 4));
1445
m.clear();
1446
cm.setCursor(Pos(0, 2));
1447
cm.replaceSelection("hi");
1448
eq(cm.getLine(0), "abhicde");
1449
eqPos(cm.getCursor(), Pos(0, 4));
1450
m = mark(0, 2, 2, 2, true);
1451
cm.setSelection(Pos(1, 1), Pos(2, 4));
1452
cm.replaceSelection("t", "end");
1453
eqPos(cm.getCursor(), Pos(2, 3));
1454
eq(cm.getLine(2), "klto");
1455
cm.execCommand("goCharLeft");
1456
cm.execCommand("goCharLeft");
1457
eqPos(cm.getCursor(), Pos(0, 2));
1458
cm.setSelection(Pos(0, 1), Pos(0, 3));
1459
cm.replaceSelection("xx");
1460
eqPos(cm.getCursor(), Pos(0, 3));
1461
eq(cm.getLine(0), "axxhicde");
1462
}, {value: "abcde\nfghij\nklmno\n"});
1463
1464
testCM("dirtyBit", function(cm) {
1465
eq(cm.isClean(), true);
1466
cm.replaceSelection("boo");
1467
eq(cm.isClean(), false);
1468
cm.undo();
1469
eq(cm.isClean(), true);
1470
cm.replaceSelection("boo");
1471
cm.replaceSelection("baz");
1472
cm.undo();
1473
eq(cm.isClean(), false);
1474
cm.markClean();
1475
eq(cm.isClean(), true);
1476
cm.undo();
1477
eq(cm.isClean(), false);
1478
cm.redo();
1479
eq(cm.isClean(), true);
1480
});
1481
1482
testCM("changeGeneration", function(cm) {
1483
cm.replaceSelection("x", null, "+insert");
1484
var softGen = cm.changeGeneration();
1485
cm.replaceSelection("x", null, "+insert");
1486
cm.undo();
1487
eq(cm.getValue(), "");
1488
is(!cm.isClean(softGen));
1489
cm.replaceSelection("x", null, "+insert");
1490
var hardGen = cm.changeGeneration(true);
1491
cm.replaceSelection("x", null, "+insert");
1492
cm.undo();
1493
eq(cm.getValue(), "x");
1494
is(cm.isClean(hardGen));
1495
});
1496
1497
testCM("addKeyMap", function(cm) {
1498
function sendKey(code) {
1499
cm.triggerOnKeyDown({type: "keydown", keyCode: code,
1500
preventDefault: function(){}, stopPropagation: function(){}});
1501
}
1502
1503
sendKey(39);
1504
eqPos(cm.getCursor(), Pos(0, 1));
1505
var test = 0;
1506
var map1 = {Right: function() { ++test; }}, map2 = {Right: function() { test += 10; }}
1507
cm.addKeyMap(map1);
1508
sendKey(39);
1509
eqPos(cm.getCursor(), Pos(0, 1));
1510
eq(test, 1);
1511
cm.addKeyMap(map2, true);
1512
sendKey(39);
1513
eq(test, 2);
1514
cm.removeKeyMap(map1);
1515
sendKey(39);
1516
eq(test, 12);
1517
cm.removeKeyMap(map2);
1518
sendKey(39);
1519
eq(test, 12);
1520
eqPos(cm.getCursor(), Pos(0, 2));
1521
cm.addKeyMap({Right: function() { test = 55; }, name: "mymap"});
1522
sendKey(39);
1523
eq(test, 55);
1524
cm.removeKeyMap("mymap");
1525
sendKey(39);
1526
eqPos(cm.getCursor(), Pos(0, 3));
1527
}, {value: "abc"});
1528
1529
testCM("findPosH", function(cm) {
1530
forEach([{from: Pos(0, 0), to: Pos(0, 1), by: 1},
1531
{from: Pos(0, 0), to: Pos(0, 0), by: -1, hitSide: true},
1532
{from: Pos(0, 0), to: Pos(0, 4), by: 1, unit: "word"},
1533
{from: Pos(0, 0), to: Pos(0, 8), by: 2, unit: "word"},
1534
{from: Pos(0, 0), to: Pos(2, 0), by: 20, unit: "word", hitSide: true},
1535
{from: Pos(0, 7), to: Pos(0, 5), by: -1, unit: "word"},
1536
{from: Pos(0, 4), to: Pos(0, 8), by: 1, unit: "word"},
1537
{from: Pos(1, 0), to: Pos(1, 18), by: 3, unit: "word"},
1538
{from: Pos(1, 22), to: Pos(1, 5), by: -3, unit: "word"},
1539
{from: Pos(1, 15), to: Pos(1, 10), by: -5},
1540
{from: Pos(1, 15), to: Pos(1, 10), by: -5, unit: "column"},
1541
{from: Pos(1, 15), to: Pos(1, 0), by: -50, unit: "column", hitSide: true},
1542
{from: Pos(1, 15), to: Pos(1, 24), by: 50, unit: "column", hitSide: true},
1543
{from: Pos(1, 15), to: Pos(2, 0), by: 50, hitSide: true}], function(t) {
1544
var r = cm.findPosH(t.from, t.by, t.unit || "char");
1545
eqPos(r, t.to);
1546
eq(!!r.hitSide, !!t.hitSide);
1547
});
1548
}, {value: "line one\nline two.something.other\n"});
1549
1550
testCM("beforeChange", function(cm) {
1551
cm.on("beforeChange", function(cm, change) {
1552
var text = [];
1553
for (var i = 0; i < change.text.length; ++i)
1554
text.push(change.text[i].replace(/\s/g, "_"));
1555
change.update(null, null, text);
1556
});
1557
cm.setValue("hello, i am a\nnew document\n");
1558
eq(cm.getValue(), "hello,_i_am_a\nnew_document\n");
1559
CodeMirror.on(cm.getDoc(), "beforeChange", function(doc, change) {
1560
if (change.from.line == 0) change.cancel();
1561
});
1562
cm.setValue("oops"); // Canceled
1563
eq(cm.getValue(), "hello,_i_am_a\nnew_document\n");
1564
cm.replaceRange("hey hey hey", Pos(1, 0), Pos(2, 0));
1565
eq(cm.getValue(), "hello,_i_am_a\nhey_hey_hey");
1566
}, {value: "abcdefghijk"});
1567
1568
testCM("beforeChangeUndo", function(cm) {
1569
cm.setLine(0, "hi");
1570
cm.setLine(0, "bye");
1571
eq(cm.historySize().undo, 2);
1572
cm.on("beforeChange", function(cm, change) {
1573
is(!change.update);
1574
change.cancel();
1575
});
1576
cm.undo();
1577
eq(cm.historySize().undo, 0);
1578
eq(cm.getValue(), "bye\ntwo");
1579
}, {value: "one\ntwo"});
1580
1581
testCM("beforeSelectionChange", function(cm) {
1582
function notAtEnd(cm, pos) {
1583
var len = cm.getLine(pos.line).length;
1584
if (!len || pos.ch == len) return Pos(pos.line, pos.ch - 1);
1585
return pos;
1586
}
1587
cm.on("beforeSelectionChange", function(cm, sel) {
1588
sel.head = notAtEnd(cm, sel.head);
1589
sel.anchor = notAtEnd(cm, sel.anchor);
1590
});
1591
1592
addDoc(cm, 10, 10);
1593
cm.execCommand("goLineEnd");
1594
eqPos(cm.getCursor(), Pos(0, 9));
1595
cm.execCommand("selectAll");
1596
eqPos(cm.getCursor("start"), Pos(0, 0));
1597
eqPos(cm.getCursor("end"), Pos(9, 9));
1598
});
1599
1600
testCM("change_removedText", function(cm) {
1601
cm.setValue("abc\ndef");
1602
1603
var removedText;
1604
cm.on("change", function(cm, change) {
1605
removedText = [change.removed, change.next && change.next.removed];
1606
});
1607
1608
cm.operation(function() {
1609
cm.replaceRange("xyz", Pos(0, 0), Pos(1,1));
1610
cm.replaceRange("123", Pos(0,0));
1611
});
1612
1613
eq(removedText[0].join("\n"), "abc\nd");
1614
eq(removedText[1].join("\n"), "");
1615
1616
cm.undo();
1617
eq(removedText[0].join("\n"), "123");
1618
eq(removedText[1].join("\n"), "xyz");
1619
1620
cm.redo();
1621
eq(removedText[0].join("\n"), "abc\nd");
1622
eq(removedText[1].join("\n"), "");
1623
});
1624
1625
testCM("lineStyleFromMode", function(cm) {
1626
CodeMirror.defineMode("test_mode", function() {
1627
return {token: function(stream) {
1628
if (stream.match(/^\[[^\]]*\]/)) return "line-brackets";
1629
if (stream.match(/^\([^\]]*\)/)) return "line-background-parens";
1630
stream.match(/^\s+|^\S+/);
1631
}};
1632
});
1633
cm.setOption("mode", "test_mode");
1634
var bracketElts = byClassName(cm.getWrapperElement(), "brackets");
1635
eq(bracketElts.length, 1);
1636
eq(bracketElts[0].nodeName, "PRE");
1637
is(!/brackets.*brackets/.test(bracketElts[0].className));
1638
var parenElts = byClassName(cm.getWrapperElement(), "parens");
1639
eq(parenElts.length, 1);
1640
eq(parenElts[0].nodeName, "DIV");
1641
is(!/parens.*parens/.test(parenElts[0].className));
1642
}, {value: "line1: [br] [br]\nline2: (par) (par)\nline3: nothing"});
1643
1644
CodeMirror.registerHelper("xxx", "a", "A");
1645
CodeMirror.registerHelper("xxx", "b", "B");
1646
CodeMirror.defineMode("yyy", function() {
1647
return {
1648
token: function(stream) { stream.skipToEnd(); },
1649
xxx: ["a", "b", "q"]
1650
};
1651
});
1652
CodeMirror.registerGlobalHelper("xxx", "c", function(m) { return m.enableC; }, "C");
1653
1654
testCM("helpers", function(cm) {
1655
cm.setOption("mode", "yyy");
1656
eq(cm.getHelpers(Pos(0, 0), "xxx").join("/"), "A/B");
1657
cm.setOption("mode", {name: "yyy", modeProps: {xxx: "b", enableC: true}});
1658
eq(cm.getHelpers(Pos(0, 0), "xxx").join("/"), "B/C");
1659
cm.setOption("mode", "javascript");
1660
eq(cm.getHelpers(Pos(0, 0), "xxx").join("/"), "");
1661
});
1662
1663