supports.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. 'use strict';
  2. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. var Browsers = require('./browsers');
  5. var brackets = require('./brackets');
  6. var Value = require('./value');
  7. var utils = require('./utils');
  8. var postcss = require('postcss');
  9. var supported = [];
  10. var data = require('caniuse-lite').feature(require('caniuse-lite/data/features/css-featurequeries.js'));
  11. for (var browser in data.stats) {
  12. var versions = data.stats[browser];
  13. for (var version in versions) {
  14. var support = versions[version];
  15. if (/y/.test(support)) {
  16. supported.push(browser + ' ' + version);
  17. }
  18. }
  19. }
  20. var Supports = function () {
  21. function Supports(Prefixes, all) {
  22. _classCallCheck(this, Supports);
  23. this.Prefixes = Prefixes;
  24. this.all = all;
  25. }
  26. /**
  27. * Return prefixer only with @supports supported browsers
  28. */
  29. Supports.prototype.prefixer = function prefixer() {
  30. if (this.prefixerCache) {
  31. return this.prefixerCache;
  32. }
  33. var filtered = this.all.browsers.selected.filter(function (i) {
  34. return supported.indexOf(i) !== -1;
  35. });
  36. var browsers = new Browsers(this.all.browsers.data, filtered, this.all.options);
  37. this.prefixerCache = new this.Prefixes(this.all.data, browsers, this.all.options);
  38. return this.prefixerCache;
  39. };
  40. /**
  41. * Parse string into declaration property and value
  42. */
  43. Supports.prototype.parse = function parse(str) {
  44. var _str$split = str.split(':'),
  45. prop = _str$split[0],
  46. value = _str$split[1];
  47. if (!value) value = '';
  48. return [prop.trim(), value.trim()];
  49. };
  50. /**
  51. * Create virtual rule to process it by prefixer
  52. */
  53. Supports.prototype.virtual = function virtual(str) {
  54. var _parse = this.parse(str),
  55. prop = _parse[0],
  56. value = _parse[1];
  57. var rule = postcss.parse('a{}').first;
  58. rule.append({ prop: prop, value: value, raws: { before: '' } });
  59. return rule;
  60. };
  61. /**
  62. * Return array of Declaration with all necessary prefixes
  63. */
  64. Supports.prototype.prefixed = function prefixed(str) {
  65. var rule = this.virtual(str);
  66. if (this.disabled(rule.first)) {
  67. return rule.nodes;
  68. }
  69. var result = { warn: function warn() {
  70. return null;
  71. } };
  72. var prefixer = this.prefixer().add[rule.first.prop];
  73. prefixer && prefixer.process && prefixer.process(rule.first, result);
  74. for (var _iterator = rule.nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  75. var _ref;
  76. if (_isArray) {
  77. if (_i >= _iterator.length) break;
  78. _ref = _iterator[_i++];
  79. } else {
  80. _i = _iterator.next();
  81. if (_i.done) break;
  82. _ref = _i.value;
  83. }
  84. var decl = _ref;
  85. for (var _iterator2 = this.prefixer().values('add', rule.first.prop), _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  86. var _ref2;
  87. if (_isArray2) {
  88. if (_i2 >= _iterator2.length) break;
  89. _ref2 = _iterator2[_i2++];
  90. } else {
  91. _i2 = _iterator2.next();
  92. if (_i2.done) break;
  93. _ref2 = _i2.value;
  94. }
  95. var value = _ref2;
  96. value.process(decl);
  97. }
  98. Value.save(this.all, decl);
  99. }
  100. return rule.nodes;
  101. };
  102. /**
  103. * Return true if brackets node is "not" word
  104. */
  105. Supports.prototype.isNot = function isNot(node) {
  106. return typeof node === 'string' && /not\s*/i.test(node);
  107. };
  108. /**
  109. * Return true if brackets node is "or" word
  110. */
  111. Supports.prototype.isOr = function isOr(node) {
  112. return typeof node === 'string' && /\s*or\s*/i.test(node);
  113. };
  114. /**
  115. * Return true if brackets node is (prop: value)
  116. */
  117. Supports.prototype.isProp = function isProp(node) {
  118. return (typeof node === 'undefined' ? 'undefined' : _typeof(node)) === 'object' && node.length === 1 && typeof node[0] === 'string';
  119. };
  120. /**
  121. * Return true if prefixed property has no unprefixed
  122. */
  123. Supports.prototype.isHack = function isHack(all, unprefixed) {
  124. var check = new RegExp('(\\(|\\s)' + utils.escapeRegexp(unprefixed) + ':');
  125. return !check.test(all);
  126. };
  127. /**
  128. * Return true if we need to remove node
  129. */
  130. Supports.prototype.toRemove = function toRemove(str, all) {
  131. var _parse2 = this.parse(str),
  132. prop = _parse2[0],
  133. value = _parse2[1];
  134. var unprefixed = this.all.unprefixed(prop);
  135. var cleaner = this.all.cleaner();
  136. if (cleaner.remove[prop] && cleaner.remove[prop].remove && !this.isHack(all, unprefixed)) {
  137. return true;
  138. }
  139. for (var _iterator3 = cleaner.values('remove', unprefixed), _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
  140. var _ref3;
  141. if (_isArray3) {
  142. if (_i3 >= _iterator3.length) break;
  143. _ref3 = _iterator3[_i3++];
  144. } else {
  145. _i3 = _iterator3.next();
  146. if (_i3.done) break;
  147. _ref3 = _i3.value;
  148. }
  149. var checker = _ref3;
  150. if (checker.check(value)) {
  151. return true;
  152. }
  153. }
  154. return false;
  155. };
  156. /**
  157. * Remove all unnecessary prefixes
  158. */
  159. Supports.prototype.remove = function remove(nodes, all) {
  160. var i = 0;
  161. while (i < nodes.length) {
  162. if (!this.isNot(nodes[i - 1]) && this.isProp(nodes[i]) && this.isOr(nodes[i + 1])) {
  163. if (this.toRemove(nodes[i][0], all)) {
  164. nodes.splice(i, 2);
  165. continue;
  166. }
  167. i += 2;
  168. continue;
  169. }
  170. if (_typeof(nodes[i]) === 'object') {
  171. nodes[i] = this.remove(nodes[i], all);
  172. }
  173. i += 1;
  174. }
  175. return nodes;
  176. };
  177. /**
  178. * Clean brackets with one child
  179. */
  180. Supports.prototype.cleanBrackets = function cleanBrackets(nodes) {
  181. var _this = this;
  182. return nodes.map(function (i) {
  183. if ((typeof i === 'undefined' ? 'undefined' : _typeof(i)) !== 'object') {
  184. return i;
  185. }
  186. if (i.length === 1 && _typeof(i[0]) === 'object') {
  187. return _this.cleanBrackets(i[0]);
  188. }
  189. return _this.cleanBrackets(i);
  190. });
  191. };
  192. /**
  193. * Add " or " between properties and convert it to brackets format
  194. */
  195. Supports.prototype.convert = function convert(progress) {
  196. var result = [''];
  197. for (var _iterator4 = progress, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
  198. var _ref4;
  199. if (_isArray4) {
  200. if (_i4 >= _iterator4.length) break;
  201. _ref4 = _iterator4[_i4++];
  202. } else {
  203. _i4 = _iterator4.next();
  204. if (_i4.done) break;
  205. _ref4 = _i4.value;
  206. }
  207. var i = _ref4;
  208. result.push([i.prop + ': ' + i.value]);
  209. result.push(' or ');
  210. }
  211. result[result.length - 1] = '';
  212. return result;
  213. };
  214. /**
  215. * Compress value functions into a string nodes
  216. */
  217. Supports.prototype.normalize = function normalize(nodes) {
  218. var _this2 = this;
  219. if ((typeof nodes === 'undefined' ? 'undefined' : _typeof(nodes)) !== 'object') {
  220. return nodes;
  221. }
  222. nodes = nodes.filter(function (i) {
  223. return i !== '';
  224. });
  225. if (typeof nodes[0] === 'string' && nodes[0].indexOf(':') !== -1) {
  226. return [brackets.stringify(nodes)];
  227. }
  228. return nodes.map(function (i) {
  229. return _this2.normalize(i);
  230. });
  231. };
  232. /**
  233. * Add prefixes
  234. */
  235. Supports.prototype.add = function add(nodes, all) {
  236. var _this3 = this;
  237. return nodes.map(function (i) {
  238. if (_this3.isProp(i)) {
  239. var prefixed = _this3.prefixed(i[0]);
  240. if (prefixed.length > 1) {
  241. return _this3.convert(prefixed);
  242. }
  243. return i;
  244. }
  245. if ((typeof i === 'undefined' ? 'undefined' : _typeof(i)) === 'object') {
  246. return _this3.add(i, all);
  247. }
  248. return i;
  249. });
  250. };
  251. /**
  252. * Add prefixed declaration
  253. */
  254. Supports.prototype.process = function process(rule) {
  255. var ast = brackets.parse(rule.params);
  256. ast = this.normalize(ast);
  257. ast = this.remove(ast, rule.params);
  258. ast = this.add(ast, rule.params);
  259. ast = this.cleanBrackets(ast);
  260. rule.params = brackets.stringify(ast);
  261. };
  262. /**
  263. * Check global options
  264. */
  265. Supports.prototype.disabled = function disabled(node) {
  266. if (!this.all.options.grid) {
  267. if (node.prop === 'display' && node.value.indexOf('grid') !== -1) {
  268. return true;
  269. }
  270. if (node.prop.indexOf('grid') !== -1 || node.prop === 'justify-items') {
  271. return true;
  272. }
  273. }
  274. if (this.all.options.flexbox === false) {
  275. if (node.prop === 'display' && node.value.indexOf('flex') !== -1) {
  276. return true;
  277. }
  278. var other = ['order', 'justify-content', 'align-items', 'align-content'];
  279. if (node.prop.indexOf('flex') !== -1 || other.indexOf(node.prop) !== -1) {
  280. return true;
  281. }
  282. }
  283. return false;
  284. };
  285. return Supports;
  286. }();
  287. module.exports = Supports;