UNPKG

8.75 kBJavaScriptView Raw
1"use strict";
2
3var Browsers = require('./browsers');
4
5var brackets = require('./brackets');
6
7var Value = require('./value');
8
9var utils = require('./utils');
10
11var postcss = require('postcss');
12
13var supported = [];
14
15var data = require('caniuse-lite').feature(require('caniuse-lite/data/features/css-featurequeries.js'));
16
17for (var browser in data.stats) {
18 var versions = data.stats[browser];
19
20 for (var version in versions) {
21 var support = versions[version];
22
23 if (/y/.test(support)) {
24 supported.push(browser + ' ' + version);
25 }
26 }
27}
28
29var Supports =
30/*#__PURE__*/
31function () {
32 function Supports(Prefixes, all) {
33 this.Prefixes = Prefixes;
34 this.all = all;
35 }
36 /**
37 * Return prefixer only with @supports supported browsers
38 */
39
40
41 var _proto = Supports.prototype;
42
43 _proto.prefixer = function prefixer() {
44 if (this.prefixerCache) {
45 return this.prefixerCache;
46 }
47
48 var filtered = this.all.browsers.selected.filter(function (i) {
49 return supported.indexOf(i) !== -1;
50 });
51 var browsers = new Browsers(this.all.browsers.data, filtered, this.all.options);
52 this.prefixerCache = new this.Prefixes(this.all.data, browsers, this.all.options);
53 return this.prefixerCache;
54 }
55 /**
56 * Parse string into declaration property and value
57 */
58 ;
59
60 _proto.parse = function parse(str) {
61 var parts = str.split(':');
62 var prop = parts[0];
63 var value = parts[1];
64 if (!value) value = '';
65 return [prop.trim(), value.trim()];
66 }
67 /**
68 * Create virtual rule to process it by prefixer
69 */
70 ;
71
72 _proto.virtual = function virtual(str) {
73 var _this$parse = this.parse(str),
74 prop = _this$parse[0],
75 value = _this$parse[1];
76
77 var rule = postcss.parse('a{}').first;
78 rule.append({
79 prop: prop,
80 value: value,
81 raws: {
82 before: ''
83 }
84 });
85 return rule;
86 }
87 /**
88 * Return array of Declaration with all necessary prefixes
89 */
90 ;
91
92 _proto.prefixed = function prefixed(str) {
93 var rule = this.virtual(str);
94
95 if (this.disabled(rule.first)) {
96 return rule.nodes;
97 }
98
99 var result = {
100 warn: function warn() {
101 return null;
102 }
103 };
104 var prefixer = this.prefixer().add[rule.first.prop];
105 prefixer && prefixer.process && prefixer.process(rule.first, result);
106
107 for (var _iterator = rule.nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
108 var _ref;
109
110 if (_isArray) {
111 if (_i >= _iterator.length) break;
112 _ref = _iterator[_i++];
113 } else {
114 _i = _iterator.next();
115 if (_i.done) break;
116 _ref = _i.value;
117 }
118
119 var decl = _ref;
120
121 for (var _iterator2 = this.prefixer().values('add', rule.first.prop), _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
122 var _ref2;
123
124 if (_isArray2) {
125 if (_i2 >= _iterator2.length) break;
126 _ref2 = _iterator2[_i2++];
127 } else {
128 _i2 = _iterator2.next();
129 if (_i2.done) break;
130 _ref2 = _i2.value;
131 }
132
133 var value = _ref2;
134 value.process(decl);
135 }
136
137 Value.save(this.all, decl);
138 }
139
140 return rule.nodes;
141 }
142 /**
143 * Return true if brackets node is "not" word
144 */
145 ;
146
147 _proto.isNot = function isNot(node) {
148 return typeof node === 'string' && /not\s*/i.test(node);
149 }
150 /**
151 * Return true if brackets node is "or" word
152 */
153 ;
154
155 _proto.isOr = function isOr(node) {
156 return typeof node === 'string' && /\s*or\s*/i.test(node);
157 }
158 /**
159 * Return true if brackets node is (prop: value)
160 */
161 ;
162
163 _proto.isProp = function isProp(node) {
164 return typeof node === 'object' && node.length === 1 && typeof node[0] === 'string';
165 }
166 /**
167 * Return true if prefixed property has no unprefixed
168 */
169 ;
170
171 _proto.isHack = function isHack(all, unprefixed) {
172 var check = new RegExp("(\\(|\\s)" + utils.escapeRegexp(unprefixed) + ":");
173 return !check.test(all);
174 }
175 /**
176 * Return true if we need to remove node
177 */
178 ;
179
180 _proto.toRemove = function toRemove(str, all) {
181 var _this$parse2 = this.parse(str),
182 prop = _this$parse2[0],
183 value = _this$parse2[1];
184
185 var unprefixed = this.all.unprefixed(prop);
186 var cleaner = this.all.cleaner();
187
188 if (cleaner.remove[prop] && cleaner.remove[prop].remove && !this.isHack(all, unprefixed)) {
189 return true;
190 }
191
192 for (var _iterator3 = cleaner.values('remove', unprefixed), _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
193 var _ref3;
194
195 if (_isArray3) {
196 if (_i3 >= _iterator3.length) break;
197 _ref3 = _iterator3[_i3++];
198 } else {
199 _i3 = _iterator3.next();
200 if (_i3.done) break;
201 _ref3 = _i3.value;
202 }
203
204 var checker = _ref3;
205
206 if (checker.check(value)) {
207 return true;
208 }
209 }
210
211 return false;
212 }
213 /**
214 * Remove all unnecessary prefixes
215 */
216 ;
217
218 _proto.remove = function remove(nodes, all) {
219 var i = 0;
220
221 while (i < nodes.length) {
222 if (!this.isNot(nodes[i - 1]) && this.isProp(nodes[i]) && this.isOr(nodes[i + 1])) {
223 if (this.toRemove(nodes[i][0], all)) {
224 nodes.splice(i, 2);
225 continue;
226 }
227
228 i += 2;
229 continue;
230 }
231
232 if (typeof nodes[i] === 'object') {
233 nodes[i] = this.remove(nodes[i], all);
234 }
235
236 i += 1;
237 }
238
239 return nodes;
240 }
241 /**
242 * Clean brackets with one child
243 */
244 ;
245
246 _proto.cleanBrackets = function cleanBrackets(nodes) {
247 var _this = this;
248
249 return nodes.map(function (i) {
250 if (typeof i !== 'object') {
251 return i;
252 }
253
254 if (i.length === 1 && typeof i[0] === 'object') {
255 return _this.cleanBrackets(i[0]);
256 }
257
258 return _this.cleanBrackets(i);
259 });
260 }
261 /**
262 * Add " or " between properties and convert it to brackets format
263 */
264 ;
265
266 _proto.convert = function convert(progress) {
267 var result = [''];
268
269 for (var _iterator4 = progress, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
270 var _ref4;
271
272 if (_isArray4) {
273 if (_i4 >= _iterator4.length) break;
274 _ref4 = _iterator4[_i4++];
275 } else {
276 _i4 = _iterator4.next();
277 if (_i4.done) break;
278 _ref4 = _i4.value;
279 }
280
281 var i = _ref4;
282 result.push([i.prop + ": " + i.value]);
283 result.push(' or ');
284 }
285
286 result[result.length - 1] = '';
287 return result;
288 }
289 /**
290 * Compress value functions into a string nodes
291 */
292 ;
293
294 _proto.normalize = function normalize(nodes) {
295 var _this2 = this;
296
297 if (typeof nodes !== 'object') {
298 return nodes;
299 }
300
301 nodes = nodes.filter(function (i) {
302 return i !== '';
303 });
304
305 if (typeof nodes[0] === 'string' && nodes[0].indexOf(':') !== -1) {
306 return [brackets.stringify(nodes)];
307 }
308
309 return nodes.map(function (i) {
310 return _this2.normalize(i);
311 });
312 }
313 /**
314 * Add prefixes
315 */
316 ;
317
318 _proto.add = function add(nodes, all) {
319 var _this3 = this;
320
321 return nodes.map(function (i) {
322 if (_this3.isProp(i)) {
323 var prefixed = _this3.prefixed(i[0]);
324
325 if (prefixed.length > 1) {
326 return _this3.convert(prefixed);
327 }
328
329 return i;
330 }
331
332 if (typeof i === 'object') {
333 return _this3.add(i, all);
334 }
335
336 return i;
337 });
338 }
339 /**
340 * Add prefixed declaration
341 */
342 ;
343
344 _proto.process = function process(rule) {
345 var ast = brackets.parse(rule.params);
346 ast = this.normalize(ast);
347 ast = this.remove(ast, rule.params);
348 ast = this.add(ast, rule.params);
349 ast = this.cleanBrackets(ast);
350 rule.params = brackets.stringify(ast);
351 }
352 /**
353 * Check global options
354 */
355 ;
356
357 _proto.disabled = function disabled(node) {
358 if (!this.all.options.grid) {
359 if (node.prop === 'display' && node.value.indexOf('grid') !== -1) {
360 return true;
361 }
362
363 if (node.prop.indexOf('grid') !== -1 || node.prop === 'justify-items') {
364 return true;
365 }
366 }
367
368 if (this.all.options.flexbox === false) {
369 if (node.prop === 'display' && node.value.indexOf('flex') !== -1) {
370 return true;
371 }
372
373 var other = ['order', 'justify-content', 'align-items', 'align-content'];
374
375 if (node.prop.indexOf('flex') !== -1 || other.indexOf(node.prop) !== -1) {
376 return true;
377 }
378 }
379
380 return false;
381 };
382
383 return Supports;
384}();
385
386module.exports = Supports;
\No newline at end of file