test.js 95 KB

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