test.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. var Pos = CodeMirror.Pos;
  2. function forEach(arr, f) {
  3. for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
  4. }
  5. function addDoc(cm, width, height) {
  6. var content = [], line = "";
  7. for (var i = 0; i < width; ++i) line += "x";
  8. for (var i = 0; i < height; ++i) content.push(line);
  9. cm.setValue(content.join("\n"));
  10. }
  11. function byClassName(elt, cls) {
  12. if (elt.getElementsByClassName) return elt.getElementsByClassName(cls);
  13. var found = [], re = new RegExp("\\b" + cls + "\\b");
  14. function search(elt) {
  15. if (elt.nodeType == 3) return;
  16. if (re.test(elt.className)) found.push(elt);
  17. for (var i = 0, e = elt.childNodes.length; i < e; ++i)
  18. search(elt.childNodes[i]);
  19. }
  20. search(elt);
  21. return found;
  22. }
  23. var ie_lt8 = /MSIE [1-7]\b/.test(navigator.userAgent);
  24. var mac = /Mac/.test(navigator.platform);
  25. var phantom = /PhantomJS/.test(navigator.userAgent);
  26. var opera = /Opera\/\./.test(navigator.userAgent);
  27. var opera_version = opera && navigator.userAgent.match(/Version\/(\d+\.\d+)/);
  28. if (opera_version) opera_version = Number(opera_version);
  29. var opera_lt10 = opera && (!opera_version || opera_version < 10);
  30. namespace = "core_";
  31. test("core_fromTextArea", function() {
  32. var te = document.getElementById("code");
  33. te.value = "CONTENT";
  34. var cm = CodeMirror.fromTextArea(te);
  35. is(!te.offsetHeight);
  36. eq(cm.getValue(), "CONTENT");
  37. cm.setValue("foo\nbar");
  38. eq(cm.getValue(), "foo\nbar");
  39. cm.save();
  40. is(/^foo\r?\nbar$/.test(te.value));
  41. cm.setValue("xxx");
  42. cm.toTextArea();
  43. is(te.offsetHeight);
  44. eq(te.value, "xxx");
  45. });
  46. testCM("getRange", function(cm) {
  47. eq(cm.getLine(0), "1234");
  48. eq(cm.getLine(1), "5678");
  49. eq(cm.getLine(2), null);
  50. eq(cm.getLine(-1), null);
  51. eq(cm.getRange(Pos(0, 0), Pos(0, 3)), "123");
  52. eq(cm.getRange(Pos(0, -1), Pos(0, 200)), "1234");
  53. eq(cm.getRange(Pos(0, 2), Pos(1, 2)), "34\n56");
  54. eq(cm.getRange(Pos(1, 2), Pos(100, 0)), "78");
  55. }, {value: "1234\n5678"});
  56. testCM("replaceRange", function(cm) {
  57. eq(cm.getValue(), "");
  58. cm.replaceRange("foo\n", Pos(0, 0));
  59. eq(cm.getValue(), "foo\n");
  60. cm.replaceRange("a\nb", Pos(0, 1));
  61. eq(cm.getValue(), "fa\nboo\n");
  62. eq(cm.lineCount(), 3);
  63. cm.replaceRange("xyzzy", Pos(0, 0), Pos(1, 1));
  64. eq(cm.getValue(), "xyzzyoo\n");
  65. cm.replaceRange("abc", Pos(0, 0), Pos(10, 0));
  66. eq(cm.getValue(), "abc");
  67. eq(cm.lineCount(), 1);
  68. });
  69. testCM("selection", function(cm) {
  70. cm.setSelection(Pos(0, 4), Pos(2, 2));
  71. is(cm.somethingSelected());
  72. eq(cm.getSelection(), "11\n222222\n33");
  73. eqPos(cm.getCursor(false), Pos(2, 2));
  74. eqPos(cm.getCursor(true), Pos(0, 4));
  75. cm.setSelection(Pos(1, 0));
  76. is(!cm.somethingSelected());
  77. eq(cm.getSelection(), "");
  78. eqPos(cm.getCursor(true), Pos(1, 0));
  79. cm.replaceSelection("abc");
  80. eq(cm.getSelection(), "abc");
  81. eq(cm.getValue(), "111111\nabc222222\n333333");
  82. cm.replaceSelection("def", "end");
  83. eq(cm.getSelection(), "");
  84. eqPos(cm.getCursor(true), Pos(1, 3));
  85. cm.setCursor(Pos(2, 1));
  86. eqPos(cm.getCursor(true), Pos(2, 1));
  87. cm.setCursor(1, 2);
  88. eqPos(cm.getCursor(true), Pos(1, 2));
  89. }, {value: "111111\n222222\n333333"});
  90. testCM("extendSelection", function(cm) {
  91. cm.setExtending(true);
  92. addDoc(cm, 10, 10);
  93. cm.setSelection(Pos(3, 5));
  94. eqPos(cm.getCursor("head"), Pos(3, 5));
  95. eqPos(cm.getCursor("anchor"), Pos(3, 5));
  96. cm.setSelection(Pos(2, 5), Pos(5, 5));
  97. eqPos(cm.getCursor("head"), Pos(5, 5));
  98. eqPos(cm.getCursor("anchor"), Pos(2, 5));
  99. eqPos(cm.getCursor("start"), Pos(2, 5));
  100. eqPos(cm.getCursor("end"), Pos(5, 5));
  101. cm.setSelection(Pos(5, 5), Pos(2, 5));
  102. eqPos(cm.getCursor("head"), Pos(2, 5));
  103. eqPos(cm.getCursor("anchor"), Pos(5, 5));
  104. eqPos(cm.getCursor("start"), Pos(2, 5));
  105. eqPos(cm.getCursor("end"), Pos(5, 5));
  106. cm.extendSelection(Pos(3, 2));
  107. eqPos(cm.getCursor("head"), Pos(3, 2));
  108. eqPos(cm.getCursor("anchor"), Pos(5, 5));
  109. cm.extendSelection(Pos(6, 2));
  110. eqPos(cm.getCursor("head"), Pos(6, 2));
  111. eqPos(cm.getCursor("anchor"), Pos(5, 5));
  112. cm.extendSelection(Pos(6, 3), Pos(6, 4));
  113. eqPos(cm.getCursor("head"), Pos(6, 4));
  114. eqPos(cm.getCursor("anchor"), Pos(5, 5));
  115. cm.extendSelection(Pos(0, 3), Pos(0, 4));
  116. eqPos(cm.getCursor("head"), Pos(0, 3));
  117. eqPos(cm.getCursor("anchor"), Pos(5, 5));
  118. cm.extendSelection(Pos(4, 5), Pos(6, 5));
  119. eqPos(cm.getCursor("head"), Pos(6, 5));
  120. eqPos(cm.getCursor("anchor"), Pos(4, 5));
  121. cm.setExtending(false);
  122. cm.extendSelection(Pos(0, 3), Pos(0, 4));
  123. eqPos(cm.getCursor("head"), Pos(0, 4));
  124. eqPos(cm.getCursor("anchor"), Pos(0, 3));
  125. });
  126. testCM("lines", function(cm) {
  127. eq(cm.getLine(0), "111111");
  128. eq(cm.getLine(1), "222222");
  129. eq(cm.getLine(-1), null);
  130. cm.removeLine(1);
  131. cm.setLine(1, "abc");
  132. eq(cm.getValue(), "111111\nabc");
  133. }, {value: "111111\n222222\n333333"});
  134. testCM("indent", function(cm) {
  135. cm.indentLine(1);
  136. eq(cm.getLine(1), " blah();");
  137. cm.setOption("indentUnit", 8);
  138. cm.indentLine(1);
  139. eq(cm.getLine(1), "\tblah();");
  140. cm.setOption("indentUnit", 10);
  141. cm.setOption("tabSize", 4);
  142. cm.indentLine(1);
  143. eq(cm.getLine(1), "\t\t blah();");
  144. }, {value: "if (x) {\nblah();\n}", indentUnit: 3, indentWithTabs: true, tabSize: 8});
  145. test("core_defaults", function() {
  146. var defsCopy = {}, defs = CodeMirror.defaults;
  147. for (var opt in defs) defsCopy[opt] = defs[opt];
  148. defs.indentUnit = 5;
  149. defs.value = "uu";
  150. defs.enterMode = "keep";
  151. defs.tabindex = 55;
  152. var place = document.getElementById("testground"), cm = CodeMirror(place);
  153. try {
  154. eq(cm.getOption("indentUnit"), 5);
  155. cm.setOption("indentUnit", 10);
  156. eq(defs.indentUnit, 5);
  157. eq(cm.getValue(), "uu");
  158. eq(cm.getOption("enterMode"), "keep");
  159. eq(cm.getInputField().tabIndex, 55);
  160. }
  161. finally {
  162. for (var opt in defsCopy) defs[opt] = defsCopy[opt];
  163. place.removeChild(cm.getWrapperElement());
  164. }
  165. });
  166. testCM("lineInfo", function(cm) {
  167. eq(cm.lineInfo(-1), null);
  168. var mark = document.createElement("span");
  169. var lh = cm.setGutterMarker(1, "FOO", mark);
  170. var info = cm.lineInfo(1);
  171. eq(info.text, "222222");
  172. eq(info.gutterMarkers.FOO, mark);
  173. eq(info.line, 1);
  174. eq(cm.lineInfo(2).gutterMarkers, null);
  175. cm.setGutterMarker(lh, "FOO", null);
  176. eq(cm.lineInfo(1).gutterMarkers, null);
  177. cm.setGutterMarker(1, "FOO", mark);
  178. cm.setGutterMarker(0, "FOO", mark);
  179. cm.clearGutter("FOO");
  180. eq(cm.lineInfo(0).gutterMarkers, null);
  181. eq(cm.lineInfo(1).gutterMarkers, null);
  182. }, {value: "111111\n222222\n333333"});
  183. testCM("coords", function(cm) {
  184. cm.setSize(null, 100);
  185. addDoc(cm, 32, 200);
  186. var top = cm.charCoords(Pos(0, 0));
  187. var bot = cm.charCoords(Pos(200, 30));
  188. is(top.left < bot.left);
  189. is(top.top < bot.top);
  190. is(top.top < top.bottom);
  191. cm.scrollTo(null, 100);
  192. var top2 = cm.charCoords(Pos(0, 0));
  193. is(top.top > top2.top);
  194. eq(top.left, top2.left);
  195. });
  196. testCM("coordsChar", function(cm) {
  197. addDoc(cm, 35, 70);
  198. for (var ch = 0; ch <= 35; ch += 5) {
  199. for (var line = 0; line < 70; line += 5) {
  200. cm.setCursor(line, ch);
  201. var coords = cm.charCoords(Pos(line, ch));
  202. var pos = cm.coordsChar({left: coords.left, top: coords.top + 5});
  203. eqPos(pos, Pos(line, ch));
  204. }
  205. }
  206. });
  207. testCM("posFromIndex", function(cm) {
  208. cm.setValue(
  209. "This function should\n" +
  210. "convert a zero based index\n" +
  211. "to line and ch."
  212. );
  213. var examples = [
  214. { index: -1, line: 0, ch: 0 }, // <- Tests clipping
  215. { index: 0, line: 0, ch: 0 },
  216. { index: 10, line: 0, ch: 10 },
  217. { index: 39, line: 1, ch: 18 },
  218. { index: 55, line: 2, ch: 7 },
  219. { index: 63, line: 2, ch: 15 },
  220. { index: 64, line: 2, ch: 15 } // <- Tests clipping
  221. ];
  222. for (var i = 0; i < examples.length; i++) {
  223. var example = examples[i];
  224. var pos = cm.posFromIndex(example.index);
  225. eq(pos.line, example.line);
  226. eq(pos.ch, example.ch);
  227. if (example.index >= 0 && example.index < 64)
  228. eq(cm.indexFromPos(pos), example.index);
  229. }
  230. });
  231. testCM("undo", function(cm) {
  232. cm.setLine(0, "def");
  233. eq(cm.historySize().undo, 1);
  234. cm.undo();
  235. eq(cm.getValue(), "abc");
  236. eq(cm.historySize().undo, 0);
  237. eq(cm.historySize().redo, 1);
  238. cm.redo();
  239. eq(cm.getValue(), "def");
  240. eq(cm.historySize().undo, 1);
  241. eq(cm.historySize().redo, 0);
  242. cm.setValue("1\n\n\n2");
  243. cm.clearHistory();
  244. eq(cm.historySize().undo, 0);
  245. for (var i = 0; i < 20; ++i) {
  246. cm.replaceRange("a", Pos(0, 0));
  247. cm.replaceRange("b", Pos(3, 0));
  248. }
  249. eq(cm.historySize().undo, 40);
  250. for (var i = 0; i < 40; ++i)
  251. cm.undo();
  252. eq(cm.historySize().redo, 40);
  253. eq(cm.getValue(), "1\n\n\n2");
  254. }, {value: "abc"});
  255. testCM("undoDepth", function(cm) {
  256. cm.replaceRange("d", Pos(0));
  257. cm.replaceRange("e", Pos(0));
  258. cm.replaceRange("f", Pos(0));
  259. cm.undo(); cm.undo(); cm.undo();
  260. eq(cm.getValue(), "abcd");
  261. }, {value: "abc", undoDepth: 2});
  262. testCM("undoDoesntClearValue", function(cm) {
  263. cm.undo();
  264. eq(cm.getValue(), "x");
  265. }, {value: "x"});
  266. testCM("undoMultiLine", function(cm) {
  267. cm.operation(function() {
  268. cm.replaceRange("x", Pos(0, 0));
  269. cm.replaceRange("y", Pos(1, 0));
  270. });
  271. cm.undo();
  272. eq(cm.getValue(), "abc\ndef\nghi");
  273. cm.operation(function() {
  274. cm.replaceRange("y", Pos(1, 0));
  275. cm.replaceRange("x", Pos(0, 0));
  276. });
  277. cm.undo();
  278. eq(cm.getValue(), "abc\ndef\nghi");
  279. cm.operation(function() {
  280. cm.replaceRange("y", Pos(2, 0));
  281. cm.replaceRange("x", Pos(1, 0));
  282. cm.replaceRange("z", Pos(2, 0));
  283. });
  284. cm.undo();
  285. eq(cm.getValue(), "abc\ndef\nghi", 3);
  286. }, {value: "abc\ndef\nghi"});
  287. testCM("undoComposite", function(cm) {
  288. cm.replaceRange("y", Pos(1));
  289. cm.operation(function() {
  290. cm.replaceRange("x", Pos(0));
  291. cm.replaceRange("z", Pos(2));
  292. });
  293. eq(cm.getValue(), "ax\nby\ncz\n");
  294. cm.undo();
  295. eq(cm.getValue(), "a\nby\nc\n");
  296. cm.undo();
  297. eq(cm.getValue(), "a\nb\nc\n");
  298. cm.redo(); cm.redo();
  299. eq(cm.getValue(), "ax\nby\ncz\n");
  300. }, {value: "a\nb\nc\n"});
  301. testCM("undoSelection", function(cm) {
  302. cm.setSelection(Pos(0, 2), Pos(0, 4));
  303. cm.replaceSelection("");
  304. cm.setCursor(Pos(1, 0));
  305. cm.undo();
  306. eqPos(cm.getCursor(true), Pos(0, 2));
  307. eqPos(cm.getCursor(false), Pos(0, 4));
  308. cm.setCursor(Pos(1, 0));
  309. cm.redo();
  310. eqPos(cm.getCursor(true), Pos(0, 2));
  311. eqPos(cm.getCursor(false), Pos(0, 2));
  312. }, {value: "abcdefgh\n"});
  313. testCM("markTextSingleLine", function(cm) {
  314. forEach([{a: 0, b: 1, c: "", f: 2, t: 5},
  315. {a: 0, b: 4, c: "", f: 0, t: 2},
  316. {a: 1, b: 2, c: "x", f: 3, t: 6},
  317. {a: 4, b: 5, c: "", f: 3, t: 5},
  318. {a: 4, b: 5, c: "xx", f: 3, t: 7},
  319. {a: 2, b: 5, c: "", f: 2, t: 3},
  320. {a: 2, b: 5, c: "abcd", f: 6, t: 7},
  321. {a: 2, b: 6, c: "x", f: null, t: null},
  322. {a: 3, b: 6, c: "", f: null, t: null},
  323. {a: 0, b: 9, c: "hallo", f: null, t: null},
  324. {a: 4, b: 6, c: "x", f: 3, t: 4},
  325. {a: 4, b: 8, c: "", f: 3, t: 4},
  326. {a: 6, b: 6, c: "a", f: 3, t: 6},
  327. {a: 8, b: 9, c: "", f: 3, t: 6}], function(test) {
  328. cm.setValue("1234567890");
  329. var r = cm.markText(Pos(0, 3), Pos(0, 6), {className: "foo"});
  330. cm.replaceRange(test.c, Pos(0, test.a), Pos(0, test.b));
  331. var f = r.find();
  332. eq(f && f.from.ch, test.f); eq(f && f.to.ch, test.t);
  333. });
  334. });
  335. testCM("markTextMultiLine", function(cm) {
  336. function p(v) { return v && Pos(v[0], v[1]); }
  337. forEach([{a: [0, 0], b: [0, 5], c: "", f: [0, 0], t: [2, 5]},
  338. {a: [0, 0], b: [0, 5], c: "foo\n", f: [1, 0], t: [3, 5]},
  339. {a: [0, 1], b: [0, 10], c: "", f: [0, 1], t: [2, 5]},
  340. {a: [0, 5], b: [0, 6], c: "x", f: [0, 6], t: [2, 5]},
  341. {a: [0, 0], b: [1, 0], c: "", f: [0, 0], t: [1, 5]},
  342. {a: [0, 6], b: [2, 4], c: "", f: [0, 5], t: [0, 7]},
  343. {a: [0, 6], b: [2, 4], c: "aa", f: [0, 5], t: [0, 9]},
  344. {a: [1, 2], b: [1, 8], c: "", f: [0, 5], t: [2, 5]},
  345. {a: [0, 5], b: [2, 5], c: "xx", f: null, t: null},
  346. {a: [0, 0], b: [2, 10], c: "x", f: null, t: null},
  347. {a: [1, 5], b: [2, 5], c: "", f: [0, 5], t: [1, 5]},
  348. {a: [2, 0], b: [2, 3], c: "", f: [0, 5], t: [2, 2]},
  349. {a: [2, 5], b: [3, 0], c: "a\nb", f: [0, 5], t: [2, 5]},
  350. {a: [2, 3], b: [3, 0], c: "x", f: [0, 5], t: [2, 3]},
  351. {a: [1, 1], b: [1, 9], c: "1\n2\n3", f: [0, 5], t: [4, 5]}], function(test) {
  352. cm.setValue("aaaaaaaaaa\nbbbbbbbbbb\ncccccccccc\ndddddddd\n");
  353. var r = cm.markText(Pos(0, 5), Pos(2, 5),
  354. {className: "CodeMirror-matchingbracket"});
  355. cm.replaceRange(test.c, p(test.a), p(test.b));
  356. var f = r.find();
  357. eqPos(f && f.from, p(test.f)); eqPos(f && f.to, p(test.t));
  358. });
  359. });
  360. testCM("markTextUndo", function(cm) {
  361. var marker1, marker2, bookmark;
  362. marker1 = cm.markText(Pos(0, 1), Pos(0, 3),
  363. {className: "CodeMirror-matchingbracket"});
  364. marker2 = cm.markText(Pos(0, 0), Pos(2, 1),
  365. {className: "CodeMirror-matchingbracket"});
  366. bookmark = cm.setBookmark(Pos(1, 5));
  367. cm.operation(function(){
  368. cm.replaceRange("foo", Pos(0, 2));
  369. cm.replaceRange("bar\nbaz\nbug\n", Pos(2, 0), Pos(3, 0));
  370. });
  371. var v1 = cm.getValue();
  372. cm.setValue("");
  373. eq(marker1.find(), null); eq(marker2.find(), null); eq(bookmark.find(), null);
  374. cm.undo();
  375. eqPos(bookmark.find(), Pos(1, 5), "still there");
  376. cm.undo();
  377. var m1Pos = marker1.find(), m2Pos = marker2.find();
  378. eqPos(m1Pos.from, Pos(0, 1)); eqPos(m1Pos.to, Pos(0, 3));
  379. eqPos(m2Pos.from, Pos(0, 0)); eqPos(m2Pos.to, Pos(2, 1));
  380. eqPos(bookmark.find(), Pos(1, 5));
  381. cm.redo(); cm.redo();
  382. eq(bookmark.find(), null);
  383. cm.undo();
  384. eqPos(bookmark.find(), Pos(1, 5));
  385. eq(cm.getValue(), v1);
  386. }, {value: "1234\n56789\n00\n"});
  387. testCM("markTextStayGone", function(cm) {
  388. var m1 = cm.markText(Pos(0, 0), Pos(0, 1));
  389. cm.replaceRange("hi", Pos(0, 2));
  390. m1.clear();
  391. cm.undo();
  392. eq(m1.find(), null);
  393. }, {value: "hello"});
  394. testCM("undoPreservesNewMarks", function(cm) {
  395. cm.markText(Pos(0, 3), Pos(0, 4));
  396. cm.markText(Pos(1, 1), Pos(1, 3));
  397. cm.replaceRange("", Pos(0, 3), Pos(3, 1));
  398. var mBefore = cm.markText(Pos(0, 0), Pos(0, 1));
  399. var mAfter = cm.markText(Pos(0, 5), Pos(0, 6));
  400. var mAround = cm.markText(Pos(0, 2), Pos(0, 4));
  401. cm.undo();
  402. eqPos(mBefore.find().from, Pos(0, 0));
  403. eqPos(mBefore.find().to, Pos(0, 1));
  404. eqPos(mAfter.find().from, Pos(3, 3));
  405. eqPos(mAfter.find().to, Pos(3, 4));
  406. eqPos(mAround.find().from, Pos(0, 2));
  407. eqPos(mAround.find().to, Pos(3, 2));
  408. var found = cm.findMarksAt(Pos(2, 2));
  409. eq(found.length, 1);
  410. eq(found[0], mAround);
  411. }, {value: "aaaa\nbbbb\ncccc\ndddd"});
  412. testCM("markClearBetween", function(cm) {
  413. cm.setValue("aaa\nbbb\nccc\nddd\n");
  414. cm.markText(Pos(0, 0), Pos(2));
  415. cm.replaceRange("aaa\nbbb\nccc", Pos(0, 0), Pos(2));
  416. eq(cm.findMarksAt(Pos(1, 1)).length, 0);
  417. });
  418. testCM("bookmark", function(cm) {
  419. function p(v) { return v && Pos(v[0], v[1]); }
  420. forEach([{a: [1, 0], b: [1, 1], c: "", d: [1, 4]},
  421. {a: [1, 1], b: [1, 1], c: "xx", d: [1, 7]},
  422. {a: [1, 4], b: [1, 5], c: "ab", d: [1, 6]},
  423. {a: [1, 4], b: [1, 6], c: "", d: null},
  424. {a: [1, 5], b: [1, 6], c: "abc", d: [1, 5]},
  425. {a: [1, 6], b: [1, 8], c: "", d: [1, 5]},
  426. {a: [1, 4], b: [1, 4], c: "\n\n", d: [3, 1]},
  427. {bm: [1, 9], a: [1, 1], b: [1, 1], c: "\n", d: [2, 8]}], function(test) {
  428. cm.setValue("1234567890\n1234567890\n1234567890");
  429. var b = cm.setBookmark(p(test.bm) || Pos(1, 5));
  430. cm.replaceRange(test.c, p(test.a), p(test.b));
  431. eqPos(b.find(), p(test.d));
  432. });
  433. });
  434. testCM("bookmarkInsertLeft", function(cm) {
  435. var br = cm.setBookmark(Pos(0, 2), {insertLeft: false});
  436. var bl = cm.setBookmark(Pos(0, 2), {insertLeft: true});
  437. cm.setCursor(Pos(0, 2));
  438. cm.replaceSelection("hi");
  439. eqPos(br.find(), Pos(0, 2));
  440. eqPos(bl.find(), Pos(0, 4));
  441. cm.replaceRange("", Pos(0, 4), Pos(0, 5));
  442. cm.replaceRange("", Pos(0, 2), Pos(0, 4));
  443. cm.replaceRange("", Pos(0, 1), Pos(0, 2));
  444. // Verify that deleting next to bookmarks doesn't kill them
  445. eqPos(br.find(), Pos(0, 1));
  446. eqPos(bl.find(), Pos(0, 1));
  447. }, {value: "abcdef"});
  448. testCM("getAllMarks", function(cm) {
  449. addDoc(cm, 10, 10);
  450. var m1 = cm.setBookmark(Pos(0, 2));
  451. var m2 = cm.markText(Pos(0, 2), Pos(3, 2));
  452. var m3 = cm.markText(Pos(1, 2), Pos(1, 8));
  453. var m4 = cm.markText(Pos(8, 0), Pos(9, 0));
  454. eq(cm.getAllMarks().length, 4);
  455. m1.clear();
  456. m3.clear();
  457. eq(cm.getAllMarks().length, 2);
  458. });
  459. testCM("bug577", function(cm) {
  460. cm.setValue("a\nb");
  461. cm.clearHistory();
  462. cm.setValue("fooooo");
  463. cm.undo();
  464. });
  465. testCM("scrollSnap", function(cm) {
  466. cm.setSize(100, 100);
  467. addDoc(cm, 200, 200);
  468. cm.setCursor(Pos(100, 180));
  469. var info = cm.getScrollInfo();
  470. is(info.left > 0 && info.top > 0);
  471. cm.setCursor(Pos(0, 0));
  472. info = cm.getScrollInfo();
  473. is(info.left == 0 && info.top == 0, "scrolled clean to top");
  474. cm.setCursor(Pos(100, 180));
  475. cm.setCursor(Pos(199, 0));
  476. info = cm.getScrollInfo();
  477. is(info.left == 0 && info.top + 2 > info.height - cm.getScrollerElement().clientHeight, "scrolled clean to bottom");
  478. });
  479. testCM("selectionPos", function(cm) {
  480. if (phantom) return;
  481. cm.setSize(100, 100);
  482. addDoc(cm, 200, 100);
  483. cm.setSelection(Pos(1, 100), Pos(98, 100));
  484. var lineWidth = cm.charCoords(Pos(0, 200), "local").left;
  485. var lineHeight = (cm.charCoords(Pos(99)).top - cm.charCoords(Pos(0)).top) / 100;
  486. cm.scrollTo(0, 0);
  487. var selElt = byClassName(cm.getWrapperElement(), "CodeMirror-selected");
  488. var outer = cm.getWrapperElement().getBoundingClientRect();
  489. var sawMiddle, sawTop, sawBottom;
  490. for (var i = 0, e = selElt.length; i < e; ++i) {
  491. var box = selElt[i].getBoundingClientRect();
  492. var atLeft = box.left - outer.left < 30;
  493. var width = box.right - box.left;
  494. var atRight = box.right - outer.left > .8 * lineWidth;
  495. if (atLeft && atRight) {
  496. sawMiddle = true;
  497. is(box.bottom - box.top > 90 * lineHeight, "middle high");
  498. is(width > .9 * lineWidth, "middle wide");
  499. } else {
  500. is(width > .4 * lineWidth, "top/bot wide enough");
  501. is(width < .6 * lineWidth, "top/bot slim enough");
  502. if (atLeft) {
  503. sawBottom = true;
  504. is(box.top - outer.top > 96 * lineHeight, "bot below");
  505. } else if (atRight) {
  506. sawTop = true;
  507. is(box.top - outer.top < 2.1 * lineHeight, "top above");
  508. }
  509. }
  510. }
  511. is(sawTop && sawBottom && sawMiddle, "all parts");
  512. }, null);
  513. testCM("restoreHistory", function(cm) {
  514. cm.setValue("abc\ndef");
  515. cm.setLine(1, "hello");
  516. cm.setLine(0, "goop");
  517. cm.undo();
  518. var storedVal = cm.getValue(), storedHist = cm.getHistory();
  519. if (window.JSON) storedHist = JSON.parse(JSON.stringify(storedHist));
  520. eq(storedVal, "abc\nhello");
  521. cm.setValue("");
  522. cm.clearHistory();
  523. eq(cm.historySize().undo, 0);
  524. cm.setValue(storedVal);
  525. cm.setHistory(storedHist);
  526. cm.redo();
  527. eq(cm.getValue(), "goop\nhello");
  528. cm.undo(); cm.undo();
  529. eq(cm.getValue(), "abc\ndef");
  530. });
  531. testCM("doubleScrollbar", function(cm) {
  532. var dummy = document.body.appendChild(document.createElement("p"));
  533. dummy.style.cssText = "height: 50px; overflow: scroll; width: 50px";
  534. var scrollbarWidth = dummy.offsetWidth + 1 - dummy.clientWidth;
  535. document.body.removeChild(dummy);
  536. if (scrollbarWidth < 2) return;
  537. cm.setSize(null, 100);
  538. addDoc(cm, 1, 300);
  539. var wrap = cm.getWrapperElement();
  540. is(wrap.offsetWidth - byClassName(wrap, "CodeMirror-lines")[0].offsetWidth <= scrollbarWidth * 1.5);
  541. });
  542. testCM("weirdLinebreaks", function(cm) {
  543. cm.setValue("foo\nbar\rbaz\r\nquux\n\rplop");
  544. is(cm.getValue(), "foo\nbar\nbaz\nquux\n\nplop");
  545. is(cm.lineCount(), 6);
  546. cm.setValue("\n\n");
  547. is(cm.lineCount(), 3);
  548. });
  549. testCM("setSize", function(cm) {
  550. cm.setSize(100, 100);
  551. var wrap = cm.getWrapperElement();
  552. is(wrap.offsetWidth, 100);
  553. is(wrap.offsetHeight, 100);
  554. cm.setSize("100%", "3em");
  555. is(wrap.style.width, "100%");
  556. is(wrap.style.height, "3em");
  557. cm.setSize(null, 40);
  558. is(wrap.style.width, "100%");
  559. is(wrap.style.height, "40px");
  560. });
  561. function foldLines(cm, start, end, autoClear) {
  562. return cm.markText(Pos(start, 0), Pos(end - 1), {
  563. inclusiveLeft: true,
  564. inclusiveRight: true,
  565. collapsed: true,
  566. clearOnEnter: autoClear
  567. });
  568. }
  569. testCM("collapsedLines", function(cm) {
  570. addDoc(cm, 4, 10);
  571. var range = foldLines(cm, 4, 5), cleared = 0;
  572. CodeMirror.on(range, "clear", function() {cleared++;});
  573. cm.setCursor(Pos(3, 0));
  574. CodeMirror.commands.goLineDown(cm);
  575. eqPos(cm.getCursor(), Pos(5, 0));
  576. cm.setLine(3, "abcdefg");
  577. cm.setCursor(Pos(3, 6));
  578. CodeMirror.commands.goLineDown(cm);
  579. eqPos(cm.getCursor(), Pos(5, 4));
  580. cm.setLine(3, "ab");
  581. cm.setCursor(Pos(3, 2));
  582. CodeMirror.commands.goLineDown(cm);
  583. eqPos(cm.getCursor(), Pos(5, 2));
  584. cm.operation(function() {range.clear(); range.clear();});
  585. eq(cleared, 1);
  586. });
  587. testCM("collapsedRangeCoordsChar", function(cm) {
  588. var pos_1_3 = cm.charCoords(Pos(1, 3));
  589. pos_1_3.left += 2; pos_1_3.top += 2;
  590. var opts = {collapsed: true, inclusiveLeft: true, inclusiveRight: true};
  591. var m1 = cm.markText(Pos(0, 0), Pos(2, 0), opts);
  592. eqPos(cm.coordsChar(pos_1_3), Pos(3, 3));
  593. m1.clear();
  594. var m1 = cm.markText(Pos(0, 0), Pos(1, 1), opts);
  595. var m2 = cm.markText(Pos(1, 1), Pos(2, 0), opts);
  596. eqPos(cm.coordsChar(pos_1_3), Pos(3, 3));
  597. m1.clear(); m2.clear();
  598. var m1 = cm.markText(Pos(0, 0), Pos(1, 6), opts);
  599. eqPos(cm.coordsChar(pos_1_3), Pos(3, 3));
  600. }, {value: "123456\nabcdef\nghijkl\nmnopqr\n"});
  601. testCM("hiddenLinesAutoUnfold", function(cm) {
  602. var range = foldLines(cm, 1, 3, true), cleared = 0;
  603. CodeMirror.on(range, "clear", function() {cleared++;});
  604. cm.setCursor(Pos(3, 0));
  605. eq(cleared, 0);
  606. cm.execCommand("goCharLeft");
  607. eq(cleared, 1);
  608. range = foldLines(cm, 1, 3, true);
  609. CodeMirror.on(range, "clear", function() {cleared++;});
  610. eqPos(cm.getCursor(), Pos(3, 0));
  611. cm.setCursor(Pos(0, 3));
  612. cm.execCommand("goCharRight");
  613. eq(cleared, 2);
  614. }, {value: "abc\ndef\nghi\njkl"});
  615. testCM("hiddenLinesSelectAll", function(cm) { // Issue #484
  616. addDoc(cm, 4, 20);
  617. foldLines(cm, 0, 10);
  618. foldLines(cm, 11, 20);
  619. CodeMirror.commands.selectAll(cm);
  620. eqPos(cm.getCursor(true), Pos(10, 0));
  621. eqPos(cm.getCursor(false), Pos(10, 4));
  622. });
  623. testCM("everythingFolded", function(cm) {
  624. addDoc(cm, 2, 2);
  625. function enterPress() {
  626. cm.triggerOnKeyDown({type: "keydown", keyCode: 13, preventDefault: function(){}, stopPropagation: function(){}});
  627. }
  628. var fold = foldLines(cm, 0, 2);
  629. enterPress();
  630. eq(cm.getValue(), "xx\nxx");
  631. fold.clear();
  632. fold = foldLines(cm, 0, 2, true);
  633. eq(fold.find(), null);
  634. enterPress();
  635. eq(cm.getValue(), "\nxx\nxx");
  636. });
  637. testCM("structuredFold", function(cm) {
  638. addDoc(cm, 4, 8);
  639. var range = cm.markText(Pos(1, 2), Pos(6, 2), {
  640. replacedWith: document.createTextNode("Q")
  641. });
  642. cm.setCursor(0, 3);
  643. CodeMirror.commands.goLineDown(cm);
  644. eqPos(cm.getCursor(), Pos(6, 2));
  645. CodeMirror.commands.goCharLeft(cm);
  646. eqPos(cm.getCursor(), Pos(1, 2));
  647. CodeMirror.commands.delCharAfter(cm);
  648. eq(cm.getValue(), "xxxx\nxxxx\nxxxx");
  649. addDoc(cm, 4, 8);
  650. range = cm.markText(Pos(1, 2), Pos(6, 2), {
  651. replacedWith: document.createTextNode("x"),
  652. clearOnEnter: true
  653. });
  654. var cleared = 0;
  655. CodeMirror.on(range, "clear", function(){++cleared;});
  656. cm.setCursor(0, 3);
  657. CodeMirror.commands.goLineDown(cm);
  658. eqPos(cm.getCursor(), Pos(6, 2));
  659. CodeMirror.commands.goCharLeft(cm);
  660. eqPos(cm.getCursor(), Pos(6, 1));
  661. eq(cleared, 1);
  662. range.clear();
  663. eq(cleared, 1);
  664. range = cm.markText(Pos(1, 2), Pos(6, 2), {
  665. replacedWith: document.createTextNode("Q"),
  666. clearOnEnter: true
  667. });
  668. range.clear();
  669. cm.setCursor(1, 2);
  670. CodeMirror.commands.goCharRight(cm);
  671. eqPos(cm.getCursor(), Pos(1, 3));
  672. }, null);
  673. testCM("nestedFold", function(cm) {
  674. addDoc(cm, 10, 3);
  675. function fold(ll, cl, lr, cr) {
  676. return cm.markText(Pos(ll, cl), Pos(lr, cr), {collapsed: true});
  677. }
  678. 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);
  679. cm.setCursor(0, 1);
  680. CodeMirror.commands.goCharRight(cm);
  681. eqPos(cm.getCursor(), Pos(2, 3));
  682. inner0.clear();
  683. CodeMirror.commands.goCharLeft(cm);
  684. eqPos(cm.getCursor(), Pos(0, 1));
  685. outer.clear();
  686. CodeMirror.commands.goCharRight(cm);
  687. eqPos(cm.getCursor(), Pos(0, 2));
  688. CodeMirror.commands.goCharRight(cm);
  689. eqPos(cm.getCursor(), Pos(1, 8));
  690. inner2.clear();
  691. CodeMirror.commands.goCharLeft(cm);
  692. eqPos(cm.getCursor(), Pos(1, 7));
  693. cm.setCursor(0, 5);
  694. CodeMirror.commands.goCharRight(cm);
  695. eqPos(cm.getCursor(), Pos(0, 6));
  696. CodeMirror.commands.goCharRight(cm);
  697. eqPos(cm.getCursor(), Pos(1, 3));
  698. });
  699. testCM("badNestedFold", function(cm) {
  700. addDoc(cm, 4, 4);
  701. cm.markText(Pos(0, 2), Pos(3, 2), {collapsed: true});
  702. var caught;
  703. try {cm.markText(Pos(0, 1), Pos(0, 3), {collapsed: true});}
  704. catch(e) {caught = e;}
  705. is(caught instanceof Error, "no error");
  706. is(/overlap/i.test(caught.message), "wrong error");
  707. });
  708. testCM("inlineWidget", function(cm) {
  709. var w = cm.setBookmark(Pos(0, 2), {widget: document.createTextNode("uu")});
  710. cm.setCursor(0, 2);
  711. CodeMirror.commands.goLineDown(cm);
  712. eqPos(cm.getCursor(), Pos(1, 4));
  713. cm.setCursor(0, 2);
  714. cm.replaceSelection("hi");
  715. eqPos(w.find(), Pos(0, 2));
  716. cm.setCursor(0, 1);
  717. cm.replaceSelection("ay");
  718. eqPos(w.find(), Pos(0, 4));
  719. eq(cm.getLine(0), "uayuhiuu");
  720. }, {value: "uuuu\nuuuuuu"});
  721. testCM("wrappingAndResizing", function(cm) {
  722. cm.setSize(null, "auto");
  723. cm.setOption("lineWrapping", true);
  724. var wrap = cm.getWrapperElement(), h0 = wrap.offsetHeight;
  725. var doc = "xxx xxx xxx xxx xxx";
  726. cm.setValue(doc);
  727. for (var step = 10, w = cm.charCoords(Pos(0, 18), "div").right;; w += step) {
  728. cm.setSize(w);
  729. if (wrap.offsetHeight <= h0 * (opera_lt10 ? 1.2 : 1.5)) {
  730. if (step == 10) { w -= 10; step = 1; }
  731. else break;
  732. }
  733. }
  734. // Ensure that putting the cursor at the end of the maximally long
  735. // line doesn't cause wrapping to happen.
  736. cm.setCursor(Pos(0, doc.length));
  737. eq(wrap.offsetHeight, h0);
  738. cm.replaceSelection("x");
  739. is(wrap.offsetHeight > h0, "wrapping happens");
  740. // Now add a max-height and, in a document consisting of
  741. // almost-wrapped lines, go over it so that a scrollbar appears.
  742. cm.setValue(doc + "\n" + doc + "\n");
  743. cm.getScrollerElement().style.maxHeight = "100px";
  744. 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));
  745. forEach([Pos(0, doc.length), Pos(0, doc.length - 1),
  746. Pos(0, 0), Pos(1, doc.length), Pos(1, doc.length - 1)],
  747. function(pos) {
  748. var coords = cm.charCoords(pos);
  749. eqPos(pos, cm.coordsChar({left: coords.left + 2, top: coords.top + 5}));
  750. });
  751. }, null, ie_lt8);
  752. testCM("measureEndOfLine", function(cm) {
  753. cm.setSize(null, "auto");
  754. var inner = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild;
  755. var lh = inner.offsetHeight;
  756. for (var step = 10, w = cm.charCoords(Pos(0, 7), "div").right;; w += step) {
  757. cm.setSize(w);
  758. if (inner.offsetHeight < 2.5 * lh) {
  759. if (step == 10) { w -= 10; step = 1; }
  760. else break;
  761. }
  762. }
  763. cm.setValue(cm.getValue() + "\n\n");
  764. var endPos = cm.charCoords(Pos(0, 18), "local");
  765. is(endPos.top > lh * .8, "not at top");
  766. is(endPos.left > w - 20, "not at right");
  767. endPos = cm.charCoords(Pos(0, 18));
  768. eqPos(cm.coordsChar({left: endPos.left, top: endPos.top + 5}), Pos(0, 18));
  769. }, {mode: "text/html", value: "<!-- foo barrr -->", lineWrapping: true}, ie_lt8 || opera_lt10);
  770. testCM("scrollVerticallyAndHorizontally", function(cm) {
  771. cm.setSize(100, 100);
  772. addDoc(cm, 40, 40);
  773. cm.setCursor(39);
  774. var wrap = cm.getWrapperElement(), bar = byClassName(wrap, "CodeMirror-vscrollbar")[0];
  775. is(bar.offsetHeight < wrap.offsetHeight, "vertical scrollbar limited by horizontal one");
  776. var cursorBox = byClassName(wrap, "CodeMirror-cursor")[0].getBoundingClientRect();
  777. var editorBox = wrap.getBoundingClientRect();
  778. is(cursorBox.bottom < editorBox.top + cm.getScrollerElement().clientHeight,
  779. "bottom line visible");
  780. }, {lineNumbers: true});
  781. testCM("moveVstuck", function(cm) {
  782. var lines = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild, h0 = lines.offsetHeight;
  783. var val = "fooooooooooooooooooooooooo baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar\n";
  784. cm.setValue(val);
  785. for (var w = cm.charCoords(Pos(0, 26), "div").right * 2.8;; w += 5) {
  786. cm.setSize(w);
  787. if (lines.offsetHeight <= 3.5 * h0) break;
  788. }
  789. cm.setCursor(Pos(0, val.length - 1));
  790. cm.moveV(-1, "line");
  791. eqPos(cm.getCursor(), Pos(0, 26));
  792. }, {lineWrapping: true}, ie_lt8 || opera_lt10);
  793. testCM("clickTab", function(cm) {
  794. var p0 = cm.charCoords(Pos(0, 0));
  795. eqPos(cm.coordsChar({left: p0.left + 5, top: p0.top + 5}), Pos(0, 0));
  796. eqPos(cm.coordsChar({left: p0.right - 5, top: p0.top + 5}), Pos(0, 1));
  797. }, {value: "\t\n\n", lineWrapping: true, tabSize: 8});
  798. testCM("verticalScroll", function(cm) {
  799. cm.setSize(100, 200);
  800. cm.setValue("foo\nbar\nbaz\n");
  801. var sc = cm.getScrollerElement(), baseWidth = sc.scrollWidth;
  802. cm.setLine(0, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah");
  803. is(sc.scrollWidth > baseWidth, "scrollbar present");
  804. cm.setLine(0, "foo");
  805. if (!phantom) eq(sc.scrollWidth, baseWidth, "scrollbar gone");
  806. cm.setLine(0, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah");
  807. cm.setLine(1, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbh");
  808. is(sc.scrollWidth > baseWidth, "present again");
  809. var curWidth = sc.scrollWidth;
  810. cm.setLine(0, "foo");
  811. is(sc.scrollWidth < curWidth, "scrollbar smaller");
  812. is(sc.scrollWidth > baseWidth, "but still present");
  813. });
  814. testCM("extraKeys", function(cm) {
  815. var outcome;
  816. function fakeKey(expected, code, props) {
  817. if (typeof code == "string") code = code.charCodeAt(0);
  818. var e = {type: "keydown", keyCode: code, preventDefault: function(){}, stopPropagation: function(){}};
  819. if (props) for (var n in props) e[n] = props[n];
  820. outcome = null;
  821. cm.triggerOnKeyDown(e);
  822. eq(outcome, expected);
  823. }
  824. CodeMirror.commands.testCommand = function() {outcome = "tc";};
  825. CodeMirror.commands.goTestCommand = function() {outcome = "gtc";};
  826. cm.setOption("extraKeys", {"Shift-X": function() {outcome = "sx";},
  827. "X": function() {outcome = "x";},
  828. "Ctrl-Alt-U": function() {outcome = "cau";},
  829. "End": "testCommand",
  830. "Home": "goTestCommand",
  831. "Tab": false});
  832. fakeKey(null, "U");
  833. fakeKey("cau", "U", {ctrlKey: true, altKey: true});
  834. fakeKey(null, "U", {shiftKey: true, ctrlKey: true, altKey: true});
  835. fakeKey("x", "X");
  836. fakeKey("sx", "X", {shiftKey: true});
  837. fakeKey("tc", 35);
  838. fakeKey(null, 35, {shiftKey: true});
  839. fakeKey("gtc", 36);
  840. fakeKey("gtc", 36, {shiftKey: true});
  841. fakeKey(null, 9);
  842. }, null, window.opera && mac);
  843. testCM("wordMovementCommands", function(cm) {
  844. cm.execCommand("goWordLeft");
  845. eqPos(cm.getCursor(), Pos(0, 0));
  846. cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
  847. eqPos(cm.getCursor(), Pos(0, 7));
  848. cm.execCommand("goWordLeft");
  849. eqPos(cm.getCursor(), Pos(0, 5));
  850. cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
  851. eqPos(cm.getCursor(), Pos(0, 12));
  852. cm.execCommand("goWordLeft");
  853. eqPos(cm.getCursor(), Pos(0, 9));
  854. cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
  855. eqPos(cm.getCursor(), Pos(0, 24));
  856. cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
  857. eqPos(cm.getCursor(), Pos(1, 9));
  858. cm.execCommand("goWordRight");
  859. eqPos(cm.getCursor(), Pos(1, 13));
  860. cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
  861. eqPos(cm.getCursor(), Pos(2, 0));
  862. }, {value: "this is (the) firstline.\na foo12\u00e9\u00f8\u00d7bar\n"});
  863. testCM("groupMovementCommands", function(cm) {
  864. cm.execCommand("goGroupLeft");
  865. eqPos(cm.getCursor(), Pos(0, 0));
  866. cm.execCommand("goGroupRight");
  867. eqPos(cm.getCursor(), Pos(0, 4));
  868. cm.execCommand("goGroupRight");
  869. eqPos(cm.getCursor(), Pos(0, 7));
  870. cm.execCommand("goGroupRight");
  871. eqPos(cm.getCursor(), Pos(0, 10));
  872. cm.execCommand("goGroupLeft");
  873. eqPos(cm.getCursor(), Pos(0, 7));
  874. cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight");
  875. eqPos(cm.getCursor(), Pos(0, 15));
  876. cm.setCursor(Pos(0, 17));
  877. cm.execCommand("goGroupLeft");
  878. eqPos(cm.getCursor(), Pos(0, 16));
  879. cm.execCommand("goGroupLeft");
  880. eqPos(cm.getCursor(), Pos(0, 14));
  881. cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight");
  882. eqPos(cm.getCursor(), Pos(0, 20));
  883. cm.execCommand("goGroupRight");
  884. eqPos(cm.getCursor(), Pos(1, 5));
  885. cm.execCommand("goGroupLeft"); cm.execCommand("goGroupLeft");
  886. eqPos(cm.getCursor(), Pos(1, 0));
  887. cm.execCommand("goGroupLeft");
  888. eqPos(cm.getCursor(), Pos(0, 16));
  889. }, {value: "booo ba---quux. ffff\n abc d"});
  890. testCM("charMovementCommands", function(cm) {
  891. cm.execCommand("goCharLeft"); cm.execCommand("goColumnLeft");
  892. eqPos(cm.getCursor(), Pos(0, 0));
  893. cm.execCommand("goCharRight"); cm.execCommand("goCharRight");
  894. eqPos(cm.getCursor(), Pos(0, 2));
  895. cm.setCursor(Pos(1, 0));
  896. cm.execCommand("goColumnLeft");
  897. eqPos(cm.getCursor(), Pos(1, 0));
  898. cm.execCommand("goCharLeft");
  899. eqPos(cm.getCursor(), Pos(0, 5));
  900. cm.execCommand("goColumnRight");
  901. eqPos(cm.getCursor(), Pos(0, 5));
  902. cm.execCommand("goCharRight");
  903. eqPos(cm.getCursor(), Pos(1, 0));
  904. cm.execCommand("goLineEnd");
  905. eqPos(cm.getCursor(), Pos(1, 5));
  906. cm.execCommand("goLineStartSmart");
  907. eqPos(cm.getCursor(), Pos(1, 1));
  908. cm.execCommand("goLineStartSmart");
  909. eqPos(cm.getCursor(), Pos(1, 0));
  910. cm.setCursor(Pos(2, 0));
  911. cm.execCommand("goCharRight"); cm.execCommand("goColumnRight");
  912. eqPos(cm.getCursor(), Pos(2, 0));
  913. }, {value: "line1\n ine2\n"});
  914. testCM("verticalMovementCommands", function(cm) {
  915. cm.execCommand("goLineUp");
  916. eqPos(cm.getCursor(), Pos(0, 0));
  917. cm.execCommand("goLineDown");
  918. if (!phantom) // This fails in PhantomJS, though not in a real Webkit
  919. eqPos(cm.getCursor(), Pos(1, 0));
  920. cm.setCursor(Pos(1, 12));
  921. cm.execCommand("goLineDown");
  922. eqPos(cm.getCursor(), Pos(2, 5));
  923. cm.execCommand("goLineDown");
  924. eqPos(cm.getCursor(), Pos(3, 0));
  925. cm.execCommand("goLineUp");
  926. eqPos(cm.getCursor(), Pos(2, 5));
  927. cm.execCommand("goLineUp");
  928. eqPos(cm.getCursor(), Pos(1, 12));
  929. cm.execCommand("goPageDown");
  930. eqPos(cm.getCursor(), Pos(5, 0));
  931. cm.execCommand("goPageDown"); cm.execCommand("goLineDown");
  932. eqPos(cm.getCursor(), Pos(5, 0));
  933. cm.execCommand("goPageUp");
  934. eqPos(cm.getCursor(), Pos(0, 0));
  935. }, {value: "line1\nlong long line2\nline3\n\nline5\n"});
  936. testCM("verticalMovementCommandsWrapping", function(cm) {
  937. cm.setSize(120);
  938. cm.setCursor(Pos(0, 5));
  939. cm.execCommand("goLineDown");
  940. eq(cm.getCursor().line, 0);
  941. is(cm.getCursor().ch > 5, "moved beyond wrap");
  942. for (var i = 0; ; ++i) {
  943. is(i < 20, "no endless loop");
  944. cm.execCommand("goLineDown");
  945. var cur = cm.getCursor();
  946. if (cur.line == 1) eq(cur.ch, 5);
  947. if (cur.line == 2) { eq(cur.ch, 1); break; }
  948. }
  949. }, {value: "a very long line that wraps around somehow so that we can test cursor movement\nshortone\nk",
  950. lineWrapping: true});
  951. testCM("rtlMovement", function(cm) {
  952. forEach(["خحج", "خحabcخحج", "abخحخحجcd", "abخde", "abخح2342خ1حج", "خ1ح2خح3حxج",
  953. "خحcd", "1خحcd", "abcdeح1ج", "خمرحبها مها!", "foobarر",
  954. "<img src=\"/בדיקה3.jpg\">"], function(line) {
  955. var inv = line.charAt(0) == "خ";
  956. cm.setValue(line + "\n"); cm.execCommand(inv ? "goLineEnd" : "goLineStart");
  957. var cursor = byClassName(cm.getWrapperElement(), "CodeMirror-cursor")[0];
  958. var prevX = cursor.offsetLeft, prevY = cursor.offsetTop;
  959. for (var i = 0; i <= line.length; ++i) {
  960. cm.execCommand("goCharRight");
  961. if (i == line.length) is(cursor.offsetTop > prevY, "next line");
  962. else is(cursor.offsetLeft > prevX, "moved right");
  963. prevX = cursor.offsetLeft; prevY = cursor.offsetTop;
  964. }
  965. cm.setCursor(0, 0); cm.execCommand(inv ? "goLineStart" : "goLineEnd");
  966. prevX = cursor.offsetLeft;
  967. for (var i = 0; i < line.length; ++i) {
  968. cm.execCommand("goCharLeft");
  969. is(cursor.offsetLeft < prevX, "moved left");
  970. prevX = cursor.offsetLeft;
  971. }
  972. });
  973. }, {rtlMoveVisually: true});
  974. // Verify that updating a line clears its bidi ordering
  975. testCM("bidiUpdate", function(cm) {
  976. cm.setCursor(Pos(0, 2));
  977. cm.replaceSelection("خحج", "start");
  978. cm.execCommand("goCharRight");
  979. eqPos(cm.getCursor(), Pos(0, 4));
  980. }, {value: "abcd\n"});
  981. testCM("movebyTextUnit", function(cm) {
  982. cm.setValue("בְּרֵאשִ\ńéée\n");
  983. cm.execCommand("goLineEnd");
  984. for (var i = 0; i < 4; ++i) cm.execCommand("goCharRight");
  985. eqPos(cm.getCursor(), Pos(0, 0));
  986. cm.execCommand("goCharRight");
  987. eqPos(cm.getCursor(), Pos(1, 0));
  988. cm.execCommand("goCharRight");
  989. cm.execCommand("goCharRight");
  990. eqPos(cm.getCursor(), Pos(1, 3));
  991. cm.execCommand("goCharRight");
  992. cm.execCommand("goCharRight");
  993. eqPos(cm.getCursor(), Pos(1, 6));
  994. });
  995. testCM("lineChangeEvents", function(cm) {
  996. addDoc(cm, 3, 5);
  997. var log = [], want = ["ch 0", "ch 1", "del 2", "ch 0", "ch 0", "del 1", "del 3", "del 4"];
  998. for (var i = 0; i < 5; ++i) {
  999. CodeMirror.on(cm.getLineHandle(i), "delete", function(i) {
  1000. return function() {log.push("del " + i);};
  1001. }(i));
  1002. CodeMirror.on(cm.getLineHandle(i), "change", function(i) {
  1003. return function() {log.push("ch " + i);};
  1004. }(i));
  1005. }
  1006. cm.replaceRange("x", Pos(0, 1));
  1007. cm.replaceRange("xy", Pos(1, 1), Pos(2));
  1008. cm.replaceRange("foo\nbar", Pos(0, 1));
  1009. cm.replaceRange("", Pos(0, 0), Pos(cm.lineCount()));
  1010. eq(log.length, want.length, "same length");
  1011. for (var i = 0; i < log.length; ++i)
  1012. eq(log[i], want[i]);
  1013. });
  1014. testCM("scrollEntirelyToRight", function(cm) {
  1015. if (phantom) return;
  1016. addDoc(cm, 500, 2);
  1017. cm.setCursor(Pos(0, 500));
  1018. var wrap = cm.getWrapperElement(), cur = byClassName(wrap, "CodeMirror-cursor")[0];
  1019. is(wrap.getBoundingClientRect().right > cur.getBoundingClientRect().left);
  1020. });
  1021. testCM("lineWidgets", function(cm) {
  1022. addDoc(cm, 500, 3);
  1023. var last = cm.charCoords(Pos(2, 0));
  1024. var node = document.createElement("div");
  1025. node.innerHTML = "hi";
  1026. var widget = cm.addLineWidget(1, node);
  1027. is(last.top < cm.charCoords(Pos(2, 0)).top, "took up space");
  1028. cm.setCursor(Pos(1, 1));
  1029. cm.execCommand("goLineDown");
  1030. eqPos(cm.getCursor(), Pos(2, 1));
  1031. cm.execCommand("goLineUp");
  1032. eqPos(cm.getCursor(), Pos(1, 1));
  1033. });
  1034. testCM("lineWidgetFocus", function(cm) {
  1035. var place = document.getElementById("testground");
  1036. place.className = "offscreen";
  1037. try {
  1038. addDoc(cm, 500, 10);
  1039. var node = document.createElement("input");
  1040. var widget = cm.addLineWidget(1, node);
  1041. node.focus();
  1042. eq(document.activeElement, node);
  1043. cm.replaceRange("new stuff", Pos(1, 0));
  1044. eq(document.activeElement, node);
  1045. } finally {
  1046. place.className = "";
  1047. }
  1048. });
  1049. testCM("getLineNumber", function(cm) {
  1050. addDoc(cm, 2, 20);
  1051. var h1 = cm.getLineHandle(1);
  1052. eq(cm.getLineNumber(h1), 1);
  1053. cm.replaceRange("hi\nbye\n", Pos(0, 0));
  1054. eq(cm.getLineNumber(h1), 3);
  1055. cm.setValue("");
  1056. eq(cm.getLineNumber(h1), null);
  1057. });
  1058. testCM("jumpTheGap", function(cm) {
  1059. var longLine = "abcdef ghiklmnop qrstuvw xyz ";
  1060. longLine += longLine; longLine += longLine; longLine += longLine;
  1061. cm.setLine(2, longLine);
  1062. cm.setSize("200px", null);
  1063. cm.getWrapperElement().style.lineHeight = 2;
  1064. cm.refresh();
  1065. cm.setCursor(Pos(0, 1));
  1066. cm.execCommand("goLineDown");
  1067. eqPos(cm.getCursor(), Pos(1, 1));
  1068. cm.execCommand("goLineDown");
  1069. eqPos(cm.getCursor(), Pos(2, 1));
  1070. cm.execCommand("goLineDown");
  1071. eq(cm.getCursor().line, 2);
  1072. is(cm.getCursor().ch > 1);
  1073. cm.execCommand("goLineUp");
  1074. eqPos(cm.getCursor(), Pos(2, 1));
  1075. cm.execCommand("goLineUp");
  1076. eqPos(cm.getCursor(), Pos(1, 1));
  1077. var node = document.createElement("div");
  1078. node.innerHTML = "hi"; node.style.height = "30px";
  1079. cm.addLineWidget(0, node);
  1080. cm.addLineWidget(1, node.cloneNode(true), {above: true});
  1081. cm.setCursor(Pos(0, 2));
  1082. cm.execCommand("goLineDown");
  1083. eqPos(cm.getCursor(), Pos(1, 2));
  1084. cm.execCommand("goLineUp");
  1085. eqPos(cm.getCursor(), Pos(0, 2));
  1086. }, {lineWrapping: true, value: "abc\ndef\nghi\njkl\n"});
  1087. testCM("addLineClass", function(cm) {
  1088. function cls(line, text, bg, wrap) {
  1089. var i = cm.lineInfo(line);
  1090. eq(i.textClass, text);
  1091. eq(i.bgClass, bg);
  1092. eq(i.wrapClass, wrap);
  1093. }
  1094. cm.addLineClass(0, "text", "foo");
  1095. cm.addLineClass(0, "text", "bar");
  1096. cm.addLineClass(1, "background", "baz");
  1097. cm.addLineClass(1, "wrap", "foo");
  1098. cls(0, "foo bar", null, null);
  1099. cls(1, null, "baz", "foo");
  1100. var lines = cm.display.lineDiv;
  1101. eq(byClassName(lines, "foo").length, 2);
  1102. eq(byClassName(lines, "bar").length, 1);
  1103. eq(byClassName(lines, "baz").length, 1);
  1104. cm.removeLineClass(0, "text", "foo");
  1105. cls(0, "bar", null, null);
  1106. cm.removeLineClass(0, "text", "foo");
  1107. cls(0, "bar", null, null);
  1108. cm.removeLineClass(0, "text", "bar");
  1109. cls(0, null, null, null);
  1110. cm.addLineClass(1, "wrap", "quux");
  1111. cls(1, null, "baz", "foo quux");
  1112. cm.removeLineClass(1, "wrap");
  1113. cls(1, null, "baz", null);
  1114. }, {value: "hohoho\n"});
  1115. testCM("atomicMarker", function(cm) {
  1116. addDoc(cm, 10, 10);
  1117. function atom(ll, cl, lr, cr, li, ri) {
  1118. return cm.markText(Pos(ll, cl), Pos(lr, cr),
  1119. {atomic: true, inclusiveLeft: li, inclusiveRight: ri});
  1120. }
  1121. var m = atom(0, 1, 0, 5);
  1122. cm.setCursor(Pos(0, 1));
  1123. cm.execCommand("goCharRight");
  1124. eqPos(cm.getCursor(), Pos(0, 5));
  1125. cm.execCommand("goCharLeft");
  1126. eqPos(cm.getCursor(), Pos(0, 1));
  1127. m.clear();
  1128. m = atom(0, 0, 0, 5, true);
  1129. eqPos(cm.getCursor(), Pos(0, 5), "pushed out");
  1130. cm.execCommand("goCharLeft");
  1131. eqPos(cm.getCursor(), Pos(0, 5));
  1132. m.clear();
  1133. m = atom(8, 4, 9, 10, false, true);
  1134. cm.setCursor(Pos(9, 8));
  1135. eqPos(cm.getCursor(), Pos(8, 4), "set");
  1136. cm.execCommand("goCharRight");
  1137. eqPos(cm.getCursor(), Pos(8, 4), "char right");
  1138. cm.execCommand("goLineDown");
  1139. eqPos(cm.getCursor(), Pos(8, 4), "line down");
  1140. cm.execCommand("goCharLeft");
  1141. eqPos(cm.getCursor(), Pos(8, 3));
  1142. m.clear();
  1143. m = atom(1, 1, 3, 8);
  1144. cm.setCursor(Pos(2, 0));
  1145. eqPos(cm.getCursor(), Pos(3, 8));
  1146. cm.execCommand("goCharLeft");
  1147. eqPos(cm.getCursor(), Pos(1, 1));
  1148. cm.execCommand("goCharRight");
  1149. eqPos(cm.getCursor(), Pos(3, 8));
  1150. cm.execCommand("goLineUp");
  1151. eqPos(cm.getCursor(), Pos(1, 1));
  1152. cm.execCommand("goLineDown");
  1153. eqPos(cm.getCursor(), Pos(3, 8));
  1154. cm.execCommand("delCharBefore");
  1155. eq(cm.getValue().length, 80, "del chunk");
  1156. m = atom(3, 0, 5, 5);
  1157. cm.setCursor(Pos(3, 0));
  1158. cm.execCommand("delWordAfter");
  1159. eq(cm.getValue().length, 53, "del chunk");
  1160. });
  1161. testCM("readOnlyMarker", function(cm) {
  1162. function mark(ll, cl, lr, cr, at) {
  1163. return cm.markText(Pos(ll, cl), Pos(lr, cr),
  1164. {readOnly: true, atomic: at});
  1165. }
  1166. var m = mark(0, 1, 0, 4);
  1167. cm.setCursor(Pos(0, 2));
  1168. cm.replaceSelection("hi", "end");
  1169. eqPos(cm.getCursor(), Pos(0, 2));
  1170. eq(cm.getLine(0), "abcde");
  1171. cm.execCommand("selectAll");
  1172. cm.replaceSelection("oops");
  1173. eq(cm.getValue(), "oopsbcd");
  1174. cm.undo();
  1175. eqPos(m.find().from, Pos(0, 1));
  1176. eqPos(m.find().to, Pos(0, 4));
  1177. m.clear();
  1178. cm.setCursor(Pos(0, 2));
  1179. cm.replaceSelection("hi");
  1180. eq(cm.getLine(0), "abhicde");
  1181. eqPos(cm.getCursor(), Pos(0, 4));
  1182. m = mark(0, 2, 2, 2, true);
  1183. cm.setSelection(Pos(1, 1), Pos(2, 4));
  1184. cm.replaceSelection("t", "end");
  1185. eqPos(cm.getCursor(), Pos(2, 3));
  1186. eq(cm.getLine(2), "klto");
  1187. cm.execCommand("goCharLeft");
  1188. cm.execCommand("goCharLeft");
  1189. eqPos(cm.getCursor(), Pos(0, 2));
  1190. cm.setSelection(Pos(0, 1), Pos(0, 3));
  1191. cm.replaceSelection("xx");
  1192. eqPos(cm.getCursor(), Pos(0, 3));
  1193. eq(cm.getLine(0), "axxhicde");
  1194. }, {value: "abcde\nfghij\nklmno\n"});
  1195. testCM("dirtyBit", function(cm) {
  1196. eq(cm.isClean(), true);
  1197. cm.replaceSelection("boo");
  1198. eq(cm.isClean(), false);
  1199. cm.undo();
  1200. eq(cm.isClean(), true);
  1201. cm.replaceSelection("boo");
  1202. cm.replaceSelection("baz");
  1203. cm.undo();
  1204. eq(cm.isClean(), false);
  1205. cm.markClean();
  1206. eq(cm.isClean(), true);
  1207. cm.undo();
  1208. eq(cm.isClean(), false);
  1209. cm.redo();
  1210. eq(cm.isClean(), true);
  1211. });
  1212. testCM("addKeyMap", function(cm) {
  1213. function sendKey(code) {
  1214. cm.triggerOnKeyDown({type: "keydown", keyCode: code,
  1215. preventDefault: function(){}, stopPropagation: function(){}});
  1216. }
  1217. sendKey(39);
  1218. eqPos(cm.getCursor(), Pos(0, 1));
  1219. var test = 0;
  1220. var map1 = {Right: function() { ++test; }}, map2 = {Right: function() { test += 10; }}
  1221. cm.addKeyMap(map1);
  1222. sendKey(39);
  1223. eqPos(cm.getCursor(), Pos(0, 1));
  1224. eq(test, 1);
  1225. cm.addKeyMap(map2, true);
  1226. sendKey(39);
  1227. eq(test, 2);
  1228. cm.removeKeyMap(map1);
  1229. sendKey(39);
  1230. eq(test, 12);
  1231. cm.removeKeyMap(map2);
  1232. sendKey(39);
  1233. eq(test, 12);
  1234. eqPos(cm.getCursor(), Pos(0, 2));
  1235. cm.addKeyMap({Right: function() { test = 55; }, name: "mymap"});
  1236. sendKey(39);
  1237. eq(test, 55);
  1238. cm.removeKeyMap("mymap");
  1239. sendKey(39);
  1240. eqPos(cm.getCursor(), Pos(0, 3));
  1241. }, {value: "abc"});
  1242. testCM("findPosH", function(cm) {
  1243. forEach([{from: Pos(0, 0), to: Pos(0, 1), by: 1},
  1244. {from: Pos(0, 0), to: Pos(0, 0), by: -1, hitSide: true},
  1245. {from: Pos(0, 0), to: Pos(0, 4), by: 1, unit: "word"},
  1246. {from: Pos(0, 0), to: Pos(0, 8), by: 2, unit: "word"},
  1247. {from: Pos(0, 0), to: Pos(2, 0), by: 20, unit: "word", hitSide: true},
  1248. {from: Pos(0, 7), to: Pos(0, 5), by: -1, unit: "word"},
  1249. {from: Pos(0, 4), to: Pos(0, 8), by: 1, unit: "word"},
  1250. {from: Pos(1, 0), to: Pos(1, 18), by: 3, unit: "word"},
  1251. {from: Pos(1, 22), to: Pos(1, 5), by: -3, unit: "word"},
  1252. {from: Pos(1, 15), to: Pos(1, 10), by: -5},
  1253. {from: Pos(1, 15), to: Pos(1, 10), by: -5, unit: "column"},
  1254. {from: Pos(1, 15), to: Pos(1, 0), by: -50, unit: "column", hitSide: true},
  1255. {from: Pos(1, 15), to: Pos(1, 24), by: 50, unit: "column", hitSide: true},
  1256. {from: Pos(1, 15), to: Pos(2, 0), by: 50, hitSide: true}], function(t) {
  1257. var r = cm.findPosH(t.from, t.by, t.unit || "char");
  1258. eqPos(r, t.to);
  1259. eq(!!r.hitSide, !!t.hitSide);
  1260. });
  1261. }, {value: "line one\nline two.something.other\n"});
  1262. testCM("beforeChange", function(cm) {
  1263. cm.on("beforeChange", function(cm, change) {
  1264. var text = [];
  1265. for (var i = 0; i < change.text.length; ++i)
  1266. text.push(change.text[i].replace(/\s/g, "_"));
  1267. change.update(null, null, text);
  1268. });
  1269. cm.setValue("hello, i am a\nnew document\n");
  1270. eq(cm.getValue(), "hello,_i_am_a\nnew_document\n");
  1271. CodeMirror.on(cm.getDoc(), "beforeChange", function(doc, change) {
  1272. if (change.from.line == 0) change.cancel();
  1273. });
  1274. cm.setValue("oops"); // Canceled
  1275. eq(cm.getValue(), "hello,_i_am_a\nnew_document\n");
  1276. cm.replaceRange("hey hey hey", Pos(1, 0), Pos(2, 0));
  1277. eq(cm.getValue(), "hello,_i_am_a\nhey_hey_hey");
  1278. }, {value: "abcdefghijk"});
  1279. testCM("beforeSelectionChange", function(cm) {
  1280. function notAtEnd(cm, pos) {
  1281. var len = cm.getLine(pos.line).length;
  1282. if (!len || pos.ch == len) return Pos(pos.line, pos.ch - 1);
  1283. return pos;
  1284. }
  1285. cm.on("beforeSelectionChange", function(cm, sel) {
  1286. sel.head = notAtEnd(cm, sel.head);
  1287. sel.anchor = notAtEnd(cm, sel.anchor);
  1288. });
  1289. addDoc(cm, 10, 10);
  1290. cm.execCommand("goLineEnd");
  1291. eqPos(cm.getCursor(), Pos(0, 9));
  1292. cm.execCommand("selectAll");
  1293. eqPos(cm.getCursor("start"), Pos(0, 0));
  1294. eqPos(cm.getCursor("end"), Pos(9, 9));
  1295. });
  1296. testCM("change_removedText", function(cm) {
  1297. cm.setValue("abc\ndef");
  1298. var removedText;
  1299. cm.on("change", function(cm, change) {
  1300. removedText = [change.removed, change.next && change.next.removed];
  1301. });
  1302. cm.operation(function() {
  1303. cm.replaceRange("xyz", Pos(0, 0), Pos(1,1));
  1304. cm.replaceRange("123", Pos(0,0));
  1305. });
  1306. eq(removedText[0].join("\n"), "abc\nd");
  1307. eq(removedText[1].join("\n"), "");
  1308. cm.undo();
  1309. eq(removedText[0].join("\n"), "123");
  1310. eq(removedText[1].join("\n"), "xyz");
  1311. cm.redo();
  1312. eq(removedText[0].join("\n"), "abc\nd");
  1313. eq(removedText[1].join("\n"), "");
  1314. });