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