-
Notifications
You must be signed in to change notification settings - Fork 324
/
longhand-overriding-test.js
144 lines (131 loc) · 5.2 KB
/
longhand-overriding-test.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
var assert = require('assert');
var vows = require('vows');
var optimize = require('../../../../lib/optimizer/level-2/compacting/optimize');
var tokenize = require('../../../../lib/tokenizer/tokenize');
var inputSourceMapTracker = require('../../../../lib/reader/input-source-map-tracker');
var compatibility = require('../../../../lib/utils/compatibility');
var validator = require('../../../../lib/optimizer/validator');
function _optimize(source) {
var tokens = tokenize(source, {
inputSourceMapTracker: inputSourceMapTracker(),
options: {},
warnings: []
});
var compat = compatibility();
var options = {
aggressiveMerging: true,
compatibility: compat,
level: {
2: {
mergeMedia: true,
restructureRules: true,
mergeSemantically: false,
compactShorthands: true
}
}
};
optimize(tokens[0][1], tokens[0][2], false, true, { options: options, validator: validator(compat) });
return tokens[0][2];
}
function longhandFirst(prefixedLonghand, prefixedShorthand, zeroValue) {
return {
'topic': function () {
return _optimize('a{' + prefixedLonghand + ':inherit;' + prefixedShorthand + ':' + zeroValue + '}');
},
'has one token': function (properties) {
assert.lengthOf(properties, 1);
},
'has zero value only': function (properties) {
assert.deepEqual(properties[0][1][1], prefixedShorthand);
assert.deepEqual(properties[0][2][1], zeroValue);
}
};
}
function shorthandFirst(prefixedLonghand, prefixedShorthand, zeroValue) {
return {
'topic': function () {
return _optimize('a{' + prefixedShorthand + ':' + zeroValue + ';' + prefixedLonghand + ':inherit}');
},
'has two tokens': function (properties) {
assert.lengthOf(properties, 2);
},
'first is shorthand': function (properties) {
assert.deepEqual(properties[0][1][1], prefixedShorthand);
assert.deepEqual(properties[0][2][1], zeroValue);
},
'second is longhand': function (properties) {
assert.deepEqual(properties[1][1][1], prefixedLonghand);
assert.deepEqual(properties[1][2][1], 'inherit');
}
};
}
function overrideContext(longhands) {
var context = {};
var vendorPrefixes = ['', '-moz-', '-o-', '-webkit-']; // there is no -ms-animation nor -ms-transition.
var vendorPrefixesFor = ['animation', 'transition'];
var defaultValues = {
'list-style-image': 'none',
'background': '0 0'
};
for (var longhand in longhands) {
for (var i = 0; i < longhands[longhand].length; i++) {
var shorthand = longhands[longhand][i];
var prefixes = vendorPrefixesFor.indexOf(shorthand) > -1 ? vendorPrefixes : [''];
for (var j = 0, m = prefixes.length; j < m; j++) {
var prefixedLonghand = prefixes[j] + longhand;
var prefixedShorthand = prefixes[j] + shorthand;
var zeroValue = defaultValues[prefixedShorthand] || '0';
context['should override ' + prefixedLonghand + ' with ' + prefixedShorthand] = longhandFirst(prefixedLonghand, prefixedShorthand, zeroValue);
context['should not override ' + prefixedShorthand + ' shorthand with ' + prefixedLonghand] = shorthandFirst(prefixedLonghand, prefixedShorthand, zeroValue);
}
}
}
return context;
}
vows.describe(optimize)
.addBatch(
overrideContext({
'background-attachment': ['background'],
'background-clip': ['background'],
'background-color': ['background'],
'background-image': ['background'],
'background-origin': ['background'],
'background-position': ['background'],
'background-repeat': ['background'],
'background-size': ['background'],
'border-color': ['border'],
'border-style': ['border'],
'border-width': ['border'],
'border-bottom': ['border'],
'border-bottom-color': ['border-bottom', 'border-color', 'border'],
'border-bottom-style': ['border-bottom', 'border-style', 'border'],
'border-bottom-width': ['border-bottom', 'border-width', 'border'],
'border-left': ['border'],
'border-left-color': ['border-left', 'border-color', 'border'],
'border-left-style': ['border-left', 'border-style', 'border'],
'border-left-width': ['border-left', 'border-width', 'border'],
'border-right': ['border'],
'border-right-color': ['border-right', 'border-color', 'border'],
'border-right-style': ['border-right', 'border-style', 'border'],
'border-right-width': ['border-right', 'border-width', 'border'],
'border-top': ['border'],
'border-top-color': ['border-top', 'border-color', 'border'],
'border-top-style': ['border-top', 'border-style', 'border'],
'border-top-width': ['border-top', 'border-width', 'border'],
'list-style-image': ['list-style'],
'list-style-position': ['list-style'],
'list-style-type': ['list-style'],
'margin-bottom': ['margin'],
'margin-left': ['margin'],
'margin-right': ['margin'],
'margin-top': ['margin'],
'outline-color': ['outline'],
'outline-style': ['outline'],
'outline-width': ['outline'],
'padding-bottom': ['padding'],
'padding-left': ['padding'],
'padding-right': ['padding'],
'padding-top': ['padding']
})
)
.export(module);