1131
1119
// Test unfortunately placed commas.
1132
1120
$css = 'a{}b,c{color:red;}d{}e{}f{}';
1133
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2, 0);
1121
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2);
1134
1122
$this->assertInternalType('array', $chunks);
1135
$this->assertCount(3, $chunks);
1123
$this->assertCount(4, $chunks);
1136
1124
$this->assertArrayHasKey(0, $chunks);
1137
1125
$this->assertArrayHasKey(1, $chunks);
1138
1126
$this->assertArrayHasKey(2, $chunks);
1139
$this->assertSame('a{}b{color:red;}', $chunks[0]);
1140
$this->assertSame('c{color:red;}d{}', $chunks[1]);
1141
$this->assertSame("@import url(styles.php?type=test&chunk=1);\n@import url(styles.php?type=test&chunk=2);\ne{}f{}", $chunks[2]);
1127
$this->assertArrayHasKey(3, $chunks);
1128
$this->assertSame('a{}', $chunks[0]);
1129
$this->assertSame('b,c{color:red;}', $chunks[1]);
1130
$this->assertSame('d{}e{}', $chunks[2]);
1131
$this->assertSame("@import url(styles.php?type=test&chunk=1);\n@import url(styles.php?type=test&chunk=2);\n@import url(styles.php?type=test&chunk=3);\nf{}", $chunks[3]);
1143
1133
// Test unfortunate CSS.
1144
1134
$css = 'a,b,c,d,e,f{color:red;}';
1145
1135
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2, 0);
1146
1136
$this->assertInternalType('array', $chunks);
1147
$this->assertCount(3, $chunks);
1137
$this->assertCount(1, $chunks);
1148
1138
$this->assertArrayHasKey(0, $chunks);
1149
$this->assertArrayHasKey(1, $chunks);
1150
$this->assertArrayHasKey(2, $chunks);
1151
$this->assertSame('a,b{color:red;}', $chunks[0]);
1152
$this->assertSame('c,d{color:red;}', $chunks[1]);
1153
$this->assertSame("@import url(styles.php?type=test&chunk=1);\n@import url(styles.php?type=test&chunk=2);\ne,f{color:red;}", $chunks[2]);
1139
$this->assertSame('a,b,c,d,e,f{color:red;}', $chunks[0]);
1140
$this->assertDebuggingCalled('Could not find a safe place to split at offset(s): 6. Those were ignored.');
1155
1142
// Test to make sure invalid CSS isn't totally ruined.
1156
1143
$css = 'a{},,,e{},';
1157
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2, 0);
1144
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2);
1158
1145
// Believe it or not we want to care what comes out here as this will be parsed correctly
1159
1146
// by a browser.
1160
1147
$this->assertInternalType('array', $chunks);
1161
$this->assertCount(2, $chunks);
1148
$this->assertCount(3, $chunks);
1162
1149
$this->assertArrayHasKey(0, $chunks);
1163
1150
$this->assertArrayHasKey(1, $chunks);
1164
$this->assertSame('a{},{}', $chunks[0]);
1165
$this->assertSame("@import url(styles.php?type=test&chunk=1);\n,e{}/** Error chunking CSS **/", $chunks[1]);
1151
$this->assertArrayHasKey(2, $chunks);
1152
$this->assertSame('a{}', $chunks[0]);
1153
$this->assertSame(',,,e{}', $chunks[1]);
1154
$this->assertSame("@import url(styles.php?type=test&chunk=1);\n@import url(styles.php?type=test&chunk=2);\n,", $chunks[2]);
1155
$this->assertDebuggingCalled('Could not find a safe place to split at offset(s): 6. Those were ignored.');
1167
1157
// Test utter crap CSS to make sure we don't loop to our deaths.
1168
1158
$css = 'a,b,c,d,e,f';
1169
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2, 0);
1159
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2);
1170
1160
$this->assertInternalType('array', $chunks);
1171
$this->assertCount(3, $chunks);
1161
$this->assertCount(1, $chunks);
1172
1162
$this->assertArrayHasKey(0, $chunks);
1173
$this->assertArrayHasKey(1, $chunks);
1174
$this->assertArrayHasKey(2, $chunks);
1175
$this->assertSame('a,b/** Error chunking CSS **/', $chunks[0]);
1176
$this->assertSame('c,d/** Error chunking CSS **/', $chunks[1]);
1177
$this->assertSame("@import url(styles.php?type=test&chunk=1);\n@import url(styles.php?type=test&chunk=2);\ne,f", $chunks[2]);
1163
$this->assertSame($css, $chunks[0]);
1164
$this->assertDebuggingCalled('Could not find a safe place to split at offset(s): 6. Those were ignored.');
1178
1166
// Test another death situation to make sure we're invincible.
1179
1167
$css = 'a,,,,,e';
1180
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2, 0);
1168
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2);
1181
1169
$this->assertInternalType('array', $chunks);
1170
$this->assertDebuggingCalled('Could not find a safe place to split at offset(s): 4. Those were ignored.');
1182
1171
// I don't care what the outcome is, I just want to make sure it doesn't die.
1173
// Test media queries.
1174
$css = '@media (min-width: 980px) { .a,.b{} }';
1175
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2);
1176
$this->assertCount(1, $chunks);
1177
$this->assertSame('@media (min-width: 980px) { .a,.b{} }', $chunks[0]);
1179
// Test special rules.
1180
$css = 'a,b{ background-image: linear-gradient(to bottom, #ffffff, #cccccc);}d,e{}';
1181
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2);
1182
$this->assertCount(2, $chunks);
1183
$this->assertSame('a,b{ background-image: linear-gradient(to bottom, #ffffff, #cccccc);}', $chunks[0]);
1184
$this->assertSame("@import url(styles.php?type=test&chunk=1);\nd,e{}", $chunks[1]);
1186
// Test media queries with too many selectors.
1187
$css = '@media (min-width: 980px) { a,b,c,d{} }';
1188
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2);
1189
$this->assertCount(1, $chunks);
1190
$this->assertSame('@media (min-width: 980px) { a,b,c,d{} }', $chunks[0]);
1191
$this->assertDebuggingCalled('Could not find a safe place to split at offset(s): 34. Those were ignored.');
1194
$css = '@media (a) {b{}} c{} d,e{} f,g,h{} i,j{x:a,b,c} k,l{} @media(x){l,m{ y: a,b,c}} n{}';
1195
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 3);
1196
$this->assertCount(6, $chunks);
1197
$this->assertSame('@media (a) {b{}} c{}', $chunks[0]);
1198
$this->assertSame(' d,e{}', $chunks[1]);
1199
$this->assertSame(' f,g,h{}', $chunks[2]);
1200
$this->assertSame(' i,j{x:a,b,c}', $chunks[3]);
1201
$this->assertSame(' k,l{}', $chunks[4]);
1202
$this->assertSame("@import url(styles.php?type=test&chunk=1);\n@import url(styles.php?type=test&chunk=2);\n@import url(styles.php?type=test&chunk=3);\n@import url(styles.php?type=test&chunk=4);\n@import url(styles.php?type=test&chunk=5);\n @media(x){l,m{ y: a,b,c}} n{}", $chunks[5]);
1204
// Multiple offset errors.
1205
$css = 'a,b,c{} d,e,f{}';
1206
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2);
1207
$this->assertCount(2, $chunks);
1208
$this->assertSame('a,b,c{}', $chunks[0]);
1209
$this->assertSame("@import url(styles.php?type=test&chunk=1);\n d,e,f{}", $chunks[1]);
1210
$this->assertDebuggingCalled('Could not find a safe place to split at offset(s): 6, 14. Those were ignored.');
1212
// Test the split according to IE.
1213
$css = str_repeat('a{}', 4100);
1214
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test');
1215
$this->assertCount(2, $chunks);
1216
$this->assertSame(str_repeat('a{}', 4095), $chunks[0]);
1217
$this->assertSame("@import url(styles.php?type=test&chunk=1);\n" . str_repeat('a{}', 5), $chunks[1]);
1219
// Test strip out comments.
1220
$css = ".a {/** a\nb\nc */} /** a\nb\nc */ .b{} /** .c,.d{} */ e{}";
1221
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2);
1222
$this->assertCount(2, $chunks);
1223
$this->assertSame('.a {} .b{}', $chunks[0]);
1224
$this->assertSame("@import url(styles.php?type=test&chunk=1);\n e{}", $chunks[1]);
1226
// Test something with unicode characters.
1227
$css = 'a,b{} nav a:hover:after { content: "↓"; } b{ color:test;}';
1228
$chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2, true);
1229
$this->assertCount(2, $chunks);
1230
$this->assertSame('a,b{}', $chunks[0]);
1231
$this->assertSame("@import url(styles.php?type=test&chunk=1);\n nav a:hover:after { content: \"↓\"; } b{ color:test;}", $chunks[1]);