comparison .cms/lib/codemirror/test/test.js @ 0:78edf6b517a0 draft

24.10
author Coffee CMS <info@coffee-cms.ru>
date Fri, 11 Oct 2024 22:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78edf6b517a0
1 var Pos = CodeMirror.Pos;
2
3 CodeMirror.defaults.rtlMoveVisually = true;
4
5 function forEach(arr, f) {
6 for (var i = 0, e = arr.length; i < e; ++i) f(arr[i], i);
7 }
8
9 function addDoc(cm, width, height) {
10 var content = [], line = "";
11 for (var i = 0; i < width; ++i) line += "x";
12 for (var i = 0; i < height; ++i) content.push(line);
13 cm.setValue(content.join("\n"));
14 }
15
16 function byClassName(elt, cls) {
17 if (elt.getElementsByClassName) return elt.getElementsByClassName(cls);
18 var found = [], re = new RegExp("\\b" + cls + "\\b");
19 function search(elt) {
20 if (elt.nodeType == 3) return;
21 if (re.test(elt.className)) found.push(elt);
22 for (var i = 0, e = elt.childNodes.length; i < e; ++i)
23 search(elt.childNodes[i]);
24 }
25 search(elt);
26 return found;
27 }
28
29 var ie_lt8 = /MSIE [1-7]\b/.test(navigator.userAgent);
30 var ie_lt9 = /MSIE [1-8]\b/.test(navigator.userAgent);
31 var mac = /Mac/.test(navigator.platform);
32 var opera = /Opera\/\./.test(navigator.userAgent);
33 var opera_version = opera && navigator.userAgent.match(/Version\/(\d+\.\d+)/);
34 if (opera_version) opera_version = Number(opera_version);
35 var opera_lt10 = opera && (!opera_version || opera_version < 10);
36
37 namespace = "core_";
38
39 test("core_fromTextArea", function() {
40 var te = document.getElementById("code");
41 te.value = "CONTENT";
42 var cm = CodeMirror.fromTextArea(te);
43 is(!te.offsetHeight);
44 eq(cm.getValue(), "CONTENT");
45 cm.setValue("foo\nbar");
46 eq(cm.getValue(), "foo\nbar");
47 cm.save();
48 is(/^foo\r?\nbar$/.test(te.value));
49 cm.setValue("xxx");
50 cm.toTextArea();
51 is(te.offsetHeight);
52 eq(te.value, "xxx");
53 });
54
55 testCM("getRange", function(cm) {
56 eq(cm.getLine(0), "1234");
57 eq(cm.getLine(1), "5678");
58 eq(cm.getLine(2), null);
59 eq(cm.getLine(-1), null);
60 eq(cm.getRange(Pos(0, 0), Pos(0, 3)), "123");
61 eq(cm.getRange(Pos(0, -1), Pos(0, 200)), "1234");
62 eq(cm.getRange(Pos(0, 2), Pos(1, 2)), "34\n56");
63 eq(cm.getRange(Pos(1, 2), Pos(100, 0)), "78");
64 }, {value: "1234\n5678"});
65
66 testCM("replaceRange", function(cm) {
67 eq(cm.getValue(), "");
68 cm.replaceRange("foo\n", Pos(0, 0));
69 eq(cm.getValue(), "foo\n");
70 cm.replaceRange("a\nb", Pos(0, 1));
71 eq(cm.getValue(), "fa\nboo\n");
72 eq(cm.lineCount(), 3);
73 cm.replaceRange("xyzzy", Pos(0, 0), Pos(1, 1));
74 eq(cm.getValue(), "xyzzyoo\n");
75 cm.replaceRange("abc", Pos(0, 0), Pos(10, 0));
76 eq(cm.getValue(), "abc");
77 eq(cm.lineCount(), 1);
78 });
79
80 testCM("selection", function(cm) {
81 cm.setSelection(Pos(0, 4), Pos(2, 2));
82 is(cm.somethingSelected());
83 eq(cm.getSelection(), "11\n222222\n33");
84 eqCursorPos(cm.getCursor(false), Pos(2, 2));
85 eqCursorPos(cm.getCursor(true), Pos(0, 4));
86 cm.setSelection(Pos(1, 0));
87 is(!cm.somethingSelected());
88 eq(cm.getSelection(), "");
89 eqCursorPos(cm.getCursor(true), Pos(1, 0));
90 cm.replaceSelection("abc", "around");
91 eq(cm.getSelection(), "abc");
92 eq(cm.getValue(), "111111\nabc222222\n333333");
93 cm.replaceSelection("def", "end");
94 eq(cm.getSelection(), "");
95 eqCursorPos(cm.getCursor(true), Pos(1, 3));
96 cm.setCursor(Pos(2, 1));
97 eqCursorPos(cm.getCursor(true), Pos(2, 1));
98 cm.setCursor(1, 2);
99 eqCursorPos(cm.getCursor(true), Pos(1, 2));
100 }, {value: "111111\n222222\n333333"});
101
102 testCM("extendSelection", function(cm) {
103 cm.setExtending(true);
104 addDoc(cm, 10, 10);
105 cm.setSelection(Pos(3, 5));
106 eqCursorPos(cm.getCursor("head"), Pos(3, 5));
107 eqCursorPos(cm.getCursor("anchor"), Pos(3, 5));
108 cm.setSelection(Pos(2, 5), Pos(5, 5));
109 eqCursorPos(cm.getCursor("head"), Pos(5, 5));
110 eqCursorPos(cm.getCursor("anchor"), Pos(2, 5));
111 eqCursorPos(cm.getCursor("start"), Pos(2, 5));
112 eqCursorPos(cm.getCursor("end"), Pos(5, 5));
113 cm.setSelection(Pos(5, 5), Pos(2, 5));
114 eqCursorPos(cm.getCursor("head"), Pos(2, 5));
115 eqCursorPos(cm.getCursor("anchor"), Pos(5, 5));
116 eqCursorPos(cm.getCursor("start"), Pos(2, 5));
117 eqCursorPos(cm.getCursor("end"), Pos(5, 5));
118 cm.extendSelection(Pos(3, 2));
119 eqCursorPos(cm.getCursor("head"), Pos(3, 2));
120 eqCursorPos(cm.getCursor("anchor"), Pos(5, 5));
121 cm.extendSelection(Pos(6, 2));
122 eqCursorPos(cm.getCursor("head"), Pos(6, 2));
123 eqCursorPos(cm.getCursor("anchor"), Pos(5, 5));
124 cm.extendSelection(Pos(6, 3), Pos(6, 4));
125 eqCursorPos(cm.getCursor("head"), Pos(6, 4));
126 eqCursorPos(cm.getCursor("anchor"), Pos(5, 5));
127 cm.extendSelection(Pos(0, 3), Pos(0, 4));
128 eqCursorPos(cm.getCursor("head"), Pos(0, 3));
129 eqCursorPos(cm.getCursor("anchor"), Pos(5, 5));
130 cm.extendSelection(Pos(4, 5), Pos(6, 5));
131 eqCursorPos(cm.getCursor("head"), Pos(6, 5));
132 eqCursorPos(cm.getCursor("anchor"), Pos(4, 5));
133 cm.setExtending(false);
134 cm.extendSelection(Pos(0, 3), Pos(0, 4));
135 eqCursorPos(cm.getCursor("head"), Pos(0, 3));
136 eqCursorPos(cm.getCursor("anchor"), Pos(0, 4));
137 });
138
139 testCM("lines", function(cm) {
140 eq(cm.getLine(0), "111111");
141 eq(cm.getLine(1), "222222");
142 eq(cm.getLine(-1), null);
143 cm.replaceRange("", Pos(1, 0), Pos(2, 0))
144 cm.replaceRange("abc", Pos(1, 0), Pos(1));
145 eq(cm.getValue(), "111111\nabc");
146 }, {value: "111111\n222222\n333333"});
147
148 testCM("indent", function(cm) {
149 cm.indentLine(1);
150 eq(cm.getLine(1), " blah();");
151 cm.setOption("indentUnit", 8);
152 cm.indentLine(1);
153 eq(cm.getLine(1), "\tblah();");
154 cm.setOption("indentUnit", 10);
155 cm.setOption("tabSize", 4);
156 cm.indentLine(1);
157 eq(cm.getLine(1), "\t\t blah();");
158 }, {value: "if (x) {\nblah();\n}", indentUnit: 3, indentWithTabs: true, tabSize: 8});
159
160 testCM("indentByNumber", function(cm) {
161 cm.indentLine(0, 2);
162 eq(cm.getLine(0), " foo");
163 cm.indentLine(0, -200);
164 eq(cm.getLine(0), "foo");
165 cm.setSelection(Pos(0, 0), Pos(1, 2));
166 cm.indentSelection(3);
167 eq(cm.getValue(), " foo\n bar\nbaz");
168 }, {value: "foo\nbar\nbaz"});
169
170 test("core_defaults", function() {
171 var defsCopy = {}, defs = CodeMirror.defaults;
172 for (var opt in defs) defsCopy[opt] = defs[opt];
173 defs.indentUnit = 5;
174 defs.value = "uu";
175 defs.indentWithTabs = true;
176 defs.tabindex = 55;
177 var place = document.getElementById("testground"), cm = CodeMirror(place);
178 try {
179 eq(cm.getOption("indentUnit"), 5);
180 cm.setOption("indentUnit", 10);
181 eq(defs.indentUnit, 5);
182 eq(cm.getValue(), "uu");
183 eq(cm.getOption("indentWithTabs"), true);
184 eq(cm.getInputField().tabIndex, 55);
185 }
186 finally {
187 for (var opt in defsCopy) defs[opt] = defsCopy[opt];
188 place.removeChild(cm.getWrapperElement());
189 }
190 });
191
192 testCM("lineInfo", function(cm) {
193 eq(cm.lineInfo(-1), null);
194 var mark = document.createElement("span");
195 var lh = cm.setGutterMarker(1, "FOO", mark);
196 var info = cm.lineInfo(1);
197 eq(info.text, "222222");
198 eq(info.gutterMarkers.FOO, mark);
199 eq(info.line, 1);
200 eq(cm.lineInfo(2).gutterMarkers, null);
201 cm.setGutterMarker(lh, "FOO", null);
202 eq(cm.lineInfo(1).gutterMarkers, null);
203 cm.setGutterMarker(1, "FOO", mark);
204 cm.setGutterMarker(0, "FOO", mark);
205 cm.clearGutter("FOO");
206 eq(cm.lineInfo(0).gutterMarkers, null);
207 eq(cm.lineInfo(1).gutterMarkers, null);
208 }, {value: "111111\n222222\n333333"});
209
210 testCM("coords", function(cm) {
211 cm.setSize(null, 100);
212 addDoc(cm, 32, 200);
213 var top = cm.charCoords(Pos(0, 0));
214 var bot = cm.charCoords(Pos(200, 30));
215 is(top.left < bot.left);
216 is(top.top < bot.top);
217 is(top.top < top.bottom);
218 cm.scrollTo(null, 100);
219 var top2 = cm.charCoords(Pos(0, 0));
220 is(top.top > top2.top);
221 eq(top.left, top2.left);
222 });
223
224 testCM("coordsChar", function(cm) {
225 addDoc(cm, 35, 70);
226 for (var i = 0; i < 2; ++i) {
227 var sys = i ? "local" : "page";
228 for (var ch = 0; ch <= 35; ch += 5) {
229 for (var line = 0; line < 70; line += 5) {
230 cm.setCursor(line, ch);
231 var coords = cm.charCoords(Pos(line, ch), sys);
232 var pos = cm.coordsChar({left: coords.left + 1, top: coords.top + 1}, sys);
233 eqCharPos(pos, Pos(line, ch));
234 }
235 }
236 }
237 }, {lineNumbers: true});
238
239 testCM("coordsCharBidi", function(cm) {
240 addDoc(cm, 35, 70);
241 // Put an rtl character into each line to trigger the bidi code path in coordsChar
242 cm.setValue(cm.getValue().replace(/\bx/g, 'و'))
243 for (var i = 0; i < 2; ++i) {
244 var sys = i ? "local" : "page";
245 for (var ch = 2; ch <= 35; ch += 5) {
246 for (var line = 0; line < 70; line += 5) {
247 cm.setCursor(line, ch);
248 var coords = cm.charCoords(Pos(line, ch), sys);
249 var pos = cm.coordsChar({left: coords.left + 1, top: coords.top + 1}, sys);
250 eqCharPos(pos, Pos(line, ch));
251 }
252 }
253 }
254 }, {lineNumbers: true});
255
256 testCM("badBidiOptimization", function(cm) {
257 if (window.automatedTests) return
258 var coords = cm.charCoords(Pos(0, 34))
259 eqCharPos(cm.coordsChar({left: coords.right, top: coords.top + 2}), Pos(0, 34))
260 }, {value: "----------<p class=\"title\">هل يمكنك اختيار مستوى قسط التأمين الذي ترغب بدفعه؟</p>"})
261
262 testCM("posFromIndex", function(cm) {
263 cm.setValue(
264 "This function should\n" +
265 "convert a zero based index\n" +
266 "to line and ch."
267 );
268
269 var examples = [
270 { index: -1, line: 0, ch: 0 }, // <- Tests clipping
271 { index: 0, line: 0, ch: 0 },
272 { index: 10, line: 0, ch: 10 },
273 { index: 39, line: 1, ch: 18 },
274 { index: 55, line: 2, ch: 7 },
275 { index: 63, line: 2, ch: 15 },
276 { index: 64, line: 2, ch: 15 } // <- Tests clipping
277 ];
278
279 for (var i = 0; i < examples.length; i++) {
280 var example = examples[i];
281 var pos = cm.posFromIndex(example.index);
282 eq(pos.line, example.line);
283 eq(pos.ch, example.ch);
284 if (example.index >= 0 && example.index < 64)
285 eq(cm.indexFromPos(pos), example.index);
286 }
287 });
288
289 testCM("undo", function(cm) {
290 cm.replaceRange("def", Pos(0, 0), Pos(0));
291 eq(cm.historySize().undo, 1);
292 cm.undo();
293 eq(cm.getValue(), "abc");
294 eq(cm.historySize().undo, 0);
295 eq(cm.historySize().redo, 1);
296 cm.redo();
297 eq(cm.getValue(), "def");
298 eq(cm.historySize().undo, 1);
299 eq(cm.historySize().redo, 0);
300 cm.setValue("1\n\n\n2");
301 cm.clearHistory();
302 eq(cm.historySize().undo, 0);
303 for (var i = 0; i < 20; ++i) {
304 cm.replaceRange("a", Pos(0, 0));
305 cm.replaceRange("b", Pos(3, 0));
306 }
307 eq(cm.historySize().undo, 40);
308 for (var i = 0; i < 40; ++i)
309 cm.undo();
310 eq(cm.historySize().redo, 40);
311 eq(cm.getValue(), "1\n\n\n2");
312 }, {value: "abc"});
313
314 testCM("undoDepth", function(cm) {
315 cm.replaceRange("d", Pos(0));
316 cm.replaceRange("e", Pos(0));
317 cm.replaceRange("f", Pos(0));
318 cm.undo(); cm.undo(); cm.undo();
319 eq(cm.getValue(), "abcd");
320 }, {value: "abc", undoDepth: 4});
321
322 testCM("undoDoesntClearValue", function(cm) {
323 cm.undo();
324 eq(cm.getValue(), "x");
325 }, {value: "x"});
326
327 testCM("undoMultiLine", function(cm) {
328 cm.operation(function() {
329 cm.replaceRange("x", Pos(0, 0));
330 cm.replaceRange("y", Pos(1, 0));
331 });
332 cm.undo();
333 eq(cm.getValue(), "abc\ndef\nghi");
334 cm.operation(function() {
335 cm.replaceRange("y", Pos(1, 0));
336 cm.replaceRange("x", Pos(0, 0));
337 });
338 cm.undo();
339 eq(cm.getValue(), "abc\ndef\nghi");
340 cm.operation(function() {
341 cm.replaceRange("y", Pos(2, 0));
342 cm.replaceRange("x", Pos(1, 0));
343 cm.replaceRange("z", Pos(2, 0));
344 });
345 cm.undo();
346 eq(cm.getValue(), "abc\ndef\nghi", 3);
347 }, {value: "abc\ndef\nghi"});
348
349 testCM("undoComposite", function(cm) {
350 cm.replaceRange("y", Pos(1));
351 cm.operation(function() {
352 cm.replaceRange("x", Pos(0));
353 cm.replaceRange("z", Pos(2));
354 });
355 eq(cm.getValue(), "ax\nby\ncz\n");
356 cm.undo();
357 eq(cm.getValue(), "a\nby\nc\n");
358 cm.undo();
359 eq(cm.getValue(), "a\nb\nc\n");
360 cm.redo(); cm.redo();
361 eq(cm.getValue(), "ax\nby\ncz\n");
362 }, {value: "a\nb\nc\n"});
363
364 testCM("undoSelection", function(cm) {
365 cm.setSelection(Pos(0, 2), Pos(0, 4));
366 cm.replaceSelection("");
367 cm.setCursor(Pos(1, 0));
368 cm.undo();
369 eqCursorPos(cm.getCursor(true), Pos(0, 2));
370 eqCursorPos(cm.getCursor(false), Pos(0, 4));
371 cm.setCursor(Pos(1, 0));
372 cm.redo();
373 eqCursorPos(cm.getCursor(true), Pos(0, 2));
374 eqCursorPos(cm.getCursor(false), Pos(0, 2));
375 }, {value: "abcdefgh\n"});
376
377 testCM("undoSelectionAsBefore", function(cm) {
378 cm.replaceSelection("abc", "around");
379 cm.undo();
380 cm.redo();
381 eq(cm.getSelection(), "abc");
382 });
383
384 testCM("selectionChangeConfusesHistory", function(cm) {
385 cm.replaceSelection("abc", null, "dontmerge");
386 cm.operation(function() {
387 cm.setCursor(Pos(0, 0));
388 cm.replaceSelection("abc", null, "dontmerge");
389 });
390 eq(cm.historySize().undo, 2);
391 });
392
393 testCM("markTextSingleLine", function(cm) {
394 forEach([{a: 0, b: 1, c: "", f: 2, t: 5},
395 {a: 0, b: 4, c: "", f: 0, t: 2},
396 {a: 1, b: 2, c: "x", f: 3, t: 6},
397 {a: 4, b: 5, c: "", f: 3, t: 5},
398 {a: 4, b: 5, c: "xx", f: 3, t: 7},
399 {a: 2, b: 5, c: "", f: 2, t: 3},
400 {a: 2, b: 5, c: "abcd", f: 6, t: 7},
401 {a: 2, b: 6, c: "x", f: null, t: null},
402 {a: 3, b: 6, c: "", f: null, t: null},
403 {a: 0, b: 9, c: "hallo", f: null, t: null},
404 {a: 4, b: 6, c: "x", f: 3, t: 4},
405 {a: 4, b: 8, c: "", f: 3, t: 4},
406 {a: 6, b: 6, c: "a", f: 3, t: 6},
407 {a: 8, b: 9, c: "", f: 3, t: 6}], function(test) {
408 cm.setValue("1234567890");
409 var r = cm.markText(Pos(0, 3), Pos(0, 6), {className: "foo"});
410 cm.replaceRange(test.c, Pos(0, test.a), Pos(0, test.b));
411 var f = r.find();
412 eq(f && f.from.ch, test.f); eq(f && f.to.ch, test.t);
413 });
414 });
415
416 testCM("markTextMultiLine", function(cm) {
417 function p(v) { return v && Pos(v[0], v[1]); }
418 forEach([{a: [0, 0], b: [0, 5], c: "", f: [0, 0], t: [2, 5]},
419 {a: [0, 0], b: [0, 5], c: "foo\n", f: [1, 0], t: [3, 5]},
420 {a: [0, 1], b: [0, 10], c: "", f: [0, 1], t: [2, 5]},
421 {a: [0, 5], b: [0, 6], c: "x", f: [0, 6], t: [2, 5]},
422 {a: [0, 0], b: [1, 0], c: "", f: [0, 0], t: [1, 5]},
423 {a: [0, 6], b: [2, 4], c: "", f: [0, 5], t: [0, 7]},
424 {a: [0, 6], b: [2, 4], c: "aa", f: [0, 5], t: [0, 9]},
425 {a: [1, 2], b: [1, 8], c: "", f: [0, 5], t: [2, 5]},
426 {a: [0, 5], b: [2, 5], c: "xx", f: null, t: null},
427 {a: [0, 0], b: [2, 10], c: "x", f: null, t: null},
428 {a: [1, 5], b: [2, 5], c: "", f: [0, 5], t: [1, 5]},
429 {a: [2, 0], b: [2, 3], c: "", f: [0, 5], t: [2, 2]},
430 {a: [2, 5], b: [3, 0], c: "a\nb", f: [0, 5], t: [2, 5]},
431 {a: [2, 3], b: [3, 0], c: "x", f: [0, 5], t: [2, 3]},
432 {a: [1, 1], b: [1, 9], c: "1\n2\n3", f: [0, 5], t: [4, 5]}], function(test) {
433 cm.setValue("aaaaaaaaaa\nbbbbbbbbbb\ncccccccccc\ndddddddd\n");
434 var r = cm.markText(Pos(0, 5), Pos(2, 5),
435 {className: "CodeMirror-matchingbracket"});
436 cm.replaceRange(test.c, p(test.a), p(test.b));
437 var f = r.find();
438 eqCursorPos(f && f.from, p(test.f)); eqCursorPos(f && f.to, p(test.t));
439 });
440 });
441
442 testCM("markTextUndo", function(cm) {
443 var marker1, marker2, bookmark;
444 marker1 = cm.markText(Pos(0, 1), Pos(0, 3),
445 {className: "CodeMirror-matchingbracket"});
446 marker2 = cm.markText(Pos(0, 0), Pos(2, 1),
447 {className: "CodeMirror-matchingbracket"});
448 bookmark = cm.setBookmark(Pos(1, 5));
449 cm.operation(function(){
450 cm.replaceRange("foo", Pos(0, 2));
451 cm.replaceRange("bar\nbaz\nbug\n", Pos(2, 0), Pos(3, 0));
452 });
453 var v1 = cm.getValue();
454 cm.setValue("");
455 eq(marker1.find(), null); eq(marker2.find(), null); eq(bookmark.find(), null);
456 cm.undo();
457 eqCursorPos(bookmark.find(), Pos(1, 5), "still there");
458 cm.undo();
459 var m1Pos = marker1.find(), m2Pos = marker2.find();
460 eqCursorPos(m1Pos.from, Pos(0, 1)); eqCursorPos(m1Pos.to, Pos(0, 3));
461 eqCursorPos(m2Pos.from, Pos(0, 0)); eqCursorPos(m2Pos.to, Pos(2, 1));
462 eqCursorPos(bookmark.find(), Pos(1, 5));
463 cm.redo(); cm.redo();
464 eq(bookmark.find(), null);
465 cm.undo();
466 eqCursorPos(bookmark.find(), Pos(1, 5));
467 eq(cm.getValue(), v1);
468 }, {value: "1234\n56789\n00\n"});
469
470 testCM("markTextStayGone", function(cm) {
471 var m1 = cm.markText(Pos(0, 0), Pos(0, 1));
472 cm.replaceRange("hi", Pos(0, 2));
473 m1.clear();
474 cm.undo();
475 eq(m1.find(), null);
476 }, {value: "hello"});
477
478 testCM("markTextAllowEmpty", function(cm) {
479 var m1 = cm.markText(Pos(0, 1), Pos(0, 2), {clearWhenEmpty: false});
480 is(m1.find());
481 cm.replaceRange("x", Pos(0, 0));
482 is(m1.find());
483 cm.replaceRange("y", Pos(0, 2));
484 is(m1.find());
485 cm.replaceRange("z", Pos(0, 3), Pos(0, 4));
486 is(!m1.find());
487 var m2 = cm.markText(Pos(0, 1), Pos(0, 2), {clearWhenEmpty: false,
488 inclusiveLeft: true,
489 inclusiveRight: true});
490 cm.replaceRange("q", Pos(0, 1), Pos(0, 2));
491 is(m2.find());
492 cm.replaceRange("", Pos(0, 0), Pos(0, 3));
493 is(!m2.find());
494 var m3 = cm.markText(Pos(0, 1), Pos(0, 1), {clearWhenEmpty: false});
495 cm.replaceRange("a", Pos(0, 3));
496 is(m3.find());
497 cm.replaceRange("b", Pos(0, 1));
498 is(!m3.find());
499 }, {value: "abcde"});
500
501 testCM("markTextStacked", function(cm) {
502 var m1 = cm.markText(Pos(0, 0), Pos(0, 0), {clearWhenEmpty: false});
503 var m2 = cm.markText(Pos(0, 0), Pos(0, 0), {clearWhenEmpty: false});
504 cm.replaceRange("B", Pos(0, 1));
505 is(m1.find() && m2.find());
506 }, {value: "A"});
507
508 testCM("undoPreservesNewMarks", function(cm) {
509 cm.markText(Pos(0, 3), Pos(0, 4));
510 cm.markText(Pos(1, 1), Pos(1, 3));
511 cm.replaceRange("", Pos(0, 3), Pos(3, 1));
512 var mBefore = cm.markText(Pos(0, 0), Pos(0, 1));
513 var mAfter = cm.markText(Pos(0, 5), Pos(0, 6));
514 var mAround = cm.markText(Pos(0, 2), Pos(0, 4));
515 cm.undo();
516 eqCursorPos(mBefore.find().from, Pos(0, 0));
517 eqCursorPos(mBefore.find().to, Pos(0, 1));
518 eqCursorPos(mAfter.find().from, Pos(3, 3));
519 eqCursorPos(mAfter.find().to, Pos(3, 4));
520 eqCursorPos(mAround.find().from, Pos(0, 2));
521 eqCursorPos(mAround.find().to, Pos(3, 2));
522 var found = cm.findMarksAt(Pos(2, 2));
523 eq(found.length, 1);
524 eq(found[0], mAround);
525 }, {value: "aaaa\nbbbb\ncccc\ndddd"});
526
527 testCM("markClearBetween", function(cm) {
528 cm.setValue("aaa\nbbb\nccc\nddd\n");
529 cm.markText(Pos(0, 0), Pos(2));
530 cm.replaceRange("aaa\nbbb\nccc", Pos(0, 0), Pos(2));
531 eq(cm.findMarksAt(Pos(1, 1)).length, 0);
532 });
533
534 testCM("findMarksMiddle", function(cm) {
535 var mark = cm.markText(Pos(1, 1), Pos(3, 1));
536 var found = cm.findMarks(Pos(2, 1), Pos(2, 2));
537 eq(found.length, 1);
538 eq(found[0], mark);
539 }, {value: "line 0\nline 1\nline 2\nline 3"});
540
541 testCM("deleteSpanCollapsedInclusiveLeft", function(cm) {
542 var from = Pos(1, 0), to = Pos(1, 1);
543 var m = cm.markText(from, to, {collapsed: true, inclusiveLeft: true});
544 // Delete collapsed span.
545 cm.replaceRange("", from, to);
546 }, {value: "abc\nX\ndef"});
547
548 testCM("markTextCSS", function(cm) {
549 function present() {
550 var spans = cm.display.lineDiv.getElementsByTagName("span");
551 for (var i = 0; i < spans.length; i++)
552 if (spans[i].style.color && spans[i].textContent == "cdef") return true;
553 }
554 var m = cm.markText(Pos(0, 2), Pos(0, 6), {css: "color: cyan"});
555 is(present());
556 m.clear();
557 is(!present());
558 }, {value: "abcdefgh"});
559
560 testCM("markTextWithAttributes", function(cm) {
561 function present() {
562 var spans = cm.display.lineDiv.getElementsByTagName("span");
563 for (var i = 0; i < spans.length; i++)
564 if (spans[i].getAttribute("label") == "label" && spans[i].textContent == "cdef") return true;
565 }
566 var m = cm.markText(Pos(0, 2), Pos(0, 6), {attributes: {label: "label"}});
567 is(present());
568 m.clear();
569 is(!present());
570 }, {value: "abcdefgh"});
571
572 testCM("bookmark", function(cm) {
573 function p(v) { return v && Pos(v[0], v[1]); }
574 forEach([{a: [1, 0], b: [1, 1], c: "", d: [1, 4]},
575 {a: [1, 1], b: [1, 1], c: "xx", d: [1, 7]},
576 {a: [1, 4], b: [1, 5], c: "ab", d: [1, 6]},
577 {a: [1, 4], b: [1, 6], c: "", d: null},
578 {a: [1, 5], b: [1, 6], c: "abc", d: [1, 5]},
579 {a: [1, 6], b: [1, 8], c: "", d: [1, 5]},
580 {a: [1, 4], b: [1, 4], c: "\n\n", d: [3, 1]},
581 {bm: [1, 9], a: [1, 1], b: [1, 1], c: "\n", d: [2, 8]}], function(test) {
582 cm.setValue("1234567890\n1234567890\n1234567890");
583 var b = cm.setBookmark(p(test.bm) || Pos(1, 5));
584 cm.replaceRange(test.c, p(test.a), p(test.b));
585 eqCursorPos(b.find(), p(test.d));
586 });
587 });
588
589 testCM("bookmarkInsertLeft", function(cm) {
590 var br = cm.setBookmark(Pos(0, 2), {insertLeft: false});
591 var bl = cm.setBookmark(Pos(0, 2), {insertLeft: true});
592 cm.setCursor(Pos(0, 2));
593 cm.replaceSelection("hi");
594 eqCursorPos(br.find(), Pos(0, 2));
595 eqCursorPos(bl.find(), Pos(0, 4));
596 cm.replaceRange("", Pos(0, 4), Pos(0, 5));
597 cm.replaceRange("", Pos(0, 2), Pos(0, 4));
598 cm.replaceRange("", Pos(0, 1), Pos(0, 2));
599 // Verify that deleting next to bookmarks doesn't kill them
600 eqCursorPos(br.find(), Pos(0, 1));
601 eqCursorPos(bl.find(), Pos(0, 1));
602 }, {value: "abcdef"});
603
604 testCM("bookmarkCursor", function(cm) {
605 var pos01 = cm.cursorCoords(Pos(0, 1)), pos11 = cm.cursorCoords(Pos(1, 1)),
606 pos20 = cm.cursorCoords(Pos(2, 0)), pos30 = cm.cursorCoords(Pos(3, 0)),
607 pos41 = cm.cursorCoords(Pos(4, 1));
608 cm.setBookmark(Pos(0, 1), {widget: document.createTextNode("←"), insertLeft: true});
609 cm.setBookmark(Pos(2, 0), {widget: document.createTextNode("←"), insertLeft: true});
610 cm.setBookmark(Pos(1, 1), {widget: document.createTextNode("→")});
611 cm.setBookmark(Pos(3, 0), {widget: document.createTextNode("→")});
612 var new01 = cm.cursorCoords(Pos(0, 1)), new11 = cm.cursorCoords(Pos(1, 1)),
613 new20 = cm.cursorCoords(Pos(2, 0)), new30 = cm.cursorCoords(Pos(3, 0));
614 near(new01.left, pos01.left, 1);
615 near(new01.top, pos01.top, 1);
616 is(new11.left > pos11.left, "at right, middle of line");
617 near(new11.top == pos11.top, 1);
618 near(new20.left, pos20.left, 1);
619 near(new20.top, pos20.top, 1);
620 is(new30.left > pos30.left, "at right, empty line");
621 near(new30.top, pos30, 1);
622 cm.setBookmark(Pos(4, 0), {widget: document.createTextNode("→")});
623 is(cm.cursorCoords(Pos(4, 1)).left > pos41.left, "single-char bug");
624 }, {value: "foo\nbar\n\n\nx\ny"});
625
626 testCM("multiBookmarkCursor", function(cm) {
627 var ms = [], m;
628 function add(insertLeft) {
629 for (var i = 0; i < 3; ++i) {
630 var node = document.createElement("span");
631 node.innerHTML = "X";
632 ms.push(cm.setBookmark(Pos(0, 1), {widget: node, insertLeft: insertLeft}));
633 }
634 }
635 var base1 = cm.cursorCoords(Pos(0, 1)).left, base4 = cm.cursorCoords(Pos(0, 4)).left;
636 add(true);
637 near(base1, cm.cursorCoords(Pos(0, 1)).left, 1);
638 while (m = ms.pop()) m.clear();
639 add(false);
640 near(base4, cm.cursorCoords(Pos(0, 1)).left, 1);
641 }, {value: "abcdefg"});
642
643 testCM("getAllMarks", function(cm) {
644 addDoc(cm, 10, 10);
645 var m1 = cm.setBookmark(Pos(0, 2));
646 var m2 = cm.markText(Pos(0, 2), Pos(3, 2));
647 var m3 = cm.markText(Pos(1, 2), Pos(1, 8));
648 var m4 = cm.markText(Pos(8, 0), Pos(9, 0));
649 eq(cm.getAllMarks().length, 4);
650 m1.clear();
651 m3.clear();
652 eq(cm.getAllMarks().length, 2);
653 });
654
655 testCM("setValueClears", function(cm) {
656 cm.addLineClass(0, "wrap", "foo");
657 var mark = cm.markText(Pos(0, 0), Pos(1, 1), {inclusiveLeft: true, inclusiveRight: true});
658 cm.setValue("foo");
659 is(!cm.lineInfo(0).wrapClass);
660 is(!mark.find());
661 }, {value: "a\nb"});
662
663 testCM("bug577", function(cm) {
664 cm.setValue("a\nb");
665 cm.clearHistory();
666 cm.setValue("fooooo");
667 cm.undo();
668 });
669
670 testCM("scrollSnap", function(cm) {
671 cm.setSize(100, 100);
672 addDoc(cm, 200, 200);
673 cm.setCursor(Pos(100, 180));
674 var info = cm.getScrollInfo();
675 is(info.left > 0 && info.top > 0);
676 cm.setCursor(Pos(0, 0));
677 info = cm.getScrollInfo();
678 is(info.left == 0 && info.top == 0, "scrolled clean to top");
679 cm.setCursor(Pos(100, 180));
680 cm.setCursor(Pos(199, 0));
681 info = cm.getScrollInfo();
682 is(info.left == 0 && info.top + 2 > info.height - cm.getScrollerElement().clientHeight, "scrolled clean to bottom");
683 });
684
685 testCM("scrollIntoView", function(cm) {
686 function test(line, ch, msg) {
687 var pos = Pos(line, ch);
688 cm.scrollIntoView(pos);
689 var outer = cm.getWrapperElement().getBoundingClientRect();
690 var box = cm.charCoords(pos, "window");
691 is(box.left >= outer.left, msg + " (left)");
692 is(box.right <= outer.right, msg + " (right)");
693 is(box.top >= outer.top, msg + " (top)");
694 is(box.bottom <= outer.bottom, msg + " (bottom)");
695 }
696 addDoc(cm, 200, 200);
697 test(199, 199, "bottom right");
698 test(0, 0, "top left");
699 test(100, 100, "center");
700 test(199, 0, "bottom left");
701 test(0, 199, "top right");
702 test(100, 100, "center again");
703 });
704
705 testCM("scrollBackAndForth", function(cm) {
706 addDoc(cm, 1, 200);
707 cm.operation(function() {
708 cm.scrollIntoView(Pos(199, 0));
709 cm.scrollIntoView(Pos(4, 0));
710 });
711 is(cm.getScrollInfo().top > 0);
712 });
713
714 testCM("selectAllNoScroll", function(cm) {
715 addDoc(cm, 1, 200);
716 cm.execCommand("selectAll");
717 eq(cm.getScrollInfo().top, 0);
718 cm.setCursor(199);
719 cm.execCommand("selectAll");
720 is(cm.getScrollInfo().top > 0);
721 });
722
723 testCM("selectionPos", function(cm) {
724 if (cm.getOption("inputStyle") != "textarea") return;
725 cm.setSize(100, 100);
726 addDoc(cm, 200, 100);
727 cm.setSelection(Pos(1, 100), Pos(98, 100));
728 var lineWidth = cm.charCoords(Pos(0, 200), "local").left;
729 var lineHeight = (cm.charCoords(Pos(99)).top - cm.charCoords(Pos(0)).top) / 100;
730 cm.scrollTo(0, 0);
731 var selElt = byClassName(cm.getWrapperElement(), "CodeMirror-selected");
732 var outer = cm.getWrapperElement().getBoundingClientRect();
733 var sawMiddle, sawTop, sawBottom;
734 for (var i = 0, e = selElt.length; i < e; ++i) {
735 var box = selElt[i].getBoundingClientRect();
736 var atLeft = box.left - outer.left < 30;
737 var width = box.right - box.left;
738 var atRight = box.right - outer.left > .8 * lineWidth;
739 if (atLeft && atRight) {
740 sawMiddle = true;
741 is(box.bottom - box.top > 90 * lineHeight, "middle high");
742 is(width > .9 * lineWidth, "middle wide");
743 } else {
744 is(width > .4 * lineWidth, "top/bot wide enough");
745 is(width < .6 * lineWidth, "top/bot slim enough");
746 if (atLeft) {
747 sawBottom = true;
748 is(box.top - outer.top > 96 * lineHeight, "bot below");
749 } else if (atRight) {
750 sawTop = true;
751 is(box.top - outer.top < 2.1 * lineHeight, "top above");
752 }
753 }
754 }
755 is(sawTop && sawBottom && sawMiddle, "all parts");
756 }, null);
757
758 testCM("restoreHistory", function(cm) {
759 cm.setValue("abc\ndef");
760 cm.replaceRange("hello", Pos(1, 0), Pos(1));
761 cm.replaceRange("goop", Pos(0, 0), Pos(0));
762 cm.undo();
763 var storedVal = cm.getValue(), storedHist = cm.getHistory();
764 if (window.JSON) storedHist = JSON.parse(JSON.stringify(storedHist));
765 eq(storedVal, "abc\nhello");
766 cm.setValue("");
767 cm.clearHistory();
768 eq(cm.historySize().undo, 0);
769 cm.setValue(storedVal);
770 cm.setHistory(storedHist);
771 cm.redo();
772 eq(cm.getValue(), "goop\nhello");
773 cm.undo(); cm.undo();
774 eq(cm.getValue(), "abc\ndef");
775 });
776
777 testCM("doubleScrollbar", function(cm) {
778 var dummy = document.body.appendChild(document.createElement("p"));
779 dummy.style.cssText = "height: 50px; overflow: scroll; width: 50px";
780 var scrollbarWidth = dummy.offsetWidth + 1 - dummy.clientWidth;
781 document.body.removeChild(dummy);
782 if (scrollbarWidth < 2) return;
783 cm.setSize(null, 100);
784 addDoc(cm, 1, 300);
785 var wrap = cm.getWrapperElement();
786 is(wrap.offsetWidth - byClassName(wrap, "CodeMirror-lines")[0].offsetWidth <= scrollbarWidth * 1.5);
787 });
788
789 testCM("weirdLinebreaks", function(cm) {
790 cm.setValue("foo\nbar\rbaz\r\nquux\n\rplop");
791 is(cm.getValue(), "foo\nbar\nbaz\nquux\n\nplop");
792 is(cm.lineCount(), 6);
793 cm.setValue("\n\n");
794 is(cm.lineCount(), 3);
795 });
796
797 testCM("setSize", function(cm) {
798 cm.setSize(100, 100);
799 var wrap = cm.getWrapperElement();
800 is(wrap.offsetWidth, 100);
801 is(wrap.offsetHeight, 100);
802 cm.setSize("100%", "3em");
803 is(wrap.style.width, "100%");
804 is(wrap.style.height, "3em");
805 cm.setSize(null, 40);
806 is(wrap.style.width, "100%");
807 is(wrap.style.height, "40px");
808 });
809
810 function foldLines(cm, start, end, autoClear) {
811 return cm.markText(Pos(start, 0), Pos(end - 1), {
812 inclusiveLeft: true,
813 inclusiveRight: true,
814 collapsed: true,
815 clearOnEnter: autoClear
816 });
817 }
818
819 testCM("collapsedLines", function(cm) {
820 addDoc(cm, 4, 10);
821 var range = foldLines(cm, 4, 5), cleared = 0;
822 CodeMirror.on(range, "clear", function() {cleared++;});
823 cm.setCursor(Pos(3, 0));
824 CodeMirror.commands.goLineDown(cm);
825 eqCharPos(cm.getCursor(), Pos(5, 0));
826 cm.replaceRange("abcdefg", Pos(3, 0), Pos(3));
827 cm.setCursor(Pos(3, 6));
828 CodeMirror.commands.goLineDown(cm);
829 eqCharPos(cm.getCursor(), Pos(5, 4));
830 cm.replaceRange("ab", Pos(3, 0), Pos(3));
831 cm.setCursor(Pos(3, 2));
832 CodeMirror.commands.goLineDown(cm);
833 eqCharPos(cm.getCursor(), Pos(5, 2));
834 cm.operation(function() {range.clear(); range.clear();});
835 eq(cleared, 1);
836 });
837
838 testCM("collapsedRangeCoordsChar", function(cm) {
839 var pos_1_3 = cm.charCoords(Pos(1, 3));
840 pos_1_3.left += 2; pos_1_3.top += 2;
841 var opts = {collapsed: true, inclusiveLeft: true, inclusiveRight: true};
842 var m1 = cm.markText(Pos(0, 0), Pos(2, 0), opts);
843 eqCharPos(cm.coordsChar(pos_1_3), Pos(3, 3));
844 m1.clear();
845 var m1 = cm.markText(Pos(0, 0), Pos(1, 1), {collapsed: true, inclusiveLeft: true});
846 var m2 = cm.markText(Pos(1, 1), Pos(2, 0), {collapsed: true, inclusiveRight: true});
847 eqCharPos(cm.coordsChar(pos_1_3), Pos(3, 3));
848 m1.clear(); m2.clear();
849 var m1 = cm.markText(Pos(0, 0), Pos(1, 6), opts);
850 eqCharPos(cm.coordsChar(pos_1_3), Pos(3, 3));
851 }, {value: "123456\nabcdef\nghijkl\nmnopqr\n"});
852
853 testCM("collapsedRangeBetweenLinesSelected", function(cm) {
854 if (cm.getOption("inputStyle") != "textarea") return;
855 var widget = document.createElement("span");
856 widget.textContent = "\u2194";
857 cm.markText(Pos(0, 3), Pos(1, 0), {replacedWith: widget});
858 cm.setSelection(Pos(0, 3), Pos(1, 0));
859 var selElts = byClassName(cm.getWrapperElement(), "CodeMirror-selected");
860 for (var i = 0, w = 0; i < selElts.length; i++)
861 w += selElts[i].offsetWidth;
862 is(w > 0);
863 }, {value: "one\ntwo"});
864
865 testCM("randomCollapsedRanges", function(cm) {
866 addDoc(cm, 20, 500);
867 cm.operation(function() {
868 for (var i = 0; i < 200; i++) {
869 var start = Pos(Math.floor(Math.random() * 500), Math.floor(Math.random() * 20));
870 if (i % 4)
871 try { cm.markText(start, Pos(start.line + 2, 1), {collapsed: true}); }
872 catch(e) { if (!/overlapping/.test(String(e))) throw e; }
873 else
874 cm.markText(start, Pos(start.line, start.ch + 4), {"className": "foo"});
875 }
876 });
877 });
878
879 testCM("hiddenLinesAutoUnfold", function(cm) {
880 var range = foldLines(cm, 1, 3, true), cleared = 0;
881 CodeMirror.on(range, "clear", function() {cleared++;});
882 cm.setCursor(Pos(3, 0));
883 eq(cleared, 0);
884 cm.execCommand("goCharLeft");
885 eq(cleared, 1);
886 range = foldLines(cm, 1, 3, true);
887 CodeMirror.on(range, "clear", function() {cleared++;});
888 eqCursorPos(cm.getCursor(), Pos(3, 0));
889 cm.setCursor(Pos(0, 3));
890 cm.execCommand("goCharRight");
891 eq(cleared, 2);
892 }, {value: "abc\ndef\nghi\njkl"});
893
894 testCM("hiddenLinesSelectAll", function(cm) { // Issue #484
895 addDoc(cm, 4, 20);
896 foldLines(cm, 0, 10);
897 foldLines(cm, 11, 20);
898 CodeMirror.commands.selectAll(cm);
899 eqCursorPos(cm.getCursor(true), Pos(10, 0));
900 eqCursorPos(cm.getCursor(false), Pos(10, 4));
901 });
902
903 testCM("clickFold", function(cm) { // Issue #5392
904 cm.setValue("foo { bar }")
905 var widget = document.createElement("span")
906 widget.textContent = "<>"
907 cm.markText(Pos(0, 5), Pos(0, 10), {replacedWith: widget})
908 var after = cm.charCoords(Pos(0, 10))
909 var foundOn = cm.coordsChar({left: after.left - 1, top: after.top + 4})
910 is(foundOn.ch <= 5 || foundOn.ch >= 10, "Position is not inside the folded range")
911 })
912
913 testCM("everythingFolded", function(cm) {
914 addDoc(cm, 2, 2);
915 function enterPress() {
916 cm.triggerOnKeyDown({type: "keydown", keyCode: 13, preventDefault: function(){}, stopPropagation: function(){}});
917 }
918 var fold = foldLines(cm, 0, 2);
919 enterPress();
920 eq(cm.getValue(), "xx\nxx");
921 fold.clear();
922 fold = foldLines(cm, 0, 2, true);
923 eq(fold.find(), null);
924 enterPress();
925 eq(cm.getValue(), "\nxx\nxx");
926 });
927
928 testCM("structuredFold", function(cm) {
929 addDoc(cm, 4, 8);
930 var range = cm.markText(Pos(1, 2), Pos(6, 2), {
931 replacedWith: document.createTextNode("Q")
932 });
933 cm.setCursor(0, 3);
934 CodeMirror.commands.goLineDown(cm);
935 eqCharPos(cm.getCursor(), Pos(6, 2));
936 CodeMirror.commands.goCharLeft(cm);
937 eqCharPos(cm.getCursor(), Pos(1, 2));
938 CodeMirror.commands.delCharAfter(cm);
939 eq(cm.getValue(), "xxxx\nxxxx\nxxxx");
940 addDoc(cm, 4, 8);
941 range = cm.markText(Pos(1, 2), Pos(6, 2), {
942 replacedWith: document.createTextNode("M"),
943 clearOnEnter: true
944 });
945 var cleared = 0;
946 CodeMirror.on(range, "clear", function(){++cleared;});
947 cm.setCursor(0, 3);
948 CodeMirror.commands.goLineDown(cm);
949 eqCharPos(cm.getCursor(), Pos(6, 2));
950 CodeMirror.commands.goCharLeft(cm);
951 eqCharPos(cm.getCursor(), Pos(6, 1));
952 eq(cleared, 1);
953 range.clear();
954 eq(cleared, 1);
955 range = cm.markText(Pos(1, 2), Pos(6, 2), {
956 replacedWith: document.createTextNode("Q"),
957 clearOnEnter: true
958 });
959 range.clear();
960 cm.setCursor(1, 2);
961 CodeMirror.commands.goCharRight(cm);
962 eqCharPos(cm.getCursor(), Pos(1, 3));
963 range = cm.markText(Pos(2, 0), Pos(4, 4), {
964 replacedWith: document.createTextNode("M")
965 });
966 cm.setCursor(1, 0);
967 CodeMirror.commands.goLineDown(cm);
968 eqCharPos(cm.getCursor(), Pos(2, 0));
969 }, null);
970
971 testCM("nestedFold", function(cm) {
972 addDoc(cm, 10, 3);
973 function fold(ll, cl, lr, cr) {
974 return cm.markText(Pos(ll, cl), Pos(lr, cr), {collapsed: true});
975 }
976 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);
977 cm.setCursor(0, 1);
978 CodeMirror.commands.goCharRight(cm);
979 eqCursorPos(cm.getCursor(), Pos(2, 3));
980 inner0.clear();
981 CodeMirror.commands.goCharLeft(cm);
982 eqCursorPos(cm.getCursor(), Pos(0, 1));
983 outer.clear();
984 CodeMirror.commands.goCharRight(cm);
985 eqCursorPos(cm.getCursor(), Pos(0, 2, "before"));
986 CodeMirror.commands.goCharRight(cm);
987 eqCursorPos(cm.getCursor(), Pos(1, 8));
988 inner2.clear();
989 CodeMirror.commands.goCharLeft(cm);
990 eqCursorPos(cm.getCursor(), Pos(1, 7, "after"));
991 cm.setCursor(0, 5);
992 CodeMirror.commands.goCharRight(cm);
993 eqCursorPos(cm.getCursor(), Pos(0, 6, "before"));
994 CodeMirror.commands.goCharRight(cm);
995 eqCursorPos(cm.getCursor(), Pos(1, 3));
996 });
997
998 testCM("badNestedFold", function(cm) {
999 addDoc(cm, 4, 4);
1000 cm.markText(Pos(0, 2), Pos(3, 2), {collapsed: true});
1001 var caught;
1002 try {cm.markText(Pos(0, 1), Pos(0, 3), {collapsed: true});}
1003 catch(e) {caught = e;}
1004 is(caught instanceof Error, "no error");
1005 is(/overlap/i.test(caught.message), "wrong error");
1006 });
1007
1008 testCM("nestedFoldOnSide", function(cm) {
1009 var m1 = cm.markText(Pos(0, 1), Pos(2, 1), {collapsed: true, inclusiveRight: true});
1010 var m2 = cm.markText(Pos(0, 1), Pos(0, 2), {collapsed: true});
1011 cm.markText(Pos(0, 1), Pos(0, 2), {collapsed: true}).clear();
1012 try { cm.markText(Pos(0, 1), Pos(0, 2), {collapsed: true, inclusiveLeft: true}); }
1013 catch(e) { var caught = e; }
1014 is(caught && /overlap/i.test(caught.message));
1015 var m3 = cm.markText(Pos(2, 0), Pos(2, 1), {collapsed: true});
1016 var m4 = cm.markText(Pos(2, 0), Pos(2, 1), {collapse: true, inclusiveRight: true});
1017 m1.clear(); m4.clear();
1018 m1 = cm.markText(Pos(0, 1), Pos(2, 1), {collapsed: true});
1019 cm.markText(Pos(2, 0), Pos(2, 1), {collapsed: true}).clear();
1020 try { cm.markText(Pos(2, 0), Pos(2, 1), {collapsed: true, inclusiveRight: true}); }
1021 catch(e) { var caught = e; }
1022 is(caught && /overlap/i.test(caught.message));
1023 }, {value: "ab\ncd\ef"});
1024
1025 testCM("editInFold", function(cm) {
1026 addDoc(cm, 4, 6);
1027 var m = cm.markText(Pos(1, 2), Pos(3, 2), {collapsed: true});
1028 cm.replaceRange("", Pos(0, 0), Pos(1, 3));
1029 cm.replaceRange("", Pos(2, 1), Pos(3, 3));
1030 cm.replaceRange("a\nb\nc\nd", Pos(0, 1), Pos(1, 0));
1031 cm.cursorCoords(Pos(0, 0));
1032 });
1033
1034 testCM("wrappingInlineWidget", function(cm) {
1035 cm.setSize("11em");
1036 var w = document.createElement("span");
1037 w.style.color = "red";
1038 w.innerHTML = "one two three four";
1039 cm.markText(Pos(0, 6), Pos(0, 9), {replacedWith: w});
1040 var cur0 = cm.cursorCoords(Pos(0, 0)), cur1 = cm.cursorCoords(Pos(0, 10));
1041 is(cur0.top < cur1.top);
1042 is(cur0.bottom < cur1.bottom);
1043 var curL = cm.cursorCoords(Pos(0, 6)), curR = cm.cursorCoords(Pos(0, 9));
1044 eq(curL.top, cur0.top);
1045 eq(curL.bottom, cur0.bottom);
1046 eq(curR.top, cur1.top);
1047 eq(curR.bottom, cur1.bottom);
1048 cm.replaceRange("", Pos(0, 9), Pos(0));
1049 curR = cm.cursorCoords(Pos(0, 9));
1050 eq(curR.top, cur1.top);
1051 eq(curR.bottom, cur1.bottom);
1052 }, {value: "1 2 3 xxx 4", lineWrapping: true});
1053
1054 testCM("showEmptyWidgetSpan", function(cm) {
1055 var marker = cm.markText(Pos(0, 2), Pos(0, 2), {
1056 clearWhenEmpty: false,
1057 replacedWith: document.createTextNode("X")
1058 });
1059 var text = cm.display.view[0].text;
1060 eq(text.textContent || text.innerText, "abXc");
1061 }, {value: "abc"});
1062
1063 testCM("changedInlineWidget", function(cm) {
1064 cm.setSize("10em");
1065 var w = document.createElement("span");
1066 w.innerHTML = "x";
1067 var m = cm.markText(Pos(0, 4), Pos(0, 5), {replacedWith: w});
1068 w.innerHTML = "and now the widget is really really long all of a sudden and a scrollbar is needed";
1069 m.changed();
1070 var hScroll = byClassName(cm.getWrapperElement(), "CodeMirror-hscrollbar")[0];
1071 is(hScroll.scrollWidth > hScroll.clientWidth);
1072 }, {value: "hello there"});
1073
1074 testCM("changedBookmark", function(cm) {
1075 cm.setSize("10em");
1076 var w = document.createElement("span");
1077 w.innerHTML = "x";
1078 var m = cm.setBookmark(Pos(0, 4), {widget: w});
1079 w.innerHTML = "and now the widget is really really long all of a sudden and a scrollbar is needed";
1080 m.changed();
1081 var hScroll = byClassName(cm.getWrapperElement(), "CodeMirror-hscrollbar")[0];
1082 is(hScroll.scrollWidth > hScroll.clientWidth);
1083 }, {value: "abcdefg"});
1084
1085 testCM("inlineWidget", function(cm) {
1086 var w = cm.setBookmark(Pos(0, 2), {widget: document.createTextNode("uu")});
1087 cm.setCursor(0, 2);
1088 CodeMirror.commands.goLineDown(cm);
1089 eqCharPos(cm.getCursor(), Pos(1, 4));
1090 cm.setCursor(0, 2);
1091 cm.replaceSelection("hi");
1092 eqCharPos(w.find(), Pos(0, 2));
1093 cm.setCursor(0, 1);
1094 cm.replaceSelection("ay");
1095 eqCharPos(w.find(), Pos(0, 4));
1096 eq(cm.getLine(0), "uayuhiuu");
1097 }, {value: "uuuu\nuuuuuu"});
1098
1099 testCM("wrappingAndResizing", function(cm) {
1100 cm.setSize(null, "auto");
1101 cm.setOption("lineWrapping", true);
1102 var wrap = cm.getWrapperElement(), h0 = wrap.offsetHeight;
1103 var doc = "xxx xxx xxx xxx xxx";
1104 cm.setValue(doc);
1105 for (var step = 10, w = cm.charCoords(Pos(0, 18), "div").right;; w += step) {
1106 cm.setSize(w);
1107 if (wrap.offsetHeight <= h0 * (opera_lt10 ? 1.2 : 1.5)) {
1108 if (step == 10) { w -= 10; step = 1; }
1109 else break;
1110 }
1111 }
1112 // Ensure that putting the cursor at the end of the maximally long
1113 // line doesn't cause wrapping to happen.
1114 cm.setCursor(Pos(0, doc.length));
1115 eq(wrap.offsetHeight, h0);
1116 cm.replaceSelection("x");
1117 is(wrap.offsetHeight > h0, "wrapping happens");
1118 // Now add a max-height and, in a document consisting of
1119 // almost-wrapped lines, go over it so that a scrollbar appears.
1120 cm.setValue(doc + "\n" + doc + "\n");
1121 cm.getScrollerElement().style.maxHeight = "100px";
1122 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));
1123 forEach([Pos(0, doc.length), Pos(0, doc.length - 1),
1124 Pos(0, 0), Pos(1, doc.length), Pos(1, doc.length - 1)],
1125 function(pos) {
1126 var coords = cm.charCoords(pos);
1127 eqCharPos(pos, cm.coordsChar({left: coords.left + 2, top: coords.top + 5}));
1128 });
1129 }, null, ie_lt8);
1130
1131 testCM("measureEndOfLine", function(cm) {
1132 cm.setSize(null, "auto");
1133 var inner = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild;
1134 var lh = inner.offsetHeight;
1135 for (var step = 10, w = cm.charCoords(Pos(0, 7), "div").right;; w += step) {
1136 cm.setSize(w);
1137 if (inner.offsetHeight < 2.5 * lh) {
1138 if (step == 10) { w -= 10; step = 1; }
1139 else break;
1140 }
1141 }
1142 cm.setValue(cm.getValue() + "\n\n");
1143 var endPos = cm.charCoords(Pos(0, 18), "local");
1144 is(endPos.top > lh * .8, "not at top");
1145 is(endPos.left > w - 20, "at right");
1146 endPos = cm.charCoords(Pos(0, 18));
1147 eqCursorPos(cm.coordsChar({left: endPos.left, top: endPos.top + 5}), Pos(0, 18, "before"));
1148
1149 var wrapPos = cm.cursorCoords(Pos(0, 9, "before"));
1150 is(wrapPos.top < endPos.top, "wrapPos is actually in first line");
1151 eqCursorPos(cm.coordsChar({left: wrapPos.left + 10, top: wrapPos.top}), Pos(0, 9, "before"));
1152 }, {mode: "text/html", value: "<!-- foo barrr -->", lineWrapping: true}, ie_lt8 || opera_lt10);
1153
1154 testCM("measureWrappedEndOfLine", function(cm) {
1155 cm.setSize(null, "auto");
1156 var inner = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild;
1157 var lh = inner.offsetHeight;
1158 for (var step = 10, w = cm.charCoords(Pos(0, 7), "div").right;; w += step) {
1159 cm.setSize(w);
1160 if (inner.offsetHeight < 2.5 * lh) {
1161 if (step == 10) { w -= 10; step = 1; }
1162 else break;
1163 }
1164 }
1165 for (var i = 0; i < 3; ++i) {
1166 var endPos = cm.charCoords(Pos(0, 12)); // Next-to-last since last would wrap (#1862)
1167 endPos.left += w; // Add width of editor just to be sure that we are behind last character
1168 eqCursorPos(cm.coordsChar(endPos), Pos(0, 13, "before"));
1169 endPos.left += w * 100;
1170 eqCursorPos(cm.coordsChar(endPos), Pos(0, 13, "before"));
1171 cm.setValue("0123456789abcابجابجابجابج");
1172 if (i == 1) {
1173 var node = document.createElement("div");
1174 node.innerHTML = "hi"; node.style.height = "30px";
1175 cm.addLineWidget(0, node, {above: true});
1176 }
1177 }
1178 }, {mode: "text/html", value: "0123456789abcde0123456789", lineWrapping: true}, ie_lt8 || opera_lt10);
1179
1180 testCM("measureEndOfLineBidi", function(cm) {
1181 eqCursorPos(cm.coordsChar({left: 5000, top: cm.charCoords(Pos(0, 0)).top}), Pos(0, 8, "after"))
1182 }, {value: "إإإإuuuuإإإإ"})
1183
1184 testCM("measureWrappedBidiLevel2", function(cm) {
1185 cm.setSize(cm.charCoords(Pos(0, 6), "editor").right + 60)
1186 var c9 = cm.charCoords(Pos(0, 9))
1187 eqCharPos(cm.coordsChar({left: c9.right - 1, top: c9.top + 1}), Pos(0, 9))
1188 }, {value: "foobar إإ إإ إإ إإ 555 بببببب", lineWrapping: true})
1189
1190 testCM("measureWrappedBeginOfLine", function(cm) {
1191 cm.setSize(null, "auto");
1192 var inner = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild;
1193 var lh = inner.offsetHeight;
1194 for (var step = 10, w = cm.charCoords(Pos(0, 7), "div").right;; w += step) {
1195 cm.setSize(w);
1196 if (inner.offsetHeight < 2.5 * lh) {
1197 if (step == 10) { w -= 10; step = 1; }
1198 else break;
1199 }
1200 }
1201 var beginOfSecondLine = Pos(0, 13, "after");
1202 for (var i = 0; i < 2; ++i) {
1203 var beginPos = cm.charCoords(Pos(0, 0));
1204 beginPos.left -= w;
1205 eqCursorPos(cm.coordsChar(beginPos), Pos(0, 0, "after"));
1206 beginPos = cm.cursorCoords(beginOfSecondLine);
1207 beginPos.left = 0;
1208 eqCursorPos(cm.coordsChar(beginPos), beginOfSecondLine);
1209 cm.setValue("0123456789abcابجابجابجابج");
1210 beginOfSecondLine = Pos(0, 25, "before");
1211 }
1212 }, {mode: "text/html", value: "0123456789abcde0123456789", lineWrapping: true});
1213
1214 testCM("scrollVerticallyAndHorizontally", function(cm) {
1215 if (cm.getOption("inputStyle") != "textarea") return;
1216 cm.setSize(100, 100);
1217 addDoc(cm, 40, 40);
1218 cm.setCursor(39);
1219 var wrap = cm.getWrapperElement(), bar = byClassName(wrap, "CodeMirror-vscrollbar")[0];
1220 is(bar.offsetHeight < wrap.offsetHeight, "vertical scrollbar limited by horizontal one");
1221 var cursorBox = byClassName(wrap, "CodeMirror-cursor")[0].getBoundingClientRect();
1222 var editorBox = wrap.getBoundingClientRect();
1223 is(cursorBox.bottom < editorBox.top + cm.getScrollerElement().clientHeight,
1224 "bottom line visible");
1225 }, {lineNumbers: true});
1226
1227 testCM("moveVstuck", function(cm) {
1228 var lines = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild, h0 = lines.offsetHeight;
1229 var val = "fooooooooooooooooooooooooo baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar\n";
1230 cm.setValue(val);
1231 for (var w = cm.charCoords(Pos(0, 26), "div").right * 2.8;; w += 5) {
1232 cm.setSize(w);
1233 if (lines.offsetHeight <= 3.5 * h0) break;
1234 }
1235 cm.setCursor(Pos(0, val.length - 1));
1236 cm.moveV(-1, "line");
1237 eqCursorPos(cm.getCursor(), Pos(0, 27, "before"));
1238 is(cm.cursorCoords(null, "local").top < h0, "cursor is in first visual line");
1239 }, {lineWrapping: true}, ie_lt8 || opera_lt10);
1240
1241 testCM("collapseOnMove", function(cm) {
1242 cm.setSelection(Pos(0, 1), Pos(2, 4));
1243 cm.execCommand("goLineUp");
1244 is(!cm.somethingSelected());
1245 eqCharPos(cm.getCursor(), Pos(0, 1));
1246 cm.setSelection(Pos(0, 1), Pos(2, 4));
1247 cm.execCommand("goPageDown");
1248 is(!cm.somethingSelected());
1249 eqCharPos(cm.getCursor(), Pos(2, 4));
1250 cm.execCommand("goLineUp");
1251 cm.execCommand("goLineUp");
1252 eqCharPos(cm.getCursor(), Pos(0, 4));
1253 cm.setSelection(Pos(0, 1), Pos(2, 4));
1254 cm.execCommand("goCharLeft");
1255 is(!cm.somethingSelected());
1256 eqCharPos(cm.getCursor(), Pos(0, 1));
1257 }, {value: "aaaaa\nb\nccccc"});
1258
1259 testCM("clickTab", function(cm) {
1260 var p0 = cm.charCoords(Pos(0, 0));
1261 eqCharPos(cm.coordsChar({left: p0.left + 5, top: p0.top + 5}), Pos(0, 0));
1262 eqCharPos(cm.coordsChar({left: p0.right - 5, top: p0.top + 5}), Pos(0, 1));
1263 }, {value: "\t\n\n", lineWrapping: true, tabSize: 8});
1264
1265 testCM("verticalScroll", function(cm) {
1266 cm.setSize(100, 200);
1267 cm.setValue("foo\nbar\nbaz\n");
1268 var sc = cm.getScrollerElement(), baseWidth = sc.scrollWidth;
1269 cm.replaceRange("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah", Pos(0, 0), Pos(0));
1270 is(sc.scrollWidth > baseWidth, "scrollbar present");
1271 cm.replaceRange("foo", Pos(0, 0), Pos(0));
1272 eq(sc.scrollWidth, baseWidth, "scrollbar gone");
1273 cm.replaceRange("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah", Pos(0, 0), Pos(0));
1274 cm.replaceRange("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbh", Pos(1, 0), Pos(1));
1275 is(sc.scrollWidth > baseWidth, "present again");
1276 var curWidth = sc.scrollWidth;
1277 cm.replaceRange("foo", Pos(0, 0), Pos(0));
1278 is(sc.scrollWidth < curWidth, "scrollbar smaller");
1279 is(sc.scrollWidth > baseWidth, "but still present");
1280 });
1281
1282 testCM("extraKeys", function(cm) {
1283 var outcome;
1284 function fakeKey(expected, code, props) {
1285 if (typeof code == "string") code = code.charCodeAt(0);
1286 var e = {type: "keydown", keyCode: code, preventDefault: function(){}, stopPropagation: function(){}};
1287 if (props) for (var n in props) e[n] = props[n];
1288 outcome = null;
1289 cm.triggerOnKeyDown(e);
1290 eq(outcome, expected);
1291 }
1292 CodeMirror.commands.testCommand = function() {outcome = "tc";};
1293 CodeMirror.commands.goTestCommand = function() {outcome = "gtc";};
1294 cm.setOption("extraKeys", {"Shift-X": function() {outcome = "sx";},
1295 "X": function() {outcome = "x";},
1296 "Ctrl-Alt-U": function() {outcome = "cau";},
1297 "End": "testCommand",
1298 "Home": "goTestCommand",
1299 "Tab": false});
1300 fakeKey(null, "U");
1301 fakeKey("cau", "U", {ctrlKey: true, altKey: true});
1302 fakeKey(null, "U", {shiftKey: true, ctrlKey: true, altKey: true});
1303 fakeKey("x", "X");
1304 fakeKey("sx", "X", {shiftKey: true});
1305 fakeKey("tc", 35);
1306 fakeKey(null, 35, {shiftKey: true});
1307 fakeKey("gtc", 36);
1308 fakeKey("gtc", 36, {shiftKey: true});
1309 fakeKey(null, 9);
1310 }, null, window.opera && mac);
1311
1312 testCM("wordMovementCommands", function(cm) {
1313 cm.execCommand("goWordLeft");
1314 eqCursorPos(cm.getCursor(), Pos(0, 0));
1315 cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
1316 eqCursorPos(cm.getCursor(), Pos(0, 7, "before"));
1317 cm.execCommand("goWordLeft");
1318 eqCursorPos(cm.getCursor(), Pos(0, 5, "after"));
1319 cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
1320 eqCursorPos(cm.getCursor(), Pos(0, 12, "before"));
1321 cm.execCommand("goWordLeft");
1322 eqCursorPos(cm.getCursor(), Pos(0, 9, "after"));
1323 cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
1324 eqCursorPos(cm.getCursor(), Pos(0, 24, "before"));
1325 cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
1326 eqCursorPos(cm.getCursor(), Pos(1, 9, "before"));
1327 cm.execCommand("goWordRight");
1328 eqCursorPos(cm.getCursor(), Pos(1, 13, "before"));
1329 cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
1330 eqCharPos(cm.getCursor(), Pos(2, 0));
1331 }, {value: "this is (the) firstline.\na foo12\u00e9\u00f8\u00d7bar\n"});
1332
1333 testCM("groupMovementCommands", function(cm) {
1334 cm.execCommand("goGroupLeft");
1335 eqCursorPos(cm.getCursor(), Pos(0, 0));
1336 cm.execCommand("goGroupRight");
1337 eqCursorPos(cm.getCursor(), Pos(0, 4, "before"));
1338 cm.execCommand("goGroupRight");
1339 eqCursorPos(cm.getCursor(), Pos(0, 7, "before"));
1340 cm.execCommand("goGroupRight");
1341 eqCursorPos(cm.getCursor(), Pos(0, 10, "before"));
1342 cm.execCommand("goGroupLeft");
1343 eqCursorPos(cm.getCursor(), Pos(0, 7, "after"));
1344 cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight");
1345 eqCursorPos(cm.getCursor(), Pos(0, 15, "before"));
1346 cm.setCursor(Pos(0, 17));
1347 cm.execCommand("goGroupLeft");
1348 eqCursorPos(cm.getCursor(), Pos(0, 16, "after"));
1349 cm.execCommand("goGroupLeft");
1350 eqCursorPos(cm.getCursor(), Pos(0, 14, "after"));
1351 cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight");
1352 eqCursorPos(cm.getCursor(), Pos(0, 20, "before"));
1353 cm.execCommand("goGroupRight");
1354 eqCursorPos(cm.getCursor(), Pos(1, 0, "after"));
1355 cm.execCommand("goGroupRight");
1356 eqCursorPos(cm.getCursor(), Pos(1, 2, "before"));
1357 cm.execCommand("goGroupRight");
1358 eqCursorPos(cm.getCursor(), Pos(1, 5, "before"));
1359 cm.execCommand("goGroupLeft"); cm.execCommand("goGroupLeft");
1360 eqCursorPos(cm.getCursor(), Pos(1, 0, "after"));
1361 cm.execCommand("goGroupLeft");
1362 eqCursorPos(cm.getCursor(), Pos(0, 20, "after"));
1363 cm.execCommand("goGroupLeft");
1364 eqCursorPos(cm.getCursor(), Pos(0, 16, "after"));
1365 }, {value: "booo ba---quux. ffff\n abc d"});
1366
1367 testCM("groupsAndWhitespace", function(cm) {
1368 var positions = [Pos(0, 0), Pos(0, 2), Pos(0, 5), Pos(0, 9), Pos(0, 11),
1369 Pos(1, 0), Pos(1, 2), Pos(1, 5)];
1370 for (var i = 1; i < positions.length; i++) {
1371 cm.execCommand("goGroupRight");
1372 eqCharPos(cm.getCursor(), positions[i]);
1373 }
1374 for (var i = positions.length - 2; i >= 0; i--) {
1375 cm.execCommand("goGroupLeft");
1376 eqCharPos(cm.getCursor(), i == 2 ? Pos(0, 6, "before") : positions[i]);
1377 }
1378 }, {value: " foo +++ \n bar"});
1379
1380 testCM("charMovementCommands", function(cm) {
1381 cm.execCommand("goCharLeft"); cm.execCommand("goColumnLeft");
1382 eqCursorPos(cm.getCursor(), Pos(0, 0));
1383 cm.execCommand("goCharRight"); cm.execCommand("goCharRight");
1384 eqCursorPos(cm.getCursor(), Pos(0, 2, "before"));
1385 cm.setCursor(Pos(1, 0));
1386 cm.execCommand("goColumnLeft");
1387 eqCursorPos(cm.getCursor(), Pos(1, 0));
1388 cm.execCommand("goCharLeft");
1389 eqCursorPos(cm.getCursor(), Pos(0, 5, "before"));
1390 cm.execCommand("goColumnRight");
1391 eqCursorPos(cm.getCursor(), Pos(0, 5, "before"));
1392 cm.execCommand("goCharRight");
1393 eqCursorPos(cm.getCursor(), Pos(1, 0, "after"));
1394 cm.execCommand("goLineEnd");
1395 eqCursorPos(cm.getCursor(), Pos(1, 5, "before"));
1396 cm.execCommand("goLineStartSmart");
1397 eqCursorPos(cm.getCursor(), Pos(1, 1, "after"));
1398 cm.execCommand("goLineStartSmart");
1399 eqCursorPos(cm.getCursor(), Pos(1, 0, "after"));
1400 cm.setCursor(Pos(2, 0));
1401 cm.execCommand("goCharRight"); cm.execCommand("goColumnRight");
1402 eqCursorPos(cm.getCursor(), Pos(2, 0));
1403 }, {value: "line1\n ine2\n"});
1404
1405 testCM("verticalMovementCommands", function(cm) {
1406 cm.execCommand("goLineUp");
1407 eqCharPos(cm.getCursor(), Pos(0, 0));
1408 cm.execCommand("goLineDown");
1409 eqCharPos(cm.getCursor(), Pos(1, 0));
1410 cm.setCursor(Pos(1, 12));
1411 cm.execCommand("goLineDown");
1412 eqCharPos(cm.getCursor(), Pos(2, 5));
1413 cm.execCommand("goLineDown");
1414 eqCharPos(cm.getCursor(), Pos(3, 0));
1415 cm.execCommand("goLineUp");
1416 eqCharPos(cm.getCursor(), Pos(2, 5));
1417 cm.execCommand("goLineUp");
1418 eqCharPos(cm.getCursor(), Pos(1, 12));
1419 cm.execCommand("goPageDown");
1420 eqCharPos(cm.getCursor(), Pos(5, 0));
1421 cm.execCommand("goPageDown"); cm.execCommand("goLineDown");
1422 eqCharPos(cm.getCursor(), Pos(5, 0));
1423 cm.execCommand("goPageUp");
1424 eqCharPos(cm.getCursor(), Pos(0, 0));
1425 }, {value: "line1\nlong long line2\nline3\n\nline5\n"});
1426
1427 testCM("verticalMovementCommandsWrapping", function(cm) {
1428 cm.setSize(120);
1429 cm.setCursor(Pos(0, 5));
1430 cm.execCommand("goLineDown");
1431 eq(cm.getCursor().line, 0);
1432 is(cm.getCursor().ch > 5, "moved beyond wrap");
1433 for (var i = 0; ; ++i) {
1434 is(i < 20, "no endless loop");
1435 cm.execCommand("goLineDown");
1436 var cur = cm.getCursor();
1437 if (cur.line == 1) eq(cur.ch, 5);
1438 if (cur.line == 2) { eq(cur.ch, 1); break; }
1439 }
1440 }, {value: "a very long line that wraps around somehow so that we can test cursor movement\nshortone\nk",
1441 lineWrapping: true});
1442
1443 testCM("verticalMovementCommandsSingleLine", function(cm) {
1444 cm.display.wrapper.style.height = "auto";
1445 cm.refresh();
1446 cm.execCommand("goLineUp");
1447 eqCursorPos(cm.getCursor(), Pos(0, 0));
1448 cm.execCommand("goLineDown");
1449 eqCursorPos(cm.getCursor(), Pos(0, 11));
1450 cm.setCursor(Pos(0, 5));
1451 cm.execCommand("goLineDown");
1452 eqCursorPos(cm.getCursor(), Pos(0, 11));
1453 cm.execCommand("goLineDown");
1454 eqCursorPos(cm.getCursor(), Pos(0, 11));
1455 cm.execCommand("goLineUp");
1456 eqCursorPos(cm.getCursor(), Pos(0, 0));
1457 cm.execCommand("goLineUp");
1458 eqCursorPos(cm.getCursor(), Pos(0, 0));
1459 cm.execCommand("goPageDown");
1460 eqCursorPos(cm.getCursor(), Pos(0, 11));
1461 cm.execCommand("goPageDown"); cm.execCommand("goLineDown");
1462 eqCursorPos(cm.getCursor(), Pos(0, 11));
1463 cm.execCommand("goPageUp");
1464 eqCursorPos(cm.getCursor(), Pos(0, 0));
1465 cm.setCursor(Pos(0, 5));
1466 cm.execCommand("goPageUp");
1467 eqCursorPos(cm.getCursor(), Pos(0, 0));
1468 cm.setCursor(Pos(0, 5));
1469 cm.execCommand("goPageDown");
1470 eqCursorPos(cm.getCursor(), Pos(0, 11));
1471 }, {value: "single line"});
1472
1473
1474 testCM("rtlMovement", function(cm) {
1475 if (cm.getOption("inputStyle") != "textarea") return;
1476 forEach(["خحج", "خحabcخحج", "abخحخحجcd", "abخde", "abخح2342خ1حج", "خ1ح2خح3حxج",
1477 "خحcd", "1خحcd", "abcdeح1ج", "خمرحبها مها!", "foobarر", "خ ة ق",
1478 "<img src=\"/בדיקה3.jpg\">", "يتم السحب في 05 فبراير 2014"], function(line) {
1479 cm.setValue(line + "\n"); cm.execCommand("goLineStart");
1480 var cursors = byClassName(cm.getWrapperElement(), "CodeMirror-cursors")[0];
1481 var cursor = cursors.firstChild;
1482 var prevX = cursor.offsetLeft, prevY = cursor.offsetTop;
1483 for (var i = 0; i <= line.length; ++i) {
1484 cm.execCommand("goCharRight");
1485 cursor = cursors.firstChild;
1486 if (i == line.length) is(cursor.offsetTop > prevY, "next line");
1487 else is(cursor.offsetLeft > prevX, "moved right");
1488 prevX = cursor.offsetLeft; prevY = cursor.offsetTop;
1489 }
1490 cm.setCursor(0, 0); cm.execCommand("goLineEnd");
1491 prevX = cursors.firstChild.offsetLeft;
1492 for (var i = 0; i < line.length; ++i) {
1493 cm.execCommand("goCharLeft");
1494 cursor = cursors.firstChild;
1495 is(cursor.offsetLeft < prevX, "moved left");
1496 prevX = cursor.offsetLeft;
1497 }
1498 });
1499 }, null, ie_lt9);
1500
1501 // Verify that updating a line clears its bidi ordering
1502 testCM("bidiUpdate", function(cm) {
1503 cm.setCursor(Pos(0, 2, "before"));
1504 cm.replaceSelection("خحج", "start");
1505 cm.execCommand("goCharRight");
1506 eqCursorPos(cm.getCursor(), Pos(0, 6, "before"));
1507 }, {value: "abcd\n"});
1508
1509 testCM("movebyTextUnit", function(cm) {
1510 cm.setValue("בְּרֵאשִ\nééé́\n");
1511 cm.execCommand("goLineStart");
1512 for (var i = 0; i < 4; ++i) cm.execCommand("goCharRight");
1513 eqCursorPos(cm.getCursor(), Pos(0, 0, "after"));
1514 cm.execCommand("goCharRight");
1515 eqCursorPos(cm.getCursor(), Pos(1, 0, "after"));
1516 cm.execCommand("goCharRight");
1517 cm.execCommand("goCharRight");
1518 eqCursorPos(cm.getCursor(), Pos(1, 4, "before"));
1519 cm.execCommand("goCharRight");
1520 eqCursorPos(cm.getCursor(), Pos(1, 7, "before"));
1521 });
1522
1523 testCM("lineChangeEvents", function(cm) {
1524 addDoc(cm, 3, 5);
1525 var log = [], want = ["ch 0", "ch 1", "del 2", "ch 0", "ch 0", "del 1", "del 3", "del 4"];
1526 for (var i = 0; i < 5; ++i) {
1527 CodeMirror.on(cm.getLineHandle(i), "delete", function(i) {
1528 return function() {log.push("del " + i);};
1529 }(i));
1530 CodeMirror.on(cm.getLineHandle(i), "change", function(i) {
1531 return function() {log.push("ch " + i);};
1532 }(i));
1533 }
1534 cm.replaceRange("x", Pos(0, 1));
1535 cm.replaceRange("xy", Pos(1, 1), Pos(2));
1536 cm.replaceRange("foo\nbar", Pos(0, 1));
1537 cm.replaceRange("", Pos(0, 0), Pos(cm.lineCount()));
1538 eq(log.length, want.length, "same length");
1539 for (var i = 0; i < log.length; ++i)
1540 eq(log[i], want[i]);
1541 });
1542
1543 testCM("scrollEntirelyToRight", function(cm) {
1544 if (cm.getOption("inputStyle") != "textarea") return;
1545 addDoc(cm, 500, 2);
1546 cm.setCursor(Pos(0, 500));
1547 var wrap = cm.getWrapperElement(), cur = byClassName(wrap, "CodeMirror-cursor")[0];
1548 is(wrap.getBoundingClientRect().right > cur.getBoundingClientRect().left);
1549 });
1550
1551 testCM("lineWidgets", function(cm) {
1552 addDoc(cm, 500, 3);
1553 var last = cm.charCoords(Pos(2, 0));
1554 var node = document.createElement("div");
1555 node.innerHTML = "hi";
1556 var widget = cm.addLineWidget(1, node);
1557 is(last.top < cm.charCoords(Pos(2, 0)).top, "took up space");
1558 cm.setCursor(Pos(1, 1));
1559 cm.execCommand("goLineDown");
1560 eqCharPos(cm.getCursor(), Pos(2, 1));
1561 cm.execCommand("goLineUp");
1562 eqCharPos(cm.getCursor(), Pos(1, 1));
1563 });
1564
1565 testCM("lineWidgetFocus", function(cm) {
1566 var place = document.getElementById("testground");
1567 place.className = "offscreen";
1568 try {
1569 addDoc(cm, 500, 10);
1570 var node = document.createElement("input");
1571 var widget = cm.addLineWidget(1, node);
1572 node.focus();
1573 eq(document.activeElement, node);
1574 cm.replaceRange("new stuff", Pos(1, 0));
1575 eq(document.activeElement, node);
1576 } finally {
1577 place.className = "";
1578 }
1579 });
1580
1581 testCM("lineWidgetCautiousRedraw", function(cm) {
1582 var node = document.createElement("div");
1583 node.innerHTML = "hahah";
1584 var w = cm.addLineWidget(0, node);
1585 var redrawn = false;
1586 w.on("redraw", function() { redrawn = true; });
1587 cm.replaceSelection("0");
1588 is(!redrawn);
1589 }, {value: "123\n456"});
1590
1591
1592 var knownScrollbarWidth;
1593 function scrollbarWidth(measure) {
1594 if (knownScrollbarWidth != null) return knownScrollbarWidth;
1595 var div = document.createElement('div');
1596 div.style.cssText = "width: 50px; height: 50px; overflow-x: scroll";
1597 document.body.appendChild(div);
1598 knownScrollbarWidth = div.offsetHeight - div.clientHeight;
1599 document.body.removeChild(div);
1600 return knownScrollbarWidth || 0;
1601 }
1602
1603 testCM("lineWidgetChanged", function(cm) {
1604 addDoc(cm, 2, 300);
1605 var halfScrollbarWidth = scrollbarWidth(cm.display.measure)/2;
1606 cm.setOption('lineNumbers', true);
1607 cm.setSize(600, cm.defaultTextHeight() * 50);
1608 cm.scrollTo(null, cm.heightAtLine(125, "local"));
1609
1610 var expectedWidgetHeight = 60;
1611 var expectedLinesInWidget = 3;
1612 function w() {
1613 var node = document.createElement("div");
1614 // we use these children with just under half width of the line to check measurements are made with correct width
1615 // when placed in the measure div.
1616 // If the widget is measured at a width much narrower than it is displayed at, the underHalf children will span two lines and break the test.
1617 // If the widget is measured at a width much wider than it is displayed at, the overHalf children will combine and break the test.
1618 // Note that this test only checks widgets where coverGutter is true, because these require extra styling to get the width right.
1619 // It may also be worthwhile to check this for non-coverGutter widgets.
1620 // Visually:
1621 // Good:
1622 // | ------------- display width ------------- |
1623 // | ------- widget-width when measured ------ |
1624 // | | -- under-half -- | | -- under-half -- | |
1625 // | | --- over-half --- | |
1626 // | | --- over-half --- | |
1627 // Height: measured as 3 lines, same as it will be when actually displayed
1628
1629 // Bad (too narrow):
1630 // | ------------- display width ------------- |
1631 // | ------ widget-width when measured ----- | < -- uh oh
1632 // | | -- under-half -- | |
1633 // | | -- under-half -- | | < -- when measured, shoved to next line
1634 // | | --- over-half --- | |
1635 // | | --- over-half --- | |
1636 // Height: measured as 4 lines, more than expected . Will be displayed as 3 lines!
1637
1638 // Bad (too wide):
1639 // | ------------- display width ------------- |
1640 // | -------- widget-width when measured ------- | < -- uh oh
1641 // | | -- under-half -- | | -- under-half -- | |
1642 // | | --- over-half --- | | --- over-half --- | | < -- when measured, combined on one line
1643 // Height: measured as 2 lines, less than expected. Will be displayed as 3 lines!
1644
1645 var barelyUnderHalfWidthHtml = '<div style="display: inline-block; height: 1px; width: '+(285 - halfScrollbarWidth)+'px;"></div>';
1646 var barelyOverHalfWidthHtml = '<div style="display: inline-block; height: 1px; width: '+(305 - halfScrollbarWidth)+'px;"></div>';
1647 node.innerHTML = new Array(3).join(barelyUnderHalfWidthHtml) + new Array(3).join(barelyOverHalfWidthHtml);
1648 node.style.cssText = "background: yellow;font-size:0;line-height: " + (expectedWidgetHeight/expectedLinesInWidget) + "px;";
1649 return node;
1650 }
1651 var info0 = cm.getScrollInfo();
1652 var w0 = cm.addLineWidget(0, w(), { coverGutter: true });
1653 var w150 = cm.addLineWidget(150, w(), { coverGutter: true });
1654 var w300 = cm.addLineWidget(300, w(), { coverGutter: true });
1655 var info1 = cm.getScrollInfo();
1656 eq(info0.height + (3 * expectedWidgetHeight), info1.height);
1657 eq(info0.top + expectedWidgetHeight, info1.top);
1658 expectedWidgetHeight = 12;
1659 w0.node.style.lineHeight = w150.node.style.lineHeight = w300.node.style.lineHeight = (expectedWidgetHeight/expectedLinesInWidget) + "px";
1660 w0.changed(); w150.changed(); w300.changed();
1661 var info2 = cm.getScrollInfo();
1662 eq(info0.height + (3 * expectedWidgetHeight), info2.height);
1663 eq(info0.top + expectedWidgetHeight, info2.top);
1664 });
1665
1666 testCM("lineWidgetIssue5486", function(cm) {
1667 // [prepare]
1668 // 2nd line is combined to 1st line due to markText
1669 // 2nd line has a lineWidget below
1670
1671 cm.setValue("Lorem\nIpsue\nDollar")
1672
1673 var el = document.createElement('div')
1674 el.style.height='50px'
1675 el.textContent = '[[LINE WIDGET]]'
1676
1677 var lineWidget = cm.addLineWidget(1, el, {
1678 above: false,
1679 coverGutter: false,
1680 noHScroll: false,
1681 showIfHidden: false,
1682 })
1683
1684 var marker = document.createElement('span')
1685 marker.textContent = '[--]'
1686
1687 cm.markText({line:0, ch: 1}, {line:1, ch: 4}, {
1688 replacedWith: marker
1689 })
1690
1691 // before resizing the lineWidget, measure 3rd line position
1692
1693 var measure_1 = Math.round(cm.charCoords({line:2, ch:0}).top)
1694
1695 // resize lineWidget, height + 50 px
1696
1697 el.style.height='100px'
1698 el.textContent += "\nlineWidget size changed.\nTry moving cursor to line 3?"
1699
1700 lineWidget.changed()
1701
1702 // re-measure 3rd line position
1703 var measure_2 = Math.round(cm.charCoords({line:2, ch:0}).top)
1704 eq(measure_2, measure_1 + 50)
1705
1706 // (extra test)
1707 //
1708 // add char to the right of the folded marker
1709 // and re-measure 3rd line position
1710
1711 cm.replaceRange('-', {line:1, ch: 5})
1712 var measure_3 = Math.round(cm.charCoords({line:2, ch:0}).top)
1713 eq(measure_3, measure_2)
1714 });
1715
1716 testCM("getLineNumber", function(cm) {
1717 addDoc(cm, 2, 20);
1718 var h1 = cm.getLineHandle(1);
1719 eq(cm.getLineNumber(h1), 1);
1720 cm.replaceRange("hi\nbye\n", Pos(0, 0));
1721 eq(cm.getLineNumber(h1), 3);
1722 cm.setValue("");
1723 eq(cm.getLineNumber(h1), null);
1724 });
1725
1726 testCM("jumpTheGap", function(cm) {
1727 var longLine = "abcdef ghiklmnop qrstuvw xyz ";
1728 longLine += longLine; longLine += longLine; longLine += longLine;
1729 cm.replaceRange(longLine, Pos(2, 0), Pos(2));
1730 cm.setSize("200px", null);
1731 cm.getWrapperElement().style.lineHeight = 2;
1732 cm.refresh();
1733 cm.setCursor(Pos(0, 1));
1734 cm.execCommand("goLineDown");
1735 eqCharPos(cm.getCursor(), Pos(1, 1));
1736 cm.execCommand("goLineDown");
1737 eqCharPos(cm.getCursor(), Pos(2, 1));
1738 cm.execCommand("goLineDown");
1739 eq(cm.getCursor().line, 2);
1740 is(cm.getCursor().ch > 1);
1741 cm.execCommand("goLineUp");
1742 eqCharPos(cm.getCursor(), Pos(2, 1));
1743 cm.execCommand("goLineUp");
1744 eqCharPos(cm.getCursor(), Pos(1, 1));
1745 var node = document.createElement("div");
1746 node.innerHTML = "hi"; node.style.height = "30px";
1747 cm.addLineWidget(0, node);
1748 cm.addLineWidget(1, node.cloneNode(true), {above: true});
1749 cm.setCursor(Pos(0, 2));
1750 cm.execCommand("goLineDown");
1751 eqCharPos(cm.getCursor(), Pos(1, 2));
1752 cm.execCommand("goLineUp");
1753 eqCharPos(cm.getCursor(), Pos(0, 2));
1754 }, {lineWrapping: true, value: "abc\ndef\nghi\njkl\n"});
1755
1756 testCM("addLineClass", function(cm) {
1757 function cls(line, text, bg, wrap, gutter) {
1758 var i = cm.lineInfo(line);
1759 eq(i.textClass, text);
1760 eq(i.bgClass, bg);
1761 eq(i.wrapClass, wrap);
1762 if (typeof i.handle.gutterClass !== 'undefined') {
1763 eq(i.handle.gutterClass, gutter);
1764 }
1765 }
1766 cm.addLineClass(0, "text", "foo");
1767 cm.addLineClass(0, "text", "bar");
1768 cm.addLineClass(1, "background", "baz");
1769 cm.addLineClass(1, "wrap", "foo");
1770 cm.addLineClass(1, "gutter", "gutter-class");
1771 cls(0, "foo bar", null, null, null);
1772 cls(1, null, "baz", "foo", "gutter-class");
1773 var lines = cm.display.lineDiv;
1774 eq(byClassName(lines, "foo").length, 2);
1775 eq(byClassName(lines, "bar").length, 1);
1776 eq(byClassName(lines, "baz").length, 1);
1777 eq(byClassName(lines, "gutter-class").length, 2); // Gutter classes are reflected in 2 nodes
1778 cm.removeLineClass(0, "text", "foo");
1779 cls(0, "bar", null, null, null);
1780 cm.removeLineClass(0, "text", "foo");
1781 cls(0, "bar", null, null, null);
1782 cm.removeLineClass(0, "text", "bar");
1783 cls(0, null, null, null);
1784
1785 cm.addLineClass(1, "wrap", "quux");
1786 cls(1, null, "baz", "foo quux", "gutter-class");
1787 cm.removeLineClass(1, "wrap");
1788 cls(1, null, "baz", null, "gutter-class");
1789 cm.removeLineClass(1, "gutter", "gutter-class");
1790 eq(byClassName(lines, "gutter-class").length, 0);
1791 cls(1, null, "baz", null, null);
1792
1793 cm.addLineClass(1, "gutter", "gutter-class");
1794 cls(1, null, "baz", null, "gutter-class");
1795 cm.removeLineClass(1, "gutter", "gutter-class");
1796 cls(1, null, "baz", null, null);
1797
1798 }, {value: "hohoho\n", lineNumbers: true});
1799
1800 testCM("atomicMarker", function(cm) {
1801 addDoc(cm, 10, 10);
1802
1803 function atom(ll, cl, lr, cr, li, ri, ls, rs) {
1804 var options = {
1805 atomic: true,
1806 inclusiveLeft: li,
1807 inclusiveRight: ri
1808 };
1809
1810 if (ls === true || ls === false) options.selectLeft = ls;
1811 if (rs === true || rs === false) options.selectRight = rs;
1812
1813 return cm.markText(Pos(ll, cl), Pos(lr, cr), options);
1814 }
1815
1816 // Can cursor to the left and right of a normal marker by jumping across it
1817 var m = atom(0, 1, 0, 5);
1818 cm.setCursor(Pos(0, 1));
1819 cm.execCommand("goCharRight");
1820 eqCursorPos(cm.getCursor(), Pos(0, 5));
1821 cm.execCommand("goCharLeft");
1822 eqCursorPos(cm.getCursor(), Pos(0, 1));
1823 m.clear();
1824
1825 // Can't cursor to the left of a marker when inclusiveLeft=true
1826 m = atom(0, 0, 0, 5, true);
1827 eqCursorPos(cm.getCursor(), Pos(0, 5), "pushed out");
1828 cm.execCommand("goCharLeft");
1829 eqCursorPos(cm.getCursor(), Pos(0, 5));
1830 m.clear();
1831
1832 // Can't cursor to the left of a marker when inclusiveLeft=false and selectLeft=false
1833 m = atom(0, 0, 0, 5, false, false, false);
1834 cm.setCursor(Pos(0, 5));
1835 eqCursorPos(cm.getCursor(), Pos(0, 5), "pushed out");
1836 cm.execCommand("goCharLeft");
1837 eqCursorPos(cm.getCursor(), Pos(0, 5));
1838 m.clear();
1839
1840 // Can cursor to the left of a marker when inclusiveLeft=false and selectLeft=True
1841 m = atom(0, 0, 0, 5, false, false, true);
1842 cm.setCursor(Pos(0, 5));
1843 eqCursorPos(cm.getCursor(), Pos(0, 5), "pushed out");
1844 cm.execCommand("goCharLeft");
1845 eqCursorPos(cm.getCursor(), Pos(0, 0));
1846 m.clear();
1847
1848 // Can't cursor to the right of a marker when inclusiveRight=true
1849 m = atom(0, 0, 0, 5, false, true);
1850 cm.setCursor(Pos(0, 0));
1851 eqCursorPos(cm.getCursor(), Pos(0, 0));
1852 cm.execCommand("goCharRight");
1853 eqCursorPos(cm.getCursor(), Pos(0, 6));
1854 m.clear();
1855
1856 // Can't cursor to the right of a marker when inclusiveRight=false and selectRight=false
1857 m = atom(0, 0, 0, 5, false, false, true, false);
1858 cm.setCursor(Pos(0, 0));
1859 eqCursorPos(cm.getCursor(), Pos(0, 0));
1860 cm.execCommand("goCharRight");
1861 eqCursorPos(cm.getCursor(), Pos(0, 6));
1862 m.clear();
1863
1864 // Can cursor to the right of a marker when inclusiveRight=false and selectRight=True
1865 m = atom(0, 0, 0, 5, false, false, true, true);
1866 cm.setCursor(Pos(0, 0));
1867 eqCursorPos(cm.getCursor(), Pos(0, 0));
1868 cm.execCommand("goCharRight");
1869 eqCursorPos(cm.getCursor(), Pos(0, 5));
1870 m.clear();
1871
1872 // Can't cursor to the right of a multiline marker when inclusiveRight=true
1873 m = atom(8, 4, 9, 10, false, true);
1874 cm.setCursor(Pos(9, 8));
1875 eqCursorPos(cm.getCursor(), Pos(8, 4), "set");
1876 cm.execCommand("goCharRight");
1877 eqCursorPos(cm.getCursor(), Pos(8, 4), "char right");
1878 cm.execCommand("goLineDown");
1879 eqCursorPos(cm.getCursor(), Pos(8, 4), "line down");
1880 cm.execCommand("goCharLeft");
1881 eqCursorPos(cm.getCursor(), Pos(8, 3, "after"));
1882 m.clear();
1883
1884 // Cursor jumps across a multiline atomic marker,
1885 // and backspace deletes the entire marker
1886 m = atom(1, 1, 3, 8);
1887 cm.setCursor(Pos(0, 0));
1888 cm.setCursor(Pos(2, 0));
1889 eqCursorPos(cm.getCursor(), Pos(3, 8));
1890 cm.execCommand("goCharLeft");
1891 eqCursorPos(cm.getCursor(), Pos(1, 1));
1892 cm.execCommand("goCharRight");
1893 eqCursorPos(cm.getCursor(), Pos(3, 8));
1894 cm.execCommand("goLineUp");
1895 eqCursorPos(cm.getCursor(), Pos(1, 1));
1896 cm.execCommand("goLineDown");
1897 eqCursorPos(cm.getCursor(), Pos(3, 8));
1898 cm.execCommand("delCharBefore");
1899 eq(cm.getValue().length, 80, "del chunk");
1900 m.clear();
1901 addDoc(cm, 10, 10);
1902
1903 // Delete before an atomic marker deletes the entire marker
1904 m = atom(3, 0, 5, 5);
1905 cm.setCursor(Pos(3, 0));
1906 cm.execCommand("delWordAfter");
1907 eq(cm.getValue().length, 82, "del chunk");
1908 m.clear();
1909 addDoc(cm, 10, 10);
1910 });
1911
1912 testCM("selectionBias", function(cm) {
1913 cm.markText(Pos(0, 1), Pos(0, 3), {atomic: true});
1914 cm.setCursor(Pos(0, 2));
1915 eqCursorPos(cm.getCursor(), Pos(0, 1));
1916 cm.setCursor(Pos(0, 2));
1917 eqCursorPos(cm.getCursor(), Pos(0, 3));
1918 cm.setCursor(Pos(0, 2));
1919 eqCursorPos(cm.getCursor(), Pos(0, 1));
1920 cm.setCursor(Pos(0, 2), null, {bias: -1});
1921 eqCursorPos(cm.getCursor(), Pos(0, 1));
1922 cm.setCursor(Pos(0, 4));
1923 cm.setCursor(Pos(0, 2), null, {bias: 1});
1924 eqCursorPos(cm.getCursor(), Pos(0, 3));
1925 }, {value: "12345"});
1926
1927 testCM("selectionHomeEnd", function(cm) {
1928 cm.markText(Pos(1, 0), Pos(1, 1), {atomic: true, inclusiveLeft: true});
1929 cm.markText(Pos(1, 3), Pos(1, 4), {atomic: true, inclusiveRight: true});
1930 cm.setCursor(Pos(1, 2));
1931 cm.execCommand("goLineStart");
1932 eqCursorPos(cm.getCursor(), Pos(1, 1));
1933 cm.execCommand("goLineEnd");
1934 eqCursorPos(cm.getCursor(), Pos(1, 3));
1935 }, {value: "ab\ncdef\ngh"});
1936
1937 testCM("readOnlyMarker", function(cm) {
1938 function mark(ll, cl, lr, cr, at) {
1939 return cm.markText(Pos(ll, cl), Pos(lr, cr),
1940 {readOnly: true, atomic: at});
1941 }
1942 var m = mark(0, 1, 0, 4);
1943 cm.setCursor(Pos(0, 2));
1944 cm.replaceSelection("hi", "end");
1945 eqCursorPos(cm.getCursor(), Pos(0, 2));
1946 eq(cm.getLine(0), "abcde");
1947 cm.execCommand("selectAll");
1948 cm.replaceSelection("oops", "around");
1949 eq(cm.getValue(), "oopsbcd");
1950 cm.undo();
1951 eqCursorPos(m.find().from, Pos(0, 1));
1952 eqCursorPos(m.find().to, Pos(0, 4));
1953 m.clear();
1954 cm.setCursor(Pos(0, 2));
1955 cm.replaceSelection("hi", "around");
1956 eq(cm.getLine(0), "abhicde");
1957 eqCursorPos(cm.getCursor(), Pos(0, 4));
1958 m = mark(0, 2, 2, 2, true);
1959 cm.setSelection(Pos(1, 1), Pos(2, 4));
1960 cm.replaceSelection("t", "end");
1961 eqCursorPos(cm.getCursor(), Pos(2, 3));
1962 eq(cm.getLine(2), "klto");
1963 cm.execCommand("goCharLeft");
1964 cm.execCommand("goCharLeft");
1965 eqCursorPos(cm.getCursor(), Pos(0, 2));
1966 cm.setSelection(Pos(0, 1), Pos(0, 3));
1967 cm.replaceSelection("xx", "around");
1968 eqCursorPos(cm.getCursor(), Pos(0, 3));
1969 eq(cm.getLine(0), "axxhicde");
1970 }, {value: "abcde\nfghij\nklmno\n"});
1971
1972 testCM("dirtyBit", function(cm) {
1973 eq(cm.isClean(), true);
1974 cm.replaceSelection("boo", null, "test");
1975 eq(cm.isClean(), false);
1976 cm.undo();
1977 eq(cm.isClean(), true);
1978 cm.replaceSelection("boo", null, "test");
1979 cm.replaceSelection("baz", null, "test");
1980 cm.undo();
1981 eq(cm.isClean(), false);
1982 cm.markClean();
1983 eq(cm.isClean(), true);
1984 cm.undo();
1985 eq(cm.isClean(), false);
1986 cm.redo();
1987 eq(cm.isClean(), true);
1988 });
1989
1990 testCM("changeGeneration", function(cm) {
1991 cm.replaceSelection("x");
1992 var softGen = cm.changeGeneration();
1993 cm.replaceSelection("x");
1994 cm.undo();
1995 eq(cm.getValue(), "");
1996 is(!cm.isClean(softGen));
1997 cm.replaceSelection("x");
1998 var hardGen = cm.changeGeneration(true);
1999 cm.replaceSelection("x");
2000 cm.undo();
2001 eq(cm.getValue(), "x");
2002 is(cm.isClean(hardGen));
2003 });
2004
2005 testCM("addKeyMap", function(cm) {
2006 function sendKey(code) {
2007 cm.triggerOnKeyDown({type: "keydown", keyCode: code,
2008 preventDefault: function(){}, stopPropagation: function(){}});
2009 }
2010
2011 sendKey(39);
2012 eqCursorPos(cm.getCursor(), Pos(0, 1, "before"));
2013 var test = 0;
2014 var map1 = {Right: function() { ++test; }}, map2 = {Right: function() { test += 10; }}
2015 cm.addKeyMap(map1);
2016 sendKey(39);
2017 eqCursorPos(cm.getCursor(), Pos(0, 1, "before"));
2018 eq(test, 1);
2019 cm.addKeyMap(map2, true);
2020 sendKey(39);
2021 eq(test, 2);
2022 cm.removeKeyMap(map1);
2023 sendKey(39);
2024 eq(test, 12);
2025 cm.removeKeyMap(map2);
2026 sendKey(39);
2027 eq(test, 12);
2028 eqCursorPos(cm.getCursor(), Pos(0, 2, "before"));
2029 cm.addKeyMap({Right: function() { test = 55; }, name: "mymap"});
2030 sendKey(39);
2031 eq(test, 55);
2032 cm.removeKeyMap("mymap");
2033 sendKey(39);
2034 eqCursorPos(cm.getCursor(), Pos(0, 3, "before"));
2035 }, {value: "abc"});
2036
2037 function mouseDown(cm, button, pos, mods) {
2038 var coords = cm.charCoords(pos, "window")
2039 var event = {type: "mousedown",
2040 preventDefault: Math.min,
2041 which: button,
2042 target: cm.display.lineDiv,
2043 clientX: coords.left, clientY: coords.top}
2044 if (mods) for (var prop in mods) event[prop] = mods[prop]
2045 cm.triggerOnMouseDown(event)
2046 }
2047
2048 testCM("mouseBinding", function(cm) {
2049 var fired = []
2050 cm.addKeyMap({
2051 "Shift-LeftClick": function(_cm, pos) {
2052 eqCharPos(pos, Pos(1, 2))
2053 fired.push("a")
2054 },
2055 "Shift-LeftDoubleClick": function() { fired.push("b") },
2056 "Shift-LeftTripleClick": function() { fired.push("c") }
2057 })
2058
2059 function send(button, mods) { mouseDown(cm, button, Pos(1, 2), mods) }
2060 send(1, {shiftKey: true})
2061 send(1, {shiftKey: true})
2062 send(1, {shiftKey: true})
2063 send(1, {})
2064 send(2, {ctrlKey: true})
2065 send(2, {ctrlKey: true})
2066 eq(fired.join(" "), "a b c")
2067 }, {value: "foo\nbar\nbaz"})
2068
2069 testCM("configureMouse", function(cm) {
2070 cm.setOption("configureMouse", function() { return {unit: "word"} })
2071 mouseDown(cm, 1, Pos(0, 5))
2072 eqCharPos(cm.getCursor("from"), Pos(0, 4))
2073 eqCharPos(cm.getCursor("to"), Pos(0, 7))
2074 cm.setOption("configureMouse", function() { return {extend: true} })
2075 mouseDown(cm, 1, Pos(0, 0))
2076 eqCharPos(cm.getCursor("from"), Pos(0, 0))
2077 eqCharPos(cm.getCursor("to"), Pos(0, 4))
2078 }, {value: "foo bar baz"})
2079
2080 testCM("findPosH", function(cm) {
2081 forEach([{from: Pos(0, 0), to: Pos(0, 1, "before"), by: 1},
2082 {from: Pos(0, 0), to: Pos(0, 0), by: -1, hitSide: true},
2083 {from: Pos(0, 0), to: Pos(0, 4, "before"), by: 1, unit: "word"},
2084 {from: Pos(0, 0), to: Pos(0, 8, "before"), by: 2, unit: "word"},
2085 {from: Pos(0, 0), to: Pos(2, 0, "after"), by: 20, unit: "word", hitSide: true},
2086 {from: Pos(0, 7), to: Pos(0, 5, "after"), by: -1, unit: "word"},
2087 {from: Pos(0, 4), to: Pos(0, 8, "before"), by: 1, unit: "word"},
2088 {from: Pos(1, 0), to: Pos(1, 18, "before"), by: 3, unit: "word"},
2089 {from: Pos(1, 22), to: Pos(1, 5, "after"), by: -3, unit: "word"},
2090 {from: Pos(1, 15), to: Pos(1, 10, "after"), by: -5},
2091 {from: Pos(1, 15), to: Pos(1, 10, "after"), by: -5, unit: "column"},
2092 {from: Pos(1, 15), to: Pos(1, 0, "after"), by: -50, unit: "column", hitSide: true},
2093 {from: Pos(1, 15), to: Pos(1, 24, "before"), by: 50, unit: "column", hitSide: true},
2094 {from: Pos(1, 15), to: Pos(2, 0, "after"), by: 50, hitSide: true}], function(t) {
2095 var r = cm.findPosH(t.from, t.by, t.unit || "char");
2096 eqCursorPos(r, t.to);
2097 eq(!!r.hitSide, !!t.hitSide);
2098 });
2099 }, {value: "line one\nline two.something.other\n"});
2100
2101 testCM("beforeChange", function(cm) {
2102 cm.on("beforeChange", function(cm, change) {
2103 var text = [];
2104 for (var i = 0; i < change.text.length; ++i)
2105 text.push(change.text[i].replace(/\s/g, "_"));
2106 change.update(null, null, text);
2107 });
2108 cm.setValue("hello, i am a\nnew document\n");
2109 eq(cm.getValue(), "hello,_i_am_a\nnew_document\n");
2110 CodeMirror.on(cm.getDoc(), "beforeChange", function(doc, change) {
2111 if (change.from.line == 0) change.cancel();
2112 });
2113 cm.setValue("oops"); // Canceled
2114 eq(cm.getValue(), "hello,_i_am_a\nnew_document\n");
2115 cm.replaceRange("hey hey hey", Pos(1, 0), Pos(2, 0));
2116 eq(cm.getValue(), "hello,_i_am_a\nhey_hey_hey");
2117 }, {value: "abcdefghijk"});
2118
2119 testCM("beforeChangeUndo", function(cm) {
2120 cm.replaceRange("hi", Pos(0, 0), Pos(0));
2121 cm.replaceRange("bye", Pos(0, 0), Pos(0));
2122 eq(cm.historySize().undo, 2);
2123 cm.on("beforeChange", function(cm, change) {
2124 is(!change.update);
2125 change.cancel();
2126 });
2127 cm.undo();
2128 eq(cm.historySize().undo, 0);
2129 eq(cm.getValue(), "bye\ntwo");
2130 }, {value: "one\ntwo"});
2131
2132 testCM("beforeSelectionChange", function(cm) {
2133 function notAtEnd(cm, pos) {
2134 var len = cm.getLine(pos.line).length;
2135 if (!len || pos.ch == len) return Pos(pos.line, pos.ch - 1);
2136 return pos;
2137 }
2138 cm.on("beforeSelectionChange", function(cm, obj) {
2139 obj.update([{anchor: notAtEnd(cm, obj.ranges[0].anchor),
2140 head: notAtEnd(cm, obj.ranges[0].head)}]);
2141 });
2142
2143 addDoc(cm, 10, 10);
2144 cm.execCommand("goLineEnd");
2145 eqCursorPos(cm.getCursor(), Pos(0, 9));
2146 cm.execCommand("selectAll");
2147 eqCursorPos(cm.getCursor("start"), Pos(0, 0));
2148 eqCursorPos(cm.getCursor("end"), Pos(9, 9));
2149 });
2150
2151 testCM("change_removedText", function(cm) {
2152 cm.setValue("abc\ndef");
2153
2154 var removedText = [];
2155 cm.on("change", function(cm, change) {
2156 removedText.push(change.removed);
2157 });
2158
2159 cm.operation(function() {
2160 cm.replaceRange("xyz", Pos(0, 0), Pos(1,1));
2161 cm.replaceRange("123", Pos(0,0));
2162 });
2163
2164 eq(removedText.length, 2);
2165 eq(removedText[0].join("\n"), "abc\nd");
2166 eq(removedText[1].join("\n"), "");
2167
2168 var removedText = [];
2169 cm.undo();
2170 eq(removedText.length, 2);
2171 eq(removedText[0].join("\n"), "123");
2172 eq(removedText[1].join("\n"), "xyz");
2173
2174 var removedText = [];
2175 cm.redo();
2176 eq(removedText.length, 2);
2177 eq(removedText[0].join("\n"), "abc\nd");
2178 eq(removedText[1].join("\n"), "");
2179 });
2180
2181 testCM("lineStyleFromMode", function(cm) {
2182 CodeMirror.defineMode("test_mode", function() {
2183 return {token: function(stream) {
2184 if (stream.match(/^\[[^\]]*\]/)) return " line-brackets ";
2185 if (stream.match(/^\([^\)]*\)/)) return " line-background-parens ";
2186 if (stream.match(/^<[^>]*>/)) return " span line-line line-background-bg ";
2187 stream.match(/^\s+|^\S+/);
2188 }};
2189 });
2190 cm.setOption("mode", "test_mode");
2191 var bracketElts = byClassName(cm.getWrapperElement(), "brackets");
2192 eq(bracketElts.length, 1, "brackets count");
2193 eq(bracketElts[0].nodeName, "PRE");
2194 is(!/brackets.*brackets/.test(bracketElts[0].className));
2195 var parenElts = byClassName(cm.getWrapperElement(), "parens");
2196 eq(parenElts.length, 1, "parens count");
2197 eq(parenElts[0].nodeName, "DIV");
2198 is(!/parens.*parens/.test(parenElts[0].className));
2199 eq(parenElts[0].parentElement.nodeName, "DIV");
2200
2201 is(byClassName(cm.getWrapperElement(), "bg").length > 0);
2202 is(byClassName(cm.getWrapperElement(), "line").length > 0);
2203 var spanElts = byClassName(cm.getWrapperElement(), "cm-span");
2204 eq(spanElts.length, 2);
2205 is(/^\s*cm-span\s*$/.test(spanElts[0].className));
2206 }, {value: "line1: [br] [br]\nline2: (par) (par)\nline3: <tag> <tag>"});
2207
2208 testCM("lineStyleFromBlankLine", function(cm) {
2209 CodeMirror.defineMode("lineStyleFromBlankLine_mode", function() {
2210 return {token: function(stream) { stream.skipToEnd(); return "comment"; },
2211 blankLine: function() { return "line-blank"; }};
2212 });
2213 cm.setOption("mode", "lineStyleFromBlankLine_mode");
2214 var blankElts = byClassName(cm.getWrapperElement(), "blank");
2215 eq(blankElts.length, 1);
2216 eq(blankElts[0].nodeName, "PRE");
2217 cm.replaceRange("x", Pos(1, 0));
2218 blankElts = byClassName(cm.getWrapperElement(), "blank");
2219 eq(blankElts.length, 0);
2220 }, {value: "foo\n\nbar"});
2221
2222 CodeMirror.registerHelper("xxx", "a", "A");
2223 CodeMirror.registerHelper("xxx", "b", "B");
2224 CodeMirror.defineMode("yyy", function() {
2225 return {
2226 token: function(stream) { stream.skipToEnd(); },
2227 xxx: ["a", "b", "q"]
2228 };
2229 });
2230 CodeMirror.registerGlobalHelper("xxx", "c", function(m) { return m.enableC; }, "C");
2231
2232 testCM("helpers", function(cm) {
2233 cm.setOption("mode", "yyy");
2234 eq(cm.getHelpers(Pos(0, 0), "xxx").join("/"), "A/B");
2235 cm.setOption("mode", {name: "yyy", modeProps: {xxx: "b", enableC: true}});
2236 eq(cm.getHelpers(Pos(0, 0), "xxx").join("/"), "B/C");
2237 cm.setOption("mode", "javascript");
2238 eq(cm.getHelpers(Pos(0, 0), "xxx").join("/"), "");
2239 });
2240
2241 testCM("selectionHistory", function(cm) {
2242 for (var i = 0; i < 3; i++) {
2243 cm.setExtending(true);
2244 cm.execCommand("goCharRight");
2245 cm.setExtending(false);
2246 cm.execCommand("goCharRight");
2247 cm.execCommand("goCharRight");
2248 }
2249 cm.execCommand("undoSelection");
2250 eq(cm.getSelection(), "c");
2251 cm.execCommand("undoSelection");
2252 eq(cm.getSelection(), "");
2253 eqCursorPos(cm.getCursor(), Pos(0, 4, "before"));
2254 cm.execCommand("undoSelection");
2255 eq(cm.getSelection(), "b");
2256 cm.execCommand("redoSelection");
2257 eq(cm.getSelection(), "");
2258 eqCursorPos(cm.getCursor(), Pos(0, 4, "before"));
2259 cm.execCommand("redoSelection");
2260 eq(cm.getSelection(), "c");
2261 cm.execCommand("redoSelection");
2262 eq(cm.getSelection(), "");
2263 eqCursorPos(cm.getCursor(), Pos(0, 6, "before"));
2264 }, {value: "a b c d"});
2265
2266 testCM("selectionChangeReducesRedo", function(cm) {
2267 cm.replaceSelection("X");
2268 cm.execCommand("goCharRight");
2269 cm.undoSelection();
2270 cm.execCommand("selectAll");
2271 cm.undoSelection();
2272 eq(cm.getValue(), "Xabc");
2273 eqCursorPos(cm.getCursor(), Pos(0, 1));
2274 cm.undoSelection();
2275 eq(cm.getValue(), "abc");
2276 }, {value: "abc"});
2277
2278 testCM("selectionHistoryNonOverlapping", function(cm) {
2279 cm.setSelection(Pos(0, 0), Pos(0, 1));
2280 cm.setSelection(Pos(0, 2), Pos(0, 3));
2281 cm.execCommand("undoSelection");
2282 eqCursorPos(cm.getCursor("anchor"), Pos(0, 0));
2283 eqCursorPos(cm.getCursor("head"), Pos(0, 1));
2284 }, {value: "1234"});
2285
2286 testCM("cursorMotionSplitsHistory", function(cm) {
2287 cm.replaceSelection("a");
2288 cm.execCommand("goCharRight");
2289 cm.replaceSelection("b");
2290 cm.replaceSelection("c");
2291 cm.undo();
2292 eq(cm.getValue(), "a1234");
2293 eqCursorPos(cm.getCursor(), Pos(0, 2, "before"));
2294 cm.undo();
2295 eq(cm.getValue(), "1234");
2296 eqCursorPos(cm.getCursor(), Pos(0, 0));
2297 }, {value: "1234"});
2298
2299 testCM("selChangeInOperationDoesNotSplit", function(cm) {
2300 for (var i = 0; i < 4; i++) {
2301 cm.operation(function() {
2302 cm.replaceSelection("x");
2303 cm.setCursor(Pos(0, cm.getCursor().ch - 1));
2304 });
2305 }
2306 eqCursorPos(cm.getCursor(), Pos(0, 0));
2307 eq(cm.getValue(), "xxxxa");
2308 cm.undo();
2309 eq(cm.getValue(), "a");
2310 }, {value: "a"});
2311
2312 testCM("alwaysMergeSelEventWithChangeOrigin", function(cm) {
2313 cm.replaceSelection("U", null, "foo");
2314 cm.setSelection(Pos(0, 0), Pos(0, 1), {origin: "foo"});
2315 cm.undoSelection();
2316 eq(cm.getValue(), "a");
2317 cm.replaceSelection("V", null, "foo");
2318 cm.setSelection(Pos(0, 0), Pos(0, 1), {origin: "bar"});
2319 cm.undoSelection();
2320 eq(cm.getValue(), "Va");
2321 }, {value: "a"});
2322
2323 testCM("getTokenAt", function(cm) {
2324 var tokPlus = cm.getTokenAt(Pos(0, 2));
2325 eq(tokPlus.type, "operator");
2326 eq(tokPlus.string, "+");
2327 var toks = cm.getLineTokens(0);
2328 eq(toks.length, 3);
2329 forEach([["number", "1"], ["operator", "+"], ["number", "2"]], function(expect, i) {
2330 eq(toks[i].type, expect[0]);
2331 eq(toks[i].string, expect[1]);
2332 });
2333 }, {value: "1+2", mode: "javascript"});
2334
2335 testCM("getTokenTypeAt", function(cm) {
2336 eq(cm.getTokenTypeAt(Pos(0, 0)), "number");
2337 eq(cm.getTokenTypeAt(Pos(0, 6)), "string");
2338 cm.addOverlay({
2339 token: function(stream) {
2340 if (stream.match("foo")) return "foo";
2341 else stream.next();
2342 }
2343 });
2344 eq(byClassName(cm.getWrapperElement(), "cm-foo").length, 1);
2345 eq(cm.getTokenTypeAt(Pos(0, 6)), "string");
2346 }, {value: "1 + 'foo'", mode: "javascript"});
2347
2348 testCM("addOverlay", function(cm) {
2349 cm.addOverlay({
2350 token: function(stream) {
2351 var base = stream.baseToken()
2352 if (!/comment/.test(base.type) && stream.match(/\d+/)) return "x"
2353 stream.next()
2354 }
2355 })
2356 var x = byClassName(cm.getWrapperElement(), "cm-x")
2357 is(x.length, 1)
2358 is(x[0].textContent, "233")
2359 cm.replaceRange("", Pos(0, 4), Pos(0, 6))
2360 is(byClassName(cm.getWrapperElement(), "cm-x").length, 2)
2361 }, {value: "foo /* 100 */\nbar + 233;\nbaz", mode: "javascript"})
2362
2363 testCM("resizeLineWidget", function(cm) {
2364 addDoc(cm, 200, 3);
2365 var widget = document.createElement("pre");
2366 widget.innerHTML = "imwidget";
2367 widget.style.background = "yellow";
2368 cm.addLineWidget(1, widget, {noHScroll: true});
2369 cm.setSize(40);
2370 is(widget.parentNode.offsetWidth < 42);
2371 });
2372
2373 testCM("combinedOperations", function(cm) {
2374 var place = document.getElementById("testground");
2375 var other = CodeMirror(place, {value: "123"});
2376 try {
2377 cm.operation(function() {
2378 cm.addLineClass(0, "wrap", "foo");
2379 other.addLineClass(0, "wrap", "foo");
2380 });
2381 eq(byClassName(cm.getWrapperElement(), "foo").length, 1);
2382 eq(byClassName(other.getWrapperElement(), "foo").length, 1);
2383 cm.operation(function() {
2384 cm.removeLineClass(0, "wrap", "foo");
2385 other.removeLineClass(0, "wrap", "foo");
2386 });
2387 eq(byClassName(cm.getWrapperElement(), "foo").length, 0);
2388 eq(byClassName(other.getWrapperElement(), "foo").length, 0);
2389 } finally {
2390 place.removeChild(other.getWrapperElement());
2391 }
2392 }, {value: "abc"});
2393
2394 testCM("eventOrder", function(cm) {
2395 var seen = [];
2396 cm.on("change", function() {
2397 if (!seen.length) cm.replaceSelection(".");
2398 seen.push("change");
2399 });
2400 cm.on("cursorActivity", function() {
2401 cm.replaceSelection("!");
2402 seen.push("activity");
2403 });
2404 cm.replaceSelection("/");
2405 eq(seen.join(","), "change,change,activity,change");
2406 });
2407
2408 testCM("splitSpaces_nonspecial", function(cm) {
2409 eq(byClassName(cm.getWrapperElement(), "cm-invalidchar").length, 0);
2410 }, {
2411 specialChars: /[\u00a0]/,
2412 value: "spaces -> <- between"
2413 });
2414
2415 test("core_rmClass", function() {
2416 var node = document.createElement("div");
2417 node.className = "foo-bar baz-quux yadda";
2418 CodeMirror.rmClass(node, "quux");
2419 eq(node.className, "foo-bar baz-quux yadda");
2420 CodeMirror.rmClass(node, "baz-quux");
2421 eq(node.className, "foo-bar yadda");
2422 CodeMirror.rmClass(node, "yadda");
2423 eq(node.className, "foo-bar");
2424 CodeMirror.rmClass(node, "foo-bar");
2425 eq(node.className, "");
2426 node.className = " foo ";
2427 CodeMirror.rmClass(node, "foo");
2428 eq(node.className, "");
2429 });
2430
2431 test("core_addClass", function() {
2432 var node = document.createElement("div");
2433 CodeMirror.addClass(node, "a");
2434 eq(node.className, "a");
2435 CodeMirror.addClass(node, "a");
2436 eq(node.className, "a");
2437 CodeMirror.addClass(node, "b");
2438 eq(node.className, "a b");
2439 CodeMirror.addClass(node, "a");
2440 CodeMirror.addClass(node, "b");
2441 eq(node.className, "a b");
2442 });
2443
2444 testCM("lineSeparator", function(cm) {
2445 eq(cm.lineCount(), 3);
2446 eq(cm.getLine(1), "bar\r");
2447 eq(cm.getLine(2), "baz\rquux");
2448 cm.setOption("lineSeparator", "\r");
2449 eq(cm.lineCount(), 5);
2450 eq(cm.getLine(4), "quux");
2451 eq(cm.getValue(), "foo\rbar\r\rbaz\rquux");
2452 eq(cm.getValue("\n"), "foo\nbar\n\nbaz\nquux");
2453 cm.setOption("lineSeparator", null);
2454 cm.setValue("foo\nbar\r\nbaz\rquux");
2455 eq(cm.lineCount(), 4);
2456 }, {value: "foo\nbar\r\nbaz\rquux",
2457 lineSeparator: "\n"});
2458
2459 var extendingChars = /[\u0300-\u036f\u0483-\u0489\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u065e\u0670\u06d6-\u06dc\u06de-\u06e4\u06e7\u06e8\u06ea-\u06ed\u0711\u0730-\u074a\u07a6-\u07b0\u07eb-\u07f3\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0900-\u0902\u093c\u0941-\u0948\u094d\u0951-\u0955\u0962\u0963\u0981\u09bc\u09be\u09c1-\u09c4\u09cd\u09d7\u09e2\u09e3\u0a01\u0a02\u0a3c\u0a41\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a70\u0a71\u0a75\u0a81\u0a82\u0abc\u0ac1-\u0ac5\u0ac7\u0ac8\u0acd\u0ae2\u0ae3\u0b01\u0b3c\u0b3e\u0b3f\u0b41-\u0b44\u0b4d\u0b56\u0b57\u0b62\u0b63\u0b82\u0bbe\u0bc0\u0bcd\u0bd7\u0c3e-\u0c40\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0cbc\u0cbf\u0cc2\u0cc6\u0ccc\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0d3e\u0d41-\u0d44\u0d4d\u0d57\u0d62\u0d63\u0dca\u0dcf\u0dd2-\u0dd4\u0dd6\u0ddf\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0eb1\u0eb4-\u0eb9\u0ebb\u0ebc\u0ec8-\u0ecd\u0f18\u0f19\u0f35\u0f37\u0f39\u0f71-\u0f7e\u0f80-\u0f84\u0f86\u0f87\u0f90-\u0f97\u0f99-\u0fbc\u0fc6\u102d-\u1030\u1032-\u1037\u1039\u103a\u103d\u103e\u1058\u1059\u105e-\u1060\u1071-\u1074\u1082\u1085\u1086\u108d\u109d\u135f\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b7-\u17bd\u17c6\u17c9-\u17d3\u17dd\u180b-\u180d\u18a9\u1920-\u1922\u1927\u1928\u1932\u1939-\u193b\u1a17\u1a18\u1a56\u1a58-\u1a5e\u1a60\u1a62\u1a65-\u1a6c\u1a73-\u1a7c\u1a7f\u1b00-\u1b03\u1b34\u1b36-\u1b3a\u1b3c\u1b42\u1b6b-\u1b73\u1b80\u1b81\u1ba2-\u1ba5\u1ba8\u1ba9\u1c2c-\u1c33\u1c36\u1c37\u1cd0-\u1cd2\u1cd4-\u1ce0\u1ce2-\u1ce8\u1ced\u1dc0-\u1de6\u1dfd-\u1dff\u200c\u200d\u20d0-\u20f0\u2cef-\u2cf1\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua66f-\ua672\ua67c\ua67d\ua6f0\ua6f1\ua802\ua806\ua80b\ua825\ua826\ua8c4\ua8e0-\ua8f1\ua926-\ua92d\ua947-\ua951\ua980-\ua982\ua9b3\ua9b6-\ua9b9\ua9bc\uaa29-\uaa2e\uaa31\uaa32\uaa35\uaa36\uaa43\uaa4c\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uabe5\uabe8\uabed\udc00-\udfff\ufb1e\ufe00-\ufe0f\ufe20-\ufe26\uff9e\uff9f]/
2460 var getChar = function (noExtending) { var res; do {res = String.fromCharCode(Math.floor(Math.random()*0x8ac)); } while ([0x90].indexOf(res.charCodeAt(0)) != -1 || (noExtending && extendingChars.test(res))); return res }
2461 var getString = function (n) { var res = getChar(true); while (--n > 0) res += getChar(); return res }
2462
2463 function makeItWrapAfter(cm, pos) {
2464 var firstLineTop = cm.cursorCoords(Pos(0, 0)).top;
2465 for(var w = 0, posTop; posTop != firstLineTop; ++w) {
2466 cm.setSize(w);
2467 posTop = cm.charCoords(pos).top;
2468 }
2469 }
2470
2471 function countIf(arr, f) {
2472 var result = 0
2473 for (var i = 0; i < arr.length; i++) if (f[arr[i]]) result++
2474 return result
2475 }
2476
2477 function testMoveBidi(str) {
2478 testCM("move_bidi_" + str, function(cm) {
2479 if (cm.getOption("inputStyle") != "textarea" || !cm.getOption("rtlMoveVisually")) return;
2480 cm.getScrollerElement().style.fontFamily = "monospace";
2481 makeItWrapAfter(cm, Pos(0, 5));
2482
2483 var steps = str.length - countIf(str.split(""), function(ch) { return extendingChars.test(ch) });
2484 var lineBreaks = {}
2485 lineBreaks[6 - countIf(str.substr(0, 5).split(""), function(ch) { return extendingChars.test(ch) })] = 'w';
2486 if (str.indexOf("\n") != -1) {
2487 lineBreaks[steps - 2] = 'n';
2488 }
2489
2490 // Make sure we are at the visual beginning of the first line
2491 cm.execCommand("goLineStart");
2492
2493 var prevCoords = cm.cursorCoords(), coords;
2494 for(var i = 0; i < steps; ++i) {
2495 cm.execCommand("goCharRight");
2496 coords = cm.cursorCoords();
2497 if ((i >= 10 && i <= 12) && !lineBreaks[i] && coords.left < prevCoords.left && coords.top > prevCoords.top) {
2498 // The first line wraps twice
2499 lineBreaks[i] = 'w';
2500 }
2501 if (!lineBreaks[i]) {
2502 is(coords.left > prevCoords.left, "In step " + i + ", cursor didn't move right");
2503 eq(coords.top, prevCoords.top, "In step " + i + ", cursor moved out of line");
2504 } else {
2505 is(coords.left < prevCoords.left, i);
2506 is(coords.top > prevCoords.top, i);
2507 }
2508 prevCoords = coords;
2509 }
2510
2511 cm.execCommand("goCharRight");
2512 coords = cm.cursorCoords();
2513 eq(coords.left, prevCoords.left, "Moving " + steps + " steps right didn't reach the end");
2514 eq(coords.top, prevCoords.top, "Moving " + steps + " steps right didn't reach the end");
2515
2516 for(i = steps - 1; i >= 0; --i) {
2517 cm.execCommand("goCharLeft");
2518 coords = cm.cursorCoords();
2519 if (!(lineBreaks[i] == 'n' || lineBreaks[i + 1] == 'w')) {
2520 is(coords.left < prevCoords.left, "In step " + i + ", cursor didn't move left");
2521 eq(coords.top, prevCoords.top, "In step " + i + ", cursor is not at the same line anymore");
2522 } else {
2523 is(coords.left > prevCoords.left, i);
2524 is(coords.top < prevCoords.top, i);
2525 }
2526 prevCoords = coords;
2527 }
2528
2529 cm.execCommand("goCharLeft");
2530 coords = cm.cursorCoords();
2531 eq(coords.left, prevCoords.left, "Moving " + steps + " steps left didn't reach the beginning");
2532 eq(coords.top, prevCoords.top, "Moving " + steps + " steps left didn't reach the beginning");
2533 }, {value: str, lineWrapping: true})
2534 };
2535
2536 function testMoveEndBidi(str) {
2537 testCM("move_end_bidi_" + str, function(cm) {
2538 cm.getScrollerElement().style.fontFamily = "monospace";
2539 makeItWrapAfter(cm, Pos(0, 5));
2540
2541 cm.execCommand("goLineStart");
2542 var pos = cm.doc.getCursor();
2543 cm.execCommand("goCharLeft");
2544 eqCursorPos(pos, cm.doc.getCursor());
2545
2546 cm.execCommand("goLineEnd");
2547 pos = cm.doc.getCursor();
2548 cm.execCommand("goColumnRight");
2549 eqCursorPos(pos, cm.doc.getCursor());
2550 }, {value: str, lineWrapping: true})
2551 };
2552
2553 var bidiTests = [];
2554
2555 // We don't correctly implement L1 UBA
2556 // See https://bugzilla.mozilla.org/show_bug.cgi?id=1331501
2557 // and https://bugs.chromium.org/p/chromium/issues/detail?id=673405
2558 /*
2559 bidiTests.push("Say ا ب جabj\nS");
2560 bidiTests.push("Sayyy ا ا ب ج");
2561 */
2562
2563 bidiTests.push("Όȝǝڪȉۥ״ۺ׆ɀҩۏ\nҳ");
2564 if (!window.automatedTests) bidiTests.push("ŌӰтقȤ؁ƥ؅٣ĎȺ١\nϚ");
2565 bidiTests.push("ٻоҤѕѽΩ־؉ïίքdz\nٵ");
2566 bidiTests.push("؅؁ĆՕƿɁǞϮؠȩóć\nď");
2567 bidiTests.push("RŨďңŪzϢŎƏԖڇڦ\nӈ");
2568 bidiTests.push("ό׊۷٢ԜһОצЉيčǟ\nѩ");
2569 bidiTests.push("ۑÚҳҕڬġڹհяųKV\nr");
2570 bidiTests.push("źڻғúہ4ם1Ƞc1a\nԁ");
2571 bidiTests.push("ҒȨҟփƞ٦ԓȦڰғâƥ\nڤ");
2572 bidiTests.push("ϖسՉȏŧΔԛdžĎӟیڡ\nέ");
2573 bidiTests.push("۹ؼL۵ĺȧКԙػא7״\nم");
2574 bidiTests.push("ن (ي)\u2009أقواس"); // thin space to throw off Firefox 51's broken white-space compressing behavior
2575
2576 bidiTests.push("քմѧǮßپüŢҍҞўڳ\nӧ");
2577
2578 //bidiTests.push("Count ١ ٢ ٣ ٤");
2579 //bidiTests.push("ӣאƦϰ؊ȓېÛوը٬ز\nϪ");
2580 //bidiTests.push("ҾճٳџIՖӻ٥׭֐؜ڏ\nێ");
2581 //bidiTests.push("ҬÓФ؜ڂį٦Ͽɓڐͳٵ\nՈ");
2582 //bidiTests.push("aѴNijȻهˇ҃ڱӧǻֵ\na");
2583 //bidiTests.push(" a٧ا٢ ب جa\nS");
2584
2585 for (var i = 0; i < bidiTests.length; ++i) {
2586 testMoveBidi(bidiTests[i]);
2587 testMoveEndBidi(bidiTests[i]);
2588 }
2589
2590 /*
2591 for (var i = 0; i < 5; ++i) {
2592 testMoveBidi(getString(12) + "\n" + getString(1));
2593 }
2594 */
2595
2596 function testCoordsWrappedBidi(str) {
2597 testCM("coords_wrapped_bidi_" + str, function(cm) {
2598 cm.getScrollerElement().style.fontFamily = "monospace";
2599 makeItWrapAfter(cm, Pos(0, 5));
2600
2601 // Make sure we are at the visual beginning of the first line
2602 var pos = Pos(0, 0), lastPos;
2603 cm.doc.setCursor(pos);
2604 do {
2605 lastPos = pos;
2606 cm.execCommand("goCharLeft");
2607 pos = cm.doc.getCursor();
2608 } while (pos != lastPos)
2609
2610 var top = cm.charCoords(Pos(0, 0)).top, lastTop;
2611 for (var i = 1; i < str.length; ++i) {
2612 lastTop = top;
2613 top = cm.charCoords(Pos(0, i)).top;
2614 is(top >= lastTop);
2615 }
2616 }, {value: str, lineWrapping: true})
2617 };
2618
2619 testCoordsWrappedBidi("Count ١ ٢ ٣ ٤");
2620 /*
2621 for (var i = 0; i < 5; ++i) {
2622 testCoordsWrappedBidi(getString(50));
2623 }
2624 */
2625
2626 testCM("rtl_wrapped_selection", function(cm) {
2627 cm.setSelection(Pos(0, 10), Pos(0, 190))
2628 is(byClassName(cm.getWrapperElement(), "CodeMirror-selected").length >= 3)
2629 }, {value: new Array(10).join(" فتي تم تضمينها فتي تم"), lineWrapping: true})
2630
2631 testCM("bidi_wrapped_selection", function(cm) {
2632 cm.setSize(cm.charCoords(Pos(0, 10), "editor").left)
2633 cm.setSelection(Pos(0, 37), Pos(0, 80))
2634 var blocks = byClassName(cm.getWrapperElement(), "CodeMirror-selected")
2635 is(blocks.length >= 2)
2636 is(blocks.length <= 3)
2637 var boxTop = blocks[0].getBoundingClientRect(), boxBot = blocks[blocks.length - 1].getBoundingClientRect()
2638 is(boxTop.left > cm.charCoords(Pos(0, 1)).right)
2639 is(boxBot.right < cm.charCoords(Pos(0, cm.getLine(0).length - 2)).left)
2640 }, {value: "<p>مفتي11 تم تضمينهفتي تم تضمينها فتي تفتي تم تضمينها فتي تفتي تم تضمينها فتي تفتي تم تضمينها فتي تا فت10ي ت</p>", lineWrapping: true})
2641
2642 testCM("delete_wrapped", function(cm) {
2643 makeItWrapAfter(cm, Pos(0, 2));
2644 cm.doc.setCursor(Pos(0, 3, "after"));
2645 cm.deleteH(-1, "char");
2646 eq(cm.getLine(0), "1245");
2647 }, {value: "12345", lineWrapping: true})
2648
2649 testCM("issue_4878", function(cm) {
2650 if (window.automatedTests) return
2651 cm.setCursor(Pos(1, 12, "after"));
2652 cm.moveH(-1, "char");
2653 eqCursorPos(cm.getCursor(), Pos(0, 113, "before"));
2654 }, {value: " في تطبيق السمات مرة واحدة https://github.com/codemirror/CodeMirror/issues/4878#issuecomment-330550964على سبيل المثال <code>\"foo bar\"</code>\n" +
2655 " سيتم تعيين", direction: "rtl", lineWrapping: true});
2656
2657 CodeMirror.defineMode("lookahead_mode", function() {
2658 // Colors text as atom if the line two lines down has an x in it
2659 return {
2660 token: function(stream) {
2661 stream.skipToEnd()
2662 return /x/.test(stream.lookAhead(2)) ? "atom" : null
2663 }
2664 }
2665 })
2666
2667 testCM("mode_lookahead", function(cm) {
2668 eq(cm.getTokenAt(Pos(0, 1)).type, "atom")
2669 eq(cm.getTokenAt(Pos(1, 1)).type, "atom")
2670 eq(cm.getTokenAt(Pos(2, 1)).type, null)
2671 cm.replaceRange("\n", Pos(2, 0))
2672 eq(cm.getTokenAt(Pos(0, 1)).type, null)
2673 eq(cm.getTokenAt(Pos(1, 1)).type, "atom")
2674 }, {value: "foo\na\nx\nx\n", mode: "lookahead_mode"})
2675
2676 testCM("should have translate=no attribute", function(cm) {
2677 eq(cm.getWrapperElement().getAttribute("translate"), "no")
2678 }, {})