forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCSSUtils-test.js
More file actions
2559 lines (2192 loc) · 121 KB
/
CSSUtils-test.js
File metadata and controls
2559 lines (2192 loc) · 121 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*global describe, it, expect, beforeEach, afterEach, awaitsForDone */
define(function (require, exports, module) {
var FileSystem = require("filesystem/FileSystem"),
FileUtils = require("file/FileUtils"),
CSSUtils = require("language/CSSUtils"),
HTMLUtils = require("language/HTMLUtils"),
SpecRunnerUtils = require("spec/SpecRunnerUtils"),
TextRange = require("document/TextRange").TextRange;
var testPath = SpecRunnerUtils.getTestPath("/spec/CSSUtils-test-files"),
simpleCssFileEntry = FileSystem.getFileForPath(testPath + "/simple.css"),
universalCssFileEntry = FileSystem.getFileForPath(testPath + "/universal.css"),
propListCssFileEntry = FileSystem.getFileForPath(testPath + "/property-list.css"),
groupsFileEntry = FileSystem.getFileForPath(testPath + "/groups.css"),
offsetsCssFileEntry = FileSystem.getFileForPath(testPath + "/offsets.css"),
bootstrapCssFileEntry = FileSystem.getFileForPath(testPath + "/bootstrap.css"),
escapesCssFileEntry = FileSystem.getFileForPath(testPath + "/escaped-identifiers.css"),
embeddedHtmlFileEntry = FileSystem.getFileForPath(testPath + "/embedded.html"),
cssRegionsFileEntry = FileSystem.getFileForPath(testPath + "/regions.css"),
nestedGroupsFileEntry = FileSystem.getFileForPath(testPath + "/panels.less");
var contextTestCss = require("text!spec/CSSUtils-test-files/contexts.css"),
selectorPositionsTestCss = require("text!spec/CSSUtils-test-files/selector-positions.css"),
rangesTestCss = require("text!spec/CSSUtils-test-files/ranges.css"),
simpleTestCss = require("text!spec/CSSUtils-test-files/simple.css"),
mediaTestScss = require("text!spec/CSSUtils-test-files/navbar.scss"),
mediaTestLess = require("text!spec/CSSUtils-test-files/print.less"),
mixinTestScss = require("text!spec/CSSUtils-test-files/table&button.scss"),
mixinTestLess = require("text!spec/CSSUtils-test-files/mixins.less"),
includeMixinTestScss = require("text!spec/CSSUtils-test-files/include-mixin.scss"),
parentSelectorTestLess = require("text!spec/CSSUtils-test-files/parent-selector.less"),
varInterpolationTestScss = require("text!spec/CSSUtils-test-files/variables.scss"),
varInterpolationTestLess = require("text!spec/CSSUtils-test-files/variables.less");
async function init(spec, fileEntry) {
spec.fileContent = null;
if (fileEntry) {
let promise = FileUtils.readAsText(fileEntry)
.done(function (text) {
spec.fileContent = text;
});
await awaitsForDone(promise);
}
}
describe("CSSUtils", function () {
beforeEach(async function () {
await init(this);
});
describe("basics", function () {
it("should parse an empty string", function () {
let result = CSSUtils._findAllMatchingSelectorsInText("", { tag: "div" });
expect(result.length).toEqual(0);
});
it("should parse an empty string with less mode", function () {
let result = CSSUtils._findAllMatchingSelectorsInText("", { tag: "div" }, "text/x-less");
expect(result.length).toEqual(0);
});
it("should parse an empty string with scss mode", function () {
let result = CSSUtils._findAllMatchingSelectorsInText("", { tag: "div" }, "text/x-scss");
expect(result.length).toEqual(0);
});
// it("should parse simple selectors from more than one file", function () {
// // TODO: it'd be nice to revive this test by shimming FileIndexManager.getFileInfoList() or something
// });
});
describe("line offsets", function () {
/**
* Checks the lines ranges of the results returned by CSSUtils. Expects the numbers of
* results to equal the length of 'ranges'; each entry in range gives the {start, end}
* of the expected line range for that Nth result.
*/
function expectRuleRanges(_spec, cssCode, selector, ranges, mode) {
var result = CSSUtils._findAllMatchingSelectorsInText(cssCode, selector, mode);
expect(result.length).toEqual(ranges.length);
ranges.forEach(function (range, i) {
expect(result[i].ruleStartLine).toEqual(range.start);
expect(result[i].declListEndLine).toEqual(range.end);
});
}
/**
* Similar to the above function, but instead checks the lines ranges for the entire selector *group*
* of the results returned by CSSUtils. For example, if a rule looks like ".foo,[newline].bar"
* and the caller asks for selector ".bar", this function checks against the entire range
* starting with the line ".foo. is on.
*
* Expects the numbers of results to equal the length of 'ranges'; each entry in range gives
* the {start, end} of the expected line range for that Nth result.
*/
function expectGroupRanges(_spec, cssCode, selector, ranges, mode) {
var result = CSSUtils._findAllMatchingSelectorsInText(cssCode, selector, mode);
expect(result.length).toEqual(ranges.length);
ranges.forEach(function (range, i) {
expect(result[i].selectorGroupStartLine).toEqual(range.start);
expect(result[i].declListEndLine).toEqual(range.end);
});
}
it("should return correct start and end line numbers for simple rules", async function () {
await init(this, simpleCssFileEntry);
expectRuleRanges(this, this.fileContent, "html", [ {start: 0, end: 2}, {start: 4, end: 6 }]);
expectRuleRanges(this, this.fileContent, ".firstGrade", [ {start: 8, end: 10} ]);
expectRuleRanges(this, this.fileContent, "#brack3ts",
[ {start: 16, end: 18} ]);
});
it("should handle rules on adjacent lines", async function () {
await init(this, offsetsCssFileEntry);
expectRuleRanges(this, this.fileContent, "a", [
{start: 0, end: 2}, {start: 3, end: 5}, {start: 7, end: 7},
{start: 8, end: 8}, {start: 10, end: 10}, {start: 10, end: 10},
{start: 16, end: 19}, {start: 23, end: 25}, {start: 29, end: 32},
{start: 33, end: 35}, {start: 38, end: 41}
]);
});
it("should return correct group range when selector group spans multiple lines", async function () {
await init(this, groupsFileEntry);
expectGroupRanges(this, this.fileContent, ".a", [{start: 24, end: 29}]);
expectGroupRanges(this, this.fileContent, ".b", [{start: 24, end: 29}]);
expectGroupRanges(this, this.fileContent, ".c", [{start: 24, end: 29}]);
expectGroupRanges(this, this.fileContent, ".d", [{start: 24, end: 29}]);
expectGroupRanges(this, this.fileContent, ".f", [{start: 31, end: 31}]);
expectGroupRanges(this, this.fileContent, ".g", [{start: 31, end: 34}]);
expectGroupRanges(this, this.fileContent, ".h", [{start: 31, end: 34}]);
});
it("should return correct rule ranges for rules with comma separators in property values", async function () {
await init(this, propListCssFileEntry);
expectRuleRanges(this, this.fileContent, "h1", [{start: 0, end: 2}]);
// https://github.com/adobe/brackets/issues/8966
expectRuleRanges(this, this.fileContent, ".alert", [{start: 4, end: 8}]);
});
it("should return correct rule range and group range for different nested levels", async function () {
await init(this, nestedGroupsFileEntry);
expectRuleRanges(this, this.fileContent, ".table", [
{start: 6, end: 9}, {start: 6, end: 9},
{start: 10, end: 26}, {start: 10, end: 26},
{start: 27, end: 43}, {start: 27, end: 43},
{start: 44, end: 47}
], "text/x-less");
expectGroupRanges(this, this.fileContent, ".table", [
{start: 6, end: 9}, {start: 6, end: 9},
{start: 11, end: 26}, {start: 11, end: 26},
{start: 28, end: 43}, {start: 28, end: 43},
{start: 44, end: 47}
], "text/x-less");
expectRuleRanges(this, this.fileContent, "tbody", [
{start: 13, end: 25}, {start: 30, end: 42}, {start: 55, end: 76}
], "text/x-less");
expectGroupRanges(this, this.fileContent, "tbody", [
{start: 13, end: 25}, {start: 30, end: 42}, {start: 55, end: 76}
], "text/x-less");
expectRuleRanges(this, this.fileContent, "thead", [
{start: 13, end: 25}, {start: 55, end: 76}
], "text/x-less");
expectGroupRanges(this, this.fileContent, "thead", [
{start: 13, end: 25}, {start: 55, end: 76}
], "text/x-less");
expectRuleRanges(this, this.fileContent, "tr", [
{start: 15, end: 24}, {start: 32, end: 41}, {start: 58, end: 75}
], "text/x-less");
expectRuleRanges(this, this.fileContent, "th", [
{start: 16, end: 19}, {start: 20, end: 23}, {start: 33, end: 36},
{start: 37, end: 40}, {start: 48, end: 51}, {start: 59, end: 62},
{start: 63, end: 66}, {start: 67, end: 70}, {start: 71, end: 74}
], "text/x-less");
expectGroupRanges(this, this.fileContent, "th", [
{start: 16, end: 19}, {start: 20, end: 23}, {start: 33, end: 36},
{start: 37, end: 40}, {start: 48, end: 51}, {start: 59, end: 62},
{start: 63, end: 66}, {start: 67, end: 70}, {start: 71, end: 74}
], "text/x-less");
});
});
describe("with the universal selector", function () {
beforeEach(async function () {
await init(this, universalCssFileEntry);
});
it("should match a tag name not referenced anywhere in the CSS", function () {
let matches = CSSUtils._findAllMatchingSelectorsInText(this.fileContent, "blockquote");
expect(matches.length).toEqual(1);
expect(matches[0].selector).toEqual("*");
});
it("should match a tag name also referenced elsewhere in the CSS", function () {
let matches = CSSUtils._findAllMatchingSelectorsInText(this.fileContent, "p");
expect(matches.length).toEqual(2);
expect(matches[0].selector).toEqual("*");
expect(matches[1].selector).toEqual("p");
});
});
describe("with sprint 4 exemptions", function () {
beforeEach(async function () {
let sprint4exemptions = FileSystem.getFileForPath(testPath + "/sprint4.css");
await init(this, sprint4exemptions);
});
it("should match a class selector (right-most only, no pseudo or attr selectors)", function () {
let matches = CSSUtils._findAllMatchingSelectorsInText(this.fileContent, ".message");
expect(matches.length).toEqual(7);
expect(matches[0].selector).toEqual("div.message");
expect(matches[1].selector).toEqual("footer .message");
expect(matches[2].selector).toEqual("footer div.message");
expect(matches[3].selector).toEqual("h2.message");
expect(matches[4].selector).toEqual("div.message:hovered");
expect(matches[5].selector).toEqual(".message:hover");
expect(matches[6].selector).toEqual(".message[data-attr='42']");
});
it("should match a type selector (can terminate with class name, ID, pseudo or attr selectors)", function () {
let matches = CSSUtils._findAllMatchingSelectorsInText(this.fileContent, "h4");
expect(matches.length).toEqual(5);
});
});
describe("with real-world Bootstrap CSS code", function () {
beforeEach(async function () {
await init(this, bootstrapCssFileEntry);
});
it("should find the first instance of the h2 selector", function () {
let selectors = CSSUtils._findAllMatchingSelectorsInText(this.fileContent, "h2");
expect(selectors).toBeTruthy();
expect(selectors.length).toBeGreaterThan(0);
expect(selectors[0]).toBeTruthy();
expect(selectors[0].selectorStartLine).toBe(292);
expect(selectors[0].declListEndLine).toBe(301);
});
it("should find all instances of the h2 selector", function () {
let selectors = CSSUtils._findAllMatchingSelectorsInText(this.fileContent, "h2");
expect(selectors.length).toBe(2);
expect(selectors[0].selectorStartLine).toBe(292);
expect(selectors[0].declListEndLine).toBe(301);
expect(selectors[1].selectorStartLine).toBe(318);
expect(selectors[1].declListEndLine).toBe(321);
});
it("should return an empty array when findAllMatchingSelectors() can't find any matches", function () {
var selectors = CSSUtils._findAllMatchingSelectorsInText(this.fileContent, "NO-SUCH-SELECTOR");
expect(selectors.length).toBe(0);
});
});
describe("escapes", function () {
beforeEach(async function () {
await init(this, escapesCssFileEntry);
});
it("should remove simple backslashes for simple characters", function () {
var selectors = CSSUtils.extractAllSelectors(this.fileContent);
expect(selectors[0].selector).toEqual(".simple");
});
it("should remove simple backslashes with escaped characters", function () {
var selectors = CSSUtils.extractAllSelectors(this.fileContent);
expect(selectors[1].selector).toEqual(".not\\so|simple?");
});
it("should parse '\\XX ' as a single character", function () {
var selectors = CSSUtils.extractAllSelectors(this.fileContent);
expect(selectors[2].selector).toEqual(".twodigits");
});
it("should parse '\\XXXX ' as a single character", function () {
var selectors = CSSUtils.extractAllSelectors(this.fileContent);
expect(selectors[3].selector).toEqual(".fourdigits");
});
it("should parse '\\XXXXXX' as a single character", function () {
var selectors = CSSUtils.extractAllSelectors(this.fileContent);
expect(selectors[4].selector).toEqual(".sixdigits");
});
it("should not trim end spaces", function () {
var selectors = CSSUtils.extractAllSelectors(this.fileContent);
expect(selectors[5].selector).toEqual(".two-digit-endspace");
selectors = CSSUtils.extractAllSelectors(this.fileContent);
expect(selectors[6].selector).toEqual(".four-digit-endspace");
selectors = CSSUtils.extractAllSelectors(this.fileContent);
expect(selectors[7].selector).toEqual(".six-digit-endspace");
});
it("should detect all combinations", function () {
var selectors = CSSUtils.extractAllSelectors(this.fileContent);
expect(selectors[8].selector).toEqual(".mixin-it-all");
});
it("should parse '\\AX' as AX", function () {
var selectors = CSSUtils.extractAllSelectors(this.fileContent);
expect(selectors[9].selector).toEqual(".two-wi74out-space");
});
it("should parse '\\AXXX' as AXXX", function () {
var selectors = CSSUtils.extractAllSelectors(this.fileContent);
expect(selectors[10].selector).toEqual(".four-n0085-space");
});
it("should replace out of range characters with U+FFFD", function () {
var selectors = CSSUtils.extractAllSelectors(this.fileContent);
expect(selectors[11].selector).toEqual(".\uFFFDut\uFFFDfrange");
});
it("should parse everything less does", function () {
var selectors = CSSUtils.extractAllSelectors(this.fileContent);
expect(selectors[12].selector).toEqual(".escape|random|char");
expect(selectors[13].selector).toEqual(".mixin!tUp");
expect(selectors[14].selector).toEqual(".404");
expect(selectors[15].selector).toEqual(".404 strong");
expect(selectors[16].selector).toEqual(".trailingTest+");
expect(selectors[17].selector).toEqual("blockquote");
});
});
describe("findSelectorAtDocumentPos selector groups", function () {
var editor;
beforeEach(async function () {
await init(this, groupsFileEntry);
editor = SpecRunnerUtils.createMockEditor(this.fileContent, "css").editor;
});
afterEach(function () {
SpecRunnerUtils.destroyMockEditor(editor.document);
editor = null;
});
it("should find the selector at a document pos", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 9, ch: 0});
expect(selector).toEqual("h1");
});
it("should return empty string if selection is not in a style rule", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 11, ch: 0});
expect(selector).toEqual("");
});
it("should return a comma separated string of all selectors for the rule", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 13, ch: 0});
expect(selector).toEqual("h3, h2, h1");
});
it("should support multiple rules on the same line", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 31, ch: 24});
expect(selector).toEqual(".g,.h");
});
it("should support multiple rules on multiple lines", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 28, ch: 0});
expect(selector).toEqual(".a,.b, .c,.d");
});
});
describe("findSelectorAtDocumentPos comments", function () {
var editor;
beforeEach(async function () {
await init(this, offsetsCssFileEntry);
editor = SpecRunnerUtils.createMockEditor(this.fileContent, "css").editor;
});
afterEach(function () {
SpecRunnerUtils.destroyMockEditor(editor.document);
editor = null;
});
it("should ignore rules inside comments", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 45, ch: 22});
expect(selector).toEqual("");
});
// https://github.com/adobe/brackets/issues/9002
it("should not hang when the cursor is after '{' or '}' inside comments", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 53, ch: 3}); // after {
expect(selector).toEqual("");
selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 55, ch: 1}); // after }
expect(selector).toEqual("");
});
it("should find rules adjacent to comments", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 47, ch: 4});
expect(selector).toEqual("div");
});
it("should find rules when the position is inside a nested comment", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 49, ch: 14});
expect(selector).toEqual("div");
});
});
describe("findSelectorAtDocumentPos pseudo-classes and at-rules", function () {
var editor;
beforeEach(async function () {
await init(this, offsetsCssFileEntry);
editor = SpecRunnerUtils.createMockEditor(this.fileContent, "css").editor;
});
afterEach(function () {
SpecRunnerUtils.destroyMockEditor(editor.document);
editor = null;
});
it("should find a simple pseudo selector", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 8, ch: 11});
expect(selector).toEqual("a:visited");
});
it("should find a selector with a preceding at-rule", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 18, ch: 0});
expect(selector).toEqual("a");
});
it("should not find a selector when inside an at-rule", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 15, ch: 12});
expect(selector).toEqual("");
selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 28, ch: 31});
expect(selector).toEqual("");
selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 22, ch: 16});
expect(selector).toEqual("");
});
});
describe("findSelectorAtDocumentPos complex selectors", function () {
var editor;
beforeEach(async function () {
await init(this, bootstrapCssFileEntry);
editor = SpecRunnerUtils.createMockEditor(this.fileContent, "css").editor;
});
afterEach(function () {
SpecRunnerUtils.destroyMockEditor(editor.document);
editor = null;
});
it("should find pseudo selectors", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 72, ch: 0});
expect(selector).toEqual("button::-moz-focus-inner, input::-moz-focus-inner");
});
it("should find attribute selectors", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 83, ch: 0});
expect(selector).toEqual('input[type="search"]');
});
it("should find structural pseudo-classes", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 1053, ch: 0});
expect(selector).toEqual(".table-striped tbody tr:nth-child(odd) td, .table-striped tbody tr:nth-child(odd) th");
});
it("should find combinators", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 2073, ch: 0});
expect(selector).toEqual(".alert-block p + p");
});
});
describe("findSelectorAtDocumentPos beginning, middle and end of selector", function () {
var editor;
beforeEach(async function () {
await init(this, groupsFileEntry);
editor = SpecRunnerUtils.createMockEditor(this.fileContent, "css").editor;
});
afterEach(function () {
SpecRunnerUtils.destroyMockEditor(editor.document);
editor = null;
});
it("should find selector when pos is at beginning of selector name", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 12, ch: 0});
expect(selector).toEqual("h3, h2, h1");
});
it("should find selector when pos is in the middle of selector name", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 12, ch: 3});
expect(selector).toEqual('h3, h2, h1');
});
it("should find selector when pos is at the end of a selector name", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 12, ch: 10});
expect(selector).toEqual("h3, h2, h1");
});
it("should not find selector when pos is before a selector name", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 11, ch: 0});
expect(selector).toEqual("");
});
});
describe("find correct positions of selectors", function () {
it("should find selector positions when whitespace between selector and '{'", function () {
var selectors = CSSUtils.extractAllSelectors(selectorPositionsTestCss);
expect([selectors[0].selectorStartChar, selectors[0].selectorEndChar]).toEqual([0, 3]);
});
it("should find selector positions when no whitespace between selector and '{'", function () {
var selectors = CSSUtils.extractAllSelectors(selectorPositionsTestCss);
expect([selectors[1].selectorStartChar, selectors[1].selectorEndChar]).toEqual([0, 3]);
});
it("should find selector positions when '{' on the next line", function () {
var selectors = CSSUtils.extractAllSelectors(selectorPositionsTestCss);
expect([selectors[2].selectorStartChar, selectors[2].selectorEndChar]).toEqual([0, 3]);
});
it("should find selector positions when '{' on the next line and selector is indented", function () {
var selectors = CSSUtils.extractAllSelectors(selectorPositionsTestCss);
expect([selectors[3].selectorStartChar, selectors[3].selectorEndChar]).toEqual([4, 7]);
});
it("should find selector positions when '{' on the next line and selector is indented with tabs", function () {
var selectors = CSSUtils.extractAllSelectors(selectorPositionsTestCss);
expect([selectors[4].selectorStartChar, selectors[4].selectorEndChar]).toEqual([1, 4]);
});
it("should find selector positions in a selector group when '{' on the next line", function () {
var selectors = CSSUtils.extractAllSelectors(selectorPositionsTestCss),
expected = [0, 2, 4, 6, 8, 10],
result = [
selectors[5].selectorStartChar, selectors[5].selectorEndChar,
selectors[6].selectorStartChar, selectors[6].selectorEndChar,
selectors[7].selectorStartChar, selectors[7].selectorEndChar
];
expect(result).toEqual(expected);
});
it("should find selector positions in a selector group when '{' on the next line and selector group is indented", function () {
var selectors = CSSUtils.extractAllSelectors(selectorPositionsTestCss),
expected = [4, 6, 8, 10, 12, 14],
result = [
selectors[8].selectorStartChar, selectors[8].selectorEndChar,
selectors[9].selectorStartChar, selectors[9].selectorEndChar,
selectors[10].selectorStartChar, selectors[10].selectorEndChar
];
expect(result).toEqual(expected);
});
});
describe("findSelectorAtDocumentPos in embedded <style> blocks", function () {
var editor;
beforeEach(async function () {
await init(this, embeddedHtmlFileEntry);
editor = SpecRunnerUtils.createMockEditor(this.fileContent, "html").editor;
});
afterEach(function () {
SpecRunnerUtils.destroyMockEditor(editor.document);
editor = null;
});
// Indexes of external UI are 1-based. Internal indexes are 0-based.
it("should find the first selector when pos is at beginning of selector name", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 6, ch: 0});
expect(selector).toEqual("div");
});
it("should find the second selector when pos is at beginning of selector name", function () {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, {line: 11, ch: 0});
expect(selector).toEqual(".foo");
});
});
describe("reduceStyleSheetForRegExParsing", function () {
it("should remove css comment in a line", function () {
var result = CSSUtils.reduceStyleSheetForRegExParsing(".test { color: #123; /* unbalanced paren :) */ margin: 0; }");
expect(result).toEqual(".test { color: #123; margin: 0; }");
});
it("should remove css comment across newlines", function () {
var result = CSSUtils.reduceStyleSheetForRegExParsing(".foo {\n background-color: rgb(0, 63, 255); /* start of comment\n end of comment */\n margin: 0;\n}\n");
expect(result).toEqual(".foo {\n background-color: rgb(0, 63, 255); \n margin: 0;\n}\n");
});
it("should remove multiple css comments", function () {
var result = CSSUtils.reduceStyleSheetForRegExParsing(".test { color: hsl(0, 100%, 50%); /* unbalanced paren :) */ margin: 0; } .foo { background-color: orange; /* comment */ margin: 1px; }");
expect(result).toEqual(".test { color: hsl(0, 100%, 50%); margin: 0; } .foo { background-color: orange; margin: 1px; }");
});
it("should remove css string with single quotes", function () {
var result = CSSUtils.reduceStyleSheetForRegExParsing(".test { content: '('; background-image: url('bg.svg'); padding: 0; }");
expect(result).toEqual(".test { content: ; background-image: url(); padding: 0; }");
});
it("should remove css string with double quotes", function () {
var result = CSSUtils.reduceStyleSheetForRegExParsing('.test-content { border: 0; background-image: url("bg.svg"); content: ">"; }');
expect(result).toEqual('.test-content { border: 0; background-image: url(); content: ; }');
});
it("should remove both comment and css content property", function () {
var result = CSSUtils.reduceStyleSheetForRegExParsing(".test { color: #123; /* unbalanced paren :-) */ margin: 0; content: ')'; padding: 0; }");
expect(result).toEqual(".test { color: #123; margin: 0; content: ; padding: 0; }");
});
it("should otherwise not alter stylesheet text", function () {
var result = CSSUtils.reduceStyleSheetForRegExParsing(".test { color: #1254ef; margin: 0.1em; filter: blur(); }");
expect(result).toEqual(".test { color: #1254ef; margin: 0.1em; filter: blur(); }");
});
});
}); // describe("CSSUtils")
describe("CSS Parsing", function () {
var lastCssCode,
match,
expectParseError;
function _findMatchingRules(cssCode, tagInfo, mode) {
if (tagInfo) {
var selector = "";
if (tagInfo.tag) {
selector += tagInfo.tag;
}
if (tagInfo.clazz) {
selector += "." + tagInfo.clazz;
}
if (tagInfo.id) {
selector += "#" + tagInfo.id;
}
return CSSUtils._findAllMatchingSelectorsInText(cssCode, selector, mode);
}
// If !tagInfo, we don't care about results; only making sure parse/search doesn't crash
CSSUtils._findAllMatchingSelectorsInText(cssCode, "dummy", mode);
return null;
}
/**
* Test helper function; tagInfo object contains one of: tag, id, clazz. Tests against only
* the given cssCode string in isolation (no CSS files are loaded). If tagInfo not specified,
* returns no results; only tests that parsing plus a simple search won't crash.
*/
var _match = function (cssCode, tagInfo, mode) {
lastCssCode = cssCode;
return _findMatchingRules(cssCode, tagInfo, mode);
};
/** Tests against the same CSS text as the last call to match() */
function matchAgain(tagInfo, mode) {
return match(lastCssCode, tagInfo, mode);
}
function expectCompleteSelectors(selectorInfo, expectedStr) {
expect(CSSUtils.getCompleteSelectors(selectorInfo)).toBe(expectedStr);
}
/**
* Test helper function: expects CSS parsing to fail at the given 0-based offset within the
* cssCode string, with the given error message.
*/
var _expectParseError = function (cssCode, expectedCodeOffset, expectedErrorMessage) {
try {
_findMatchingRules(cssCode, null);
// shouldn't get here since _findMatchingRules() is expected to throw
expect("Should never reach here").toBeFalse();
} catch (error) {
expect(error.index).toBe(expectedCodeOffset);
expect(error.message).toBe(expectedErrorMessage);
}
};
/** To call fail(), these helpers need access to the value of 'this' inside each it() */
beforeEach(function () {
match = _match;
expectParseError = _expectParseError.bind(this);
});
describe("Simple selectors", function () {
it("should match a lone type selector given a type", function () {
var result = match("div { color:red }", { tag: "div" });
expect(result.length).toBe(1);
expect(result[0].selectorGroup).toBeUndefined();
result = matchAgain({ tag: "span" });
expect(result.length).toBe(0);
result = matchAgain({ tag: "divfoo" }); //selector is a prefix of search
expect(result.length).toBe(0);
result = matchAgain({ tag: "di" }); //search is a prefix of selector
expect(result.length).toBe(0);
});
it("should match a lone class selector given a class", function () {
var result = match(".foo { color:red }", { clazz: "foo" });
expect(result.length).toBe(1);
expect(result[0].selectorGroup).toBeUndefined();
result = matchAgain({ clazz: "bar" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "foobar" }); //selector is a prefix of search
expect(result.length).toBe(0);
result = matchAgain({ clazz: "fo" }); //search is a prefix of selector
expect(result.length).toBe(0);
result = matchAgain({ clazz: ".foo" }); //search has extra '.' (invalid search)
expect(result.length).toBe(0);
});
it("should match a lone id selector given an id", function () {
var result = match("#foo { color:red }", { id: "foo" });
expect(result.length).toBe(1);
expect(result[0].selectorGroup).toBeUndefined();
result = matchAgain({ id: "bar" });
expect(result.length).toBe(0);
result = matchAgain({ id: "foobar" });
expect(result.length).toBe(0);
result = matchAgain({ id: "fo" });
expect(result.length).toBe(0);
result = matchAgain({ id: "#foo" });
expect(result.length).toBe(0);
});
it("shouldn't confuse type, class, and id", function () {
var css = "div { color:red } \n" +
".foo { color:green } \n" +
"#bar { color:blue }";
var result = match(css, { tag: "div" });
expect(result.length).toBe(1);
expect(result[0].selectorGroup).toBeUndefined();
result = matchAgain({ tag: "foo" });
expect(result.length).toBe(0);
result = matchAgain({ tag: "bar" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "div" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(1);
expect(result[0].selectorGroup).toBeUndefined();
result = matchAgain({ clazz: "bar" });
expect(result.length).toBe(0);
result = matchAgain({ id: "div" });
expect(result.length).toBe(0);
result = matchAgain({ id: "foo" });
expect(result.length).toBe(0);
result = matchAgain({ id: "bar" });
expect(result.length).toBe(1);
expect(result[0].selectorGroup).toBeUndefined();
});
it("should be case-sensitive for all but types", function () {
var css = "div { color:red } \n" +
"DIV { color:red } \n" +
".foo { color:green } \n" +
".Foo { color:black } \n" +
"#bar { color:blue } \n" +
"#baR { color:white }";
var result = match(css, { tag: "div" });
expect(result.length).toBe(2);
result = matchAgain({ tag: "Div" });
expect(result.length).toBe(2);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(1);
result = matchAgain({ clazz: "Foo" });
expect(result.length).toBe(1);
result = matchAgain({ clazz: "FOO" });
expect(result.length).toBe(0);
result = matchAgain({ id: "bar" });
expect(result.length).toBe(1);
result = matchAgain({ id: "baR" });
expect(result.length).toBe(1);
result = matchAgain({ id: "BAR" });
expect(result.length).toBe(0);
});
it("should match permissively", function () {
var css = "div.foo { color:red } \n" +
"div#bar { color:green } \n" +
"div.foo#bar { color:blue } \n" +
".foo#bar { color:black } \n" +
"div.foo.class2 { color: white } \n" +
".foo.class2 { color: yellow } \n" +
".foo#bar.class2 { color: cyan }";
// note last line: id selectors don't necessarily need to come last
var result = match(css, { tag: "div" }); // all selectors including a 'div' type selector
expect(result.length).toBe(4);
result = matchAgain({ clazz: "foo" }); // all selectors including a '.foo' class selector
expect(result.length).toBe(6);
result = matchAgain({ clazz: "class2" }); // all selectors including a '.class2' class selector
expect(result.length).toBe(3);
result = matchAgain({ id: "bar" }); // all selectors including a '#bar' id selector
expect(result.length).toBe(4);
});
it("should allow searching conjunctions of type, class, and id", function () {
// TODO: not required for Sprint 4
// var css = "div.foo { color:red } \n" +
// "div#bar { color:green } \n" +
// "div.foo#bar { color:blue } \n" +
// ".foo#bar { color:black } \n" +
// "div.foo.class2 { color: white } \n" +
// ".foo.class2 { color: yellow } \n" +
// ".foo#bar.class2 { color: cyan }";
// // note last line: id selectors don't necessarily need to come last
//
// var result = match(css, { tag: "div", clazz: "foo" }); // all selectors including a 'div' type selector AND a '.foo' class selector
// expect(result.length).toBe(3);
//
// // TODO: any way to search two of the same thing? (e.g. all selectors including a '.foo' AND a '.class2' class selector)
//
// result = matchAgain({ clazz: "foo", id: "bar" }); // all selectors including a '.foo' class selector AND a '#bar' id selector
// expect(result.length).toBe(3);
//
// result = matchAgain({ tag: "div", id: "bar" }); // all selectors including a 'div' type selector AND a '#bar' id selector
// expect(result.length).toBe(2);
//
// result = matchAgain({ tag: "div", clazz: "foo", id: "bar" });
// expect(result.length).toBe(1);
//
// result = matchAgain({ tag: "div", clazz: "class2", id: "bar" });
// expect(result.length).toBe(0);
});
it("should match lone '*' given any tag; else ignore", function () {
var css = "* { color:red } \n" +
"*.foo { color:green } \n" +
"*#bar { color:blue } \n" +
"*.foo#bar { color:yellow }";
// should be treated the same as: *, .foo, #bar, .foo#bar respectively
var result = match(css, { tag: "div" });
expect(result.length).toBe(1);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(2);
result = matchAgain({ id: "bar" });
expect(result.length).toBe(2);
result = matchAgain({ clazz: "otherClass" });
expect(result.length).toBe(0);
result = matchAgain({ id: "otherId" });
expect(result.length).toBe(0);
result = match("div * { color:red }", { tag: "span"});
expect(result.length).toBe(1);
matchAgain({ tag: "div"}); // only because '*' matches 'div'
expect(result.length).toBe(1);
result = match(".foo * { color:red }", { tag: "span"});
expect(result.length).toBe(1);
result = matchAgain({ clazz: "foo"});
expect(result.length).toBe(0);
result = match("#bar * { color:red }", { tag: "span"});
expect(result.length).toBe(1);
result = matchAgain({ id: "bar"});
expect(result.length).toBe(0);
});
it("should ignore pseudo-class selectors", function () {
// Note: not actually required for Sprint 4
var css = "div:hover { color:red } \n" +
".foo:hover { color:green } \n" +
"div.foo:hover { color:blue } \n" +
"#bar:hover { color:yellow } \n" +
"div#bar:hover { color:black } \n" +
".foo.class2:hover { color:white } \n" +
"div:focus:hover { color:cyan } \n" +
"div.foo:focus:hover { color:brown } \n" +
".foo:focus:hover { color:pink } \n" +
"div:focus.class3:hover { color:purple } \n" +
":focus.class3:hover { color:gray }";
var result = match(css, { tag: "div" });
expect(result.length).toBe(6);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(5);
result = matchAgain({ id: "bar" });
expect(result.length).toBe(2);
result = matchAgain({ clazz: "class3" });
expect(result.length).toBe(2);
result = matchAgain({ tag: "hover" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "hover" });
expect(result.length).toBe(0);
result = matchAgain({ id: "hover" });
expect(result.length).toBe(0);
result = matchAgain({ tag: "focus" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "focus" });
expect(result.length).toBe(0);
result = matchAgain({ id: "focus" });
expect(result.length).toBe(0);
});
it("should ignore pseudo-elements with arguments", function () {
// Note: not actually required for Sprint 4
var css = "div:nth-child(3) { color:red } \n" +
".foo:nth-child(3) { color:green } \n" +
"div.foo:nth-child(3) { color:blue } \n" +
"#bar:nth-child(3) { color:yellow } \n" +
"div#bar:nth-child(3) { color:black } \n" +
".foo.class2:nth-child(3) { color:white } \n" +
"div:nth-child(2n):nth-child(3) { color:cyan } \n" +
"div.foo:nth-child(2n):nth-child(3) { color:brown } \n" +
".foo:nth-child(2n):nth-child(3) { color:pink } \n" +
"div:nth-child(2n).class3:nth-child(3) { color:purple } \n" +
":nth-child(2n).class3:nth-child(3) { color:gray }";
var result = match(css, { tag: "div" });
expect(result.length).toBe(6);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(5);
result = matchAgain({ id: "bar" });
expect(result.length).toBe(2);
result = matchAgain({ clazz: "class3" });
expect(result.length).toBe(2);
result = matchAgain({ tag: "hover" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "hover" });