Mercurial
comparison .cms/lib/codemirror/test/multi_test.js @ 0:78edf6b517a0 draft
24.10
author | Coffee CMS <info@coffee-cms.ru> |
---|---|
date | Fri, 11 Oct 2024 22:40:23 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:78edf6b517a0 |
---|---|
1 (function() { | |
2 namespace = "multi_"; | |
3 | |
4 function hasSelections(cm) { | |
5 var sels = cm.listSelections(); | |
6 var given = (arguments.length - 1) / 4; | |
7 if (sels.length != given) | |
8 throw new Failure("expected " + given + " selections, found " + sels.length); | |
9 for (var i = 0, p = 1; i < given; i++, p += 4) { | |
10 var anchor = Pos(arguments[p], arguments[p + 1]); | |
11 var head = Pos(arguments[p + 2], arguments[p + 3]); | |
12 eqCharPos(sels[i].anchor, anchor, "anchor of selection " + i); | |
13 eqCharPos(sels[i].head, head, "head of selection " + i); | |
14 } | |
15 } | |
16 function hasCursors(cm) { | |
17 var sels = cm.listSelections(); | |
18 var given = (arguments.length - 1) / 2; | |
19 if (sels.length != given) | |
20 throw new Failure("expected " + given + " selections, found " + sels.length); | |
21 for (var i = 0, p = 1; i < given; i++, p += 2) { | |
22 eqCursorPos(sels[i].anchor, sels[i].head, "something selected for " + i); | |
23 var head = Pos(arguments[p], arguments[p + 1]); | |
24 eqCharPos(sels[i].head, head, "selection " + i); | |
25 } | |
26 } | |
27 | |
28 testCM("getSelection", function(cm) { | |
29 select(cm, {anchor: Pos(0, 0), head: Pos(1, 2)}, {anchor: Pos(2, 2), head: Pos(2, 0)}); | |
30 eq(cm.getSelection(), "1234\n56\n90"); | |
31 eq(cm.getSelection(false).join("|"), "1234|56|90"); | |
32 eq(cm.getSelections().join("|"), "1234\n56|90"); | |
33 }, {value: "1234\n5678\n90"}); | |
34 | |
35 testCM("setSelection", function(cm) { | |
36 select(cm, Pos(3, 0), Pos(0, 0), {anchor: Pos(2, 5), head: Pos(1, 0)}); | |
37 hasSelections(cm, 0, 0, 0, 0, | |
38 2, 5, 1, 0, | |
39 3, 0, 3, 0); | |
40 cm.setSelection(Pos(1, 2), Pos(1, 1)); | |
41 hasSelections(cm, 1, 2, 1, 1); | |
42 select(cm, {anchor: Pos(1, 1), head: Pos(2, 4)}, | |
43 {anchor: Pos(0, 0), head: Pos(1, 3)}, | |
44 Pos(3, 0), Pos(2, 2)); | |
45 hasSelections(cm, 0, 0, 2, 4, | |
46 3, 0, 3, 0); | |
47 cm.setSelections([{anchor: Pos(0, 1), head: Pos(0, 2)}, | |
48 {anchor: Pos(1, 1), head: Pos(1, 2)}, | |
49 {anchor: Pos(2, 1), head: Pos(2, 2)}], 1); | |
50 eqCharPos(cm.getCursor("head"), Pos(1, 2)); | |
51 eqCharPos(cm.getCursor("anchor"), Pos(1, 1)); | |
52 eqCharPos(cm.getCursor("from"), Pos(1, 1)); | |
53 eqCharPos(cm.getCursor("to"), Pos(1, 2)); | |
54 cm.setCursor(Pos(1, 1)); | |
55 hasCursors(cm, 1, 1); | |
56 }, {value: "abcde\nabcde\nabcde\n"}); | |
57 | |
58 testCM("somethingSelected", function(cm) { | |
59 select(cm, Pos(0, 1), {anchor: Pos(0, 3), head: Pos(0, 5)}); | |
60 eq(cm.somethingSelected(), true); | |
61 select(cm, Pos(0, 1), Pos(0, 3), Pos(0, 5)); | |
62 eq(cm.somethingSelected(), false); | |
63 }, {value: "123456789"}); | |
64 | |
65 testCM("extendSelection", function(cm) { | |
66 select(cm, Pos(0, 1), Pos(1, 1), Pos(2, 1)); | |
67 cm.setExtending(true); | |
68 cm.extendSelections([Pos(0, 2), Pos(1, 0), Pos(2, 3)]); | |
69 hasSelections(cm, 0, 1, 0, 2, | |
70 1, 1, 1, 0, | |
71 2, 1, 2, 3); | |
72 cm.extendSelection(Pos(2, 4), Pos(2, 0)); | |
73 hasSelections(cm, 2, 4, 2, 0); | |
74 }, {value: "1234\n1234\n1234"}); | |
75 | |
76 testCM("addSelection", function(cm) { | |
77 select(cm, Pos(0, 1), Pos(1, 1)); | |
78 cm.addSelection(Pos(0, 0), Pos(0, 4)); | |
79 hasSelections(cm, 0, 0, 0, 4, | |
80 1, 1, 1, 1); | |
81 cm.addSelection(Pos(2, 2)); | |
82 hasSelections(cm, 0, 0, 0, 4, | |
83 1, 1, 1, 1, | |
84 2, 2, 2, 2); | |
85 }, {value: "1234\n1234\n1234"}); | |
86 | |
87 testCM("replaceSelection", function(cm) { | |
88 var selections = [{anchor: Pos(0, 0), head: Pos(0, 1)}, | |
89 {anchor: Pos(0, 2), head: Pos(0, 3)}, | |
90 {anchor: Pos(0, 4), head: Pos(0, 5)}, | |
91 {anchor: Pos(2, 1), head: Pos(2, 4)}, | |
92 {anchor: Pos(2, 5), head: Pos(2, 6)}]; | |
93 var val = "123456\n123456\n123456"; | |
94 cm.setValue(val); | |
95 cm.setSelections(selections); | |
96 cm.replaceSelection("ab", "around"); | |
97 eq(cm.getValue(), "ab2ab4ab6\n123456\n1ab5ab"); | |
98 hasSelections(cm, 0, 0, 0, 2, | |
99 0, 3, 0, 5, | |
100 0, 6, 0, 8, | |
101 2, 1, 2, 3, | |
102 2, 4, 2, 6); | |
103 cm.setValue(val); | |
104 cm.setSelections(selections); | |
105 cm.replaceSelection("", "around"); | |
106 eq(cm.getValue(), "246\n123456\n15"); | |
107 hasSelections(cm, 0, 0, 0, 0, | |
108 0, 1, 0, 1, | |
109 0, 2, 0, 2, | |
110 2, 1, 2, 1, | |
111 2, 2, 2, 2); | |
112 cm.setValue(val); | |
113 cm.setSelections(selections); | |
114 cm.replaceSelection("X\nY\nZ", "around"); | |
115 hasSelections(cm, 0, 0, 2, 1, | |
116 2, 2, 4, 1, | |
117 4, 2, 6, 1, | |
118 8, 1, 10, 1, | |
119 10, 2, 12, 1); | |
120 cm.replaceSelection("a", "around"); | |
121 hasSelections(cm, 0, 0, 0, 1, | |
122 0, 2, 0, 3, | |
123 0, 4, 0, 5, | |
124 2, 1, 2, 2, | |
125 2, 3, 2, 4); | |
126 cm.replaceSelection("xy", "start"); | |
127 hasSelections(cm, 0, 0, 0, 0, | |
128 0, 3, 0, 3, | |
129 0, 6, 0, 6, | |
130 2, 1, 2, 1, | |
131 2, 4, 2, 4); | |
132 cm.replaceSelection("z\nf"); | |
133 hasSelections(cm, 1, 1, 1, 1, | |
134 2, 1, 2, 1, | |
135 3, 1, 3, 1, | |
136 6, 1, 6, 1, | |
137 7, 1, 7, 1); | |
138 eq(cm.getValue(), "z\nfxy2z\nfxy4z\nfxy6\n123456\n1z\nfxy5z\nfxy"); | |
139 }); | |
140 | |
141 function select(cm) { | |
142 var sels = []; | |
143 for (var i = 1; i < arguments.length; i++) { | |
144 var arg = arguments[i]; | |
145 if (arg.head) sels.push(arg); | |
146 else sels.push({head: arg, anchor: arg}); | |
147 } | |
148 cm.setSelections(sels, sels.length - 1); | |
149 } | |
150 | |
151 testCM("indentSelection", function(cm) { | |
152 select(cm, Pos(0, 1), Pos(1, 1)); | |
153 cm.indentSelection(4); | |
154 eq(cm.getValue(), " foo\n bar\nbaz"); | |
155 | |
156 select(cm, Pos(0, 2), Pos(0, 3), Pos(0, 4)); | |
157 cm.indentSelection(-2); | |
158 eq(cm.getValue(), " foo\n bar\nbaz"); | |
159 | |
160 select(cm, {anchor: Pos(0, 0), head: Pos(1, 2)}, | |
161 {anchor: Pos(1, 3), head: Pos(2, 0)}); | |
162 cm.indentSelection(-2); | |
163 eq(cm.getValue(), "foo\n bar\nbaz"); | |
164 }, {value: "foo\nbar\nbaz"}); | |
165 | |
166 testCM("killLine", function(cm) { | |
167 select(cm, Pos(0, 1), Pos(0, 2), Pos(1, 1)); | |
168 cm.execCommand("killLine"); | |
169 eq(cm.getValue(), "f\nb\nbaz"); | |
170 cm.execCommand("killLine"); | |
171 eq(cm.getValue(), "fbbaz"); | |
172 cm.setValue("foo\nbar\nbaz"); | |
173 select(cm, Pos(0, 1), {anchor: Pos(0, 2), head: Pos(2, 1)}); | |
174 cm.execCommand("killLine"); | |
175 eq(cm.getValue(), "faz"); | |
176 }, {value: "foo\nbar\nbaz"}); | |
177 | |
178 testCM("deleteLine", function(cm) { | |
179 select(cm, Pos(0, 0), | |
180 {head: Pos(0, 1), anchor: Pos(2, 0)}, | |
181 Pos(4, 0)); | |
182 cm.execCommand("deleteLine"); | |
183 eq(cm.getValue(), "4\n6\n7"); | |
184 select(cm, Pos(2, 1)); | |
185 cm.execCommand("deleteLine"); | |
186 eq(cm.getValue(), "4\n6\n"); | |
187 }, {value: "1\n2\n3\n4\n5\n6\n7"}); | |
188 | |
189 testCM("deleteH", function(cm) { | |
190 select(cm, Pos(0, 4), {anchor: Pos(1, 4), head: Pos(1, 5)}); | |
191 cm.execCommand("delWordAfter"); | |
192 eq(cm.getValue(), "foo bar baz\nabc ef ghi\n"); | |
193 cm.execCommand("delWordAfter"); | |
194 eq(cm.getValue(), "foo baz\nabc ghi\n"); | |
195 cm.execCommand("delCharBefore"); | |
196 cm.execCommand("delCharBefore"); | |
197 eq(cm.getValue(), "fo baz\nab ghi\n"); | |
198 select(cm, Pos(0, 3), Pos(0, 4), Pos(0, 5)); | |
199 cm.execCommand("delWordAfter"); | |
200 eq(cm.getValue(), "fo \nab ghi\n"); | |
201 }, {value: "foo bar baz\nabc def ghi\n"}); | |
202 | |
203 testCM("goLineStart", function(cm) { | |
204 select(cm, Pos(0, 2), Pos(0, 3), Pos(1, 1)); | |
205 cm.execCommand("goLineStart"); | |
206 hasCursors(cm, 0, 0, 1, 0); | |
207 select(cm, Pos(1, 1), Pos(0, 1)); | |
208 cm.setExtending(true); | |
209 cm.execCommand("goLineStart"); | |
210 hasSelections(cm, 0, 1, 0, 0, | |
211 1, 1, 1, 0); | |
212 }, {value: "foo\nbar\nbaz"}); | |
213 | |
214 testCM("moveV", function(cm) { | |
215 select(cm, Pos(0, 2), Pos(1, 2)); | |
216 cm.execCommand("goLineDown"); | |
217 hasCursors(cm, 1, 2, 2, 2); | |
218 cm.execCommand("goLineUp"); | |
219 hasCursors(cm, 0, 2, 1, 2); | |
220 cm.execCommand("goLineUp"); | |
221 hasCursors(cm, 0, 0, 0, 2); | |
222 cm.execCommand("goLineUp"); | |
223 hasCursors(cm, 0, 0); | |
224 select(cm, Pos(0, 2), Pos(1, 2)); | |
225 cm.setExtending(true); | |
226 cm.execCommand("goLineDown"); | |
227 hasSelections(cm, 0, 2, 2, 2); | |
228 }, {value: "12345\n12345\n12345"}); | |
229 | |
230 testCM("moveH", function(cm) { | |
231 select(cm, Pos(0, 1), Pos(0, 3), Pos(0, 5), Pos(2, 3)); | |
232 cm.execCommand("goCharRight"); | |
233 hasCursors(cm, 0, 2, 0, 4, 1, 0, 2, 4); | |
234 cm.execCommand("goCharLeft"); | |
235 hasCursors(cm, 0, 1, 0, 3, 0, 5, 2, 3); | |
236 for (var i = 0; i < 15; i++) | |
237 cm.execCommand("goCharRight"); | |
238 hasCursors(cm, 2, 4, 2, 5); | |
239 }, {value: "12345\n12345\n12345"}); | |
240 | |
241 testCM("newlineAndIndent", function(cm) { | |
242 select(cm, Pos(0, 5), Pos(1, 5)); | |
243 cm.execCommand("newlineAndIndent"); | |
244 hasCursors(cm, 1, 2, 3, 2); | |
245 eq(cm.getValue(), "x = [\n 1];\ny = [\n 2];"); | |
246 cm.undo(); | |
247 eq(cm.getValue(), "x = [1];\ny = [2];"); | |
248 hasCursors(cm, 0, 5, 1, 5); | |
249 select(cm, Pos(0, 5), Pos(0, 6)); | |
250 cm.execCommand("newlineAndIndent"); | |
251 hasCursors(cm, 1, 2, 2, 0); | |
252 eq(cm.getValue(), "x = [\n 1\n];\ny = [2];"); | |
253 }, {value: "x = [1];\ny = [2];", mode: "javascript"}); | |
254 | |
255 testCM("goDocStartEnd", function(cm) { | |
256 select(cm, Pos(0, 1), Pos(1, 1)); | |
257 cm.execCommand("goDocStart"); | |
258 hasCursors(cm, 0, 0); | |
259 select(cm, Pos(0, 1), Pos(1, 1)); | |
260 cm.execCommand("goDocEnd"); | |
261 hasCursors(cm, 1, 3); | |
262 select(cm, Pos(0, 1), Pos(1, 1)); | |
263 cm.setExtending(true); | |
264 cm.execCommand("goDocEnd"); | |
265 hasSelections(cm, 1, 1, 1, 3); | |
266 }, {value: "abc\ndef"}); | |
267 | |
268 testCM("selectionHistory", function(cm) { | |
269 for (var i = 0; i < 3; ++i) | |
270 cm.addSelection(Pos(0, i * 2), Pos(0, i * 2 + 1)); | |
271 cm.execCommand("undoSelection"); | |
272 eq(cm.getSelection(), "1\n2"); | |
273 cm.execCommand("undoSelection"); | |
274 eq(cm.getSelection(), "1"); | |
275 cm.execCommand("undoSelection"); | |
276 eq(cm.getSelection(), ""); | |
277 eqCharPos(cm.getCursor(), Pos(0, 0)); | |
278 cm.execCommand("redoSelection"); | |
279 eq(cm.getSelection(), "1"); | |
280 cm.execCommand("redoSelection"); | |
281 eq(cm.getSelection(), "1\n2"); | |
282 cm.execCommand("redoSelection"); | |
283 eq(cm.getSelection(), "1\n2\n3"); | |
284 }, {value: "1 2 3"}); | |
285 | |
286 testCM("selectionsMayTouch", function(cm) { | |
287 select(cm, Pos(0, 0), Pos(0, 2)) | |
288 cm.setExtending(true); | |
289 cm.extendSelections([Pos(0, 2), Pos(0, 4)]) | |
290 hasSelections(cm, 0, 0, 0, 2, | |
291 0, 2, 0, 4) | |
292 cm.extendSelections([Pos(0, 3), Pos(0, 4)]) | |
293 hasSelections(cm, 0, 0, 0, 4) | |
294 }, {selectionsMayTouch: true, value: "1234"}) | |
295 })(); |