move a few things away in to-be-integrated/
[myslice.git] / to-be-integrated / third-party / codemirror-3.15 / test / test.js
1 var Pos = CodeMirror.Pos;
2
3 function forEach(arr, f) {
4   for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
5 }
6
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"));
12 }
13
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]);
22   }
23   search(elt);
24   return found;
25 }
26
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);
34
35 namespace = "core_";
36
37 test("core_fromTextArea", function() {
38   var te = document.getElementById("code");
39   te.value = "CONTENT";
40   var cm = CodeMirror.fromTextArea(te);
41   is(!te.offsetHeight);
42   eq(cm.getValue(), "CONTENT");
43   cm.setValue("foo\nbar");
44   eq(cm.getValue(), "foo\nbar");
45   cm.save();
46   is(/^foo\r?\nbar$/.test(te.value));
47   cm.setValue("xxx");
48   cm.toTextArea();
49   is(te.offsetHeight);
50   eq(te.value, "xxx");
51 });
52
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"});
63
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);
76 });
77
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));
96   cm.setCursor(1, 2);
97   eqPos(cm.getCursor(true), Pos(1, 2));
98 }, {value: "111111\n222222\n333333"});
99
100 testCM("extendSelection", function(cm) {
101   cm.setExtending(true);
102   addDoc(cm, 10, 10);
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));
135 });
136
137 testCM("lines", function(cm) {
138   eq(cm.getLine(0), "111111");
139   eq(cm.getLine(1), "222222");
140   eq(cm.getLine(-1), null);
141   cm.removeLine(1);
142   cm.setLine(1, "abc");
143   eq(cm.getValue(), "111111\nabc");
144 }, {value: "111111\n222222\n333333"});
145
146 testCM("indent", function(cm) {
147   cm.indentLine(1);
148   eq(cm.getLine(1), "   blah();");
149   cm.setOption("indentUnit", 8);
150   cm.indentLine(1);
151   eq(cm.getLine(1), "\tblah();");
152   cm.setOption("indentUnit", 10);
153   cm.setOption("tabSize", 4);
154   cm.indentLine(1);
155   eq(cm.getLine(1), "\t\t  blah();");
156 }, {value: "if (x) {\nblah();\n}", indentUnit: 3, indentWithTabs: true, tabSize: 8});
157
158 testCM("indentByNumber", function(cm) {
159   cm.indentLine(0, 2);
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"});
167
168 test("core_defaults", function() {
169   var defsCopy = {}, defs = CodeMirror.defaults;
170   for (var opt in defs) defsCopy[opt] = defs[opt];
171   defs.indentUnit = 5;
172   defs.value = "uu";
173   defs.enterMode = "keep";
174   defs.tabindex = 55;
175   var place = document.getElementById("testground"), cm = CodeMirror(place);
176   try {
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);
183   }
184   finally {
185     for (var opt in defsCopy) defs[opt] = defsCopy[opt];
186     place.removeChild(cm.getWrapperElement());
187   }
188 });
189
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);
197   eq(info.line, 1);
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"});
207
208 testCM("coords", function(cm) {
209   cm.setSize(null, 100);
210   addDoc(cm, 32, 200);
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);
220 });
221
222 testCM("coordsChar", function(cm) {
223   addDoc(cm, 35, 70);
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));
232       }
233     }
234   }
235 }, {lineNumbers: true});
236
237 testCM("posFromIndex", function(cm) {
238   cm.setValue(
239     "This function should\n" +
240     "convert a zero based index\n" +
241     "to line and ch."
242   );
243
244   var examples = [
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
252   ];
253
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);
261   }
262 });
263
264 testCM("undo", function(cm) {
265   cm.setLine(0, "def");
266   eq(cm.historySize().undo, 1);
267   cm.undo();
268   eq(cm.getValue(), "abc");
269   eq(cm.historySize().undo, 0);
270   eq(cm.historySize().redo, 1);
271   cm.redo();
272   eq(cm.getValue(), "def");
273   eq(cm.historySize().undo, 1);
274   eq(cm.historySize().redo, 0);
275   cm.setValue("1\n\n\n2");
276   cm.clearHistory();
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));
281   }
282   eq(cm.historySize().undo, 40);
283   for (var i = 0; i < 40; ++i)
284     cm.undo();
285   eq(cm.historySize().redo, 40);
286   eq(cm.getValue(), "1\n\n\n2");
287 }, {value: "abc"});
288
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});
296
297 testCM("undoDoesntClearValue", function(cm) {
298   cm.undo();
299   eq(cm.getValue(), "x");
300 }, {value: "x"});
301
302 testCM("undoMultiLine", function(cm) {
303   cm.operation(function() {
304     cm.replaceRange("x", Pos(0, 0));
305     cm.replaceRange("y", Pos(1, 0));
306   });
307   cm.undo();
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));
312   });
313   cm.undo();
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));
319   });
320   cm.undo();
321   eq(cm.getValue(), "abc\ndef\nghi", 3);
322 }, {value: "abc\ndef\nghi"});
323
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));
329   });
330   eq(cm.getValue(), "ax\nby\ncz\n");
331   cm.undo();
332   eq(cm.getValue(), "a\nby\nc\n");
333   cm.undo();
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"});
338
339 testCM("undoSelection", function(cm) {
340   cm.setSelection(Pos(0, 2), Pos(0, 4));
341   cm.replaceSelection("");
342   cm.setCursor(Pos(1, 0));
343   cm.undo();
344   eqPos(cm.getCursor(true), Pos(0, 2));
345   eqPos(cm.getCursor(false), Pos(0, 4));
346   cm.setCursor(Pos(1, 0));
347   cm.redo();
348   eqPos(cm.getCursor(true), Pos(0, 2));
349   eqPos(cm.getCursor(false), Pos(0, 2));
350 }, {value: "abcdefgh\n"});
351
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));
370     var f = r.find();
371     eq(f && f.from.ch, test.f); eq(f && f.to.ch, test.t);
372   });
373 });
374
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));
396     var f = r.find();
397     eqPos(f && f.from, p(test.f)); eqPos(f && f.to, p(test.t));
398   });
399 });
400
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));
411   });
412   var v1 = cm.getValue();
413   cm.setValue("");
414   eq(marker1.find(), null); eq(marker2.find(), null); eq(bookmark.find(), null);
415   cm.undo();
416   eqPos(bookmark.find(), Pos(1, 5), "still there");
417   cm.undo();
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);
424   cm.undo();
425   eqPos(bookmark.find(), Pos(1, 5));
426   eq(cm.getValue(), v1);
427 }, {value: "1234\n56789\n00\n"});
428
429 testCM("markTextStayGone", function(cm) {
430   var m1 = cm.markText(Pos(0, 0), Pos(0, 1));
431   cm.replaceRange("hi", Pos(0, 2));
432   m1.clear();
433   cm.undo();
434   eq(m1.find(), null);
435 }, {value: "hello"});
436
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));
444   cm.undo();
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));
452   eq(found.length, 1);
453   eq(found[0], mAround);
454 }, {value: "aaaa\nbbbb\ncccc\ndddd"});
455
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);
461 });
462
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"});
469
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));
484   });
485 });
486
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"});
501
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"});
519
520 testCM("getAllMarks", function(cm) {
521   addDoc(cm, 10, 10);
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);
527   m1.clear();
528   m3.clear();
529   eq(cm.getAllMarks().length, 2);
530 });
531
532 testCM("bug577", function(cm) {
533   cm.setValue("a\nb");
534   cm.clearHistory();
535   cm.setValue("fooooo");
536   cm.undo();
537 });
538
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");
552 });
553
554 testCM("scrollIntoView", function(cm) {
555   if (phantom) return;
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);
563   }
564   addDoc(cm, 200, 200);
565   test(199, 199);
566   test(0, 0);
567   test(100, 100);
568   test(199, 0);
569   test(0, 199);
570   test(100, 100);
571 });
572
573 testCM("selectionPos", function(cm) {
574   if (phantom) return;
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;
580   cm.scrollTo(0, 0);
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) {
590       sawMiddle = true;
591       is(box.bottom - box.top > 90 * lineHeight, "middle high");
592       is(width > .9 * lineWidth, "middle wide");
593     } else {
594       is(width > .4 * lineWidth, "top/bot wide enough");
595       is(width < .6 * lineWidth, "top/bot slim enough");
596       if (atLeft) {
597         sawBottom = true;
598         is(box.top - outer.top > 96 * lineHeight, "bot below");
599       } else if (atRight) {
600         sawTop = true;
601         is(box.top - outer.top < 2.1 * lineHeight, "top above");
602       }
603     }
604   }
605   is(sawTop && sawBottom && sawMiddle, "all parts");
606 }, null);
607
608 testCM("restoreHistory", function(cm) {
609   cm.setValue("abc\ndef");
610   cm.setLine(1, "hello");
611   cm.setLine(0, "goop");
612   cm.undo();
613   var storedVal = cm.getValue(), storedHist = cm.getHistory();
614   if (window.JSON) storedHist = JSON.parse(JSON.stringify(storedHist));
615   eq(storedVal, "abc\nhello");
616   cm.setValue("");
617   cm.clearHistory();
618   eq(cm.historySize().undo, 0);
619   cm.setValue(storedVal);
620   cm.setHistory(storedHist);
621   cm.redo();
622   eq(cm.getValue(), "goop\nhello");
623   cm.undo(); cm.undo();
624   eq(cm.getValue(), "abc\ndef");
625 });
626
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);
634   addDoc(cm, 1, 300);
635   var wrap = cm.getWrapperElement();
636   is(wrap.offsetWidth - byClassName(wrap, "CodeMirror-lines")[0].offsetWidth <= scrollbarWidth * 1.5);
637 });
638
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);
643   cm.setValue("\n\n");
644   is(cm.lineCount(), 3);
645 });
646
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");
658 });
659
660 function foldLines(cm, start, end, autoClear) {
661   return cm.markText(Pos(start, 0), Pos(end - 1), {
662     inclusiveLeft: true,
663     inclusiveRight: true,
664     collapsed: true,
665     clearOnEnter: autoClear
666   });
667 }
668
669 testCM("collapsedLines", function(cm) {
670   addDoc(cm, 4, 10);
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));
680   cm.setLine(3, "ab");
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();});
685   eq(cleared, 1);
686 });
687
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));
694   m1.clear();
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"});
702
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));
707   eq(cleared, 0);
708   cm.execCommand("goCharLeft");
709   eq(cleared, 1);
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");
715   eq(cleared, 2);
716 }, {value: "abc\ndef\nghi\njkl"});
717
718 testCM("hiddenLinesSelectAll", function(cm) {  // Issue #484
719   addDoc(cm, 4, 20);
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));
725 });
726
727
728 testCM("everythingFolded", function(cm) {
729   addDoc(cm, 2, 2);
730   function enterPress() {
731     cm.triggerOnKeyDown({type: "keydown", keyCode: 13, preventDefault: function(){}, stopPropagation: function(){}});
732   }
733   var fold = foldLines(cm, 0, 2);
734   enterPress();
735   eq(cm.getValue(), "xx\nxx");
736   fold.clear();
737   fold = foldLines(cm, 0, 2, true);
738   eq(fold.find(), null);
739   enterPress();
740   eq(cm.getValue(), "\nxx\nxx");
741 });
742
743 testCM("structuredFold", function(cm) {
744   if (phantom) return;
745   addDoc(cm, 4, 8);
746   var range = cm.markText(Pos(1, 2), Pos(6, 2), {
747     replacedWith: document.createTextNode("Q")
748   });
749   cm.setCursor(0, 3);
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");
756   addDoc(cm, 4, 8);
757   range = cm.markText(Pos(1, 2), Pos(6, 2), {
758     replacedWith: document.createTextNode("M"),
759     clearOnEnter: true
760   });
761   var cleared = 0;
762   CodeMirror.on(range, "clear", function(){++cleared;});
763   cm.setCursor(0, 3);
764   CodeMirror.commands.goLineDown(cm);
765   eqPos(cm.getCursor(), Pos(6, 2));
766   CodeMirror.commands.goCharLeft(cm);
767   eqPos(cm.getCursor(), Pos(6, 1));
768   eq(cleared, 1);
769   range.clear();
770   eq(cleared, 1);
771   range = cm.markText(Pos(1, 2), Pos(6, 2), {
772     replacedWith: document.createTextNode("Q"),
773     clearOnEnter: true
774   });
775   range.clear();
776   cm.setCursor(1, 2);
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")
781   });
782   cm.setCursor(1, 0);
783   CodeMirror.commands.goLineDown(cm);
784   eqPos(cm.getCursor(), Pos(2, 0));
785 }, null);
786
787 testCM("nestedFold", function(cm) {
788   addDoc(cm, 10, 3);
789   function fold(ll, cl, lr, cr) {
790     return cm.markText(Pos(ll, cl), Pos(lr, cr), {collapsed: true});
791   }
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);
793   cm.setCursor(0, 1);
794   CodeMirror.commands.goCharRight(cm);
795   eqPos(cm.getCursor(), Pos(2, 3));
796   inner0.clear();
797   CodeMirror.commands.goCharLeft(cm);
798   eqPos(cm.getCursor(), Pos(0, 1));
799   outer.clear();
800   CodeMirror.commands.goCharRight(cm);
801   eqPos(cm.getCursor(), Pos(0, 2));
802   CodeMirror.commands.goCharRight(cm);
803   eqPos(cm.getCursor(), Pos(1, 8));
804   inner2.clear();
805   CodeMirror.commands.goCharLeft(cm);
806   eqPos(cm.getCursor(), Pos(1, 7));
807   cm.setCursor(0, 5);
808   CodeMirror.commands.goCharRight(cm);
809   eqPos(cm.getCursor(), Pos(0, 6));
810   CodeMirror.commands.goCharRight(cm);
811   eqPos(cm.getCursor(), Pos(1, 3));
812 });
813
814 testCM("badNestedFold", function(cm) {
815   addDoc(cm, 4, 4);
816   cm.markText(Pos(0, 2), Pos(3, 2), {collapsed: true});
817   var caught;
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");
822 });
823
824 testCM("wrappingInlineWidget", function(cm) {
825   cm.setSize("11em");
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});
843
844 testCM("changedInlineWidget", function(cm) {
845   cm.setSize("10em");
846   var w = document.createElement("span");
847   w.innerHTML = "x";
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";
850   m.changed();
851   var hScroll = byClassName(cm.getWrapperElement(), "CodeMirror-hscrollbar")[0];
852   is(hScroll.scrollWidth > hScroll.clientWidth);
853 }, {value: "hello there"});
854
855 testCM("inlineWidget", function(cm) {
856   var w = cm.setBookmark(Pos(0, 2), {widget: document.createTextNode("uu")});
857   cm.setCursor(0, 2);
858   CodeMirror.commands.goLineDown(cm);
859   eqPos(cm.getCursor(), Pos(1, 4));
860   cm.setCursor(0, 2);
861   cm.replaceSelection("hi");
862   eqPos(w.find(), Pos(0, 2));
863   cm.setCursor(0, 1);
864   cm.replaceSelection("ay");
865   eqPos(w.find(), Pos(0, 4));
866   eq(cm.getLine(0), "uayuhiuu");
867 }, {value: "uuuu\nuuuuuu"});
868
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";
874   cm.setValue(doc);
875   for (var step = 10, w = cm.charCoords(Pos(0, 18), "div").right;; w += step) {
876     cm.setSize(w);
877     if (wrap.offsetHeight <= h0 * (opera_lt10 ? 1.2 : 1.5)) {
878       if (step == 10) { w -= 10; step = 1; }
879       else break;
880     }
881   }
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)],
895           function(pos) {
896     var coords = cm.charCoords(pos);
897     eqPos(pos, cm.coordsChar({left: coords.left + 2, top: coords.top + 5}));
898   });
899 }, null, ie_lt8);
900
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) {
906     cm.setSize(w);
907     if (inner.offsetHeight < 2.5 * lh) {
908       if (step == 10) { w -= 10; step = 1; }
909       else break;
910     }
911   }
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);
919
920 testCM("scrollVerticallyAndHorizontally", function(cm) {
921   cm.setSize(100, 100);
922   addDoc(cm, 40, 40);
923   cm.setCursor(39);
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});
931
932 testCM("moveVstuck", function(cm) {
933   var lines = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild, h0 = lines.offsetHeight;
934   var val = "fooooooooooooooooooooooooo baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar\n";
935   cm.setValue(val);
936   for (var w = cm.charCoords(Pos(0, 26), "div").right * 2.8;; w += 5) {
937     cm.setSize(w);
938     if (lines.offsetHeight <= 3.5 * h0) break;
939   }
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);
944
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});
950
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");
966 });
967
968 testCM("extraKeys", function(cm) {
969   var outcome;
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];
974     outcome = null;
975     cm.triggerOnKeyDown(e);
976     eq(outcome, expected);
977   }
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",
985                              "Tab": false});
986   fakeKey(null, "U");
987   fakeKey("cau", "U", {ctrlKey: true, altKey: true});
988   fakeKey(null, "U", {shiftKey: true, ctrlKey: true, altKey: true});
989   fakeKey("x", "X");
990   fakeKey("sx", "X", {shiftKey: true});
991   fakeKey("tc", 35);
992   fakeKey(null, 35, {shiftKey: true});
993   fakeKey("gtc", 36);
994   fakeKey("gtc", 36, {shiftKey: true});
995   fakeKey(null, 9);
996 }, null, window.opera && mac);
997
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"});
1018
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"});
1046
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"});
1071
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"});
1094
1095 testCM("verticalMovementCommandsWrapping", function(cm) {
1096   cm.setSize(120);
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; }
1107   }
1108 }, {value: "a very long line that wraps around somehow so that we can test cursor movement\nshortone\nk",
1109     lineWrapping: true});
1110
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;
1124     }
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;
1131     }
1132   });
1133 }, {rtlMoveVisually: true});
1134
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"});
1142
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));
1156 });
1157
1158 testCM("lineChangeEvents", function(cm) {
1159   addDoc(cm, 3, 5);
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);};
1164     }(i));
1165     CodeMirror.on(cm.getLineHandle(i), "change", function(i) {
1166       return function() {log.push("ch " + i);};
1167     }(i));
1168   }
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]);
1176 });
1177
1178 testCM("scrollEntirelyToRight", function(cm) {
1179   if (phantom) return;
1180   addDoc(cm, 500, 2);
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);
1184 });
1185
1186 testCM("lineWidgets", function(cm) {
1187   addDoc(cm, 500, 3);
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));
1198 });
1199
1200 testCM("lineWidgetFocus", function(cm) {
1201   var place = document.getElementById("testground");
1202   place.className = "offscreen";
1203   try {
1204     addDoc(cm, 500, 10);
1205     var node = document.createElement("input");
1206     var widget = cm.addLineWidget(1, node);
1207     node.focus();
1208     eq(document.activeElement, node);
1209     cm.replaceRange("new stuff", Pos(1, 0));
1210     eq(document.activeElement, node);
1211   } finally {
1212     place.className = "";
1213   }
1214 });
1215
1216 testCM("getLineNumber", function(cm) {
1217   addDoc(cm, 2, 20);
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);
1222   cm.setValue("");
1223   eq(cm.getLineNumber(h1), null);
1224 });
1225
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;
1232   cm.refresh();
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"});
1255
1256 testCM("addLineClass", function(cm) {
1257   function cls(line, text, bg, wrap) {
1258     var i = cm.lineInfo(line);
1259     eq(i.textClass, text);
1260     eq(i.bgClass, bg);
1261     eq(i.wrapClass, wrap);
1262   }
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"});
1284
1285 testCM("atomicMarker", function(cm) {
1286   addDoc(cm, 10, 10);
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});
1290   }
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));
1297   m.clear();
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));
1302   m.clear();
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));
1312   m.clear();
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");
1330 });
1331
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});
1336   }
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");
1345   cm.undo();
1346   eqPos(m.find().from, Pos(0, 1));
1347   eqPos(m.find().to, Pos(0, 4));
1348   m.clear();
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"});
1366
1367 testCM("dirtyBit", function(cm) {
1368   eq(cm.isClean(), true);
1369   cm.replaceSelection("boo");
1370   eq(cm.isClean(), false);
1371   cm.undo();
1372   eq(cm.isClean(), true);
1373   cm.replaceSelection("boo");
1374   cm.replaceSelection("baz");
1375   cm.undo();
1376   eq(cm.isClean(), false);
1377   cm.markClean();
1378   eq(cm.isClean(), true);
1379   cm.undo();
1380   eq(cm.isClean(), false);
1381   cm.redo();
1382   eq(cm.isClean(), true);
1383 });
1384
1385 testCM("addKeyMap", function(cm) {
1386   function sendKey(code) {
1387     cm.triggerOnKeyDown({type: "keydown", keyCode: code,
1388                          preventDefault: function(){}, stopPropagation: function(){}});
1389   }
1390
1391   sendKey(39);
1392   eqPos(cm.getCursor(), Pos(0, 1));
1393   var test = 0;
1394   var map1 = {Right: function() { ++test; }}, map2 = {Right: function() { test += 10; }}
1395   cm.addKeyMap(map1);
1396   sendKey(39);
1397   eqPos(cm.getCursor(), Pos(0, 1));
1398   eq(test, 1);
1399   cm.addKeyMap(map2, true);
1400   sendKey(39);
1401   eq(test, 2);
1402   cm.removeKeyMap(map1);
1403   sendKey(39);
1404   eq(test, 12);
1405   cm.removeKeyMap(map2);
1406   sendKey(39);
1407   eq(test, 12);
1408   eqPos(cm.getCursor(), Pos(0, 2));
1409   cm.addKeyMap({Right: function() { test = 55; }, name: "mymap"});
1410   sendKey(39);
1411   eq(test, 55);
1412   cm.removeKeyMap("mymap");
1413   sendKey(39);
1414   eqPos(cm.getCursor(), Pos(0, 3));
1415 }, {value: "abc"});
1416
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");
1433     eqPos(r, t.to);
1434     eq(!!r.hitSide, !!t.hitSide);
1435   });
1436 }, {value: "line one\nline two.something.other\n"});
1437
1438 testCM("beforeChange", function(cm) {
1439   cm.on("beforeChange", function(cm, change) {
1440     var text = [];
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);
1444   });
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();
1449   });
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"});
1455
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) {
1461     is(!change.update);
1462     change.cancel();
1463   });
1464   cm.undo();
1465   eq(cm.historySize().undo, 0);
1466   eq(cm.getValue(), "bye\ntwo");
1467 }, {value: "one\ntwo"});
1468
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);
1473     return pos;
1474   }
1475   cm.on("beforeSelectionChange", function(cm, sel) {
1476     sel.head = notAtEnd(cm, sel.head);
1477     sel.anchor = notAtEnd(cm, sel.anchor);
1478   });
1479
1480   addDoc(cm, 10, 10);
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));
1486 });
1487
1488 testCM("change_removedText", function(cm) {
1489   cm.setValue("abc\ndef");
1490
1491   var removedText;
1492   cm.on("change", function(cm, change) {
1493     removedText = [change.removed, change.next && change.next.removed];
1494   });
1495
1496   cm.operation(function() {
1497     cm.replaceRange("xyz", Pos(0, 0), Pos(1,1));
1498     cm.replaceRange("123", Pos(0,0));
1499   });
1500
1501   eq(removedText[0].join("\n"), "abc\nd");
1502   eq(removedText[1].join("\n"), "");
1503
1504   cm.undo();
1505   eq(removedText[0].join("\n"), "123");
1506   eq(removedText[1].join("\n"), "xyz");
1507
1508   cm.redo();
1509   eq(removedText[0].join("\n"), "abc\nd");
1510   eq(removedText[1].join("\n"), "");
1511 });