This repository has been archived by the owner on May 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
style.js
558 lines (449 loc) · 16.4 KB
/
style.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
/**
* @file check style
* @author xuexb <fe.xiaowu@gmail.com>
*/
var fs = require('fs');
var path = require('path');
var chai = require('chai');
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
var extend = require('extend');
var del = require('del');
var mkdirp = require('mkdirp');
var exec = require('../lib/style');
var expect = chai.expect;
chai.use(sinonChai);
/* eslint-disable fecs-camelcase */
describe('style.js', function () {
var mockBase = path.resolve(__dirname, 'mock');
var styleFilePath = path.resolve(mockBase, 'source/css');
var HTML_TEMPLATE = [
'<!doctype html>',
'<html mip>',
'<head>',
'<title>test</title>',
'</head>',
'<body>',
'<h1>test</h1>',
'</body>',
'</html>'
].join('');
/**
* 写入测试数据
*
* @param {string} content 内容
* @param {string | undefined} filename 文件名
*/
var mock = function (content, filename) {
filename = path.resolve(styleFilePath, filename || Date.now() + Math.round(Math.random() * 1000) + '.css');
fs.writeFileSync(filename, content);
};
beforeEach(function () {
mkdirp.sync(styleFilePath);
});
afterEach(function () {
del.sync(mockBase);
});
describe('mipcss()', function () {
var callback;
var helper;
/**
* 注册 helper
*
* @param {Ojbect | undefined} data 注入数据
* @param {Object | undefined} config 注入配置数据
*/
var register = function (data, config) {
var hexo = {};
hexo.extend = {};
hexo.extend.filter = {};
hexo.extend.helper = {};
hexo.extend.filter.register = function () {};
hexo.extend.helper.register = function () {};
extend(hexo, data);
helper = sinon.spy(hexo.extend.helper, 'register');
exec(hexo, config || {});
callback = helper.getCall(0).args[1];
};
beforeEach(function () {
register();
});
it('register helper', function () {
expect(helper).to.have.been.calledOnce;
expect(helper).to.have.been.calledWith('mipcss');
});
it('callback function', function () {
expect(callback).to.be.a('function');
});
it('not files', function () {
expect(callback()).to.equal('');
expect(callback('', '')).to.equal('');
});
it('one file', function () {
mock('body {height: 0;}', 'one.css');
register({
theme_dir: mockBase
});
expect(callback('one.css')).to.equal('<style mip-custom>body {height: 0;}</style>');
});
it('more file', function () {
mock('1', '1.css');
mock('', '2.css');
mock('3', '3.css');
register({
theme_dir: mockBase
});
expect(callback('1.css', '2.css', '3.css')).to.equal('<style mip-custom>13</style>');
});
it('empty file', function () {
mock('', 'empty.css');
register({
theme_dir: mockBase
});
expect(callback('empty.css')).to.equal('');
});
describe('there are `mip.cssmin` in the configuration file', function () {
it('`mip.cssmin` is a true', function () {
mock('body {width:0px;} ', '1.css');
register({
theme_dir: mockBase
}, {
cssmin: true
});
expect(callback('1.css')).to.have.string('<style mip-custom>').and.have.string('body{width:0}');
});
it('`mip.cssmin` is a false', function () {
mock('body {width:0px;} ', '1.css');
register({
theme_dir: mockBase
}, {
cssmin: false
});
expect(callback('1.css')).to.have.string('<style mip-custom>').and.have.string('body {width:0px;} ');
});
});
});
describe('auto append `<style mip-custom>`', function () {
var filter;
var callback;
/**
* 注册 filter
*
* @param {Ojbect | undefined} data 注入数据
* @param {Object | undefined} config 注入配置数据
*/
var register = function (data, config) {
var hexo = {};
hexo.extend = {};
hexo.extend.filter = {};
hexo.extend.helper = {};
hexo.extend.helper.register = function () {};
hexo.extend.filter.register = function () {};
extend(hexo, data);
filter = sinon.spy(hexo.extend.filter, 'register');
exec(hexo, config || {});
callback = filter.getCall(0).args[1];
};
beforeEach(function () {
register();
});
it('register filter', function () {
expect(filter).to.have.been.calledOnce;
expect(filter).to.have.been.calledWith('after_render:html');
});
it('callback function', function () {
expect(callback).to.be.a('function');
});
it('<head> has <style mip-custom>', function () {
var head = [
'<html mip>',
'<head>',
'<style mip-custom>body {}</style>',
'</head>'
].join('');
expect(callback(head)).to.equal(head);
});
describe('isMip', function () {
it('true', function () {
mock('body {height: 0;}');
register({
theme_dir: mockBase
});
var head = [
'<html mip>',
'<head>',
'</head>'
].join('');
expect(callback(head)).to.equal([
'<html mip>',
'<head>',
'<style mip-custom>body {height: 0;}</style>',
'</head>'
].join(''));
});
it('false', function () {
mock('body {height: 0;}');
register({
theme_dir: mockBase
});
var head = [
'<html>',
'<head>',
'</head>'
].join('');
expect(callback(head)).to.equal([
'<html>',
'<head>',
'</head>'
].join(''));
});
});
it('<body> has <style mip-custom>', function () {
mock('body {height: 0;}');
register({
theme_dir: mockBase
});
var head = [
'<html mip>',
'<head>',
'</head>',
'<body>',
'<style mip-custom>body {}</style>',
'</body>'
].join('');
expect(callback(head)).to.equal([
'<html mip>',
'<head>',
'<style mip-custom>body {height: 0;}body {}</style>',
'</head>',
'<body>',
'</body>'
].join(''));
});
describe('the default file is loaded under `config.theme_dir`', function () {
it('css files should be loaded successfully', function () {
mock('html {width: 0;}');
mock('body {height: 0;}');
register({
theme_dir: mockBase
});
var result = callback(HTML_TEMPLATE);
expect(result).to.be.a('string');
expect(result).to.have.string('<style mip-custom>');
expect(result).to.have.string('html {width: 0;}');
expect(result).to.have.string('body {height: 0;}');
});
it('empty css folders should not be processed', function () {
register({
theme_dir: mockBase
});
var result = callback(HTML_TEMPLATE);
expect(result).to.be.a('string').and.not.have.string('<style mip-custom>');
});
it('should ignore the _ at the beginning of the file', function () {
mock('html {width: 0;}', '_html.css');
mock('body {height: 0;}');
register({
theme_dir: mockBase
});
var result = callback(HTML_TEMPLATE);
expect(result).to.have.string('<style mip-custom>');
expect(result).to.have.string('body {height: 0;}').and.not.have.string('html {width: 0;}');
});
});
describe('there are `mip.css` in the configuration file', function () {
it('`mip.css` is a string', function () {
mock('html {width: 0;}', 'html.css');
register({
theme_dir: mockBase
}, {
css: 'html.css'
});
var result = callback(HTML_TEMPLATE);
expect(result).to.have.string('<style mip-custom>').and.have.string('html {width: 0;}');
});
it('`mip.css` is a empty string', function () {
register({
theme_dir: mockBase
}, {
css: ''
});
var result = callback(HTML_TEMPLATE);
expect(result).to.not.have.string('<style mip-custom>');
});
it('`mip.css` is a array', function () {
mock('html {width: 0;}', 'html.css');
register({
theme_dir: mockBase
}, {
css: ['html.css']
});
var result = callback(HTML_TEMPLATE);
expect(result).to.have.string('<style mip-custom>').and.have.string('html {width: 0;}');
});
it('`mip.css` is a empty array', function () {
register({
theme_dir: mockBase
}, {
css: []
});
var result = callback(HTML_TEMPLATE);
expect(result).to.not.have.string('<style mip-custom>');
});
it('`mip.css` is a non-existent file', function () {
register({
theme_dir: mockBase
}, {
css: 'error.css'
});
expect(function () {
callback(HTML_TEMPLATE);
}).to.throw('error.css');
});
it('`mip.css` is a empty file', function () {
mock('', 'html.css');
register({
theme_dir: mockBase
}, {
css: 'html.css'
});
var result = callback(HTML_TEMPLATE);
expect(result).to.not.have.string('<style mip-custom>');
});
});
describe('there are `mip.cssmin` in the configuration file', function () {
it('`mip.cssmin` is a true', function () {
mock('body {width:0px;} ');
register({
theme_dir: mockBase
}, {
cssmin: true
});
var result = callback(HTML_TEMPLATE);
expect(result).to.have.string('<style mip-custom>').and.have.string('body{width:0}');
});
it('`mip.cssmin` is a false', function () {
mock('body {width:0px;} ');
register({
theme_dir: mockBase
}, {
cssmin: false
});
var result = callback(HTML_TEMPLATE);
expect(result).to.have.string('<style mip-custom>').and.have.string('body {width:0px;} ');
});
});
describe('separate the `<noscript>` tag in `<head>`', function () {
it('inserted before `<noscript>`', function () {
mock('body {width:0px;} ');
register({
theme_dir: mockBase
});
var result = callback([
'<!doctype html>',
'<html mip>',
'<head>',
'<title>test</title>',
'<noscript>test</noscript>',
'</head>',
'<body>',
'<h1>test</h1>',
'</body>',
'</html>'
].join('\n'));
expect(result).to.have.string('<style mip-custom>').and.have.string('</style><noscript>');
});
it('`<noscript>` in body', function () {
mock('body {width:0px;} ');
register({
theme_dir: mockBase
});
var result = callback([
'<!doctype html>',
'<html mip>',
'<head>',
'<title>test</title>',
'</head>',
'<body>',
'<noscript>test</noscript>',
'<h1>test</h1>',
'</body>',
'</html>'
].join('\n'));
expect(result).to.have.string('<style mip-custom>').and.not.have.string('</style><noscript>');
});
});
describe('multiple `<style>` tag', function () {
it('<head> not has <style mip-custom>', function () {
register({
theme_dir: mockBase
});
var head = [
'<html mip>',
'<head>',
'</head>',
'<body>',
'<style>.demo {}</style>',
'<style>.demo2 {}</style>',
'</body>'
].join('');
expect(callback(head)).to.equal([
'<html mip>',
'<head>',
'<style mip-custom>.demo {}.demo2 {}</style>',
'</head>',
'<body>',
'</body>'
].join(''));
});
it('<head> has <style mip-custom>', function () {
register({
theme_dir: mockBase
});
var head = [
'<html mip>',
'<head>',
'<style mip-custom>body {}</style>',
'</head>',
'<body>',
'<style>.demo {}</style>',
'<style>.demo2 {}</style>',
'</body>'
].join('');
expect(callback(head)).to.equal([
'<html mip>',
'<head>',
'<style mip-custom>body {}.demo {}.demo2 {}</style>',
'</head>',
'<body>',
'</body>'
].join(''));
});
it('<head> has <style mip-officialrelease>', function () {
register({
theme_dir: mockBase
});
var head = [
'<html mip>',
'<head>',
'<style mip-officialrelease>body {}</style>',
'</head>',
'<body>',
'<style>.demo {}</style>',
'<style>.demo2 {}</style>',
'</body>'
].join('');
expect(callback(head)).to.equal([
'<html mip>',
'<head>',
'<style mip-officialrelease>body {}</style>',
'<style mip-custom>.demo {}.demo2 {}</style>',
'</head>',
'<body>',
'</body>'
].join(''));
});
});
});
});
/* eslint-enable fecs-camelcase */