1 var Pos = CodeMirror.Pos;
3 function forEach(arr, f) {
4 for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
7 function addDoc(cm, width, height) {
8 var content = [], line = "";
9 for (var i = 0; i < width; ++i) line += "x";
10 for (var i = 0; i < height; ++i) content.push(line);
11 cm.setValue(content.join("\n"));
14 function byClassName(elt, cls) {
15 if (elt.getElementsByClassName) return elt.getElementsByClassName(cls);
16 var found = [], re = new RegExp("\\b" + cls + "\\b");
17 function search(elt) {
18 if (elt.nodeType == 3) return;
19 if (re.test(elt.className)) found.push(elt);
20 for (var i = 0, e = elt.childNodes.length; i < e; ++i)
21 search(elt.childNodes[i]);
27 var ie_lt8 = /MSIE [1-7]\b/.test(navigator.userAgent);
28 var mac = /Mac/.test(navigator.platform);
29 var phantom = /PhantomJS/.test(navigator.userAgent);
30 var opera = /Opera\/\./.test(navigator.userAgent);
31 var opera_version = opera && navigator.userAgent.match(/Version\/(\d+\.\d+)/);
32 if (opera_version) opera_version = Number(opera_version);
33 var opera_lt10 = opera && (!opera_version || opera_version < 10);
37 test("core_fromTextArea", function() {
38 var te = document.getElementById("code");
40 var cm = CodeMirror.fromTextArea(te);
42 eq(cm.getValue(), "CONTENT");
43 cm.setValue("foo\nbar");
44 eq(cm.getValue(), "foo\nbar");
46 is(/^foo\r?\nbar$/.test(te.value));
53 testCM("getRange", function(cm) {
54 eq(cm.getLine(0), "1234");
55 eq(cm.getLine(1), "5678");
56 eq(cm.getLine(2), null);
57 eq(cm.getLine(-1), null);
58 eq(cm.getRange(Pos(0, 0), Pos(0, 3)), "123");
59 eq(cm.getRange(Pos(0, -1), Pos(0, 200)), "1234");
60 eq(cm.getRange(Pos(0, 2), Pos(1, 2)), "34\n56");
61 eq(cm.getRange(Pos(1, 2), Pos(100, 0)), "78");
62 }, {value: "1234\n5678"});
64 testCM("replaceRange", function(cm) {
65 eq(cm.getValue(), "");
66 cm.replaceRange("foo\n", Pos(0, 0));
67 eq(cm.getValue(), "foo\n");
68 cm.replaceRange("a\nb", Pos(0, 1));
69 eq(cm.getValue(), "fa\nboo\n");
70 eq(cm.lineCount(), 3);
71 cm.replaceRange("xyzzy", Pos(0, 0), Pos(1, 1));
72 eq(cm.getValue(), "xyzzyoo\n");
73 cm.replaceRange("abc", Pos(0, 0), Pos(10, 0));
74 eq(cm.getValue(), "abc");
75 eq(cm.lineCount(), 1);
78 testCM("selection", function(cm) {
79 cm.setSelection(Pos(0, 4), Pos(2, 2));
80 is(cm.somethingSelected());
81 eq(cm.getSelection(), "11\n222222\n33");
82 eqPos(cm.getCursor(false), Pos(2, 2));
83 eqPos(cm.getCursor(true), Pos(0, 4));
84 cm.setSelection(Pos(1, 0));
85 is(!cm.somethingSelected());
86 eq(cm.getSelection(), "");
87 eqPos(cm.getCursor(true), Pos(1, 0));
88 cm.replaceSelection("abc");
89 eq(cm.getSelection(), "abc");
90 eq(cm.getValue(), "111111\nabc222222\n333333");
91 cm.replaceSelection("def", "end");
92 eq(cm.getSelection(), "");
93 eqPos(cm.getCursor(true), Pos(1, 3));
94 cm.setCursor(Pos(2, 1));
95 eqPos(cm.getCursor(true), Pos(2, 1));
97 eqPos(cm.getCursor(true), Pos(1, 2));
98 }, {value: "111111\n222222\n333333"});
100 testCM("extendSelection", function(cm) {
101 cm.setExtending(true);
103 cm.setSelection(Pos(3, 5));
104 eqPos(cm.getCursor("head"), Pos(3, 5));
105 eqPos(cm.getCursor("anchor"), Pos(3, 5));
106 cm.setSelection(Pos(2, 5), Pos(5, 5));
107 eqPos(cm.getCursor("head"), Pos(5, 5));
108 eqPos(cm.getCursor("anchor"), Pos(2, 5));
109 eqPos(cm.getCursor("start"), Pos(2, 5));
110 eqPos(cm.getCursor("end"), Pos(5, 5));
111 cm.setSelection(Pos(5, 5), Pos(2, 5));
112 eqPos(cm.getCursor("head"), Pos(2, 5));
113 eqPos(cm.getCursor("anchor"), Pos(5, 5));
114 eqPos(cm.getCursor("start"), Pos(2, 5));
115 eqPos(cm.getCursor("end"), Pos(5, 5));
116 cm.extendSelection(Pos(3, 2));
117 eqPos(cm.getCursor("head"), Pos(3, 2));
118 eqPos(cm.getCursor("anchor"), Pos(5, 5));
119 cm.extendSelection(Pos(6, 2));
120 eqPos(cm.getCursor("head"), Pos(6, 2));
121 eqPos(cm.getCursor("anchor"), Pos(5, 5));
122 cm.extendSelection(Pos(6, 3), Pos(6, 4));
123 eqPos(cm.getCursor("head"), Pos(6, 4));
124 eqPos(cm.getCursor("anchor"), Pos(5, 5));
125 cm.extendSelection(Pos(0, 3), Pos(0, 4));
126 eqPos(cm.getCursor("head"), Pos(0, 3));
127 eqPos(cm.getCursor("anchor"), Pos(5, 5));
128 cm.extendSelection(Pos(4, 5), Pos(6, 5));
129 eqPos(cm.getCursor("head"), Pos(6, 5));
130 eqPos(cm.getCursor("anchor"), Pos(4, 5));
131 cm.setExtending(false);
132 cm.extendSelection(Pos(0, 3), Pos(0, 4));
133 eqPos(cm.getCursor("head"), Pos(0, 4));
134 eqPos(cm.getCursor("anchor"), Pos(0, 3));
137 testCM("lines", function(cm) {
138 eq(cm.getLine(0), "111111");
139 eq(cm.getLine(1), "222222");
140 eq(cm.getLine(-1), null);
142 cm.setLine(1, "abc");
143 eq(cm.getValue(), "111111\nabc");
144 }, {value: "111111\n222222\n333333"});
146 testCM("indent", function(cm) {
148 eq(cm.getLine(1), " blah();");
149 cm.setOption("indentUnit", 8);
151 eq(cm.getLine(1), "\tblah();");
152 cm.setOption("indentUnit", 10);
153 cm.setOption("tabSize", 4);
155 eq(cm.getLine(1), "\t\t blah();");
156 }, {value: "if (x) {\nblah();\n}", indentUnit: 3, indentWithTabs: true, tabSize: 8});
158 testCM("indentByNumber", function(cm) {
160 eq(cm.getLine(0), " foo");
161 cm.indentLine(0, -200);
162 eq(cm.getLine(0), "foo");
163 cm.setSelection(Pos(0, 0), Pos(1, 2));
164 cm.indentSelection(3);
165 eq(cm.getValue(), " foo\n bar\nbaz");
166 }, {value: "foo\nbar\nbaz"});
168 test("core_defaults", function() {
169 var defsCopy = {}, defs = CodeMirror.defaults;
170 for (var opt in defs) defsCopy[opt] = defs[opt];
173 defs.enterMode = "keep";
175 var place = document.getElementById("testground"), cm = CodeMirror(place);
177 eq(cm.getOption("indentUnit"), 5);
178 cm.setOption("indentUnit", 10);
179 eq(defs.indentUnit, 5);
180 eq(cm.getValue(), "uu");
181 eq(cm.getOption("enterMode"), "keep");
182 eq(cm.getInputField().tabIndex, 55);
185 for (var opt in defsCopy) defs[opt] = defsCopy[opt];
186 place.removeChild(cm.getWrapperElement());
190 testCM("lineInfo", function(cm) {
191 eq(cm.lineInfo(-1), null);
192 var mark = document.createElement("span");
193 var lh = cm.setGutterMarker(1, "FOO", mark);
194 var info = cm.lineInfo(1);
195 eq(info.text, "222222");
196 eq(info.gutterMarkers.FOO, mark);
198 eq(cm.lineInfo(2).gutterMarkers, null);
199 cm.setGutterMarker(lh, "FOO", null);
200 eq(cm.lineInfo(1).gutterMarkers, null);
201 cm.setGutterMarker(1, "FOO", mark);
202 cm.setGutterMarker(0, "FOO", mark);
203 cm.clearGutter("FOO");
204 eq(cm.lineInfo(0).gutterMarkers, null);
205 eq(cm.lineInfo(1).gutterMarkers, null);
206 }, {value: "111111\n222222\n333333"});
208 testCM("coords", function(cm) {
209 cm.setSize(null, 100);
211 var top = cm.charCoords(Pos(0, 0));
212 var bot = cm.charCoords(Pos(200, 30));
213 is(top.left < bot.left);
214 is(top.top < bot.top);
215 is(top.top < top.bottom);
216 cm.scrollTo(null, 100);
217 var top2 = cm.charCoords(Pos(0, 0));
218 is(top.top > top2.top);
219 eq(top.left, top2.left);
222 testCM("coordsChar", function(cm) {
224 for (var i = 0; i < 2; ++i) {
225 var sys = i ? "local" : "page";
226 for (var ch = 0; ch <= 35; ch += 5) {
227 for (var line = 0; line < 70; line += 5) {
228 cm.setCursor(line, ch);
229 var coords = cm.charCoords(Pos(line, ch), sys);
230 var pos = cm.coordsChar({left: coords.left + 1, top: coords.top + 1}, sys);
231 eqPos(pos, Pos(line, ch));
235 }, {lineNumbers: true});
237 testCM("posFromIndex", function(cm) {
239 "This function should\n" +
240 "convert a zero based index\n" +
245 { index: -1, line: 0, ch: 0 }, // <- Tests clipping
246 { index: 0, line: 0, ch: 0 },
247 { index: 10, line: 0, ch: 10 },
248 { index: 39, line: 1, ch: 18 },
249 { index: 55, line: 2, ch: 7 },
250 { index: 63, line: 2, ch: 15 },
251 { index: 64, line: 2, ch: 15 } // <- Tests clipping
254 for (var i = 0; i < examples.length; i++) {
255 var example = examples[i];
256 var pos = cm.posFromIndex(example.index);
257 eq(pos.line, example.line);
258 eq(pos.ch, example.ch);
259 if (example.index >= 0 && example.index < 64)
260 eq(cm.indexFromPos(pos), example.index);
264 testCM("undo", function(cm) {
265 cm.setLine(0, "def");
266 eq(cm.historySize().undo, 1);
268 eq(cm.getValue(), "abc");
269 eq(cm.historySize().undo, 0);
270 eq(cm.historySize().redo, 1);
272 eq(cm.getValue(), "def");
273 eq(cm.historySize().undo, 1);
274 eq(cm.historySize().redo, 0);
275 cm.setValue("1\n\n\n2");
277 eq(cm.historySize().undo, 0);
278 for (var i = 0; i < 20; ++i) {
279 cm.replaceRange("a", Pos(0, 0));
280 cm.replaceRange("b", Pos(3, 0));
282 eq(cm.historySize().undo, 40);
283 for (var i = 0; i < 40; ++i)
285 eq(cm.historySize().redo, 40);
286 eq(cm.getValue(), "1\n\n\n2");
289 testCM("undoDepth", function(cm) {
290 cm.replaceRange("d", Pos(0));
291 cm.replaceRange("e", Pos(0));
292 cm.replaceRange("f", Pos(0));
293 cm.undo(); cm.undo(); cm.undo();
294 eq(cm.getValue(), "abcd");
295 }, {value: "abc", undoDepth: 2});
297 testCM("undoDoesntClearValue", function(cm) {
299 eq(cm.getValue(), "x");
302 testCM("undoMultiLine", function(cm) {
303 cm.operation(function() {
304 cm.replaceRange("x", Pos(0, 0));
305 cm.replaceRange("y", Pos(1, 0));
308 eq(cm.getValue(), "abc\ndef\nghi");
309 cm.operation(function() {
310 cm.replaceRange("y", Pos(1, 0));
311 cm.replaceRange("x", Pos(0, 0));
314 eq(cm.getValue(), "abc\ndef\nghi");
315 cm.operation(function() {
316 cm.replaceRange("y", Pos(2, 0));
317 cm.replaceRange("x", Pos(1, 0));
318 cm.replaceRange("z", Pos(2, 0));
321 eq(cm.getValue(), "abc\ndef\nghi", 3);
322 }, {value: "abc\ndef\nghi"});
324 testCM("undoComposite", function(cm) {
325 cm.replaceRange("y", Pos(1));
326 cm.operation(function() {
327 cm.replaceRange("x", Pos(0));
328 cm.replaceRange("z", Pos(2));
330 eq(cm.getValue(), "ax\nby\ncz\n");
332 eq(cm.getValue(), "a\nby\nc\n");
334 eq(cm.getValue(), "a\nb\nc\n");
335 cm.redo(); cm.redo();
336 eq(cm.getValue(), "ax\nby\ncz\n");
337 }, {value: "a\nb\nc\n"});
339 testCM("undoSelection", function(cm) {
340 cm.setSelection(Pos(0, 2), Pos(0, 4));
341 cm.replaceSelection("");
342 cm.setCursor(Pos(1, 0));
344 eqPos(cm.getCursor(true), Pos(0, 2));
345 eqPos(cm.getCursor(false), Pos(0, 4));
346 cm.setCursor(Pos(1, 0));
348 eqPos(cm.getCursor(true), Pos(0, 2));
349 eqPos(cm.getCursor(false), Pos(0, 2));
350 }, {value: "abcdefgh\n"});
352 testCM("markTextSingleLine", function(cm) {
353 forEach([{a: 0, b: 1, c: "", f: 2, t: 5},
354 {a: 0, b: 4, c: "", f: 0, t: 2},
355 {a: 1, b: 2, c: "x", f: 3, t: 6},
356 {a: 4, b: 5, c: "", f: 3, t: 5},
357 {a: 4, b: 5, c: "xx", f: 3, t: 7},
358 {a: 2, b: 5, c: "", f: 2, t: 3},
359 {a: 2, b: 5, c: "abcd", f: 6, t: 7},
360 {a: 2, b: 6, c: "x", f: null, t: null},
361 {a: 3, b: 6, c: "", f: null, t: null},
362 {a: 0, b: 9, c: "hallo", f: null, t: null},
363 {a: 4, b: 6, c: "x", f: 3, t: 4},
364 {a: 4, b: 8, c: "", f: 3, t: 4},
365 {a: 6, b: 6, c: "a", f: 3, t: 6},
366 {a: 8, b: 9, c: "", f: 3, t: 6}], function(test) {
367 cm.setValue("1234567890");
368 var r = cm.markText(Pos(0, 3), Pos(0, 6), {className: "foo"});
369 cm.replaceRange(test.c, Pos(0, test.a), Pos(0, test.b));
371 eq(f && f.from.ch, test.f); eq(f && f.to.ch, test.t);
375 testCM("markTextMultiLine", function(cm) {
376 function p(v) { return v && Pos(v[0], v[1]); }
377 forEach([{a: [0, 0], b: [0, 5], c: "", f: [0, 0], t: [2, 5]},
378 {a: [0, 0], b: [0, 5], c: "foo\n", f: [1, 0], t: [3, 5]},
379 {a: [0, 1], b: [0, 10], c: "", f: [0, 1], t: [2, 5]},
380 {a: [0, 5], b: [0, 6], c: "x", f: [0, 6], t: [2, 5]},
381 {a: [0, 0], b: [1, 0], c: "", f: [0, 0], t: [1, 5]},
382 {a: [0, 6], b: [2, 4], c: "", f: [0, 5], t: [0, 7]},
383 {a: [0, 6], b: [2, 4], c: "aa", f: [0, 5], t: [0, 9]},
384 {a: [1, 2], b: [1, 8], c: "", f: [0, 5], t: [2, 5]},
385 {a: [0, 5], b: [2, 5], c: "xx", f: null, t: null},
386 {a: [0, 0], b: [2, 10], c: "x", f: null, t: null},
387 {a: [1, 5], b: [2, 5], c: "", f: [0, 5], t: [1, 5]},
388 {a: [2, 0], b: [2, 3], c: "", f: [0, 5], t: [2, 2]},
389 {a: [2, 5], b: [3, 0], c: "a\nb", f: [0, 5], t: [2, 5]},
390 {a: [2, 3], b: [3, 0], c: "x", f: [0, 5], t: [2, 3]},
391 {a: [1, 1], b: [1, 9], c: "1\n2\n3", f: [0, 5], t: [4, 5]}], function(test) {
392 cm.setValue("aaaaaaaaaa\nbbbbbbbbbb\ncccccccccc\ndddddddd\n");
393 var r = cm.markText(Pos(0, 5), Pos(2, 5),
394 {className: "CodeMirror-matchingbracket"});
395 cm.replaceRange(test.c, p(test.a), p(test.b));
397 eqPos(f && f.from, p(test.f)); eqPos(f && f.to, p(test.t));
401 testCM("markTextUndo", function(cm) {
402 var marker1, marker2, bookmark;
403 marker1 = cm.markText(Pos(0, 1), Pos(0, 3),
404 {className: "CodeMirror-matchingbracket"});
405 marker2 = cm.markText(Pos(0, 0), Pos(2, 1),
406 {className: "CodeMirror-matchingbracket"});
407 bookmark = cm.setBookmark(Pos(1, 5));
408 cm.operation(function(){
409 cm.replaceRange("foo", Pos(0, 2));
410 cm.replaceRange("bar\nbaz\nbug\n", Pos(2, 0), Pos(3, 0));
412 var v1 = cm.getValue();
414 eq(marker1.find(), null); eq(marker2.find(), null); eq(bookmark.find(), null);
416 eqPos(bookmark.find(), Pos(1, 5), "still there");
418 var m1Pos = marker1.find(), m2Pos = marker2.find();
419 eqPos(m1Pos.from, Pos(0, 1)); eqPos(m1Pos.to, Pos(0, 3));
420 eqPos(m2Pos.from, Pos(0, 0)); eqPos(m2Pos.to, Pos(2, 1));
421 eqPos(bookmark.find(), Pos(1, 5));
422 cm.redo(); cm.redo();
423 eq(bookmark.find(), null);
425 eqPos(bookmark.find(), Pos(1, 5));
426 eq(cm.getValue(), v1);
427 }, {value: "1234\n56789\n00\n"});
429 testCM("markTextStayGone", function(cm) {
430 var m1 = cm.markText(Pos(0, 0), Pos(0, 1));
431 cm.replaceRange("hi", Pos(0, 2));
435 }, {value: "hello"});
437 testCM("undoPreservesNewMarks", function(cm) {
438 cm.markText(Pos(0, 3), Pos(0, 4));
439 cm.markText(Pos(1, 1), Pos(1, 3));
440 cm.replaceRange("", Pos(0, 3), Pos(3, 1));
441 var mBefore = cm.markText(Pos(0, 0), Pos(0, 1));
442 var mAfter = cm.markText(Pos(0, 5), Pos(0, 6));
443 var mAround = cm.markText(Pos(0, 2), Pos(0, 4));
445 eqPos(mBefore.find().from, Pos(0, 0));
446 eqPos(mBefore.find().to, Pos(0, 1));
447 eqPos(mAfter.find().from, Pos(3, 3));
448 eqPos(mAfter.find().to, Pos(3, 4));
449 eqPos(mAround.find().from, Pos(0, 2));
450 eqPos(mAround.find().to, Pos(3, 2));
451 var found = cm.findMarksAt(Pos(2, 2));
453 eq(found[0], mAround);
454 }, {value: "aaaa\nbbbb\ncccc\ndddd"});
456 testCM("markClearBetween", function(cm) {
457 cm.setValue("aaa\nbbb\nccc\nddd\n");
458 cm.markText(Pos(0, 0), Pos(2));
459 cm.replaceRange("aaa\nbbb\nccc", Pos(0, 0), Pos(2));
460 eq(cm.findMarksAt(Pos(1, 1)).length, 0);
463 testCM("deleteSpanCollapsedInclusiveLeft", function(cm) {
464 var from = Pos(1, 0), to = Pos(1, 1);
465 var m = cm.markText(from, to, {collapsed: true, inclusiveLeft: true});
466 // Delete collapsed span.
467 cm.replaceRange("", from, to);
468 }, {value: "abc\nX\ndef"});
470 testCM("bookmark", function(cm) {
471 function p(v) { return v && Pos(v[0], v[1]); }
472 forEach([{a: [1, 0], b: [1, 1], c: "", d: [1, 4]},
473 {a: [1, 1], b: [1, 1], c: "xx", d: [1, 7]},
474 {a: [1, 4], b: [1, 5], c: "ab", d: [1, 6]},
475 {a: [1, 4], b: [1, 6], c: "", d: null},
476 {a: [1, 5], b: [1, 6], c: "abc", d: [1, 5]},
477 {a: [1, 6], b: [1, 8], c: "", d: [1, 5]},
478 {a: [1, 4], b: [1, 4], c: "\n\n", d: [3, 1]},
479 {bm: [1, 9], a: [1, 1], b: [1, 1], c: "\n", d: [2, 8]}], function(test) {
480 cm.setValue("1234567890\n1234567890\n1234567890");
481 var b = cm.setBookmark(p(test.bm) || Pos(1, 5));
482 cm.replaceRange(test.c, p(test.a), p(test.b));
483 eqPos(b.find(), p(test.d));
487 testCM("bookmarkInsertLeft", function(cm) {
488 var br = cm.setBookmark(Pos(0, 2), {insertLeft: false});
489 var bl = cm.setBookmark(Pos(0, 2), {insertLeft: true});
490 cm.setCursor(Pos(0, 2));
491 cm.replaceSelection("hi");
492 eqPos(br.find(), Pos(0, 2));
493 eqPos(bl.find(), Pos(0, 4));
494 cm.replaceRange("", Pos(0, 4), Pos(0, 5));
495 cm.replaceRange("", Pos(0, 2), Pos(0, 4));
496 cm.replaceRange("", Pos(0, 1), Pos(0, 2));
497 // Verify that deleting next to bookmarks doesn't kill them
498 eqPos(br.find(), Pos(0, 1));
499 eqPos(bl.find(), Pos(0, 1));
500 }, {value: "abcdef"});
502 testCM("bookmarkCursor", function(cm) {
503 var pos01 = cm.cursorCoords(Pos(0, 1)), pos11 = cm.cursorCoords(Pos(1, 1)),
504 pos20 = cm.cursorCoords(Pos(2, 0)), pos30 = cm.cursorCoords(Pos(3, 0)),
505 pos41 = cm.cursorCoords(Pos(4, 1));
506 cm.setBookmark(Pos(0, 1), {widget: document.createTextNode("←"), insertLeft: true});
507 cm.setBookmark(Pos(2, 0), {widget: document.createTextNode("←"), insertLeft: true});
508 cm.setBookmark(Pos(1, 1), {widget: document.createTextNode("→")});
509 cm.setBookmark(Pos(3, 0), {widget: document.createTextNode("→")});
510 var new01 = cm.cursorCoords(Pos(0, 1)), new11 = cm.cursorCoords(Pos(1, 1)),
511 new20 = cm.cursorCoords(Pos(2, 0)), new30 = cm.cursorCoords(Pos(3, 0));
512 is(new01.left == pos01.left && new01.top == pos01.top, "at left, middle of line");
513 is(new11.left > pos11.left && new11.top == pos11.top, "at right, middle of line");
514 is(new20.left == pos20.left && new20.top == pos20.top, "at left, empty line");
515 is(new30.left > pos30.left && new30.top == pos30.top, "at right, empty line");
516 cm.setBookmark(Pos(4, 0), {widget: document.createTextNode("→")});
517 is(cm.cursorCoords(Pos(4, 1)).left > pos41.left, "single-char bug");
518 }, {value: "foo\nbar\n\n\nx\ny"});
520 testCM("getAllMarks", function(cm) {
522 var m1 = cm.setBookmark(Pos(0, 2));
523 var m2 = cm.markText(Pos(0, 2), Pos(3, 2));
524 var m3 = cm.markText(Pos(1, 2), Pos(1, 8));
525 var m4 = cm.markText(Pos(8, 0), Pos(9, 0));
526 eq(cm.getAllMarks().length, 4);
529 eq(cm.getAllMarks().length, 2);
532 testCM("bug577", function(cm) {
535 cm.setValue("fooooo");
539 testCM("scrollSnap", function(cm) {
540 cm.setSize(100, 100);
541 addDoc(cm, 200, 200);
542 cm.setCursor(Pos(100, 180));
543 var info = cm.getScrollInfo();
544 is(info.left > 0 && info.top > 0);
545 cm.setCursor(Pos(0, 0));
546 info = cm.getScrollInfo();
547 is(info.left == 0 && info.top == 0, "scrolled clean to top");
548 cm.setCursor(Pos(100, 180));
549 cm.setCursor(Pos(199, 0));
550 info = cm.getScrollInfo();
551 is(info.left == 0 && info.top + 2 > info.height - cm.getScrollerElement().clientHeight, "scrolled clean to bottom");
554 testCM("scrollIntoView", function(cm) {
556 var outer = cm.getWrapperElement().getBoundingClientRect();
557 function test(line, ch) {
558 var pos = Pos(line, ch);
559 cm.scrollIntoView(pos);
560 var box = cm.charCoords(pos, "window");
561 is(box.left >= outer.left && box.right <= outer.right &&
562 box.top >= outer.top && box.bottom <= outer.bottom);
564 addDoc(cm, 200, 200);
573 testCM("selectionPos", function(cm) {
575 cm.setSize(100, 100);
576 addDoc(cm, 200, 100);
577 cm.setSelection(Pos(1, 100), Pos(98, 100));
578 var lineWidth = cm.charCoords(Pos(0, 200), "local").left;
579 var lineHeight = (cm.charCoords(Pos(99)).top - cm.charCoords(Pos(0)).top) / 100;
581 var selElt = byClassName(cm.getWrapperElement(), "CodeMirror-selected");
582 var outer = cm.getWrapperElement().getBoundingClientRect();
583 var sawMiddle, sawTop, sawBottom;
584 for (var i = 0, e = selElt.length; i < e; ++i) {
585 var box = selElt[i].getBoundingClientRect();
586 var atLeft = box.left - outer.left < 30;
587 var width = box.right - box.left;
588 var atRight = box.right - outer.left > .8 * lineWidth;
589 if (atLeft && atRight) {
591 is(box.bottom - box.top > 90 * lineHeight, "middle high");
592 is(width > .9 * lineWidth, "middle wide");
594 is(width > .4 * lineWidth, "top/bot wide enough");
595 is(width < .6 * lineWidth, "top/bot slim enough");
598 is(box.top - outer.top > 96 * lineHeight, "bot below");
599 } else if (atRight) {
601 is(box.top - outer.top < 2.1 * lineHeight, "top above");
605 is(sawTop && sawBottom && sawMiddle, "all parts");
608 testCM("restoreHistory", function(cm) {
609 cm.setValue("abc\ndef");
610 cm.setLine(1, "hello");
611 cm.setLine(0, "goop");
613 var storedVal = cm.getValue(), storedHist = cm.getHistory();
614 if (window.JSON) storedHist = JSON.parse(JSON.stringify(storedHist));
615 eq(storedVal, "abc\nhello");
618 eq(cm.historySize().undo, 0);
619 cm.setValue(storedVal);
620 cm.setHistory(storedHist);
622 eq(cm.getValue(), "goop\nhello");
623 cm.undo(); cm.undo();
624 eq(cm.getValue(), "abc\ndef");
627 testCM("doubleScrollbar", function(cm) {
628 var dummy = document.body.appendChild(document.createElement("p"));
629 dummy.style.cssText = "height: 50px; overflow: scroll; width: 50px";
630 var scrollbarWidth = dummy.offsetWidth + 1 - dummy.clientWidth;
631 document.body.removeChild(dummy);
632 if (scrollbarWidth < 2) return;
633 cm.setSize(null, 100);
635 var wrap = cm.getWrapperElement();
636 is(wrap.offsetWidth - byClassName(wrap, "CodeMirror-lines")[0].offsetWidth <= scrollbarWidth * 1.5);
639 testCM("weirdLinebreaks", function(cm) {
640 cm.setValue("foo\nbar\rbaz\r\nquux\n\rplop");
641 is(cm.getValue(), "foo\nbar\nbaz\nquux\n\nplop");
642 is(cm.lineCount(), 6);
644 is(cm.lineCount(), 3);
647 testCM("setSize", function(cm) {
648 cm.setSize(100, 100);
649 var wrap = cm.getWrapperElement();
650 is(wrap.offsetWidth, 100);
651 is(wrap.offsetHeight, 100);
652 cm.setSize("100%", "3em");
653 is(wrap.style.width, "100%");
654 is(wrap.style.height, "3em");
655 cm.setSize(null, 40);
656 is(wrap.style.width, "100%");
657 is(wrap.style.height, "40px");
660 function foldLines(cm, start, end, autoClear) {
661 return cm.markText(Pos(start, 0), Pos(end - 1), {
663 inclusiveRight: true,
665 clearOnEnter: autoClear
669 testCM("collapsedLines", function(cm) {
671 var range = foldLines(cm, 4, 5), cleared = 0;
672 CodeMirror.on(range, "clear", function() {cleared++;});
673 cm.setCursor(Pos(3, 0));
674 CodeMirror.commands.goLineDown(cm);
675 eqPos(cm.getCursor(), Pos(5, 0));
676 cm.setLine(3, "abcdefg");
677 cm.setCursor(Pos(3, 6));
678 CodeMirror.commands.goLineDown(cm);
679 eqPos(cm.getCursor(), Pos(5, 4));
681 cm.setCursor(Pos(3, 2));
682 CodeMirror.commands.goLineDown(cm);
683 eqPos(cm.getCursor(), Pos(5, 2));
684 cm.operation(function() {range.clear(); range.clear();});
688 testCM("collapsedRangeCoordsChar", function(cm) {
689 var pos_1_3 = cm.charCoords(Pos(1, 3));
690 pos_1_3.left += 2; pos_1_3.top += 2;
691 var opts = {collapsed: true, inclusiveLeft: true, inclusiveRight: true};
692 var m1 = cm.markText(Pos(0, 0), Pos(2, 0), opts);
693 eqPos(cm.coordsChar(pos_1_3), Pos(3, 3));
695 var m1 = cm.markText(Pos(0, 0), Pos(1, 1), opts);
696 var m2 = cm.markText(Pos(1, 1), Pos(2, 0), opts);
697 eqPos(cm.coordsChar(pos_1_3), Pos(3, 3));
698 m1.clear(); m2.clear();
699 var m1 = cm.markText(Pos(0, 0), Pos(1, 6), opts);
700 eqPos(cm.coordsChar(pos_1_3), Pos(3, 3));
701 }, {value: "123456\nabcdef\nghijkl\nmnopqr\n"});
703 testCM("hiddenLinesAutoUnfold", function(cm) {
704 var range = foldLines(cm, 1, 3, true), cleared = 0;
705 CodeMirror.on(range, "clear", function() {cleared++;});
706 cm.setCursor(Pos(3, 0));
708 cm.execCommand("goCharLeft");
710 range = foldLines(cm, 1, 3, true);
711 CodeMirror.on(range, "clear", function() {cleared++;});
712 eqPos(cm.getCursor(), Pos(3, 0));
713 cm.setCursor(Pos(0, 3));
714 cm.execCommand("goCharRight");
716 }, {value: "abc\ndef\nghi\njkl"});
718 testCM("hiddenLinesSelectAll", function(cm) { // Issue #484
720 foldLines(cm, 0, 10);
721 foldLines(cm, 11, 20);
722 CodeMirror.commands.selectAll(cm);
723 eqPos(cm.getCursor(true), Pos(10, 0));
724 eqPos(cm.getCursor(false), Pos(10, 4));
728 testCM("everythingFolded", function(cm) {
730 function enterPress() {
731 cm.triggerOnKeyDown({type: "keydown", keyCode: 13, preventDefault: function(){}, stopPropagation: function(){}});
733 var fold = foldLines(cm, 0, 2);
735 eq(cm.getValue(), "xx\nxx");
737 fold = foldLines(cm, 0, 2, true);
738 eq(fold.find(), null);
740 eq(cm.getValue(), "\nxx\nxx");
743 testCM("structuredFold", function(cm) {
746 var range = cm.markText(Pos(1, 2), Pos(6, 2), {
747 replacedWith: document.createTextNode("Q")
750 CodeMirror.commands.goLineDown(cm);
751 eqPos(cm.getCursor(), Pos(6, 2));
752 CodeMirror.commands.goCharLeft(cm);
753 eqPos(cm.getCursor(), Pos(1, 2));
754 CodeMirror.commands.delCharAfter(cm);
755 eq(cm.getValue(), "xxxx\nxxxx\nxxxx");
757 range = cm.markText(Pos(1, 2), Pos(6, 2), {
758 replacedWith: document.createTextNode("M"),
762 CodeMirror.on(range, "clear", function(){++cleared;});
764 CodeMirror.commands.goLineDown(cm);
765 eqPos(cm.getCursor(), Pos(6, 2));
766 CodeMirror.commands.goCharLeft(cm);
767 eqPos(cm.getCursor(), Pos(6, 1));
771 range = cm.markText(Pos(1, 2), Pos(6, 2), {
772 replacedWith: document.createTextNode("Q"),
777 CodeMirror.commands.goCharRight(cm);
778 eqPos(cm.getCursor(), Pos(1, 3));
779 range = cm.markText(Pos(2, 0), Pos(4, 4), {
780 replacedWith: document.createTextNode("M")
783 CodeMirror.commands.goLineDown(cm);
784 eqPos(cm.getCursor(), Pos(2, 0));
787 testCM("nestedFold", function(cm) {
789 function fold(ll, cl, lr, cr) {
790 return cm.markText(Pos(ll, cl), Pos(lr, cr), {collapsed: true});
792 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);
794 CodeMirror.commands.goCharRight(cm);
795 eqPos(cm.getCursor(), Pos(2, 3));
797 CodeMirror.commands.goCharLeft(cm);
798 eqPos(cm.getCursor(), Pos(0, 1));
800 CodeMirror.commands.goCharRight(cm);
801 eqPos(cm.getCursor(), Pos(0, 2));
802 CodeMirror.commands.goCharRight(cm);
803 eqPos(cm.getCursor(), Pos(1, 8));
805 CodeMirror.commands.goCharLeft(cm);
806 eqPos(cm.getCursor(), Pos(1, 7));
808 CodeMirror.commands.goCharRight(cm);
809 eqPos(cm.getCursor(), Pos(0, 6));
810 CodeMirror.commands.goCharRight(cm);
811 eqPos(cm.getCursor(), Pos(1, 3));
814 testCM("badNestedFold", function(cm) {
816 cm.markText(Pos(0, 2), Pos(3, 2), {collapsed: true});
818 try {cm.markText(Pos(0, 1), Pos(0, 3), {collapsed: true});}
819 catch(e) {caught = e;}
820 is(caught instanceof Error, "no error");
821 is(/overlap/i.test(caught.message), "wrong error");
824 testCM("wrappingInlineWidget", function(cm) {
826 var w = document.createElement("span");
827 w.style.color = "red";
828 w.innerHTML = "one two three four";
829 cm.markText(Pos(0, 6), Pos(0, 9), {replacedWith: w});
830 var cur0 = cm.cursorCoords(Pos(0, 0)), cur1 = cm.cursorCoords(Pos(0, 10));
831 is(cur0.top < cur1.top);
832 is(cur0.bottom < cur1.bottom);
833 var curL = cm.cursorCoords(Pos(0, 6)), curR = cm.cursorCoords(Pos(0, 9));
834 eq(curL.top, cur0.top);
835 eq(curL.bottom, cur0.bottom);
836 eq(curR.top, cur1.top);
837 eq(curR.bottom, cur1.bottom);
838 cm.replaceRange("", Pos(0, 9), Pos(0));
839 curR = cm.cursorCoords(Pos(0, 9));
840 eq(curR.top, cur1.top);
841 eq(curR.bottom, cur1.bottom);
842 }, {value: "1 2 3 xxx 4", lineWrapping: true});
844 testCM("changedInlineWidget", function(cm) {
846 var w = document.createElement("span");
848 var m = cm.markText(Pos(0, 4), Pos(0, 5), {replacedWith: w});
849 w.innerHTML = "and now the widget is really really long all of a sudden and a scrollbar is needed";
851 var hScroll = byClassName(cm.getWrapperElement(), "CodeMirror-hscrollbar")[0];
852 is(hScroll.scrollWidth > hScroll.clientWidth);
853 }, {value: "hello there"});
855 testCM("inlineWidget", function(cm) {
856 var w = cm.setBookmark(Pos(0, 2), {widget: document.createTextNode("uu")});
858 CodeMirror.commands.goLineDown(cm);
859 eqPos(cm.getCursor(), Pos(1, 4));
861 cm.replaceSelection("hi");
862 eqPos(w.find(), Pos(0, 2));
864 cm.replaceSelection("ay");
865 eqPos(w.find(), Pos(0, 4));
866 eq(cm.getLine(0), "uayuhiuu");
867 }, {value: "uuuu\nuuuuuu"});
869 testCM("wrappingAndResizing", function(cm) {
870 cm.setSize(null, "auto");
871 cm.setOption("lineWrapping", true);
872 var wrap = cm.getWrapperElement(), h0 = wrap.offsetHeight;
873 var doc = "xxx xxx xxx xxx xxx";
875 for (var step = 10, w = cm.charCoords(Pos(0, 18), "div").right;; w += step) {
877 if (wrap.offsetHeight <= h0 * (opera_lt10 ? 1.2 : 1.5)) {
878 if (step == 10) { w -= 10; step = 1; }
882 // Ensure that putting the cursor at the end of the maximally long
883 // line doesn't cause wrapping to happen.
884 cm.setCursor(Pos(0, doc.length));
885 eq(wrap.offsetHeight, h0);
886 cm.replaceSelection("x");
887 is(wrap.offsetHeight > h0, "wrapping happens");
888 // Now add a max-height and, in a document consisting of
889 // almost-wrapped lines, go over it so that a scrollbar appears.
890 cm.setValue(doc + "\n" + doc + "\n");
891 cm.getScrollerElement().style.maxHeight = "100px";
892 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));
893 forEach([Pos(0, doc.length), Pos(0, doc.length - 1),
894 Pos(0, 0), Pos(1, doc.length), Pos(1, doc.length - 1)],
896 var coords = cm.charCoords(pos);
897 eqPos(pos, cm.coordsChar({left: coords.left + 2, top: coords.top + 5}));
901 testCM("measureEndOfLine", function(cm) {
902 cm.setSize(null, "auto");
903 var inner = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild;
904 var lh = inner.offsetHeight;
905 for (var step = 10, w = cm.charCoords(Pos(0, 7), "div").right;; w += step) {
907 if (inner.offsetHeight < 2.5 * lh) {
908 if (step == 10) { w -= 10; step = 1; }
912 cm.setValue(cm.getValue() + "\n\n");
913 var endPos = cm.charCoords(Pos(0, 18), "local");
914 is(endPos.top > lh * .8, "not at top");
915 is(endPos.left > w - 20, "not at right");
916 endPos = cm.charCoords(Pos(0, 18));
917 eqPos(cm.coordsChar({left: endPos.left, top: endPos.top + 5}), Pos(0, 18));
918 }, {mode: "text/html", value: "<!-- foo barrr -->", lineWrapping: true}, ie_lt8 || opera_lt10);
920 testCM("scrollVerticallyAndHorizontally", function(cm) {
921 cm.setSize(100, 100);
924 var wrap = cm.getWrapperElement(), bar = byClassName(wrap, "CodeMirror-vscrollbar")[0];
925 is(bar.offsetHeight < wrap.offsetHeight, "vertical scrollbar limited by horizontal one");
926 var cursorBox = byClassName(wrap, "CodeMirror-cursor")[0].getBoundingClientRect();
927 var editorBox = wrap.getBoundingClientRect();
928 is(cursorBox.bottom < editorBox.top + cm.getScrollerElement().clientHeight,
929 "bottom line visible");
930 }, {lineNumbers: true});
932 testCM("moveVstuck", function(cm) {
933 var lines = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild, h0 = lines.offsetHeight;
934 var val = "fooooooooooooooooooooooooo baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar\n";
936 for (var w = cm.charCoords(Pos(0, 26), "div").right * 2.8;; w += 5) {
938 if (lines.offsetHeight <= 3.5 * h0) break;
940 cm.setCursor(Pos(0, val.length - 1));
941 cm.moveV(-1, "line");
942 eqPos(cm.getCursor(), Pos(0, 26));
943 }, {lineWrapping: true}, ie_lt8 || opera_lt10);
945 testCM("clickTab", function(cm) {
946 var p0 = cm.charCoords(Pos(0, 0));
947 eqPos(cm.coordsChar({left: p0.left + 5, top: p0.top + 5}), Pos(0, 0));
948 eqPos(cm.coordsChar({left: p0.right - 5, top: p0.top + 5}), Pos(0, 1));
949 }, {value: "\t\n\n", lineWrapping: true, tabSize: 8});
951 testCM("verticalScroll", function(cm) {
952 cm.setSize(100, 200);
953 cm.setValue("foo\nbar\nbaz\n");
954 var sc = cm.getScrollerElement(), baseWidth = sc.scrollWidth;
955 cm.setLine(0, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah");
956 is(sc.scrollWidth > baseWidth, "scrollbar present");
957 cm.setLine(0, "foo");
958 if (!phantom) eq(sc.scrollWidth, baseWidth, "scrollbar gone");
959 cm.setLine(0, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah");
960 cm.setLine(1, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbh");
961 is(sc.scrollWidth > baseWidth, "present again");
962 var curWidth = sc.scrollWidth;
963 cm.setLine(0, "foo");
964 is(sc.scrollWidth < curWidth, "scrollbar smaller");
965 is(sc.scrollWidth > baseWidth, "but still present");
968 testCM("extraKeys", function(cm) {
970 function fakeKey(expected, code, props) {
971 if (typeof code == "string") code = code.charCodeAt(0);
972 var e = {type: "keydown", keyCode: code, preventDefault: function(){}, stopPropagation: function(){}};
973 if (props) for (var n in props) e[n] = props[n];
975 cm.triggerOnKeyDown(e);
976 eq(outcome, expected);
978 CodeMirror.commands.testCommand = function() {outcome = "tc";};
979 CodeMirror.commands.goTestCommand = function() {outcome = "gtc";};
980 cm.setOption("extraKeys", {"Shift-X": function() {outcome = "sx";},
981 "X": function() {outcome = "x";},
982 "Ctrl-Alt-U": function() {outcome = "cau";},
983 "End": "testCommand",
984 "Home": "goTestCommand",
987 fakeKey("cau", "U", {ctrlKey: true, altKey: true});
988 fakeKey(null, "U", {shiftKey: true, ctrlKey: true, altKey: true});
990 fakeKey("sx", "X", {shiftKey: true});
992 fakeKey(null, 35, {shiftKey: true});
994 fakeKey("gtc", 36, {shiftKey: true});
996 }, null, window.opera && mac);
998 testCM("wordMovementCommands", function(cm) {
999 cm.execCommand("goWordLeft");
1000 eqPos(cm.getCursor(), Pos(0, 0));
1001 cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
1002 eqPos(cm.getCursor(), Pos(0, 7));
1003 cm.execCommand("goWordLeft");
1004 eqPos(cm.getCursor(), Pos(0, 5));
1005 cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
1006 eqPos(cm.getCursor(), Pos(0, 12));
1007 cm.execCommand("goWordLeft");
1008 eqPos(cm.getCursor(), Pos(0, 9));
1009 cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
1010 eqPos(cm.getCursor(), Pos(0, 24));
1011 cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
1012 eqPos(cm.getCursor(), Pos(1, 9));
1013 cm.execCommand("goWordRight");
1014 eqPos(cm.getCursor(), Pos(1, 13));
1015 cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
1016 eqPos(cm.getCursor(), Pos(2, 0));
1017 }, {value: "this is (the) firstline.\na foo12\u00e9\u00f8\u00d7bar\n"});
1019 testCM("groupMovementCommands", function(cm) {
1020 cm.execCommand("goGroupLeft");
1021 eqPos(cm.getCursor(), Pos(0, 0));
1022 cm.execCommand("goGroupRight");
1023 eqPos(cm.getCursor(), Pos(0, 4));
1024 cm.execCommand("goGroupRight");
1025 eqPos(cm.getCursor(), Pos(0, 7));
1026 cm.execCommand("goGroupRight");
1027 eqPos(cm.getCursor(), Pos(0, 10));
1028 cm.execCommand("goGroupLeft");
1029 eqPos(cm.getCursor(), Pos(0, 7));
1030 cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight");
1031 eqPos(cm.getCursor(), Pos(0, 15));
1032 cm.setCursor(Pos(0, 17));
1033 cm.execCommand("goGroupLeft");
1034 eqPos(cm.getCursor(), Pos(0, 16));
1035 cm.execCommand("goGroupLeft");
1036 eqPos(cm.getCursor(), Pos(0, 14));
1037 cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight");
1038 eqPos(cm.getCursor(), Pos(0, 20));
1039 cm.execCommand("goGroupRight");
1040 eqPos(cm.getCursor(), Pos(1, 5));
1041 cm.execCommand("goGroupLeft"); cm.execCommand("goGroupLeft");
1042 eqPos(cm.getCursor(), Pos(1, 0));
1043 cm.execCommand("goGroupLeft");
1044 eqPos(cm.getCursor(), Pos(0, 16));
1045 }, {value: "booo ba---quux. ffff\n abc d"});
1047 testCM("charMovementCommands", function(cm) {
1048 cm.execCommand("goCharLeft"); cm.execCommand("goColumnLeft");
1049 eqPos(cm.getCursor(), Pos(0, 0));
1050 cm.execCommand("goCharRight"); cm.execCommand("goCharRight");
1051 eqPos(cm.getCursor(), Pos(0, 2));
1052 cm.setCursor(Pos(1, 0));
1053 cm.execCommand("goColumnLeft");
1054 eqPos(cm.getCursor(), Pos(1, 0));
1055 cm.execCommand("goCharLeft");
1056 eqPos(cm.getCursor(), Pos(0, 5));
1057 cm.execCommand("goColumnRight");
1058 eqPos(cm.getCursor(), Pos(0, 5));
1059 cm.execCommand("goCharRight");
1060 eqPos(cm.getCursor(), Pos(1, 0));
1061 cm.execCommand("goLineEnd");
1062 eqPos(cm.getCursor(), Pos(1, 5));
1063 cm.execCommand("goLineStartSmart");
1064 eqPos(cm.getCursor(), Pos(1, 1));
1065 cm.execCommand("goLineStartSmart");
1066 eqPos(cm.getCursor(), Pos(1, 0));
1067 cm.setCursor(Pos(2, 0));
1068 cm.execCommand("goCharRight"); cm.execCommand("goColumnRight");
1069 eqPos(cm.getCursor(), Pos(2, 0));
1070 }, {value: "line1\n ine2\n"});
1072 testCM("verticalMovementCommands", function(cm) {
1073 cm.execCommand("goLineUp");
1074 eqPos(cm.getCursor(), Pos(0, 0));
1075 cm.execCommand("goLineDown");
1076 if (!phantom) // This fails in PhantomJS, though not in a real Webkit
1077 eqPos(cm.getCursor(), Pos(1, 0));
1078 cm.setCursor(Pos(1, 12));
1079 cm.execCommand("goLineDown");
1080 eqPos(cm.getCursor(), Pos(2, 5));
1081 cm.execCommand("goLineDown");
1082 eqPos(cm.getCursor(), Pos(3, 0));
1083 cm.execCommand("goLineUp");
1084 eqPos(cm.getCursor(), Pos(2, 5));
1085 cm.execCommand("goLineUp");
1086 eqPos(cm.getCursor(), Pos(1, 12));
1087 cm.execCommand("goPageDown");
1088 eqPos(cm.getCursor(), Pos(5, 0));
1089 cm.execCommand("goPageDown"); cm.execCommand("goLineDown");
1090 eqPos(cm.getCursor(), Pos(5, 0));
1091 cm.execCommand("goPageUp");
1092 eqPos(cm.getCursor(), Pos(0, 0));
1093 }, {value: "line1\nlong long line2\nline3\n\nline5\n"});
1095 testCM("verticalMovementCommandsWrapping", function(cm) {
1097 cm.setCursor(Pos(0, 5));
1098 cm.execCommand("goLineDown");
1099 eq(cm.getCursor().line, 0);
1100 is(cm.getCursor().ch > 5, "moved beyond wrap");
1101 for (var i = 0; ; ++i) {
1102 is(i < 20, "no endless loop");
1103 cm.execCommand("goLineDown");
1104 var cur = cm.getCursor();
1105 if (cur.line == 1) eq(cur.ch, 5);
1106 if (cur.line == 2) { eq(cur.ch, 1); break; }
1108 }, {value: "a very long line that wraps around somehow so that we can test cursor movement\nshortone\nk",
1109 lineWrapping: true});
1111 testCM("rtlMovement", function(cm) {
1112 forEach(["خحج", "خحabcخحج", "abخحخحجcd", "abخde", "abخح2342خ1حج", "خ1ح2خح3حxج",
1113 "خحcd", "1خحcd", "abcdeح1ج", "خمرحبها مها!", "foobarر",
1114 "<img src=\"/בדיקה3.jpg\">"], function(line) {
1115 var inv = line.charAt(0) == "خ";
1116 cm.setValue(line + "\n"); cm.execCommand(inv ? "goLineEnd" : "goLineStart");
1117 var cursor = byClassName(cm.getWrapperElement(), "CodeMirror-cursor")[0];
1118 var prevX = cursor.offsetLeft, prevY = cursor.offsetTop;
1119 for (var i = 0; i <= line.length; ++i) {
1120 cm.execCommand("goCharRight");
1121 if (i == line.length) is(cursor.offsetTop > prevY, "next line");
1122 else is(cursor.offsetLeft > prevX, "moved right");
1123 prevX = cursor.offsetLeft; prevY = cursor.offsetTop;
1125 cm.setCursor(0, 0); cm.execCommand(inv ? "goLineStart" : "goLineEnd");
1126 prevX = cursor.offsetLeft;
1127 for (var i = 0; i < line.length; ++i) {
1128 cm.execCommand("goCharLeft");
1129 is(cursor.offsetLeft < prevX, "moved left");
1130 prevX = cursor.offsetLeft;
1133 }, {rtlMoveVisually: true});
1135 // Verify that updating a line clears its bidi ordering
1136 testCM("bidiUpdate", function(cm) {
1137 cm.setCursor(Pos(0, 2));
1138 cm.replaceSelection("خحج", "start");
1139 cm.execCommand("goCharRight");
1140 eqPos(cm.getCursor(), Pos(0, 4));
1141 }, {value: "abcd\n"});
1143 testCM("movebyTextUnit", function(cm) {
1144 cm.setValue("בְּרֵאשִ\ńéée\n");
1145 cm.execCommand("goLineEnd");
1146 for (var i = 0; i < 4; ++i) cm.execCommand("goCharRight");
1147 eqPos(cm.getCursor(), Pos(0, 0));
1148 cm.execCommand("goCharRight");
1149 eqPos(cm.getCursor(), Pos(1, 0));
1150 cm.execCommand("goCharRight");
1151 cm.execCommand("goCharRight");
1152 eqPos(cm.getCursor(), Pos(1, 3));
1153 cm.execCommand("goCharRight");
1154 cm.execCommand("goCharRight");
1155 eqPos(cm.getCursor(), Pos(1, 6));
1158 testCM("lineChangeEvents", function(cm) {
1160 var log = [], want = ["ch 0", "ch 1", "del 2", "ch 0", "ch 0", "del 1", "del 3", "del 4"];
1161 for (var i = 0; i < 5; ++i) {
1162 CodeMirror.on(cm.getLineHandle(i), "delete", function(i) {
1163 return function() {log.push("del " + i);};
1165 CodeMirror.on(cm.getLineHandle(i), "change", function(i) {
1166 return function() {log.push("ch " + i);};
1169 cm.replaceRange("x", Pos(0, 1));
1170 cm.replaceRange("xy", Pos(1, 1), Pos(2));
1171 cm.replaceRange("foo\nbar", Pos(0, 1));
1172 cm.replaceRange("", Pos(0, 0), Pos(cm.lineCount()));
1173 eq(log.length, want.length, "same length");
1174 for (var i = 0; i < log.length; ++i)
1175 eq(log[i], want[i]);
1178 testCM("scrollEntirelyToRight", function(cm) {
1179 if (phantom) return;
1181 cm.setCursor(Pos(0, 500));
1182 var wrap = cm.getWrapperElement(), cur = byClassName(wrap, "CodeMirror-cursor")[0];
1183 is(wrap.getBoundingClientRect().right > cur.getBoundingClientRect().left);
1186 testCM("lineWidgets", function(cm) {
1188 var last = cm.charCoords(Pos(2, 0));
1189 var node = document.createElement("div");
1190 node.innerHTML = "hi";
1191 var widget = cm.addLineWidget(1, node);
1192 is(last.top < cm.charCoords(Pos(2, 0)).top, "took up space");
1193 cm.setCursor(Pos(1, 1));
1194 cm.execCommand("goLineDown");
1195 eqPos(cm.getCursor(), Pos(2, 1));
1196 cm.execCommand("goLineUp");
1197 eqPos(cm.getCursor(), Pos(1, 1));
1200 testCM("lineWidgetFocus", function(cm) {
1201 var place = document.getElementById("testground");
1202 place.className = "offscreen";
1204 addDoc(cm, 500, 10);
1205 var node = document.createElement("input");
1206 var widget = cm.addLineWidget(1, node);
1208 eq(document.activeElement, node);
1209 cm.replaceRange("new stuff", Pos(1, 0));
1210 eq(document.activeElement, node);
1212 place.className = "";
1216 testCM("getLineNumber", function(cm) {
1218 var h1 = cm.getLineHandle(1);
1219 eq(cm.getLineNumber(h1), 1);
1220 cm.replaceRange("hi\nbye\n", Pos(0, 0));
1221 eq(cm.getLineNumber(h1), 3);
1223 eq(cm.getLineNumber(h1), null);
1226 testCM("jumpTheGap", function(cm) {
1227 var longLine = "abcdef ghiklmnop qrstuvw xyz ";
1228 longLine += longLine; longLine += longLine; longLine += longLine;
1229 cm.setLine(2, longLine);
1230 cm.setSize("200px", null);
1231 cm.getWrapperElement().style.lineHeight = 2;
1233 cm.setCursor(Pos(0, 1));
1234 cm.execCommand("goLineDown");
1235 eqPos(cm.getCursor(), Pos(1, 1));
1236 cm.execCommand("goLineDown");
1237 eqPos(cm.getCursor(), Pos(2, 1));
1238 cm.execCommand("goLineDown");
1239 eq(cm.getCursor().line, 2);
1240 is(cm.getCursor().ch > 1);
1241 cm.execCommand("goLineUp");
1242 eqPos(cm.getCursor(), Pos(2, 1));
1243 cm.execCommand("goLineUp");
1244 eqPos(cm.getCursor(), Pos(1, 1));
1245 var node = document.createElement("div");
1246 node.innerHTML = "hi"; node.style.height = "30px";
1247 cm.addLineWidget(0, node);
1248 cm.addLineWidget(1, node.cloneNode(true), {above: true});
1249 cm.setCursor(Pos(0, 2));
1250 cm.execCommand("goLineDown");
1251 eqPos(cm.getCursor(), Pos(1, 2));
1252 cm.execCommand("goLineUp");
1253 eqPos(cm.getCursor(), Pos(0, 2));
1254 }, {lineWrapping: true, value: "abc\ndef\nghi\njkl\n"});
1256 testCM("addLineClass", function(cm) {
1257 function cls(line, text, bg, wrap) {
1258 var i = cm.lineInfo(line);
1259 eq(i.textClass, text);
1261 eq(i.wrapClass, wrap);
1263 cm.addLineClass(0, "text", "foo");
1264 cm.addLineClass(0, "text", "bar");
1265 cm.addLineClass(1, "background", "baz");
1266 cm.addLineClass(1, "wrap", "foo");
1267 cls(0, "foo bar", null, null);
1268 cls(1, null, "baz", "foo");
1269 var lines = cm.display.lineDiv;
1270 eq(byClassName(lines, "foo").length, 2);
1271 eq(byClassName(lines, "bar").length, 1);
1272 eq(byClassName(lines, "baz").length, 1);
1273 cm.removeLineClass(0, "text", "foo");
1274 cls(0, "bar", null, null);
1275 cm.removeLineClass(0, "text", "foo");
1276 cls(0, "bar", null, null);
1277 cm.removeLineClass(0, "text", "bar");
1278 cls(0, null, null, null);
1279 cm.addLineClass(1, "wrap", "quux");
1280 cls(1, null, "baz", "foo quux");
1281 cm.removeLineClass(1, "wrap");
1282 cls(1, null, "baz", null);
1283 }, {value: "hohoho\n"});
1285 testCM("atomicMarker", function(cm) {
1287 function atom(ll, cl, lr, cr, li, ri) {
1288 return cm.markText(Pos(ll, cl), Pos(lr, cr),
1289 {atomic: true, inclusiveLeft: li, inclusiveRight: ri});
1291 var m = atom(0, 1, 0, 5);
1292 cm.setCursor(Pos(0, 1));
1293 cm.execCommand("goCharRight");
1294 eqPos(cm.getCursor(), Pos(0, 5));
1295 cm.execCommand("goCharLeft");
1296 eqPos(cm.getCursor(), Pos(0, 1));
1298 m = atom(0, 0, 0, 5, true);
1299 eqPos(cm.getCursor(), Pos(0, 5), "pushed out");
1300 cm.execCommand("goCharLeft");
1301 eqPos(cm.getCursor(), Pos(0, 5));
1303 m = atom(8, 4, 9, 10, false, true);
1304 cm.setCursor(Pos(9, 8));
1305 eqPos(cm.getCursor(), Pos(8, 4), "set");
1306 cm.execCommand("goCharRight");
1307 eqPos(cm.getCursor(), Pos(8, 4), "char right");
1308 cm.execCommand("goLineDown");
1309 eqPos(cm.getCursor(), Pos(8, 4), "line down");
1310 cm.execCommand("goCharLeft");
1311 eqPos(cm.getCursor(), Pos(8, 3));
1313 m = atom(1, 1, 3, 8);
1314 cm.setCursor(Pos(2, 0));
1315 eqPos(cm.getCursor(), Pos(3, 8));
1316 cm.execCommand("goCharLeft");
1317 eqPos(cm.getCursor(), Pos(1, 1));
1318 cm.execCommand("goCharRight");
1319 eqPos(cm.getCursor(), Pos(3, 8));
1320 cm.execCommand("goLineUp");
1321 eqPos(cm.getCursor(), Pos(1, 1));
1322 cm.execCommand("goLineDown");
1323 eqPos(cm.getCursor(), Pos(3, 8));
1324 cm.execCommand("delCharBefore");
1325 eq(cm.getValue().length, 80, "del chunk");
1326 m = atom(3, 0, 5, 5);
1327 cm.setCursor(Pos(3, 0));
1328 cm.execCommand("delWordAfter");
1329 eq(cm.getValue().length, 53, "del chunk");
1332 testCM("readOnlyMarker", function(cm) {
1333 function mark(ll, cl, lr, cr, at) {
1334 return cm.markText(Pos(ll, cl), Pos(lr, cr),
1335 {readOnly: true, atomic: at});
1337 var m = mark(0, 1, 0, 4);
1338 cm.setCursor(Pos(0, 2));
1339 cm.replaceSelection("hi", "end");
1340 eqPos(cm.getCursor(), Pos(0, 2));
1341 eq(cm.getLine(0), "abcde");
1342 cm.execCommand("selectAll");
1343 cm.replaceSelection("oops");
1344 eq(cm.getValue(), "oopsbcd");
1346 eqPos(m.find().from, Pos(0, 1));
1347 eqPos(m.find().to, Pos(0, 4));
1349 cm.setCursor(Pos(0, 2));
1350 cm.replaceSelection("hi");
1351 eq(cm.getLine(0), "abhicde");
1352 eqPos(cm.getCursor(), Pos(0, 4));
1353 m = mark(0, 2, 2, 2, true);
1354 cm.setSelection(Pos(1, 1), Pos(2, 4));
1355 cm.replaceSelection("t", "end");
1356 eqPos(cm.getCursor(), Pos(2, 3));
1357 eq(cm.getLine(2), "klto");
1358 cm.execCommand("goCharLeft");
1359 cm.execCommand("goCharLeft");
1360 eqPos(cm.getCursor(), Pos(0, 2));
1361 cm.setSelection(Pos(0, 1), Pos(0, 3));
1362 cm.replaceSelection("xx");
1363 eqPos(cm.getCursor(), Pos(0, 3));
1364 eq(cm.getLine(0), "axxhicde");
1365 }, {value: "abcde\nfghij\nklmno\n"});
1367 testCM("dirtyBit", function(cm) {
1368 eq(cm.isClean(), true);
1369 cm.replaceSelection("boo");
1370 eq(cm.isClean(), false);
1372 eq(cm.isClean(), true);
1373 cm.replaceSelection("boo");
1374 cm.replaceSelection("baz");
1376 eq(cm.isClean(), false);
1378 eq(cm.isClean(), true);
1380 eq(cm.isClean(), false);
1382 eq(cm.isClean(), true);
1385 testCM("addKeyMap", function(cm) {
1386 function sendKey(code) {
1387 cm.triggerOnKeyDown({type: "keydown", keyCode: code,
1388 preventDefault: function(){}, stopPropagation: function(){}});
1392 eqPos(cm.getCursor(), Pos(0, 1));
1394 var map1 = {Right: function() { ++test; }}, map2 = {Right: function() { test += 10; }}
1397 eqPos(cm.getCursor(), Pos(0, 1));
1399 cm.addKeyMap(map2, true);
1402 cm.removeKeyMap(map1);
1405 cm.removeKeyMap(map2);
1408 eqPos(cm.getCursor(), Pos(0, 2));
1409 cm.addKeyMap({Right: function() { test = 55; }, name: "mymap"});
1412 cm.removeKeyMap("mymap");
1414 eqPos(cm.getCursor(), Pos(0, 3));
1417 testCM("findPosH", function(cm) {
1418 forEach([{from: Pos(0, 0), to: Pos(0, 1), by: 1},
1419 {from: Pos(0, 0), to: Pos(0, 0), by: -1, hitSide: true},
1420 {from: Pos(0, 0), to: Pos(0, 4), by: 1, unit: "word"},
1421 {from: Pos(0, 0), to: Pos(0, 8), by: 2, unit: "word"},
1422 {from: Pos(0, 0), to: Pos(2, 0), by: 20, unit: "word", hitSide: true},
1423 {from: Pos(0, 7), to: Pos(0, 5), by: -1, unit: "word"},
1424 {from: Pos(0, 4), to: Pos(0, 8), by: 1, unit: "word"},
1425 {from: Pos(1, 0), to: Pos(1, 18), by: 3, unit: "word"},
1426 {from: Pos(1, 22), to: Pos(1, 5), by: -3, unit: "word"},
1427 {from: Pos(1, 15), to: Pos(1, 10), by: -5},
1428 {from: Pos(1, 15), to: Pos(1, 10), by: -5, unit: "column"},
1429 {from: Pos(1, 15), to: Pos(1, 0), by: -50, unit: "column", hitSide: true},
1430 {from: Pos(1, 15), to: Pos(1, 24), by: 50, unit: "column", hitSide: true},
1431 {from: Pos(1, 15), to: Pos(2, 0), by: 50, hitSide: true}], function(t) {
1432 var r = cm.findPosH(t.from, t.by, t.unit || "char");
1434 eq(!!r.hitSide, !!t.hitSide);
1436 }, {value: "line one\nline two.something.other\n"});
1438 testCM("beforeChange", function(cm) {
1439 cm.on("beforeChange", function(cm, change) {
1441 for (var i = 0; i < change.text.length; ++i)
1442 text.push(change.text[i].replace(/\s/g, "_"));
1443 change.update(null, null, text);
1445 cm.setValue("hello, i am a\nnew document\n");
1446 eq(cm.getValue(), "hello,_i_am_a\nnew_document\n");
1447 CodeMirror.on(cm.getDoc(), "beforeChange", function(doc, change) {
1448 if (change.from.line == 0) change.cancel();
1450 cm.setValue("oops"); // Canceled
1451 eq(cm.getValue(), "hello,_i_am_a\nnew_document\n");
1452 cm.replaceRange("hey hey hey", Pos(1, 0), Pos(2, 0));
1453 eq(cm.getValue(), "hello,_i_am_a\nhey_hey_hey");
1454 }, {value: "abcdefghijk"});
1456 testCM("beforeChangeUndo", function(cm) {
1457 cm.setLine(0, "hi");
1458 cm.setLine(0, "bye");
1459 eq(cm.historySize().undo, 2);
1460 cm.on("beforeChange", function(cm, change) {
1465 eq(cm.historySize().undo, 0);
1466 eq(cm.getValue(), "bye\ntwo");
1467 }, {value: "one\ntwo"});
1469 testCM("beforeSelectionChange", function(cm) {
1470 function notAtEnd(cm, pos) {
1471 var len = cm.getLine(pos.line).length;
1472 if (!len || pos.ch == len) return Pos(pos.line, pos.ch - 1);
1475 cm.on("beforeSelectionChange", function(cm, sel) {
1476 sel.head = notAtEnd(cm, sel.head);
1477 sel.anchor = notAtEnd(cm, sel.anchor);
1481 cm.execCommand("goLineEnd");
1482 eqPos(cm.getCursor(), Pos(0, 9));
1483 cm.execCommand("selectAll");
1484 eqPos(cm.getCursor("start"), Pos(0, 0));
1485 eqPos(cm.getCursor("end"), Pos(9, 9));
1488 testCM("change_removedText", function(cm) {
1489 cm.setValue("abc\ndef");
1492 cm.on("change", function(cm, change) {
1493 removedText = [change.removed, change.next && change.next.removed];
1496 cm.operation(function() {
1497 cm.replaceRange("xyz", Pos(0, 0), Pos(1,1));
1498 cm.replaceRange("123", Pos(0,0));
1501 eq(removedText[0].join("\n"), "abc\nd");
1502 eq(removedText[1].join("\n"), "");
1505 eq(removedText[0].join("\n"), "123");
1506 eq(removedText[1].join("\n"), "xyz");
1509 eq(removedText[0].join("\n"), "abc\nd");
1510 eq(removedText[1].join("\n"), "");