Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/emmet/src/test/toggleComment.test.ts
4774 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import 'mocha';
7
import * as assert from 'assert';
8
import { Selection } from 'vscode';
9
import { withRandomFileEditor, closeAllEditors } from './testUtils';
10
import { toggleComment as toggleCommentImpl } from '../toggleComment';
11
12
function toggleComment(): Thenable<boolean> {
13
const result = toggleCommentImpl();
14
assert.ok(result);
15
return result!;
16
}
17
18
suite('Tests for Toggle Comment action from Emmet (HTML)', () => {
19
teardown(closeAllEditors);
20
21
const contents = `
22
<div class="hello">
23
<ul>
24
<li><span>Hello</span></li>
25
<li><span>There</span></li>
26
<div><li><span>Bye</span></li></div>
27
</ul>
28
<ul>
29
<!-- <li>Previously Commented Node</li> -->
30
<li>Another Node</li>
31
</ul>
32
<span/>
33
<style>
34
.boo {
35
margin: 10px;
36
padding: 20px;
37
}
38
.hoo {
39
margin: 10px;
40
padding: 20px;
41
}
42
</style>
43
</div>
44
`;
45
46
test('toggle comment with multiple cursors, but no selection (HTML)', () => {
47
const expectedContents = `
48
<div class="hello">
49
<ul>
50
<li><!-- <span>Hello</span> --></li>
51
<!-- <li><span>There</span></li> -->
52
<!-- <div><li><span>Bye</span></li></div> -->
53
</ul>
54
<!-- <ul>
55
<li>Previously Commented Node</li>
56
<li>Another Node</li>
57
</ul> -->
58
<span/>
59
<style>
60
.boo {
61
/* margin: 10px; */
62
padding: 20px;
63
}
64
/* .hoo {
65
margin: 10px;
66
padding: 20px;
67
} */
68
</style>
69
</div>
70
`;
71
return withRandomFileEditor(contents, 'html', (editor, doc) => {
72
editor.selections = [
73
new Selection(3, 17, 3, 17), // cursor inside the inner span element
74
new Selection(4, 5, 4, 5), // cursor inside opening tag
75
new Selection(5, 35, 5, 35), // cursor inside closing tag
76
new Selection(7, 3, 7, 3), // cursor inside open tag of <ul> one of whose children is already commented
77
new Selection(14, 8, 14, 8), // cursor inside the css property inside the style tag
78
new Selection(18, 3, 18, 3) // cursor inside the css rule inside the style tag
79
];
80
81
return toggleComment().then(() => {
82
assert.strictEqual(doc.getText(), expectedContents);
83
return Promise.resolve();
84
});
85
});
86
});
87
88
test('toggle comment with multiple cursors and whole node selected (HTML)', () => {
89
const expectedContents = `
90
<div class="hello">
91
<ul>
92
<li><!-- <span>Hello</span> --></li>
93
<!-- <li><span>There</span></li> -->
94
<div><li><span>Bye</span></li></div>
95
</ul>
96
<!-- <ul>
97
<li>Previously Commented Node</li>
98
<li>Another Node</li>
99
</ul> -->
100
<span/>
101
<style>
102
.boo {
103
/* margin: 10px; */
104
padding: 20px;
105
}
106
/* .hoo {
107
margin: 10px;
108
padding: 20px;
109
} */
110
</style>
111
</div>
112
`;
113
return withRandomFileEditor(contents, 'html', (editor, doc) => {
114
editor.selections = [
115
new Selection(3, 7, 3, 25), // <span>Hello</span><
116
new Selection(4, 3, 4, 30), // <li><span>There</span></li>
117
new Selection(7, 2, 10, 7), // The <ul> one of whose children is already commented
118
new Selection(14, 4, 14, 17), // css property inside the style tag
119
new Selection(17, 3, 20, 4) // the css rule inside the style tag
120
];
121
122
return toggleComment().then(() => {
123
assert.strictEqual(doc.getText(), expectedContents);
124
return Promise.resolve();
125
});
126
});
127
});
128
129
test('toggle comment when multiple nodes are completely under single selection (HTML)', () => {
130
const expectedContents = `
131
<div class="hello">
132
<ul>
133
<!-- <li><span>Hello</span></li>
134
<li><span>There</span></li> -->
135
<div><li><span>Bye</span></li></div>
136
</ul>
137
<ul>
138
<!-- <li>Previously Commented Node</li> -->
139
<li>Another Node</li>
140
</ul>
141
<span/>
142
<style>
143
.boo {
144
/* margin: 10px;
145
padding: 20px; */
146
}
147
.hoo {
148
margin: 10px;
149
padding: 20px;
150
}
151
</style>
152
</div>
153
`;
154
return withRandomFileEditor(contents, 'html', (editor, doc) => {
155
editor.selections = [
156
new Selection(3, 4, 4, 30),
157
new Selection(14, 4, 15, 18) // 2 css properties inside the style tag
158
];
159
160
return toggleComment().then(() => {
161
assert.strictEqual(doc.getText(), expectedContents);
162
return Promise.resolve();
163
});
164
});
165
});
166
167
test('toggle comment when multiple nodes are partially under single selection (HTML)', () => {
168
const expectedContents = `
169
<div class="hello">
170
<ul>
171
<!-- <li><span>Hello</span></li>
172
<li><span>There</span></li> -->
173
<div><li><span>Bye</span></li></div>
174
</ul>
175
<!-- <ul>
176
<li>Previously Commented Node</li>
177
<li>Another Node</li>
178
</ul> -->
179
<span/>
180
<style>
181
.boo {
182
margin: 10px;
183
padding: 20px;
184
}
185
.hoo {
186
margin: 10px;
187
padding: 20px;
188
}
189
</style>
190
</div>
191
`;
192
return withRandomFileEditor(contents, 'html', (editor, doc) => {
193
editor.selections = [
194
new Selection(3, 24, 4, 20),
195
new Selection(7, 2, 9, 10) // The <ul> one of whose children is already commented
196
];
197
198
return toggleComment().then(() => {
199
assert.strictEqual(doc.getText(), expectedContents);
200
return Promise.resolve();
201
});
202
});
203
});
204
205
test('toggle comment with multiple cursors selecting parent and child nodes', () => {
206
const expectedContents = `
207
<div class="hello">
208
<ul>
209
<li><!-- <span>Hello</span> --></li>
210
<!-- <li><span>There</span></li> -->
211
<div><li><span>Bye</span></li></div>
212
</ul>
213
<!-- <ul>
214
<li>Previously Commented Node</li>
215
<li>Another Node</li>
216
</ul> -->
217
<span/>
218
<!-- <style>
219
.boo {
220
margin: 10px;
221
padding: 20px;
222
}
223
.hoo {
224
margin: 10px;
225
padding: 20px;
226
}
227
</style> -->
228
</div>
229
`;
230
return withRandomFileEditor(contents, 'html', (editor, doc) => {
231
editor.selections = [
232
new Selection(3, 17, 3, 17), // cursor inside the inner span element
233
new Selection(4, 5, 4, 5), // two cursors: one inside opening tag
234
new Selection(4, 17, 4, 17), // and the second inside the inner span element
235
new Selection(7, 3, 7, 3), // two cursors: one inside open tag of <ul> one of whose children is already commented
236
new Selection(9, 10, 9, 10), // and the second inside inner li element, whose parent is selected
237
new Selection(12, 3, 12, 3), // four nested cursors: one inside the style open tag
238
new Selection(14, 8, 14, 8), // the second inside the css property inside the style tag
239
new Selection(18, 3, 18, 3), // the third inside the css rule inside the style tag
240
new Selection(19, 8, 19, 8) // and the fourth inside the css property inside the style tag
241
];
242
243
return toggleComment().then(() => {
244
assert.strictEqual(doc.getText(), expectedContents);
245
246
return Promise.resolve();
247
});
248
});
249
});
250
251
test('toggle comment within script template', () => {
252
const templateContents = `
253
<script type="text/template">
254
<li><span>Hello</span></li>
255
<li><!-- <span>There</span> --></li>
256
<div><li><span>Bye</span></li></div>
257
<span/>
258
</script>
259
`;
260
const expectedContents = `
261
<script type="text/template">
262
<!-- <li><span>Hello</span></li> -->
263
<li><span>There</span></li>
264
<div><li><!-- <span>Bye</span> --></li></div>
265
<span/>
266
</script>
267
`;
268
return withRandomFileEditor(templateContents, 'html', (editor, doc) => {
269
editor.selections = [
270
new Selection(2, 2, 2, 28), // select entire li element
271
new Selection(3, 17, 3, 17), // cursor inside the commented span
272
new Selection(4, 18, 4, 18), // cursor inside the noncommented span
273
];
274
return toggleComment().then(() => {
275
assert.strictEqual(doc.getText(), expectedContents);
276
return Promise.resolve();
277
});
278
});
279
});
280
});
281
282
suite('Tests for Toggle Comment action from Emmet (CSS)', () => {
283
teardown(closeAllEditors);
284
285
const contents = `
286
.one {
287
margin: 10px;
288
padding: 10px;
289
}
290
.two {
291
height: 42px;
292
display: none;
293
}
294
.three {
295
width: 42px;
296
}`;
297
298
test('toggle comment with multiple cursors, but no selection (CSS)', () => {
299
const expectedContents = `
300
.one {
301
/* margin: 10px; */
302
padding: 10px;
303
}
304
/* .two {
305
height: 42px;
306
display: none;
307
} */
308
.three {
309
width: 42px;
310
}`;
311
return withRandomFileEditor(contents, 'css', (editor, doc) => {
312
editor.selections = [
313
new Selection(2, 5, 2, 5), // cursor inside a property
314
new Selection(5, 4, 5, 4), // cursor inside selector
315
];
316
317
return toggleComment().then(() => {
318
assert.strictEqual(doc.getText(), expectedContents);
319
return toggleComment().then(() => {
320
assert.strictEqual(doc.getText(), contents);
321
return Promise.resolve();
322
});
323
});
324
});
325
});
326
327
test('toggle comment with multiple cursors and whole node selected (CSS)', () => {
328
const expectedContents = `
329
.one {
330
/* margin: 10px; */
331
/* padding: 10px; */
332
}
333
/* .two {
334
height: 42px;
335
display: none;
336
} */
337
.three {
338
width: 42px;
339
}`;
340
return withRandomFileEditor(contents, 'css', (editor, doc) => {
341
editor.selections = [
342
new Selection(2, 2, 2, 15), // A property completely selected
343
new Selection(3, 0, 3, 16), // A property completely selected along with whitespace
344
new Selection(5, 1, 8, 2), // A rule completely selected
345
];
346
347
return toggleComment().then(() => {
348
assert.strictEqual(doc.getText(), expectedContents);
349
//return toggleComment().then(() => {
350
//assert.strictEqual(doc.getText(), contents);
351
return Promise.resolve();
352
//});
353
});
354
});
355
});
356
357
358
359
test('toggle comment when multiple nodes of same parent are completely under single selection (CSS)', () => {
360
const expectedContents = `
361
.one {
362
/* margin: 10px;
363
padding: 10px; */
364
}
365
/* .two {
366
height: 42px;
367
display: none;
368
}
369
.three {
370
width: 42px;
371
} */`;
372
return withRandomFileEditor(contents, 'css', (editor, doc) => {
373
editor.selections = [
374
new Selection(2, 0, 3, 16), // 2 properties completely under a single selection along with whitespace
375
new Selection(5, 1, 11, 2), // 2 rules completely under a single selection
376
];
377
378
return toggleComment().then(() => {
379
assert.strictEqual(doc.getText(), expectedContents);
380
return toggleComment().then(() => {
381
assert.strictEqual(doc.getText(), contents);
382
return Promise.resolve();
383
});
384
});
385
});
386
});
387
388
test('toggle comment when start and end of selection is inside properties of separate rules (CSS)', () => {
389
const expectedContents = `
390
.one {
391
margin: 10px;
392
/* padding: 10px;
393
}
394
.two {
395
height: 42px; */
396
display: none;
397
}
398
.three {
399
width: 42px;
400
}`;
401
return withRandomFileEditor(contents, 'css', (editor, doc) => {
402
editor.selections = [
403
new Selection(3, 7, 6, 6)
404
];
405
406
return toggleComment().then(() => {
407
assert.strictEqual(doc.getText(), expectedContents);
408
return toggleComment().then(() => {
409
assert.strictEqual(doc.getText(), contents);
410
return Promise.resolve();
411
});
412
});
413
});
414
});
415
416
test('toggle comment when selection spans properties of separate rules, with start in whitespace and end inside the property (CSS)', () => {
417
const expectedContents = `
418
.one {
419
margin: 10px;
420
/* padding: 10px;
421
}
422
.two {
423
height: 42px; */
424
display: none;
425
}
426
.three {
427
width: 42px;
428
}`;
429
return withRandomFileEditor(contents, 'css', (editor, doc) => {
430
editor.selections = [
431
new Selection(3, 0, 6, 6)
432
];
433
434
return toggleComment().then(() => {
435
assert.strictEqual(doc.getText(), expectedContents);
436
return toggleComment().then(() => {
437
assert.strictEqual(doc.getText(), contents);
438
return Promise.resolve();
439
});
440
});
441
});
442
});
443
444
test('toggle comment when selection spans properties of separate rules, with end in whitespace and start inside the property (CSS)', () => {
445
const expectedContents = `
446
.one {
447
margin: 10px;
448
/* padding: 10px;
449
}
450
.two {
451
height: 42px; */
452
display: none;
453
}
454
.three {
455
width: 42px;
456
}`;
457
return withRandomFileEditor(contents, 'css', (editor, doc) => {
458
editor.selections = [
459
new Selection(3, 7, 7, 0)
460
];
461
462
return toggleComment().then(() => {
463
assert.strictEqual(doc.getText(), expectedContents);
464
return toggleComment().then(() => {
465
assert.strictEqual(doc.getText(), contents);
466
return Promise.resolve();
467
});
468
});
469
});
470
});
471
472
test('toggle comment when selection spans properties of separate rules, with both start and end in whitespace (CSS)', () => {
473
const expectedContents = `
474
.one {
475
margin: 10px;
476
/* padding: 10px;
477
}
478
.two {
479
height: 42px; */
480
display: none;
481
}
482
.three {
483
width: 42px;
484
}`;
485
return withRandomFileEditor(contents, 'css', (editor, doc) => {
486
editor.selections = [
487
new Selection(3, 0, 7, 0)
488
];
489
490
return toggleComment().then(() => {
491
assert.strictEqual(doc.getText(), expectedContents);
492
return toggleComment().then(() => {
493
assert.strictEqual(doc.getText(), contents);
494
return Promise.resolve();
495
});
496
});
497
});
498
});
499
500
test('toggle comment when multiple nodes of same parent are partially under single selection (CSS)', () => {
501
const expectedContents = `
502
.one {
503
/* margin: 10px;
504
padding: 10px; */
505
}
506
/* .two {
507
height: 42px;
508
display: none;
509
}
510
.three {
511
width: 42px;
512
*/ }`;
513
return withRandomFileEditor(contents, 'css', (editor, doc) => {
514
editor.selections = [
515
new Selection(2, 7, 3, 10), // 2 properties partially under a single selection
516
new Selection(5, 2, 11, 0), // 2 rules partially under a single selection
517
];
518
519
return toggleComment().then(() => {
520
assert.strictEqual(doc.getText(), expectedContents);
521
return toggleComment().then(() => {
522
assert.strictEqual(doc.getText(), contents);
523
return Promise.resolve();
524
});
525
});
526
});
527
});
528
529
530
});
531
532
533
suite('Tests for Toggle Comment action from Emmet in nested css (SCSS)', () => {
534
teardown(closeAllEditors);
535
536
const contents = `
537
.one {
538
height: 42px;
539
540
.two {
541
width: 42px;
542
}
543
544
.three {
545
padding: 10px;
546
}
547
}`;
548
549
test('toggle comment with multiple cursors selecting nested nodes (SCSS)', () => {
550
const expectedContents = `
551
.one {
552
/* height: 42px; */
553
554
/* .two {
555
width: 42px;
556
} */
557
558
.three {
559
/* padding: 10px; */
560
}
561
}`;
562
return withRandomFileEditor(contents, 'css', (editor, doc) => {
563
editor.selections = [
564
new Selection(2, 5, 2, 5), // cursor inside a property
565
new Selection(4, 4, 4, 4), // two cursors: one inside a nested rule
566
new Selection(5, 5, 5, 5), // and the second one inside a nested property
567
new Selection(9, 5, 9, 5) // cursor inside a property inside a nested rule
568
];
569
570
return toggleComment().then(() => {
571
assert.strictEqual(doc.getText(), expectedContents);
572
return toggleComment().then(() => {
573
assert.strictEqual(doc.getText(), contents);
574
return Promise.resolve();
575
});
576
});
577
});
578
});
579
test('toggle comment with multiple cursors selecting several nested nodes (SCSS)', () => {
580
const expectedContents = `
581
/* .one {
582
height: 42px;
583
584
.two {
585
width: 42px;
586
}
587
588
.three {
589
padding: 10px;
590
}
591
} */`;
592
return withRandomFileEditor(contents, 'css', (editor, doc) => {
593
editor.selections = [
594
new Selection(1, 3, 1, 3), // cursor in the outside rule. And several cursors inside:
595
new Selection(2, 5, 2, 5), // cursor inside a property
596
new Selection(4, 4, 4, 4), // two cursors: one inside a nested rule
597
new Selection(5, 5, 5, 5), // and the second one inside a nested property
598
new Selection(9, 5, 9, 5) // cursor inside a property inside a nested rule
599
];
600
601
return toggleComment().then(() => {
602
assert.strictEqual(doc.getText(), expectedContents);
603
return toggleComment().then(() => {
604
assert.strictEqual(doc.getText(), contents);
605
return Promise.resolve();
606
});
607
});
608
});
609
});
610
611
test('toggle comment with multiple cursors, but no selection (SCSS)', () => {
612
const expectedContents = `
613
.one {
614
/* height: 42px; */
615
616
/* .two {
617
width: 42px;
618
} */
619
620
.three {
621
/* padding: 10px; */
622
}
623
}`;
624
return withRandomFileEditor(contents, 'css', (editor, doc) => {
625
editor.selections = [
626
new Selection(2, 5, 2, 5), // cursor inside a property
627
new Selection(4, 4, 4, 4), // cursor inside a nested rule
628
new Selection(9, 5, 9, 5) // cursor inside a property inside a nested rule
629
];
630
631
return toggleComment().then(() => {
632
assert.strictEqual(doc.getText(), expectedContents);
633
//return toggleComment().then(() => {
634
// assert.strictEqual(doc.getText(), contents);
635
return Promise.resolve();
636
//});
637
});
638
});
639
});
640
641
test('toggle comment with multiple cursors and whole node selected (CSS)', () => {
642
const expectedContents = `
643
.one {
644
/* height: 42px; */
645
646
/* .two {
647
width: 42px;
648
} */
649
650
.three {
651
/* padding: 10px; */
652
}
653
}`;
654
return withRandomFileEditor(contents, 'css', (editor, doc) => {
655
editor.selections = [
656
new Selection(2, 2, 2, 15), // A property completely selected
657
new Selection(4, 2, 6, 3), // A rule completely selected
658
new Selection(9, 3, 9, 17) // A property inside a nested rule completely selected
659
];
660
661
return toggleComment().then(() => {
662
assert.strictEqual(doc.getText(), expectedContents);
663
return toggleComment().then(() => {
664
assert.strictEqual(doc.getText(), contents);
665
return Promise.resolve();
666
});
667
});
668
});
669
});
670
671
672
673
test('toggle comment when multiple nodes are completely under single selection (CSS)', () => {
674
const expectedContents = `
675
.one {
676
/* height: 42px;
677
678
.two {
679
width: 42px;
680
} */
681
682
.three {
683
padding: 10px;
684
}
685
}`;
686
return withRandomFileEditor(contents, 'css', (editor, doc) => {
687
editor.selections = [
688
new Selection(2, 2, 6, 3), // A properties and a nested rule completely under a single selection
689
];
690
691
return toggleComment().then(() => {
692
assert.strictEqual(doc.getText(), expectedContents);
693
return toggleComment().then(() => {
694
assert.strictEqual(doc.getText(), contents);
695
return Promise.resolve();
696
});
697
});
698
});
699
});
700
701
test('toggle comment when multiple nodes are partially under single selection (CSS)', () => {
702
const expectedContents = `
703
.one {
704
/* height: 42px;
705
706
.two {
707
width: 42px;
708
*/ }
709
710
.three {
711
padding: 10px;
712
}
713
}`;
714
return withRandomFileEditor(contents, 'css', (editor, doc) => {
715
editor.selections = [
716
new Selection(2, 6, 6, 1), // A properties and a nested rule partially under a single selection
717
];
718
719
return toggleComment().then(() => {
720
assert.strictEqual(doc.getText(), expectedContents);
721
return toggleComment().then(() => {
722
assert.strictEqual(doc.getText(), contents);
723
return Promise.resolve();
724
});
725
});
726
});
727
});
728
729
test('toggle comment doesn\'t fail when start and end nodes differ HTML', () => {
730
const contents = `
731
<div>
732
<p>Hello</p>
733
</div>
734
`;
735
const expectedContents = `
736
<!-- <div>
737
<p>Hello</p>
738
</div> -->
739
`;
740
return withRandomFileEditor(contents, 'html', (editor, doc) => {
741
editor.selections = [
742
new Selection(1, 2, 2, 9), // <div> to <p> inclusive
743
];
744
745
return toggleComment().then(() => {
746
assert.strictEqual(doc.getText(), expectedContents);
747
return toggleComment().then(() => {
748
assert.strictEqual(doc.getText(), contents);
749
return Promise.resolve();
750
});
751
});
752
});
753
});
754
});
755
756