test.js 94 KB

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