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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
|
# -*- coding: utf-8 -*-
require File.dirname(__FILE__) + '/test_helper'
require 'sass/scss/css_parser'
# These tests just test the parsing of CSS
# (both standard and any hacks we intend to support).
# Tests of SCSS-specific behavior go in scss_test.rb.
class ScssCssTest < Minitest::Test
include ScssTestHelper
def test_basic_scss
assert_parses <<SCSS
selector {
property: value;
property2: value; }
SCSS
assert_equal <<CSS, render('sel{p:v}')
sel {
p: v; }
CSS
end
def test_empty_rule
assert_equal "", render("#foo .bar {}")
assert_equal "", render(<<SCSS)
#foo .bar {
}
SCSS
end
def test_cdo_and_cdc_ignored_at_toplevel
assert_equal <<CSS, render(<<SCSS)
foo {
bar: baz; }
bar {
bar: baz; }
baz {
bar: baz; }
CSS
foo {bar: baz}
<!--
bar {bar: baz}
-->
baz {bar: baz}
SCSS
end
def test_unicode
assert_parses <<SCSS
@charset "UTF-8";
foo {
bar: föö bâr; }
SCSS
assert_equal <<CSS, render(<<SCSS)
@charset "UTF-8";
foo {
bar: föö bâr; }
CSS
foo {
bar: föö bâr; }
SCSS
end
def test_invisible_comments
assert_equal <<CSS, render(<<SCSS)
foo {
a: d; }
CSS
foo {a: /* b; c: */ d}
SCSS
assert_equal <<CSS, render(<<SCSS)
foo {
a: d; }
CSS
foo {a /*: b; c */: d}
SCSS
end
def test_crazy_comments
# http://www.w3.org/Style/CSS/Test/CSS2.1/current/xhtml1/t040109-c17-comments-00-b.xht
assert_equal <<CSS, render(<<SCSS)
/* This is a CSS comment. */
.one {
color: green; }
/* Another comment */
/* The following should not be used:
.two {color: red;} */
.three {
color: green;
/* color: red; */ }
/**
.four {color: red;} */
.five {
color: green; }
/**/
.six {
color: green; }
/*********/
.seven {
color: green; }
/* a comment **/
.eight {
color: green; }
CSS
/* This is a CSS comment. */
.one {color: green;} /* Another comment */
/* The following should not be used:
.two {color: red;} */
.three {color: green; /* color: red; */}
/**
.four {color: red;} */
.five {color: green;}
/**/
.six {color: green;}
/*********/
.seven {color: green;}
/* a comment **/
.eight {color: green;}
SCSS
end
def test_rule_comments
assert_parses <<SCSS
/* Foo */
.foo {
a: b; }
SCSS
assert_equal <<CSS, render(<<SCSS)
/* Foo
* Bar */
.foo {
a: b; }
CSS
/* Foo
* Bar */.foo {
a: b; }
SCSS
end
def test_property_comments
assert_parses <<SCSS
.foo {
/* Foo */
a: b; }
SCSS
assert_equal <<CSS, render(<<SCSS)
.foo {
/* Foo
* Bar */
a: b; }
CSS
.foo {
/* Foo
* Bar */a: b; }
SCSS
end
def test_selector_comments
assert_equal <<CSS, render(<<SCSS)
.foo #bar:baz(bip) {
a: b; }
CSS
.foo /* .a #foo */ #bar:baz(/* bang )*/ bip) {
a: b; }
SCSS
end
def test_lonely_comments
assert_parses <<SCSS
/* Foo
* Bar */
SCSS
assert_parses <<SCSS
.foo {
/* Foo
* Bar */ }
SCSS
end
def test_multiple_comments
assert_parses <<SCSS
/* Foo
* Bar */
/* Baz
* Bang */
SCSS
assert_parses <<SCSS
.foo {
/* Foo
* Bar */
/* Baz
* Bang */ }
SCSS
assert_equal <<CSS, render(<<SCSS)
.foo {
/* Foo Bar */
/* Baz Bang */ }
CSS
.foo {
/* Foo Bar *//* Baz Bang */ }
SCSS
end
def test_bizarrely_formatted_comments
assert_parses <<SCSS
.foo {
/* Foo
Bar
Baz */
a: b; }
SCSS
assert_parses <<SCSS
.foo {
/* Foo
Bar
Baz */
a: b; }
SCSS
assert_equal <<CSS, render(<<SCSS)
.foo {
/* Foo
Bar */
a: b; }
CSS
.foo {/* Foo
Bar */
a: b; }
SCSS
assert_equal <<CSS, render(<<SCSS)
.foo {
/* Foo
Bar
Baz */
a: b; }
CSS
.foo {/* Foo
Bar
Baz */
a: b; }
SCSS
end
## Declarations
def test_vendor_properties
assert_parses <<SCSS
foo {
-moz-foo-bar: blat;
-o-flat-blang: wibble; }
SCSS
end
def test_empty_declarations
assert_equal <<CSS, render(<<SCSS)
foo {
bar: baz; }
CSS
foo {;;;;
bar: baz;;;;
;;}
SCSS
end
def test_basic_property_types
assert_parses <<SCSS
foo {
a: 2;
b: 2.3em;
c: 50%;
d: "fraz bran";
e: flanny-blanny-blan;
f: url(https://sass-lang.com);
g: U+ffa?;
h: #aabbcc; }
SCSS
end
def test_functions
assert_parses <<SCSS
foo {
a: foo-bar(12);
b: -foo-bar-baz(13, 14 15); }
SCSS
end
def test_unary_minus
assert_parses <<SCSS
foo {
a: -2;
b: -2.3em;
c: -50%;
d: -foo(bar baz); }
SCSS
end
def test_operators
assert_parses <<SCSS
foo {
a: foo bar baz;
b: foo, #aabbcc, -12;
c: 1px/2px/-3px;
d: foo bar, baz/bang; }
SCSS
end
def test_important
assert_parses <<SCSS
foo {
a: foo !important;
b: foo bar !important;
b: foo, bar !important; }
SCSS
end
def test_initial_hyphen
assert_parses <<SCSS
foo {
a: -moz-bar-baz;
b: foo -o-bar-baz; }
SCSS
end
def test_ms_long_filter_syntax
assert_equal <<CSS, render(<<SCSS)
foo {
filter: progid:DXImageTransform.Microsoft.gradient(GradientType=1, startColorstr=#c0ff3300, endColorstr=#ff000000);
filter: progid:DXImageTransform.Microsoft.gradient(GradientType=1, startColorstr=#c0ff3300, endColorstr=#ff000000); }
CSS
foo {
filter: progid:DXImageTransform.Microsoft.gradient(GradientType=1, startColorstr=#c0ff3300, endColorstr=#ff000000);
filter:progid:DXImageTransform.Microsoft.gradient(GradientType=1, startColorstr=#c0ff3300, endColorstr=#ff000000); }
SCSS
end
def test_ms_short_filter_syntax
assert_parses <<SCSS
foo {
filter: alpha(opacity=20);
filter: alpha(opacity=20, enabled=true);
filter: blaznicate(foo=bar, baz=bang bip, bart=#fa4600); }
SCSS
end
def test_declaration_hacks
assert_parses <<SCSS
foo {
_name: val;
*name: val;
:name: val;
.name: val;
#name: val;
name/**/: val;
name/*\\**/: val;
name: val; }
SCSS
end
def test_trailing_hash_hack
assert_parses <<SCSS
foo {
foo: bar;
#baz: bang;
#bip: bop; }
SCSS
end
def test_zero_arg_functions
assert_parses <<SCSS
foo {
a: foo();
b: bar baz-bang() bip; }
SCSS
end
def test_expression_function
assert_parses <<SCSS
foo {
a: 12px expression(1 + (3 / Foo.bar("baz" + "bang") + function() {return 12;}) % 12); }
SCSS
end
def test_calc_function
assert_parses <<SCSS
foo {
a: 12px calc(100%/3 - 2*1em - 2*1px);
b: 12px -moz-calc(100%/3 - 2*1em - 2*1px);
b: 12px -webkit-calc(100%/3 - 2*1em - 2*1px);
b: 12px -foobar-calc(100%/3 - 2*1em - 2*1px); }
SCSS
end
def test_element_function
assert_parses <<SCSS
foo {
a: -moz-element(#foo);
b: -webkit-element(#foo);
b: -foobar-element(#foo); }
SCSS
end
def test_unary_ops
assert_equal <<CSS, render(<<SCSS)
foo {
a: -0.5em;
b: +0.5em;
c: -foo(12px);
d: +foo(12px); }
CSS
foo {
a: -0.5em;
b: +0.5em;
c: -foo(12px);
d: +foo(12px); }
SCSS
end
def test_css_string_escapes
assert_parses <<SCSS
foo {
a: "\\foo bar";
b: "foo\\ bar";
c: "\\2022 \\0020";
d: "foo\\\\bar";
e: "foo\\"'bar"; }
SCSS
end
def test_css_ident_escapes
assert_parses <<SCSS
foo {
a: \\foo bar;
b: foo\\ bar;
c: \\2022 \\0020;
d: foo\\\\bar;
e: foo\\"\\'bar; }
SCSS
end
## Directives
def test_namespace_directive
assert_parses '@namespace "http://www.w3.org/Profiles/xhtml1-strict";'
assert_parses '@namespace url(http://www.w3.org/Profiles/xhtml1-strict);'
assert_parses '@namespace html url("http://www.w3.org/Profiles/xhtml1-strict");'
end
def test_media_directive
assert_parses <<SCSS
@media all {
rule1 {
prop: val; }
rule2 {
prop: val; } }
SCSS
assert_parses <<SCSS
@media screen, print {
rule1 {
prop: val; }
rule2 {
prop: val; } }
SCSS
end
def test_media_directive_with_keywords
assert_parses <<SCSS
@media screen and (-webkit-min-device-pixel-ratio: 0) {
a: b; }
SCSS
assert_parses <<SCSS
@media only screen, print and (foo: 0px) and (bar: flam(12px solid)) {
a: b; }
SCSS
end
def test_import_directive
assert_parses '@import "foo.css";'
assert_parses '@import url("foo.css");'
assert_parses "@import url('foo.css');"
assert_parses '@import url(foo.css);'
assert_equal <<CSS, render(<<SCSS)
@import "foo.css";
CSS
@import 'foo.css';
SCSS
end
def test_import_directive_with_backslash_newline
assert_equal <<CSS, render(<<SCSS)
@import "foobar.css";
CSS
@import "foo\\
bar.css";
SCSS
end
def test_string_import_directive_with_media
assert_parses '@import "foo.css" screen;'
assert_parses '@import "foo.css" screen, print;'
assert_parses '@import "foo.css" screen, print and (foo: 0);'
assert_parses '@import "foo.css" screen, only print, screen and (foo: 0);'
end
def test_url_import_directive_with_media
assert_parses '@import url("foo.css") screen;'
assert_parses '@import url("foo.css") screen, print;'
assert_parses '@import url("foo.css") screen, print and (foo: 0);'
assert_parses '@import url("foo.css") screen, only print, screen and (foo: 0);'
end
def test_page_directive
assert_parses <<SCSS
@page {
prop1: val;
prop2: val; }
SCSS
assert_parses <<SCSS
@page flap {
prop1: val;
prop2: val; }
SCSS
assert_parses <<SCSS
@page :first {
prop1: val;
prop2: val; }
SCSS
assert_parses <<SCSS
@page flap:first {
prop1: val;
prop2: val; }
SCSS
end
def test_blockless_directive_without_semicolon
assert_equal "@foo \"bar\";\n", render('@foo "bar"')
end
def test_directive_with_lots_of_whitespace
assert_equal "@foo \"bar\";\n", render('@foo "bar" ;')
end
def test_empty_blockless_directive
assert_parses "@foo;"
end
def test_multiple_blockless_directives
assert_parses <<SCSS
@foo bar;
@bar baz;
SCSS
end
def test_empty_block_directive
assert_parses "@foo {}"
assert_equal "@foo {}\n", render(<<SCSS)
@foo {
}
SCSS
end
def test_multiple_block_directives
assert_parses <<SCSS
@foo bar {
a: b; }
@bar baz {
c: d; }
SCSS
end
def test_block_directive_with_rule_and_property
assert_parses <<SCSS
@foo {
rule {
a: b; }
a: b; }
SCSS
end
def test_block_directive_with_semicolon
assert_equal <<CSS, render(<<SCSS)
@foo {
a: b; }
@bar {
a: b; }
CSS
@foo {a:b};
@bar {a:b};
SCSS
end
def test_moz_document_directive
assert_equal <<CSS, render(<<SCSS)
@-moz-document url(http://www.w3.org/),
url-prefix(http://www.w3.org/Style/),
domain(mozilla.org),
regexp("^https:.*") {
.foo {
a: b; } }
CSS
@-moz-document url(http://www.w3.org/),
url-prefix(http://www.w3.org/Style/),
domain(mozilla.org),
regexp("^https:.*") {
.foo {a: b}
}
SCSS
end
def test_supports
assert_equal <<CSS, render(<<SCSS)
@supports (((a: b) and (c: d)) or (not (d: e))) and ((not (f: g)) or (not ((h: i) and (j: k)))) {
.foo {
a: b; } }
@supports (a: b) {
.foo {
a: b; } }
CSS
@supports (((a: b) and (c: d)) or (not (d: e))) and ((not (f: g)) or (not ((h: i) and (j: k)))) {
.foo {
a: b;
}
}
@supports (a: b) {
.foo {
a: b;
}
}
SCSS
end
def test_supports_with_prefix
assert_equal <<CSS, render(<<SCSS)
@-prefix-supports (((a: b) and (c: d)) or (not (d: e))) and ((not (f: g)) or (not ((h: i) and (j: k)))) {
.foo {
a: b; } }
CSS
@-prefix-supports (((a: b) and (c: d)) or (not (d: e))) and ((not (f: g)) or (not ((h: i) and (j: k)))) {
.foo {
a: b;
}
}
SCSS
end
def test_supports_allows_similar_operators_without_parens
assert_equal <<CSS, render(<<SCSS)
@supports (a: b) and (c: d) and (e: f) {
.foo {
a: b; } }
@supports (a: b) or (c: d) or (e: f) {
.foo {
a: b; } }
CSS
@supports (a: b) and (c: d) and (e: f) {
.foo {
a: b;
}
}
@supports (a: b) or (c: d) or (e: f) {
.foo {
a: b;
}
}
SCSS
end
def test_keyframes
assert_equal <<CSS, render(<<SCSS)
@keyframes identifier {
0% {
top: 0;
left: 0; }
30% {
top: 50px; }
68%, 72% {
left: 50px; }
100% {
top: 100px;
left: 100%; } }
CSS
@keyframes identifier {
0% {top: 0; left: 0}
30% {top: 50px}
68%, 72% {left: 50px}
100% {top: 100px; left: 100%}
}
SCSS
end
def test_keyframes_with_empty_rules
assert_equal <<CSS, render(<<SCSS)
@keyframes identifier {
50% {
background-color: black; } }
CSS
@keyframes identifier {
0% {}
50% {background-color: black}
100% {}
}
SCSS
end
def test_keyframes_with_custom_identifiers
assert_equal <<CSS, render(<<SCSS)
@-skrollr-keyframes identifier {
center-top {
left: 100%; }
top-bottom {
left: 0%; } }
CSS
@-skrollr-keyframes identifier {
center-top {
left: 100%;
}
top-bottom {
left: 0%;
}
}
SCSS
end
## Selectors
# Taken from http://dev.w3.org/csswg/selectors4/#overview
def test_summarized_selectors_with_element
assert_selector_parses('*')
assert_selector_parses('E')
assert_selector_parses('E:not(s)')
assert_selector_parses('E:not(s1, s2)')
assert_selector_parses('E:matches(s1, s2)')
assert_selector_parses('E:has(s1, s2)')
assert_selector_parses('E:has(> s1, > s2)')
assert_selector_parses('E.warning')
assert_selector_parses('E#myid')
assert_selector_parses('E[foo]')
assert_selector_parses('E[foo="bar"]')
assert_selector_parses('E[foo="bar" i]')
assert_selector_parses('E[foo~="bar"]')
assert_selector_parses('E[foo^="bar"]')
assert_selector_parses('E[foo$="bar"]')
assert_selector_parses('E[foo*="bar"]')
assert_selector_parses('E[foo|="en"]')
assert_selector_parses('E:dir(ltr)')
assert_selector_parses('E:lang(fr)')
assert_selector_parses('E:lang(zh, *-hant)')
assert_selector_parses('E:any-link')
assert_selector_parses('E:link')
assert_selector_parses('E:visited')
assert_selector_parses('E:local-link')
assert_selector_parses('E:local-link(0)')
assert_selector_parses('E:target')
assert_selector_parses('E:scope')
assert_selector_parses('E:current')
assert_selector_parses('E:current(s)')
assert_selector_parses('E:past')
assert_selector_parses('E:future')
assert_selector_parses('E:active')
assert_selector_parses('E:hover')
assert_selector_parses('E:focus')
assert_selector_parses('E:enabled')
assert_selector_parses('E:disabled')
assert_selector_parses('E:checked')
assert_selector_parses('E:indeterminate')
assert_selector_parses('E:default')
assert_selector_parses('E:in-range')
assert_selector_parses('E:out-of-range')
assert_selector_parses('E:required')
assert_selector_parses('E:optional')
assert_selector_parses('E:read-only')
assert_selector_parses('E:read-write')
assert_selector_parses('E:root')
assert_selector_parses('E:empty')
assert_selector_parses('E:first-child')
assert_selector_parses('E:nth-child(n)')
assert_selector_parses('E:last-child')
assert_selector_parses('E:nth-last-child(n)')
assert_selector_parses('E:only-child')
assert_selector_parses('E:first-of-type')
assert_selector_parses('E:nth-of-type(n)')
assert_selector_parses('E:last-of-type')
assert_selector_parses('E:nth-last-of-type(n)')
assert_selector_parses('E:only-of-type')
assert_selector_parses('E:nth-child(n of selector)')
assert_selector_parses('E:nth-last-child(n of selector)')
assert_selector_parses('E:nth-child(n)')
assert_selector_parses('E:nth-last-child(n)')
assert_selector_parses('E F')
assert_selector_parses('E > F')
assert_selector_parses('E + F')
assert_selector_parses('E ~ F')
silence_warnings {assert_selector_parses('E /foo/ F')}
silence_warnings {assert_selector_parses('E! > F')}
silence_warnings {assert_selector_parses('E /ns|foo/ F')}
# From http://dev.w3.org/csswg/css-scoping-1/
assert_selector_parses('E:host(s)')
assert_selector_parses('E:host-context(s)')
end
# Taken from http://dev.w3.org/csswg/selectors4/#overview, but without element
# names.
def test_more_summarized_selectors
assert_selector_parses(':not(s)')
assert_selector_parses(':not(s1, s2)')
assert_selector_parses(':matches(s1, s2)')
assert_selector_parses(':has(s1, s2)')
assert_selector_parses(':has(> s1, > s2)')
assert_selector_parses('.warning')
assert_selector_parses('#myid')
assert_selector_parses('[foo]')
assert_selector_parses('[foo="bar"]')
assert_selector_parses('[foo="bar" i]')
assert_selector_parses('[foo~="bar"]')
assert_selector_parses('[foo^="bar"]')
assert_selector_parses('[foo$="bar"]')
assert_selector_parses('[foo*="bar"]')
assert_selector_parses('[foo|="en"]')
assert_selector_parses(':dir(ltr)')
assert_selector_parses(':lang(fr)')
assert_selector_parses(':lang(zh, *-hant)')
assert_selector_parses(':any-link')
assert_selector_parses(':link')
assert_selector_parses(':visited')
assert_selector_parses(':local-link')
assert_selector_parses(':local-link(0)')
assert_selector_parses(':target')
assert_selector_parses(':scope')
assert_selector_parses(':current')
assert_selector_parses(':current(s)')
assert_selector_parses(':past')
assert_selector_parses(':future')
assert_selector_parses(':active')
assert_selector_parses(':hover')
assert_selector_parses(':focus')
assert_selector_parses(':enabled')
assert_selector_parses(':disabled')
assert_selector_parses(':checked')
assert_selector_parses(':indeterminate')
assert_selector_parses(':default')
assert_selector_parses(':in-range')
assert_selector_parses(':out-of-range')
assert_selector_parses(':required')
assert_selector_parses(':optional')
assert_selector_parses(':read-only')
assert_selector_parses(':read-write')
assert_selector_parses(':root')
assert_selector_parses(':empty')
assert_selector_parses(':first-child')
assert_selector_parses(':nth-child(n)')
assert_selector_parses(':last-child')
assert_selector_parses(':nth-last-child(n)')
assert_selector_parses(':only-child')
assert_selector_parses(':first-of-type')
assert_selector_parses(':nth-of-type(n)')
assert_selector_parses(':last-of-type')
assert_selector_parses(':nth-last-of-type(n)')
assert_selector_parses(':only-of-type')
assert_selector_parses(':nth-child(n of selector)')
assert_selector_parses(':nth-last-child(n of selector)')
assert_selector_parses(':nth-child(n)')
assert_selector_parses(':nth-last-child(n)')
# From http://dev.w3.org/csswg/css-scoping-1/
assert_selector_parses(':host(s)')
assert_selector_parses(':host-context(s)')
end
def test_attribute_selectors_with_identifiers
assert_selector_parses('[foo~=bar]')
assert_selector_parses('[foo^=bar]')
assert_selector_parses('[foo$=bar]')
assert_selector_parses('[foo*=bar]')
assert_selector_parses('[foo|=en]')
end
def test_nth_selectors
assert_selector_parses(':nth-child(-n)')
assert_selector_parses(':nth-child(+n)')
assert_selector_parses(':nth-child(even)')
assert_selector_parses(':nth-child(odd)')
assert_selector_parses(':nth-child(50)')
assert_selector_parses(':nth-child(-50)')
assert_selector_parses(':nth-child(+50)')
assert_selector_parses(':nth-child(2n+3)')
assert_selector_parses(':nth-child(2n-3)')
assert_selector_parses(':nth-child(+2n-3)')
assert_selector_parses(':nth-child(-2n+3)')
assert_selector_parses(':nth-child(-2n+ 3)')
assert_equal(<<CSS, render(<<SCSS))
:nth-child(2n + 3) {
a: b; }
CSS
:nth-child( 2n + 3 ) {
a: b; }
SCSS
end
def test_selectors_containing_selectors
assert_selector_can_contain_selectors(':not(<sel>)')
assert_selector_can_contain_selectors(':current(<sel>)')
assert_selector_can_contain_selectors(':nth-child(n of <sel>)')
assert_selector_can_contain_selectors(':nth-last-child(n of <sel>)')
assert_selector_can_contain_selectors(':-moz-any(<sel>)')
assert_selector_can_contain_selectors(':has(<sel>)')
assert_selector_can_contain_selectors(':has(+ <sel>)')
assert_selector_can_contain_selectors(':host(<sel>)')
assert_selector_can_contain_selectors(':host-context(<sel>)')
end
def assert_selector_can_contain_selectors(sel)
try = lambda {|subsel| assert_selector_parses(sel.gsub('<sel>', subsel))}
try['foo|bar']
try['*|bar']
try['foo|*']
try['*|*']
try['#blah']
try['.blah']
try['[foo]']
try['[foo^="bar"]']
try['[baz|foo~="bar"]']
try[':hover']
try[':nth-child(2n + 3)']
try['h1, h2, h3']
try['#foo, bar, [baz]']
# Not technically allowed for most selectors, but what the heck
try[':not(#foo)']
try['a#foo.bar']
try['#foo .bar > baz']
end
def test_namespaced_selectors
assert_selector_parses('foo|E')
assert_selector_parses('*|E')
assert_selector_parses('foo|*')
assert_selector_parses('*|*')
end
def test_namespaced_attribute_selectors
assert_selector_parses('[foo|bar=baz]')
assert_selector_parses('[*|bar=baz]')
assert_selector_parses('[foo|bar|=baz]')
end
def test_comma_selectors
assert_selector_parses('E, F')
assert_selector_parses('E F, G H')
assert_selector_parses('E > F, G > H')
end
def test_selectors_with_newlines
assert_selector_parses("E,\nF")
assert_selector_parses("E\nF")
assert_selector_parses("E, F\nG, H")
end
def test_expression_fallback_selectors
assert_directive_parses('0%')
assert_directive_parses('60%')
assert_directive_parses('100%')
assert_directive_parses('12px')
assert_directive_parses('"foo"')
end
def test_functional_pseudo_selectors
assert_selector_parses(':foo("bar")')
assert_selector_parses(':foo(bar)')
assert_selector_parses(':foo(12px)')
assert_selector_parses(':foo(+)')
assert_selector_parses(':foo(-)')
assert_selector_parses(':foo(+"bar")')
assert_selector_parses(':foo(-++--baz-"bar"12px)')
end
def test_selector_hacks
assert_selector_parses('> E')
assert_selector_parses('+ E')
assert_selector_parses('~ E')
assert_selector_parses('> > E')
assert_equal <<CSS, render(<<SCSS)
> > E {
a: b; }
CSS
>> E {
a: b; }
SCSS
assert_selector_parses('E*')
assert_selector_parses('E*.foo')
assert_selector_parses('E*:hover')
end
def test_spaceless_combo_selectors
assert_equal "E > F {\n a: b; }\n", render("E>F { a: b;} ")
assert_equal "E ~ F {\n a: b; }\n", render("E~F { a: b;} ")
assert_equal "E + F {\n a: b; }\n", render("E+F { a: b;} ")
end
def test_escapes_in_selectors
assert_selector_parses('.\!foo')
assert_equal ".ffoo {\n a: b; }\n", render('.\66 foo {a: b}')
assert_equal ".\\!foo {\n a: b; }\n", render('.\21 foo {a: b}')
end
def test_subject_selector_deprecation
assert_warning(<<WARNING) {render(".foo .bar! .baz {a: b}")}
DEPRECATION WARNING on line 1, column 1:
The subject selector operator "!" is deprecated and will be removed in a future release.
This operator has been replaced by ":has()" in the CSS spec.
For example: .foo .bar:has(.baz)
WARNING
assert_warning(<<WARNING) {render(".foo .bar! > .baz {a: b}")}
DEPRECATION WARNING on line 1, column 1:
The subject selector operator "!" is deprecated and will be removed in a future release.
This operator has been replaced by ":has()" in the CSS spec.
For example: .foo .bar:has(> .baz)
WARNING
assert_warning(<<WARNING) {render(".foo .bar! {a: b}")}
DEPRECATION WARNING on line 1, column 1:
The subject selector operator "!" is deprecated and will be removed in a future release.
This operator has been replaced by ":has()" in the CSS spec.
For example: .foo .bar
WARNING
end
## Errors
def test_invalid_directives
assert_not_parses("identifier", '@<err> import "foo";')
assert_not_parses("identifier", '@<err>12 "foo";')
end
def test_invalid_classes
assert_not_parses("class name", 'p.<err> foo {a: b}')
assert_not_parses("class name", 'p.<err>1foo {a: b}')
end
def test_invalid_ids
assert_not_parses("id name", 'p#<err> foo {a: b}')
end
def test_no_properties_at_toplevel
assert_not_parses('pseudoclass or pseudoelement', 'a:<err> b;')
end
def test_no_scss_directives
assert_parses('@import "foo.sass";')
assert_parses <<SCSS
@mixin foo {
a: b; }
SCSS
end
def test_no_variables
assert_not_parses("selector or at-rule", "<err>$var = 12;")
assert_not_parses('"}"', "foo { <err>!var = 12; }")
end
def test_no_parent_selectors
assert_not_parses('"{"', "foo <err>&.bar {a: b}")
end
def test_no_selector_interpolation
assert_not_parses('"{"', 'foo <err>#{"bar"}.baz {a: b}')
end
def test_no_prop_name_interpolation
assert_not_parses('":"', 'foo {a<err>#{"bar"}baz: b}')
end
def test_no_prop_val_interpolation
assert_not_parses('"}"', 'foo {a: b <err>#{"bar"} c}')
end
def test_no_string_interpolation
assert_parses <<SCSS
foo {
a: "bang \#{1 + " bar "} bip"; }
SCSS
end
def test_no_sass_script_values
assert_not_parses('"}"', 'foo {a: b <err>* c}')
end
def test_no_nested_rules
assert_not_parses('":"', 'foo {bar <err>{a: b}}')
assert_not_parses('"}"', 'foo {<err>[bar=baz] {a: b}}')
end
def test_no_nested_properties
assert_not_parses('expression (e.g. 1px, bold)', 'foo {bar: <err>{a: b}}')
assert_not_parses('expression (e.g. 1px, bold)', 'foo {bar: bang <err>{a: b}}')
end
def test_no_nested_directives
assert_not_parses('"}"', 'foo {<err>@bar {a: b}}')
end
def test_error_with_windows_newlines
render <<SCSS
foo {bar}\r
baz {a: b}
SCSS
assert(false, "Expected syntax error")
rescue Sass::SyntaxError => e
assert_equal 'Invalid CSS after "foo {bar": expected ":", was "}"', e.message
assert_equal 1, e.sass_line
end
def test_newline_in_property_value
assert_equal(<<CSS, render(<<SCSS))
.foo {
bar: "bazbang"; }
CSS
.foo {
bar: "baz\\
bang";
}
SCSS
end
## Regressions
def test_very_long_comment_doesnt_take_forever
string = 'asdf' * (100000)
assert_equal(<<CSS, render(<<SCSS))
/*
#{string}
*/
CSS
/*
#{string}
*/
SCSS
end
def test_long_unclosed_comment_doesnt_take_forever
assert_raise_message(Sass::SyntaxError,
'Invalid CSS after "/*": expected "/", was "//*************..."') {render(<<SCSS)}
/*
//**************************************************************************
SCSS
end
def test_double_space_string
assert_equal(<<CSS, render(<<SCSS))
.a {
content: " a"; }
CSS
.a {
content: " a";
}
SCSS
end
def test_very_long_number_with_important_doesnt_take_forever
assert_equal(<<CSS, render(<<SCSS))
.foo {
width: 97.916666666666666666666666666667% !important; }
CSS
.foo {
width: 97.916666666666666666666666666667% !important;
}
SCSS
end
def test_selector_without_closing_bracket
assert_not_parses('"]"', "foo[bar <err>{a: b}")
end
def test_closing_line_comment_end_with_compact_output
assert_equal(<<CSS, render(<<SCSS, :style => :compact))
/* foo */
bar { baz: bang; }
CSS
/*
* foo
*/
bar {baz: bang}
SCSS
end
def test_single_line_comment_within_multiline_comment
assert_equal(<<CSS, render(<<SCSS))
body {
/*
//comment here
*/ }
CSS
body {
/*
//comment here
*/
}
SCSS
end
def test_malformed_media
render <<SCSS
@media {
margin: 0;
}
SCSS
assert(false, "Expected syntax error")
rescue Sass::SyntaxError => e
assert_equal 'Invalid CSS after "@media ": expected media query (e.g. print, screen, print and screen), was "{"', e.message
assert_equal 1, e.sass_line
end
private
def assert_selector_parses(selector)
assert_parses <<SCSS
#{selector} {
a: b; }
SCSS
assert_parses <<SCSS
:not(#{selector}) {
a: b; }
SCSS
end
def assert_directive_parses(param)
assert_parses <<SCSS
@unknown #{param} {
a: b; }
SCSS
end
def render(scss, options = {})
tree = Sass::SCSS::CssParser.new(scss, options[:filename], nil).parse
tree.options = Sass::Engine::DEFAULT_OPTIONS.merge(options)
tree.render
end
end
|