UNPKG

4.85 kBJavaScriptView Raw
1var DEFAULTS = {
2 '*': {
3 colors: {
4 hexAlpha: false, // 4- and 8-character hex notation
5 opacity: true // rgba / hsla
6 },
7 customUnits: {
8 rpx: false
9 },
10 properties: {
11 backgroundClipMerging: true, // background-clip to shorthand
12 backgroundOriginMerging: true, // background-origin to shorthand
13 backgroundSizeMerging: true, // background-size to shorthand
14 colors: true, // any kind of color transformations, like `#ff00ff` to `#f0f` or `#fff` into `red`
15 ieBangHack: false, // !ie suffix hacks on IE<8
16 ieFilters: false, // whether to preserve `filter` and `-ms-filter` properties
17 iePrefixHack: false, // underscore / asterisk prefix hacks on IE
18 ieSuffixHack: false, // \9 suffix hacks on IE6-9, \0 suffix hack on IE6-11
19 merging: true, // merging properties into one
20 shorterLengthUnits: false, // optimize pixel units into `pt`, `pc` or `in` units
21 spaceAfterClosingBrace: true, // 'url() no-repeat' to 'url()no-repeat'
22 urlQuotes: true, // whether to wrap content of `url()` into quotes or not
23 zeroUnits: true // 0[unit] -> 0
24 },
25 selectors: {
26 adjacentSpace: false, // div+ nav Android stock browser hack
27 ie7Hack: false, // *+html hack
28 mergeablePseudoClasses: [
29 ':active',
30 ':after',
31 ':before',
32 ':empty',
33 ':checked',
34 ':disabled',
35 ':empty',
36 ':enabled',
37 ':first-child',
38 ':first-letter',
39 ':first-line',
40 ':first-of-type',
41 ':focus',
42 ':hover',
43 ':lang',
44 ':last-child',
45 ':last-of-type',
46 ':link',
47 ':not',
48 ':nth-child',
49 ':nth-last-child',
50 ':nth-last-of-type',
51 ':nth-of-type',
52 ':only-child',
53 ':only-of-type',
54 ':root',
55 ':target',
56 ':visited'
57 ], // selectors with these pseudo-classes can be merged as these are universally supported
58 mergeablePseudoElements: [
59 '::after',
60 '::before',
61 '::first-letter',
62 '::first-line'
63 ], // selectors with these pseudo-elements can be merged as these are universally supported
64 mergeLimit: 8191, // number of rules that can be safely merged together
65 multiplePseudoMerging: true
66 },
67 units: {
68 ch: true,
69 in: true,
70 pc: true,
71 pt: true,
72 rem: true,
73 vh: true,
74 vm: true, // vm is vmin on IE9+ see https://developer.mozilla.org/en-US/docs/Web/CSS/length
75 vmax: true,
76 vmin: true,
77 vw: true
78 }
79 }
80};
81
82DEFAULTS.ie11 = merge(DEFAULTS['*'], {
83 properties: {
84 ieSuffixHack: true
85 }
86});
87
88DEFAULTS.ie10 = merge(DEFAULTS['*'], {
89 properties: {
90 ieSuffixHack: true
91 }
92});
93
94DEFAULTS.ie9 = merge(DEFAULTS['*'], {
95 properties: {
96 ieFilters: true,
97 ieSuffixHack: true
98 }
99});
100
101DEFAULTS.ie8 = merge(DEFAULTS.ie9, {
102 colors: {
103 opacity: false
104 },
105 properties: {
106 backgroundClipMerging: false,
107 backgroundOriginMerging: false,
108 backgroundSizeMerging: false,
109 iePrefixHack: true,
110 merging: false
111 },
112 selectors: {
113 mergeablePseudoClasses: [
114 ':after',
115 ':before',
116 ':first-child',
117 ':first-letter',
118 ':focus',
119 ':hover',
120 ':visited'
121 ],
122 mergeablePseudoElements: []
123 },
124 units: {
125 ch: false,
126 rem: false,
127 vh: false,
128 vm: false,
129 vmax: false,
130 vmin: false,
131 vw: false
132 }
133});
134
135DEFAULTS.ie7 = merge(DEFAULTS.ie8, {
136 properties: {
137 ieBangHack: true
138 },
139 selectors: {
140 ie7Hack: true,
141 mergeablePseudoClasses: [
142 ':first-child',
143 ':first-letter',
144 ':hover',
145 ':visited'
146 ]
147 },
148});
149
150function compatibilityFrom(source) {
151 return merge(DEFAULTS['*'], calculateSource(source));
152}
153
154function merge(source, target) {
155 for (var key in source) {
156 if (Object.prototype.hasOwnProperty.call(source, key)) {
157 var value = source[key];
158
159 if (Object.prototype.hasOwnProperty.call(target, key) && typeof value === 'object' && !Array.isArray(value)) {
160 target[key] = merge(value, target[key] || {});
161 } else {
162 target[key] = key in target ? target[key] : value;
163 }
164 }
165 }
166
167 return target;
168}
169
170function calculateSource(source) {
171 if (typeof source == 'object')
172 return source;
173
174 if (!/[,\+\-]/.test(source))
175 return DEFAULTS[source] || DEFAULTS['*'];
176
177 var parts = source.split(',');
178 var template = parts[0] in DEFAULTS ?
179 DEFAULTS[parts.shift()] :
180 DEFAULTS['*'];
181
182 source = {};
183
184 parts.forEach(function (part) {
185 var isAdd = part[0] == '+';
186 var key = part.substring(1).split('.');
187 var group = key[0];
188 var option = key[1];
189
190 source[group] = source[group] || {};
191 source[group][option] = isAdd;
192 });
193
194 return merge(template, source);
195}
196
197module.exports = compatibilityFrom;