-
Notifications
You must be signed in to change notification settings - Fork 324
/
compactable.js
496 lines (489 loc) · 11.8 KB
/
compactable.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
// Contains the interpretation of CSS properties, as used by the property optimizer
var breakUp = require('./break-up');
var canOverride = require('./can-override');
var restore = require('./restore');
// Properties to process
// Extend this object in order to add support for more properties in the optimizer.
//
// Each key in this object represents a CSS property and should be an object.
// Such an object contains properties that describe how the represented CSS property should be handled.
// Possible options:
//
// * components: array (Only specify for shorthand properties.)
// Contains the names of the granular properties this shorthand compacts.
//
// * canOverride: function (Default is canOverride.sameValue - meaning that they'll only be merged if they have the same value.)
// Returns whether two tokens of this property can be merged with each other.
// This property has no meaning for shorthands.
//
// * defaultValue: string
// Specifies the default value of the property according to the CSS standard.
// For shorthand, this is used when every component is set to its default value, therefore it should be the shortest possible default value of all the components.
//
// * shortestValue: string
// Specifies the shortest possible value the property can possibly have.
// (Falls back to defaultValue if unspecified.)
//
// * breakUp: function (Only specify for shorthand properties.)
// Breaks the shorthand up to its components.
//
// * restore: function (Only specify for shorthand properties.)
// Puts the shorthand together from its components.
//
var compactable = {
'color': {
canOverride: canOverride.color,
defaultValue: 'transparent',
shortestValue: 'red'
},
'background': {
components: [
'background-image',
'background-position',
'background-size',
'background-repeat',
'background-attachment',
'background-origin',
'background-clip',
'background-color'
],
breakUp: breakUp.multiplex(breakUp.background),
defaultValue: '0 0',
restore: restore.multiplex(restore.background),
shortestValue: '0',
shorthand: true
},
'background-clip': {
canOverride: canOverride.always,
componentOf: [
'background'
],
defaultValue: 'border-box',
shortestValue: 'border-box'
},
'background-color': {
canOverride: canOverride.color,
componentOf: [
'background'
],
defaultValue: 'transparent',
multiplexLastOnly: true,
nonMergeableValue: 'none',
shortestValue: 'red'
},
'background-image': {
canOverride: canOverride.backgroundImage,
componentOf: [
'background'
],
defaultValue: 'none'
},
'background-origin': {
canOverride: canOverride.always,
componentOf: [
'background'
],
defaultValue: 'padding-box',
shortestValue: 'border-box'
},
'background-repeat': {
canOverride: canOverride.always,
componentOf: [
'background'
],
defaultValue: ['repeat'],
doubleValues: true
},
'background-position': {
canOverride: canOverride.alwaysButIntoFunction,
componentOf: [
'background'
],
defaultValue: ['0', '0'],
doubleValues: true,
shortestValue: '0'
},
'background-size': {
canOverride: canOverride.alwaysButIntoFunction,
componentOf: [
'background'
],
defaultValue: ['auto'],
doubleValues: true,
shortestValue: '0 0'
},
'background-attachment': {
canOverride: canOverride.always,
componentOf: [
'background'
],
defaultValue: 'scroll'
},
'border': {
breakUp: breakUp.border,
canOverride: canOverride.border,
components: [
'border-width',
'border-style',
'border-color'
],
defaultValue: 'none',
overridesShorthands: [
'border-bottom',
'border-left',
'border-right',
'border-top'
],
restore: restore.withoutDefaults,
shorthand: true,
shorthandComponents: true
},
'border-bottom': {
breakUp: breakUp.border,
canOverride: canOverride.border,
components: [
'border-bottom-width',
'border-bottom-style',
'border-bottom-color'
],
defaultValue: 'none',
restore: restore.withoutDefaults,
shorthand: true
},
'border-bottom-color': {
canOverride: canOverride.color,
componentOf: [
'border-bottom',
'border-color'
],
defaultValue: 'none'
},
'border-bottom-style': {
canOverride: canOverride.always,
componentOf: [
'border-bottom',
'border-style'
],
defaultValue: 'none'
},
'border-bottom-width': {
canOverride: canOverride.unit,
componentOf: [
'border-bottom',
'border-width'
],
defaultValue: 'medium',
shortestValue: '0'
},
'border-color': {
breakUp: breakUp.fourValues,
canOverride: canOverride.color,
componentOf: ['border'],
components: [
'border-top-color',
'border-right-color',
'border-bottom-color',
'border-left-color'
],
defaultValue: 'none',
restore: restore.fourValues,
shortestValue: 'red',
shorthand: true
},
'border-left': {
breakUp: breakUp.border,
canOverride: canOverride.border,
components: [
'border-left-width',
'border-left-style',
'border-left-color'
],
defaultValue: 'none',
restore: restore.withoutDefaults,
shorthand: true
},
'border-left-color': {
canOverride: canOverride.color,
componentOf: [
'border-color',
'border-left'
],
defaultValue: 'none'
},
'border-left-style': {
canOverride: canOverride.always,
componentOf: [
'border-left',
'border-style'
],
defaultValue: 'none'
},
'border-left-width': {
canOverride: canOverride.unit,
componentOf: [
'border-left',
'border-width'
],
defaultValue: 'medium',
shortestValue: '0'
},
'border-right': {
breakUp: breakUp.border,
canOverride: canOverride.border,
components: [
'border-right-width',
'border-right-style',
'border-right-color'
],
defaultValue: 'none',
restore: restore.withoutDefaults,
shorthand: true
},
'border-right-color': {
canOverride: canOverride.color,
componentOf: [
'border-color',
'border-right'
],
defaultValue: 'none'
},
'border-right-style': {
canOverride: canOverride.always,
componentOf: [
'border-right',
'border-style'
],
defaultValue: 'none'
},
'border-right-width': {
canOverride: canOverride.unit,
componentOf: [
'border-right',
'border-width'
],
defaultValue: 'medium',
shortestValue: '0'
},
'border-style': {
breakUp: breakUp.fourValues,
canOverride: canOverride.always,
componentOf: [
'border'
],
components: [
'border-top-style',
'border-right-style',
'border-bottom-style',
'border-left-style'
],
defaultValue: 'none',
restore: restore.fourValues,
shorthand: true
},
'border-top': {
breakUp: breakUp.border,
canOverride: canOverride.border,
components: [
'border-top-width',
'border-top-style',
'border-top-color'
],
defaultValue: 'none',
restore: restore.withoutDefaults,
shorthand: true
},
'border-top-color': {
canOverride: canOverride.color,
componentOf: [
'border-color',
'border-top'
],
defaultValue: 'none'
},
'border-top-style': {
canOverride: canOverride.always,
componentOf: [
'border-style',
'border-top'
],
defaultValue: 'none'
},
'border-top-width': {
canOverride: canOverride.unit,
componentOf: [
'border-top',
'border-width'
],
defaultValue: 'medium',
shortestValue: '0'
},
'border-width': {
breakUp: breakUp.fourValues,
canOverride: canOverride.unit,
components: [
'border-top-width',
'border-right-width',
'border-bottom-width',
'border-left-width'
],
defaultValue: 'medium',
restore: restore.fourValues,
shortestValue: '0',
shorthand: true
},
'font-size': {
canOverride: canOverride.unit,
defaultValue: 'medium',
shortestValue: '0'
},
'height': {
canOverride: canOverride.unit,
defaultValue: 'auto',
shortestValue: '0'
},
'list-style': {
components: [
'list-style-type',
'list-style-position',
'list-style-image'
],
canOverride: canOverride.always,
breakUp: breakUp.listStyle,
restore: restore.withoutDefaults,
defaultValue: 'outside', // can't use 'disc' because that'd override default 'decimal' for <ol>
shortestValue: 'none',
shorthand: true
},
'list-style-type' : {
canOverride: canOverride.always,
componentOf: [
'list-style'
],
defaultValue: '__hack',
// NOTE: we can't tell the real default value here, it's 'disc' for <ul> and 'decimal' for <ol>
// -- this is a hack, but it doesn't matter because this value will be either overridden or it will disappear at the final step anyway
shortestValue: 'none'
},
'list-style-position' : {
canOverride: canOverride.always,
componentOf: [
'list-style'
],
defaultValue: 'outside',
shortestValue: 'inside'
},
'list-style-image' : {
canOverride: canOverride.always,
componentOf: [
'list-style'
],
defaultValue: 'none'
},
'outline': {
components: [
'outline-color',
'outline-style',
'outline-width'
],
breakUp: breakUp.outline,
restore: restore.withoutDefaults,
defaultValue: '0',
shorthand: true
},
'outline-color': {
canOverride: canOverride.color,
componentOf: [
'outline'
],
defaultValue: 'invert',
shortestValue: 'red'
},
'outline-style': {
canOverride: canOverride.always,
componentOf: [
'outline'
],
defaultValue: 'none'
},
'outline-width': {
canOverride: canOverride.unit,
componentOf: [
'outline'
],
defaultValue: 'medium',
shortestValue: '0'
},
'-moz-transform': {
canOverride: canOverride.sameFunctionOrValue
},
'-ms-transform': {
canOverride: canOverride.sameFunctionOrValue
},
'-webkit-transform': {
canOverride: canOverride.sameFunctionOrValue
},
'transform': {
canOverride: canOverride.sameFunctionOrValue
},
'width': {
canOverride: canOverride.unit,
defaultValue: 'auto',
shortestValue: '0'
}
};
var addFourValueShorthand = function (prop, components, options) {
options = options || {};
compactable[prop] = {
canOverride: options.canOverride,
components: components,
breakUp: options.breakUp || breakUp.fourValues,
defaultValue: options.defaultValue || '0',
restore: options.restore || restore.fourValues,
shortestValue: options.shortestValue,
shorthand: true
};
for (var i = 0; i < components.length; i++) {
compactable[components[i]] = {
breakUp: options.breakUp || breakUp.fourValues,
canOverride: options.canOverride || canOverride.unit,
componentOf: options.componentOf,
defaultValue: options.defaultValue || '0',
shortestValue: options.shortestValue
};
}
};
['', '-moz-', '-o-', '-webkit-'].forEach(function (prefix) {
addFourValueShorthand(prefix + 'border-radius', [
prefix + 'border-top-left-radius',
prefix + 'border-top-right-radius',
prefix + 'border-bottom-right-radius',
prefix + 'border-bottom-left-radius'
], {
breakUp: breakUp.borderRadius,
componentOf: [
prefix + 'border-radius'
],
restore: restore.borderRadius
});
});
addFourValueShorthand('padding', [
'padding-top',
'padding-right',
'padding-bottom',
'padding-left'
], {
componentOf: [
'padding'
]
});
addFourValueShorthand('margin', [
'margin-top',
'margin-right',
'margin-bottom',
'margin-left'
], {
componentOf: [
'margin'
]
});
module.exports = compactable;