~ubuntu-branches/ubuntu/utopic/moodle/utopic-proposed

« back to all changes in this revision

Viewing changes to lib/tests/csslib_test.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (1.1.16)
  • Revision ID: package-import@ubuntu.com-20140512161038-2039l24hvvlan3hs
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1082
1082
    public function test_css_chunking() {
1083
1083
        // Test with an even number of styles.
1084
1084
        $css = 'a{}b{}c{}d{}e{}f{}';
1085
 
        $chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2, 0);
 
1085
        $chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2);
1086
1086
        $this->assertInternalType('array', $chunks);
1087
1087
        $this->assertCount(3, $chunks);
1088
1088
        $this->assertArrayHasKey(0, $chunks);
1094
1094
 
1095
1095
        // Test with an odd number of styles.
1096
1096
        $css = 'a{}b{}c{}d{}e{}';
1097
 
        $chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2, 0);
 
1097
        $chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2);
1098
1098
        $this->assertInternalType('array', $chunks);
1099
1099
        $this->assertCount(3, $chunks);
1100
1100
        $this->assertArrayHasKey(0, $chunks);
1104
1104
        $this->assertSame('c{}d{}', $chunks[1]);
1105
1105
        $this->assertSame("@import url(styles.php?type=test&chunk=1);\n@import url(styles.php?type=test&chunk=2);\ne{}", $chunks[2]);
1106
1106
 
1107
 
        // Test buffering. Set a buffer that will reduce the effective sheet size back to two.
1108
 
        $css = 'a{}b{}c{}d{}e{}f{}';
1109
 
        $chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 6, 4);
1110
 
        $this->assertInternalType('array', $chunks);
1111
 
        $this->assertCount(3, $chunks);
1112
 
        $this->assertArrayHasKey(0, $chunks);
1113
 
        $this->assertArrayHasKey(1, $chunks);
1114
 
        $this->assertArrayHasKey(2, $chunks);
1115
 
        $this->assertSame('a{}b{}', $chunks[0]);
1116
 
        $this->assertSame('c{}d{}', $chunks[1]);
1117
 
        $this->assertSame("@import url(styles.php?type=test&chunk=1);\n@import url(styles.php?type=test&chunk=2);\ne{}f{}", $chunks[2]);
1118
 
 
1119
1107
        // Test well placed commas.
1120
1108
        $css = 'a,b{}c,d{}e,f{}';
1121
 
        $chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2, 0);
 
1109
        $chunks = css_chunk_by_selector_count($css, 'styles.php?type=test', 2);
1122
1110
        $this->assertInternalType('array', $chunks);
1123
1111
        $this->assertCount(3, $chunks);
1124
1112
        $this->assertArrayHasKey(0, $chunks);
1130
1118
 
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]);
1142
1132
 
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.');
1154
1141
 
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.');
1166
1156
 
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.');
 
1165
 
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.
 
1172
 
 
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]);
 
1178
 
 
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]);
 
1185
 
 
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.');
 
1192
 
 
1193
        // Complex test.
 
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]);
 
1203
 
 
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.');
 
1211
 
 
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]);
 
1218
 
 
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]);
 
1225
 
 
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]);
1183
1232
    }
1184
1233
 
1185
1234
    /**