You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2678 lines
96 KiB

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