|
| 1 | +/* global describe, it */ |
| 2 | +var postcss = require('postcss'); |
| 3 | +var expect = require('chai').expect; |
| 4 | + |
| 5 | +var plugin = require('../'); |
| 6 | + |
| 7 | +var test = function(input, output, opts, done) { |
| 8 | + postcss([plugin(opts)]) |
| 9 | + .process(input) |
| 10 | + .then(function(result) { |
| 11 | + result.root.walkAtRules('font-face', function(rule) { |
| 12 | + rule.walkDecls(function(decl) { |
| 13 | + expect(typeof decl.value).to.eql('string'); |
| 14 | + }); |
| 15 | + }); |
| 16 | + |
| 17 | + expect(result.css).to.eql(output); |
| 18 | + |
| 19 | + expect(result.warnings()).to.be.empty; |
| 20 | + |
| 21 | + done(); |
| 22 | + }) |
| 23 | + .catch(function(error) { |
| 24 | + done(error); |
| 25 | + }); |
| 26 | +}; |
| 27 | + |
| 28 | +describe('postcss-font-magician', function() { |
| 29 | + it('supports bootstrap fonts', function(done) { |
| 30 | + test( |
| 31 | + 'a{font-family:monospace}b{}', |
| 32 | + |
| 33 | + '@font-face{font-family:monospace;font-style:italic;font-weight:400;src:local("Menlo Italic"),local(Menlo-Italic),local("Consolas Italic"),local(Consolas-Italic),local("Courier New Italic")}' + |
| 34 | + '@font-face{font-family:monospace;font-style:italic;font-weight:700;src:local("Menlo Bold Italic"),local(Menlo-BoldItalic),local(Monaco),local("Consolas Bold Italic"),local(Consolas-BoldItalic),local("Courier New Bold Italic")}' + |
| 35 | + '@font-face{font-family:monospace;font-style:normal;font-weight:400;src:local("Menlo Regular"),local(Menlo-Regular),local(Monaco),local(Consolas),local("Courier New")}' + |
| 36 | + '@font-face{font-family:monospace;font-style:normal;font-weight:700;src:local("Menlo Bold"),local(Menlo-Bold),local(Monaco),local("Consolas Bold"),local(Consolas-Bold),local("Courier New Bold")}' + |
| 37 | + 'a{font-family:monospace}b{}', |
| 38 | + |
| 39 | + {}, |
| 40 | + done |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + it('adds bootstrap fonts once', function(done) { |
| 45 | + test( |
| 46 | + 'a{font-family:monospace}b{font-family:monospace}', |
| 47 | + |
| 48 | + '@font-face{font-family:monospace;font-style:italic;font-weight:400;src:local("Menlo Italic"),local(Menlo-Italic),local("Consolas Italic"),local(Consolas-Italic),local("Courier New Italic")}' + |
| 49 | + '@font-face{font-family:monospace;font-style:italic;font-weight:700;src:local("Menlo Bold Italic"),local(Menlo-BoldItalic),local(Monaco),local("Consolas Bold Italic"),local(Consolas-BoldItalic),local("Courier New Bold Italic")}' + |
| 50 | + '@font-face{font-family:monospace;font-style:normal;font-weight:400;src:local("Menlo Regular"),local(Menlo-Regular),local(Monaco),local(Consolas),local("Courier New")}' + |
| 51 | + '@font-face{font-family:monospace;font-style:normal;font-weight:700;src:local("Menlo Bold"),local(Menlo-Bold),local(Monaco),local("Consolas Bold"),local(Consolas-Bold),local("Courier New Bold")}' + |
| 52 | + 'a{font-family:monospace}b{font-family:monospace}', |
| 53 | + |
| 54 | + {}, |
| 55 | + done |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it('supports google fonts', function(done) { |
| 60 | + test( |
| 61 | + 'a{font-family:"Alice"}b{}', |
| 62 | + |
| 63 | + '@font-face{font-family:Alice;font-style:normal;font-weight:400;src:local("Alice Regular"),local(Alice-Regular),url(//fonts.gstatic.com/s/alice/v9/OpNCnoEEmtHa6GcOrgo.eot?#) format("eot"),url(//fonts.gstatic.com/s/alice/v9/OpNCnoEEmtHa6GcOrg4.woff2) format("woff2"),url(//fonts.gstatic.com/s/alice/v9/OpNCnoEEmtHa6GcOrgg.woff) format("woff")}a{font-family:"Alice"}b{}', |
| 64 | + {}, |
| 65 | + done |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it('supports font-display option', function(done) { |
| 70 | + test( |
| 71 | + 'a{font-family:"Alice"}b{}', |
| 72 | + |
| 73 | + '@font-face{font-family:Alice;font-style:normal;font-weight:400;src:local("Alice Regular"),local(Alice-Regular),url(//fonts.gstatic.com/s/alice/v9/OpNCnoEEmtHa6GcOrgo.eot?#) format("eot"),url(//fonts.gstatic.com/s/alice/v9/OpNCnoEEmtHa6GcOrg4.woff2) format("woff2"),url(//fonts.gstatic.com/s/alice/v9/OpNCnoEEmtHa6GcOrgg.woff) format("woff");font-display:swap}a{font-family:"Alice"}b{}', |
| 74 | + { display: 'swap' }, |
| 75 | + done |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('Custom google fonts options:', function() { |
| 80 | + it('supports custom google fonts', function(done) { |
| 81 | + test( |
| 82 | + 'a{font-family:"Open Sans"}b{}', |
| 83 | + |
| 84 | + '@font-face{font-family:"Open Sans";font-style:normal;font-weight:300;src:url(//fonts.gstatic.com/s/opensans/v15/mem5YaGs126MiZpBA-UN_r8OUuhv.woff) format("woff")}@font-face{font-family:"Open Sans";font-style:italic;font-weight:400;src:url(//fonts.gstatic.com/s/opensans/v15/mem6YaGs126MiZpBA-UFUK0Zdck.eot?#) format("eot"),url(//fonts.gstatic.com/s/opensans/v15/mem6YaGs126MiZpBA-UFUK0Zdcs.woff) format("woff")}a{font-family:"Open Sans"}b{}', |
| 85 | + { |
| 86 | + variants: { |
| 87 | + 'Open Sans': { |
| 88 | + '300': ['woff'], |
| 89 | + '400 italic': ['eot woff'] |
| 90 | + } |
| 91 | + } |
| 92 | + }, |
| 93 | + done |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it('supports custom google fonts (default formats)', function(done) { |
| 98 | + test( |
| 99 | + 'a{font-family:"Open Sans"}b{}', |
| 100 | + |
| 101 | + '@font-face{font-family:"Open Sans";font-style:normal;font-weight:300;src:local("Open Sans Light"),local(OpenSans-Light),url(//fonts.gstatic.com/s/opensans/v15/mem5YaGs126MiZpBA-UN_r8OUuht.eot?#) format("eot"),url(//fonts.gstatic.com/s/opensans/v15/mem5YaGs126MiZpBA-UN_r8OUuhp.woff2) format("woff2"),url(//fonts.gstatic.com/s/opensans/v15/mem5YaGs126MiZpBA-UN_r8OUuhv.woff) format("woff")}a{font-family:"Open Sans"}b{}', |
| 102 | + |
| 103 | + { |
| 104 | + variants: { |
| 105 | + 'Open Sans': { |
| 106 | + '300': [] |
| 107 | + } |
| 108 | + } |
| 109 | + }, |
| 110 | + done |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it('supports custom unicode-range', function(done) { |
| 115 | + test( |
| 116 | + 'a{font-family:"Open Sans"}b{}', |
| 117 | + |
| 118 | + '@font-face{font-family:"Open Sans";font-style:normal;font-weight:300;src:url(//fonts.gstatic.com/s/opensans/v15/mem5YaGs126MiZpBA-UN_r8OUuhv.woff) format("woff");unicode-range:U+0100-024F, U+1E00-1EFF, U+20A0-20AB, U+20AD-20CF, U+2C60-2C7F, U+A720-A7FF}@font-face{font-family:"Open Sans";font-style:italic;font-weight:400;src:url(//fonts.gstatic.com/s/opensans/v15/mem6YaGs126MiZpBA-UFUK0Zdck.eot?#) format("eot"),url(//fonts.gstatic.com/s/opensans/v15/mem6YaGs126MiZpBA-UFUK0Zdcs.woff) format("woff")}a{font-family:"Open Sans"}b{}', |
| 119 | + { |
| 120 | + variants: { |
| 121 | + 'Open Sans': { |
| 122 | + '300': [ |
| 123 | + 'woff', |
| 124 | + 'U+0100-024F, U+1E00-1EFF, U+20A0-20AB, U+20AD-20CF, U+2C60-2C7F, U+A720-A7FF' |
| 125 | + ], |
| 126 | + '400 italic': ['eot woff'] |
| 127 | + } |
| 128 | + } |
| 129 | + }, |
| 130 | + done |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + it('supports custom font-stretch: omit "normal" style', function(done) { |
| 135 | + test( |
| 136 | + 'a{font-family:"Open Sans"}b{}', |
| 137 | + |
| 138 | + '@font-face{font-family:"Open Sans";font-style:normal;font-weight:300;src:url(//fonts.gstatic.com/s/opensans/v15/mem5YaGs126MiZpBA-UN_r8OUuhv.woff) format("woff");font-stretch:condensed}@font-face{font-family:"Open Sans";font-style:italic;font-weight:400;src:url(//fonts.gstatic.com/s/opensans/v15/mem6YaGs126MiZpBA-UFUK0Zdck.eot?#) format("eot"),url(//fonts.gstatic.com/s/opensans/v15/mem6YaGs126MiZpBA-UFUK0Zdcs.woff) format("woff")}a{font-family:"Open Sans"}b{}', |
| 139 | + { |
| 140 | + variants: { |
| 141 | + 'Open Sans': { |
| 142 | + '300 condensed': ['woff'], |
| 143 | + '400 italic': ['eot woff'] |
| 144 | + } |
| 145 | + } |
| 146 | + }, |
| 147 | + done |
| 148 | + ); |
| 149 | + }); |
| 150 | + |
| 151 | + it('supports custom font-stretch: with "normal" style', function(done) { |
| 152 | + test( |
| 153 | + 'a{font-family:"Open Sans"}b{}', |
| 154 | + |
| 155 | + '@font-face{font-family:"Open Sans";font-style:normal;font-weight:300;src:url(//fonts.gstatic.com/s/opensans/v15/mem5YaGs126MiZpBA-UN_r8OUuhv.woff) format("woff");font-stretch:condensed}@font-face{font-family:"Open Sans";font-style:italic;font-weight:400;src:url(//fonts.gstatic.com/s/opensans/v15/mem6YaGs126MiZpBA-UFUK0Zdck.eot?#) format("eot"),url(//fonts.gstatic.com/s/opensans/v15/mem6YaGs126MiZpBA-UFUK0Zdcs.woff) format("woff")}a{font-family:"Open Sans"}b{}', |
| 156 | + { |
| 157 | + variants: { |
| 158 | + 'Open Sans': { |
| 159 | + '300 normal condensed': ['woff'], |
| 160 | + '400 italic': ['eot woff'] |
| 161 | + } |
| 162 | + } |
| 163 | + }, |
| 164 | + done |
| 165 | + ); |
| 166 | + }); |
| 167 | + |
| 168 | + it('supports custom font-stretch: with "italic" style', function(done) { |
| 169 | + test( |
| 170 | + 'a{font-family:"Open Sans"}b{}', |
| 171 | + |
| 172 | + '@font-face{font-family:"Open Sans";font-style:normal;font-weight:300;src:url(//fonts.gstatic.com/s/opensans/v15/mem5YaGs126MiZpBA-UN_r8OUuhv.woff) format("woff")}@font-face{font-family:"Open Sans";font-style:italic;font-weight:400;src:url(//fonts.gstatic.com/s/opensans/v15/mem6YaGs126MiZpBA-UFUK0Zdck.eot?#) format("eot"),url(//fonts.gstatic.com/s/opensans/v15/mem6YaGs126MiZpBA-UFUK0Zdcs.woff) format("woff");font-stretch:ultra-condensed}a{font-family:"Open Sans"}b{}', |
| 173 | + { |
| 174 | + variants: { |
| 175 | + 'Open Sans': { |
| 176 | + '300': ['woff'], |
| 177 | + '400 italic ultra-condensed': ['eot woff'] |
| 178 | + } |
| 179 | + } |
| 180 | + }, |
| 181 | + done |
| 182 | + ); |
| 183 | + }); |
| 184 | + }); |
| 185 | + |
| 186 | + it('adds google fonts once', function(done) { |
| 187 | + test( |
| 188 | + 'a{font-family:"Alice"}b{font-family:"Alice"}', |
| 189 | + |
| 190 | + '@font-face{font-family:Alice;font-style:normal;font-weight:400;src:local("Alice Regular"),local(Alice-Regular),url(//fonts.gstatic.com/s/alice/v9/OpNCnoEEmtHa6GcOrgo.eot?#) format("eot"),url(//fonts.gstatic.com/s/alice/v9/OpNCnoEEmtHa6GcOrg4.woff2) format("woff2"),url(//fonts.gstatic.com/s/alice/v9/OpNCnoEEmtHa6GcOrgg.woff) format("woff")}a{font-family:"Alice"}b{font-family:"Alice"}', |
| 191 | + |
| 192 | + {}, |
| 193 | + done |
| 194 | + ); |
| 195 | + }); |
| 196 | + |
| 197 | + it('supports hosted fonts', function(done) { |
| 198 | + test( |
| 199 | + 'a{font-family:"Source Sans Pro"}b{}', |
| 200 | + |
| 201 | + '@font-face{font-family:"Source Sans Pro";font-style:normal;font-weight:400;src:local(SourceSansPro-Regular),local(SourceSansPro-Regular),url(./test/fonts/pathFont.woff) format("woff")}' + |
| 202 | + 'a{font-family:"Source Sans Pro"}b{}', |
| 203 | + |
| 204 | + { |
| 205 | + hosted: ['./test/fonts'] |
| 206 | + }, |
| 207 | + done |
| 208 | + ); |
| 209 | + }); |
| 210 | + |
| 211 | + it('supports hosted fonts (string fallback)', function(done) { |
| 212 | + test( |
| 213 | + 'a{font-family:"Source Sans Pro"}b{}', |
| 214 | + |
| 215 | + '@font-face{font-family:"Source Sans Pro";font-style:normal;font-weight:400;src:local(SourceSansPro-Regular),local(SourceSansPro-Regular),url(./test/fonts/pathFont.woff) format("woff")}' + |
| 216 | + 'a{font-family:"Source Sans Pro"}b{}', |
| 217 | + |
| 218 | + { |
| 219 | + hosted: './test/fonts' |
| 220 | + }, |
| 221 | + done |
| 222 | + ); |
| 223 | + }); |
| 224 | + |
| 225 | + it('supports hosted fonts with custom font path', function(done) { |
| 226 | + test( |
| 227 | + 'a{font-family:"Source Sans Pro"}b{}', |
| 228 | + |
| 229 | + '@font-face{font-family:"Source Sans Pro";font-style:normal;font-weight:400;src:local(SourceSansPro-Regular),local(SourceSansPro-Regular),url(/some/custom/path/pathFont.woff) format("woff")}' + |
| 230 | + 'a{font-family:"Source Sans Pro"}b{}', |
| 231 | + |
| 232 | + { |
| 233 | + hosted: ['./test/fonts', '/some/custom/path'] |
| 234 | + }, |
| 235 | + done |
| 236 | + ); |
| 237 | + }); |
| 238 | + |
| 239 | + it('does not overwrite existing @font-face rules', function(done) { |
| 240 | + test( |
| 241 | + '@font-face{font-family:Alice}a{font-family:"Alice"}b{}', |
| 242 | + |
| 243 | + '@font-face{font-family:Alice}a{font-family:"Alice"}b{}', |
| 244 | + |
| 245 | + {}, |
| 246 | + done |
| 247 | + ); |
| 248 | + }); |
| 249 | + |
| 250 | + it('support foundry exclusion', function(done) { |
| 251 | + test( |
| 252 | + 'a{font-family:"Alice"}b{}', |
| 253 | + |
| 254 | + 'a{font-family:"Alice"}b{}', |
| 255 | + |
| 256 | + { |
| 257 | + foundries: 'hosted' |
| 258 | + }, |
| 259 | + done |
| 260 | + ); |
| 261 | + }); |
| 262 | + |
| 263 | + it('support font aliasing', function(done) { |
| 264 | + test( |
| 265 | + 'a{font-family:body}b{}', |
| 266 | + |
| 267 | + '@font-face{font-family:body;font-style:normal;font-weight:400;src:url(//fonts.gstatic.com/s/opensans/v15/mem8YaGs126MiZpBA-UFVZ0d.woff) format("woff")}a{font-family:body}b{}', |
| 268 | + |
| 269 | + { |
| 270 | + aliases: { |
| 271 | + body: 'Open Sans' |
| 272 | + }, |
| 273 | + variants: { |
| 274 | + body: { |
| 275 | + '400': ['woff'] |
| 276 | + } |
| 277 | + } |
| 278 | + }, |
| 279 | + done |
| 280 | + ); |
| 281 | + }); |
| 282 | + |
| 283 | + it('support custom fonts', function(done) { |
| 284 | + test( |
| 285 | + 'a{font-family:body}b{}', |
| 286 | + |
| 287 | + '@font-face{font-family:body;font-style:normal;font-weight:400;src:url(path/to/my-body-font.woff2) format("woff2")}' + |
| 288 | + 'a{font-family:body}b{}', |
| 289 | + |
| 290 | + { |
| 291 | + custom: { |
| 292 | + body: { |
| 293 | + variants: { |
| 294 | + normal: { |
| 295 | + 400: { |
| 296 | + url: { |
| 297 | + woff2: 'path/to/my-body-font.woff2' |
| 298 | + } |
| 299 | + } |
| 300 | + } |
| 301 | + } |
| 302 | + } |
| 303 | + } |
| 304 | + }, |
| 305 | + done |
| 306 | + ); |
| 307 | + }); |
| 308 | + |
| 309 | + it('support async font loading', function(done) { |
| 310 | + test( |
| 311 | + 'a{font-family:Alice}b{}', |
| 312 | + |
| 313 | + 'a{font-family:Alice}b{}', |
| 314 | + |
| 315 | + { |
| 316 | + async: './test/fontface.js.result' |
| 317 | + }, |
| 318 | + done |
| 319 | + ); |
| 320 | + }); |
| 321 | + |
| 322 | + it('supports custom configuration protocol (http)', function(done) { |
| 323 | + test( |
| 324 | + 'a{font-family:"Alice"}b{}', |
| 325 | + |
| 326 | + '@font-face{font-family:Alice;font-style:normal;font-weight:400;src:local("Alice Regular"),local(Alice-Regular),url(http://fonts.gstatic.com/s/alice/v9/OpNCnoEEmtHa6GcOrgo.eot?#) format("eot"),url(http://fonts.gstatic.com/s/alice/v9/OpNCnoEEmtHa6GcOrg4.woff2) format("woff2"),url(http://fonts.gstatic.com/s/alice/v9/OpNCnoEEmtHa6GcOrgg.woff) format("woff")}a{font-family:"Alice"}b{}', |
| 327 | + |
| 328 | + { |
| 329 | + protocol: 'http:' |
| 330 | + }, |
| 331 | + done |
| 332 | + ); |
| 333 | + }); |
| 334 | + |
| 335 | + it('supports custom configuration protocol (https)', function(done) { |
| 336 | + test( |
| 337 | + 'a{font-family:"Alice"}b{}', |
| 338 | + |
| 339 | + '@font-face{font-family:Alice;font-style:normal;font-weight:400;src:local("Alice Regular"),local(Alice-Regular),url(https://fonts.gstatic.com/s/alice/v9/OpNCnoEEmtHa6GcOrgo.eot?#) format("eot"),url(https://fonts.gstatic.com/s/alice/v9/OpNCnoEEmtHa6GcOrg4.woff2) format("woff2"),url(https://fonts.gstatic.com/s/alice/v9/OpNCnoEEmtHa6GcOrgg.woff) format("woff")}a{font-family:"Alice"}b{}', |
| 340 | + |
| 341 | + { |
| 342 | + protocol: 'https:' |
| 343 | + }, |
| 344 | + done |
| 345 | + ); |
| 346 | + }); |
| 347 | +}); |
0 commit comments