This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
/
CSSUtils-test.js
948 lines (787 loc) · 44.1 KB
/
CSSUtils-test.js
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
/*
* Copyright 2012 Adobe Systems Incorporated. All Rights Reserved.
*/
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */
/*global define: false, describe: false, it: false, xit: false, expect: false, beforeEach: false, afterEach: false, waitsFor: false, runs: false, $: false, CodeMirror: false */
define(function (require, exports, module) {
'use strict';
var NativeFileSystem = require("NativeFileSystem").NativeFileSystem,
Async = require("Async"),
FileUtils = require("FileUtils"),
CSSUtils = require("CSSUtils"),
SpecRunnerUtils = require("./SpecRunnerUtils.js");
var testPath = SpecRunnerUtils.getTestPath("/spec/CSSUtils-test-files"),
simpleCssFileEntry = new NativeFileSystem.FileEntry(testPath + "/simple.css"),
universalCssFileEntry = new NativeFileSystem.FileEntry(testPath + "/universal.css"),
groupsFileEntry = new NativeFileSystem.FileEntry(testPath + "/groups.css"),
offsetsCssFileEntry = new NativeFileSystem.FileEntry(testPath + "/offsets.css"),
bootstrapCssFileEntry = new NativeFileSystem.FileEntry(testPath + "/bootstrap.css");
/**
* Verifies whether one of the results returned by CSSUtils._findAllMatchingSelectorsInText()
* came from the expected selector string or not. String is the complete compound selector, not
* the larger selector group or the smaller rightmost-simple-selector. E.g. if some rule
* "div, foo .bar { ... }" matches a search for ".bar", the selector will be "foo .bar"
*/
var toMatchSelector = function (expected) {
return this.actual.selector.trim() === expected;
};
function init(spec, fileEntry) {
spec.fileCssContent = null;
if (fileEntry) {
spec.addMatchers({toMatchSelector: toMatchSelector});
var doneLoading = false;
runs(function () {
FileUtils.readAsText(fileEntry)
.done(function (text) {
spec.fileCssContent = text;
});
});
waitsFor(function () { return (spec.fileCssContent !== null); }, 1000);
}
}
describe("CSSUtils", function () {
beforeEach(function () {
init(this);
});
describe("basics", function () {
it("should parse an empty string", function () {
runs(function () {
var result = CSSUtils._findAllMatchingSelectorsInText("", { tag: "div" });
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) {
var result = CSSUtils._findAllMatchingSelectorsInText(cssCode, selector);
spec.expect(result.length).toEqual(ranges.length);
ranges.forEach(function (range, i) {
spec.expect(result[i].line).toEqual(range.start);
spec.expect(result[i].ruleEndLine).toEqual(range.end);
});
}
it("should return correct start and end line numbers for simple rules", function () {
runs(function () {
init(this, simpleCssFileEntry);
});
runs(function () {
expectRuleRanges(this, this.fileCssContent, "html", [ {start: 0, end: 2}, {start: 4, end: 6 }]);
expectRuleRanges(this, this.fileCssContent, ".firstGrade", [ {start: 8, end: 10} ]);
expectRuleRanges(this, this.fileCssContent, "#brack3ts",
[ {start: 16, end: 18} ]);
});
});
it("should handle rules on adjacent lines", function () {
runs(function () {
init(this, offsetsCssFileEntry);
});
runs(function () {
expectRuleRanges(this, this.fileCssContent, "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}
]);
});
});
});
describe("with the universal selector", function () {
beforeEach(function () {
init(this, universalCssFileEntry);
});
it("should match a tag name not referenced anywhere in the CSS", function () {
var matches = CSSUtils._findAllMatchingSelectorsInText(this.fileCssContent, "blockquote");
expect(matches.length).toEqual(1);
expect(matches[0]).toMatchSelector("*");
});
it("should match a tag name also referenced elsewhere in the CSS", function () {
var matches = CSSUtils._findAllMatchingSelectorsInText(this.fileCssContent, "p");
expect(matches.length).toEqual(2);
expect(matches[0]).toMatchSelector("*");
expect(matches[1]).toMatchSelector("p");
});
});
describe("with sprint 4 exemptions", function () {
beforeEach(function () {
var sprint4exemptions = new NativeFileSystem.FileEntry(testPath + "/sprint4.css");
init(this, sprint4exemptions);
});
it("should match a class selector (right-most only, no pseudo or attr selectors)", function () {
var matches = CSSUtils._findAllMatchingSelectorsInText(this.fileCssContent, ".message");
expect(matches.length).toEqual(7);
expect(matches[0]).toMatchSelector("div.message");
expect(matches[1]).toMatchSelector("footer .message");
expect(matches[2]).toMatchSelector("footer div.message");
expect(matches[3]).toMatchSelector("h2.message");
expect(matches[4]).toMatchSelector("div.message:hovered");
expect(matches[5]).toMatchSelector(".message:hover");
expect(matches[6]).toMatchSelector(".message[data-attr='42']");
});
it("should match a type selector (can terminate with class name, ID, pseudo or attr selectors)", function () {
var matches = CSSUtils._findAllMatchingSelectorsInText(this.fileCssContent, "h4");
expect(matches.length).toEqual(5);
});
});
describe("with real-world Bootstrap CSS code", function () {
beforeEach(function () {
init(this, bootstrapCssFileEntry);
});
it("should find the first instance of the h2 selector", function () {
var selectors = CSSUtils._findAllMatchingSelectorsInText(this.fileCssContent, "h2");
expect(selectors).not.toBe(null);
expect(selectors.length).toBeGreaterThan(0);
expect(selectors[0]).not.toBe(null);
expect(selectors[0].line).toBe(292);
expect(selectors[0].ruleEndLine).toBe(301);
});
it("should find all instances of the h2 selector", function () {
var selectors = CSSUtils._findAllMatchingSelectorsInText(this.fileCssContent, "h2");
expect(selectors.length).toBe(2);
expect(selectors[0].line).toBe(292);
expect(selectors[0].ruleEndLine).toBe(301);
expect(selectors[1].line).toBe(318);
expect(selectors[1].ruleEndLine).toBe(321);
});
it("should return an empty array when findAllMatchingSelectors() can't find any matches", function () {
var selectors = CSSUtils._findAllMatchingSelectorsInText(this.fileCssContent, "NO-SUCH-SELECTOR");
expect(selectors.length).toBe(0);
});
});
}); // describe("CSSUtils")
describe("CSS Parsing: ", function () {
var lastCssCode,
match,
expectParseError;
function _findMatchingRules(cssCode, tagInfo) {
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);
} else {
// If !tagInfo, we don't care about results; only making sure parse/search doesn't crash
CSSUtils._findAllMatchingSelectorsInText(cssCode, "dummy");
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) {
lastCssCode = cssCode;
try {
return _findMatchingRules(cssCode, tagInfo);
} catch (e) {
this.fail(e.message + ": " + cssCode);
return [];
}
};
/** Tests against the same CSS text as the last call to match() */
function matchAgain(tagInfo) {
return match(lastCssCode, tagInfo);
}
/**
* 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
this.fail("Expected parse error: " + cssCode);
} 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.bind(this);
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);
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);
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);
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);
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);
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);
});
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 attribute selectors", function () {
// Note: not actually required for Sprint 4
var css = "div { color:red } \n" +
"div[foo] { color:green } \n" +
"h4[div] { color:blue } \n" +
"h4[foo] { color:blue } \n" +
"h4[title=\"div\"] { color:yellow } \n" +
"h4[title=\".foo\"] { color:yellow } \n" +
"h4[attr].bar { color:black } \n" +
"h4.bar[attr] { color:black }";
var result = match(css, { tag: "div" });
expect(result.length).toBe(2);
result = matchAgain({ tag: "h4" });
expect(result.length).toBe(6);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "bar" });
expect(result.length).toBe(2);
});
it("should ignore the content of strings", function () {
// Spaces inside string, single quotes
var result = match("div[attr='.foo #bar'] {}", { tag: "div" });
expect(result.length).toBe(1);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(0);
result = matchAgain({ id: "bar" });
expect(result.length).toBe(0);
// ...double quotes
result = match("div[attr=\".foo #bar\"] {}", { tag: "div" });
expect(result.length).toBe(1);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(0);
result = matchAgain({ id: "bar" });
expect(result.length).toBe(0);
// Quotes nested within quotes
var css = "div[attr=\"value with \\\"nested double .quotes\\\"\"] { color:red } \n" +
"div[attr=\"value with 'single .quotes'\"] { color:green } \n" +
"div[attr='value with \\'nested single .quotes\\''] { color:blue } \n" +
"div[attr='value with \"double .quotes\"'] { color:blue } \n" +
"div[attr='value with \"double .quotes\"'].foo { color:yellow } \n" +
"div#bar[attr='value with \"double .quotes\"h4'] { color:white } \n" +
"div[attr='value with \"double .quotes\"h4'] .foo { color:black } \n" +
".foo { color:cyan }";
result = match(css, { tag: "div"});
expect(result.length).toBe(6);
result = matchAgain({ clazz: "foo"});
expect(result.length).toBe(3);
result = matchAgain({ id: "bar"});
expect(result.length).toBe(1);
result = matchAgain({ clazz: "quotes"});
expect(result.length).toBe(0);
result = matchAgain({ tag: "h4"});
expect(result.length).toBe(0);
// Braces inside string; string inside rule (not inside selector)
css = "a::after { content: ' {' attr(href) '}'; } \n" +
".foo { color:red } \n" +
"a::after { content: \" {\" attr(href) \"}\"; } \n" +
"li::before { content: \"} h4 { color:black }\"; } \n" +
"div { color:green }";
result = match(css, { tag: "a" });
expect(result.length).toBe(2);
result = matchAgain({ tag: "div" });
expect(result.length).toBe(1);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(1);
result = matchAgain({ tag: "h4" });
expect(result.length).toBe(0);
// Quotes AND braces nested inside string
result = match("div::after { content: \"\\\"}\\\"\"; }\nh4 { color: red}", { tag: "div" });
expect(result.length).toBe(1);
result = matchAgain({ tag: "h4" });
expect(result.length).toBe(1);
result = match("div::after { content: \"\\\"{\"; }\nh4 { color: red}", { tag: "div" });
expect(result.length).toBe(1);
result = matchAgain({ tag: "h4" });
expect(result.length).toBe(1);
css = "@import \"null?\\\"{\"; \n" + // a real-world CSS hack similar to the above case
"div { color: red }";
result = match(css, { tag: "div" });
expect(result.length).toBe(1);
// Newline inside string (escaped)
result = match("li::before { content: 'foo\\nbar'; } \n div { color:red }", { tag: "div" });
expect(result.length).toBe(1);
// Newline inside string (unescaped, with backslash line terminator)
result = match("li::before { content: 'foo\\\nbar'; } \n div { color:red }", { tag: "div" });
expect(result.length).toBe(1);
// Comments inside strings
// '/*' in a string does not start a comment...
result = match("div::before { content: \"/*\"; } \n h4 { color: red }", { tag: "div" });
expect(result.length).toBe(1);
result = matchAgain({ tag: "h4" });
expect(result.length).toBe(1);
// If not in a comment, '*/' is also fine...
result = match("div::before { content: \"/**/\"; } \n h4 { color:red }", { tag: "div" });
expect(result.length).toBe(1);
result = matchAgain({ tag: "h4" });
expect(result.length).toBe(1);
result = match("div::before { content: \"/*\"; } \n h4::before { content: \"*/\" }", { tag: "div" });
expect(result.length).toBe(1);
result = matchAgain({ tag: "h4" });
expect(result.length).toBe(1);
// But if already in a comment, '*/' in a string DOES END the comment
// So the rule below is equivalent to "span::before { content:\"foo\"; }"
result = match("span::before { content:/*div::before { content: \"*/\"foo\"; } \n h4 { color:red }", { tag: "div" });
expect(result.length).toBe(0);
result = matchAgain({ tag: "span" });
expect(result.length).toBe(1);
result = matchAgain({ tag: "h4" });
expect(result.length).toBe(1);
});
it("should handle unusual whitespace", function () {
// This is valid CSS, but both Chrome and FF treat it as invalid
// It *should* be treated as ".foo .bar" (not ".foo.bar")
var css = ".foo\n" +
".bar\n" +
"{ color: red; }";
var result = match(css, { clazz: "foo" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "bar" });
expect(result.length).toBe(1);
css = ".foo\n" +
"{\n" +
" color: red;\n" +
"}";
result = match(css, { clazz: "foo" });
expect(result.length).toBe(1);
});
it("shouldn't crash on CSS3 selectors", function () {
// See spec: http://www.w3.org/TR/selectors/
// Attribute selectors
match("[role] {}");
match("a[href] {}");
match(".slider[role] {}");
match("a[href][nofollow] {}");
match("[href][nofollow] {}");
match("[attr='value'] {}");
match("[attr=\"value\"] {}");
match("[attr~='value'] {}");
match("[attr|='value'] {}");
match("[attr^='value'] {}");
match("[attr$='value'] {}");
match("[attr*='value'] {}");
match("div[attr*='value'].myClass#myId {}");
match("div#myId[attr*='value'].myClass {}");
match(":focus[attr=\"value\"].className {}");
match("tagName[attr2='value'] {}");
match("tagName[attr2 = 'value'] {}");
match("tagName[attr2 ='value'] {}");
match("tagName[attr2= 'value'] {}");
match("tagName[attr2=\"value\"] {}");
match("[attr2='value'] {}");
match("tagName[attr=\"value\"][attr2=\"value2\"] {}");
match("tagName[attr='value'][attr2='value2'] {}");
match(":not([attr2=\"value2\"]) {}");
// Pseudo-classes with complicated syntax
match(":lang(de) {}");
match(":lang(en-US) {}");
match("ul:nth-child(+2n) {}");
match("ul:nth-child( 2n + 1 ) {}");
match("ul:nth-child(2n + 1) {}");
match("ul:nth-child(2n+1) {}");
match("ul:nth-child(-2n+1) {}");
match("ul:nth-child(+n+1) {}");
match("ul:nth-child(n-1) {}");
match("ul:nth-child(0n+5) {}");
match("ul:nth-child(-0n+5) {}");
match("ul:nth-child(n) {}");
match("ul:nth-child(2) {}");
match("ul:nth-child(-2) {}");
match("ul:nth-child(-2)[href^='ftp']#myId.myClass {}");
// :not is especially nasty
match(":not(:link) {}");
match(":not(:link):not(:focus) {}");
match("tagName:not(.className).className2 {}");
match(".className:not(tagName) {}");
match("tagName:not(.className) {}");
match(":not(tagName.className) {}");
match("tagName:not([attr=\"value\"]) {}");
match("tagName:not([attr=\"value\"])[attrB='valueB'] {}");
match("tagName[attr='value']:not([attrB=\"valueB\"]) {}");
// Pseudo-elements (can only occur once, and must be after the rightmost combinator)
match("::first-line {}");
match("tagName::first-line {}");
match(".className::first-line {}");
match("::first-line.className {}"); //spec says this is valid but no browsers seem to support it
match("p:hover::first-line {}");
// not valid: :not(::first-line) - because pseudo-elements aren't simple selectors
// Namespaces
var nsDecl = "@namespace ns \"http://www.example.com\"\n";
match("[*|role] {}");
match("[|role] {}");
match(nsDecl + "[ns|role] {}");
match(nsDecl + "[ns|role|='value'] {}");
match("*|div {}");
match("|div {}");
match(nsDecl + "ns|div {}");
match("*|* {}");
match("|* {}");
match(nsDecl + "ns|* {}");
match("*|*:not(*) {}"); // actual example from W3C spec; 5 points if you can figure out what it means!
});
it("shouldn't crash on CSS Animation syntax (@keyframes)", function () {
var css = "div { color:red } \n" +
"@keyframes slide { \n" +
" from { left: 0; } \n" +
" 50% { left: 30px; } \n" +
" to { left: 100px; } \n" +
"} \n" +
".foo { color:green }";
var result = match(css, { tag: "div" });
expect(result.length).toBe(1);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(1);
// TODO (issue #389): false positive match from @keyframes animation identifier
// result = matchAgain({ tag: "slide" });
// expect(result.length).toBe(0);
result = matchAgain({ tag: "from" });
expect(result.length).toBe(0);
});
}); // describe("Simple selectors")
describe("Combinators", function () {
it("should ignore descendant combinators", function () {
var result = match("h4 .foo { color:red }", { tag: "h4" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(1);
result = match("p h4 div { color:red }", { tag: "p" });
expect(result.length).toBe(0);
result = matchAgain({ tag: "h4" });
expect(result.length).toBe(0);
result = matchAgain({ tag: "div" });
expect(result.length).toBe(1);
result = match(".foo h4 { color:red }", { tag: "h4" });
expect(result.length).toBe(1);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(0);
result = match("div div { color:red }", { tag: "div" });
expect(result.length).toBe(1);
result = match(".foo .foo { color:red }", { clazz: "foo" });
expect(result.length).toBe(1);
result = matchAgain({ tag: "foo" });
expect(result.length).toBe(0);
});
it("should ignore other combinators", function () {
var result = match("h4 > .foo { color:red }", { tag: "h4" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(1);
result = match(".foo > h4 { color:red }", { tag: "h4" });
expect(result.length).toBe(1);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(0);
result = match("h4 + .foo { color:red }", { tag: "h4" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(1);
result = match(".foo + h4 { color:red }", { tag: "h4" });
expect(result.length).toBe(1);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(0);
result = match("p > h4 + div { color:red }", { tag: "p" });
expect(result.length).toBe(0);
result = matchAgain({ tag: "h4" });
expect(result.length).toBe(0);
result = matchAgain({ tag: "div" });
expect(result.length).toBe(1);
result = match("p > h4 div { color:red }", { tag: "p" });
expect(result.length).toBe(0);
result = matchAgain({ tag: "h4" });
expect(result.length).toBe(0);
result = match("p > h4 div { color:red }", { tag: "div" });
expect(result.length).toBe(1);
result = match("h4>.foo { color:red }", { tag: "h4" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(1);
result = match("h4> .foo { color:red }", { tag: "h4" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(1);
result = match("h4 >.foo { color:red }", { tag: "h4" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(1);
result = match("h4+.foo { color:red }", { tag: "h4" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(1);
result = match("h4 * .foo { color:red }", { tag: "h4" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(1);
result = match("h4 ~ .foo { color:red }", { tag: "h4" });
expect(result.length).toBe(0);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(1);
});
}); // describe("Combinators")
describe("Selector groups", function () {
it("should match any item in selector group", function () {
// Comma- and space- separated
var result = match("h4, .foo, #bar { color:red }", { tag: "h4" });
expect(result.length).toBe(1);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(1);
result = matchAgain({ id: "bar" });
expect(result.length).toBe(1);
// Comma only
result = match("h4,.foo,#bar { color:red }", { tag: "h4" });
expect(result.length).toBe(1);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(1);
result = matchAgain({ id: "bar" });
expect(result.length).toBe(1);
// Newline-separated
result = match("h4,\n.foo,\r\n#bar { color:red }", { tag: "h4" });
expect(result.length).toBe(1);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(1);
result = matchAgain({ id: "bar" });
expect(result.length).toBe(1);
// Space-separated with a space combinator
result = match("h4, .foo #bar { color:red }", { tag: "h4" });
expect(result.length).toBe(1);
result = matchAgain({ clazz: "foo" });
expect(result.length).toBe(0);
result = matchAgain({ id: "bar" });
expect(result.length).toBe(1);
// Test items of each type in all positions (first, last, middle)
result = match("h4, h4, h4 { color:red }", { tag: "h4" });
expect(result.length).toBe(3);
result = match(".foo, .foo, .foo { color:red }", { clazz: "foo" });
expect(result.length).toBe(3);
result = match("#bar, #bar, #bar { color:red }", { id: "bar" });
expect(result.length).toBe(3);
});
}); // describe("Selector groups")
describe("At-rules", function () {
it("should handle @media", function () {
// TODO - not required for Sprint 4
});
it("should handle @page", function () {
// TODO - not required for Sprint 4
});
}); // describe("At-rules")
// The following tests were for known failures in the LESS parser
describe("LESS Known Issues", function () {
it("should handle an empty declaration (extra semi-colon)", function () {
var result = match("h4 { color:red;; }", { tag: "h4" });
expect(result.length).toBe(1);
result = match("div{; color:red}", { tag: "div" });
expect(result.length).toBe(1);
});
it("should handle IE filter syntaxes", function () {
var result = match("div{opacity:0; filter:alpha(opacity = 0)}", { tag: "div" });
expect(result.length).toBe(1);
result = match("div { filter:alpha(opacity = 0) }", { tag: "div" });
expect(result.length).toBe(1);
result = match("div { filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr='#92CBE0',EndColorStr='#6B9EBC'); }",
{ tag: "div" });
expect(result.length).toBe(1);
});
it("should handle unnecessary escape codes", function () {
var result = match("div { f\\loat: left; }", { tag: "div" });
expect(result.length).toBe(1);
});
it("should handle comments within properties", function () {
match("div { display/**/: block; }");
match("div/**/ { display: block; }");
match("div /**/{ display: block; }");
match("div {/**/ display: block; }");
match("div { /**/display: block; }");
match("div { display:/**/ block; }");
match("div { display: /**/block; }");
match("div { display: block/**/; }");
match("div { display: block /**/; }");
});
}); // describe("Known Issues")
}); //describe("CSS Parsing")
});