Front end of the Slack clone application.

supports.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 prefixer = this.prefixer().add[rule.first.prop];
  70. prefixer && prefixer.process && prefixer.process(rule.first);
  71. for (var _iterator = rule.nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  72. var _ref;
  73. if (_isArray) {
  74. if (_i >= _iterator.length) break;
  75. _ref = _iterator[_i++];
  76. } else {
  77. _i = _iterator.next();
  78. if (_i.done) break;
  79. _ref = _i.value;
  80. }
  81. var decl = _ref;
  82. for (var _iterator2 = this.prefixer().values('add', rule.first.prop), _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  83. var _ref2;
  84. if (_isArray2) {
  85. if (_i2 >= _iterator2.length) break;
  86. _ref2 = _iterator2[_i2++];
  87. } else {
  88. _i2 = _iterator2.next();
  89. if (_i2.done) break;
  90. _ref2 = _i2.value;
  91. }
  92. var value = _ref2;
  93. value.process(decl);
  94. }
  95. Value.save(this.all, decl);
  96. }
  97. return rule.nodes;
  98. };
  99. /**
  100. * Return true if brackets node is "not" word
  101. */
  102. Supports.prototype.isNot = function isNot(node) {
  103. return typeof node === 'string' && /not\s*/i.test(node);
  104. };
  105. /**
  106. * Return true if brackets node is "or" word
  107. */
  108. Supports.prototype.isOr = function isOr(node) {
  109. return typeof node === 'string' && /\s*or\s*/i.test(node);
  110. };
  111. /**
  112. * Return true if brackets node is (prop: value)
  113. */
  114. Supports.prototype.isProp = function isProp(node) {
  115. return (typeof node === 'undefined' ? 'undefined' : _typeof(node)) === 'object' && node.length === 1 && typeof node[0] === 'string';
  116. };
  117. /**
  118. * Return true if prefixed property has no unprefixed
  119. */
  120. Supports.prototype.isHack = function isHack(all, unprefixed) {
  121. var check = new RegExp('(\\(|\\s)' + utils.escapeRegexp(unprefixed) + ':');
  122. return !check.test(all);
  123. };
  124. /**
  125. * Return true if we need to remove node
  126. */
  127. Supports.prototype.toRemove = function toRemove(str, all) {
  128. var _parse2 = this.parse(str),
  129. prop = _parse2[0],
  130. value = _parse2[1];
  131. var unprefixed = this.all.unprefixed(prop);
  132. var cleaner = this.all.cleaner();
  133. if (cleaner.remove[prop] && cleaner.remove[prop].remove && !this.isHack(all, unprefixed)) {
  134. return true;
  135. }
  136. for (var _iterator3 = cleaner.values('remove', unprefixed), _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
  137. var _ref3;
  138. if (_isArray3) {
  139. if (_i3 >= _iterator3.length) break;
  140. _ref3 = _iterator3[_i3++];
  141. } else {
  142. _i3 = _iterator3.next();
  143. if (_i3.done) break;
  144. _ref3 = _i3.value;
  145. }
  146. var checker = _ref3;
  147. if (checker.check(value)) {
  148. return true;
  149. }
  150. }
  151. return false;
  152. };
  153. /**
  154. * Remove all unnecessary prefixes
  155. */
  156. Supports.prototype.remove = function remove(nodes, all) {
  157. var i = 0;
  158. while (i < nodes.length) {
  159. if (!this.isNot(nodes[i - 1]) && this.isProp(nodes[i]) && this.isOr(nodes[i + 1])) {
  160. if (this.toRemove(nodes[i][0], all)) {
  161. nodes.splice(i, 2);
  162. continue;
  163. }
  164. i += 2;
  165. continue;
  166. }
  167. if (_typeof(nodes[i]) === 'object') {
  168. nodes[i] = this.remove(nodes[i], all);
  169. }
  170. i += 1;
  171. }
  172. return nodes;
  173. };
  174. /**
  175. * Clean brackets with one child
  176. */
  177. Supports.prototype.cleanBrackets = function cleanBrackets(nodes) {
  178. var _this = this;
  179. return nodes.map(function (i) {
  180. if ((typeof i === 'undefined' ? 'undefined' : _typeof(i)) !== 'object') {
  181. return i;
  182. }
  183. if (i.length === 1 && _typeof(i[0]) === 'object') {
  184. return _this.cleanBrackets(i[0]);
  185. }
  186. return _this.cleanBrackets(i);
  187. });
  188. };
  189. /**
  190. * Add " or " between properties and convert it to brackets format
  191. */
  192. Supports.prototype.convert = function convert(progress) {
  193. var result = [''];
  194. for (var _iterator4 = progress, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
  195. var _ref4;
  196. if (_isArray4) {
  197. if (_i4 >= _iterator4.length) break;
  198. _ref4 = _iterator4[_i4++];
  199. } else {
  200. _i4 = _iterator4.next();
  201. if (_i4.done) break;
  202. _ref4 = _i4.value;
  203. }
  204. var i = _ref4;
  205. result.push([i.prop + ': ' + i.value]);
  206. result.push(' or ');
  207. }
  208. result[result.length - 1] = '';
  209. return result;
  210. };
  211. /**
  212. * Compress value functions into a string nodes
  213. */
  214. Supports.prototype.normalize = function normalize(nodes) {
  215. var _this2 = this;
  216. if ((typeof nodes === 'undefined' ? 'undefined' : _typeof(nodes)) !== 'object') {
  217. return nodes;
  218. }
  219. nodes = nodes.filter(function (i) {
  220. return i !== '';
  221. });
  222. if (typeof nodes[0] === 'string' && nodes[0].indexOf(':') !== -1) {
  223. return [brackets.stringify(nodes)];
  224. }
  225. return nodes.map(function (i) {
  226. return _this2.normalize(i);
  227. });
  228. };
  229. /**
  230. * Add prefixes
  231. */
  232. Supports.prototype.add = function add(nodes, all) {
  233. var _this3 = this;
  234. return nodes.map(function (i) {
  235. if (_this3.isProp(i)) {
  236. var prefixed = _this3.prefixed(i[0]);
  237. if (prefixed.length > 1) {
  238. return _this3.convert(prefixed);
  239. }
  240. return i;
  241. }
  242. if ((typeof i === 'undefined' ? 'undefined' : _typeof(i)) === 'object') {
  243. return _this3.add(i, all);
  244. }
  245. return i;
  246. });
  247. };
  248. /**
  249. * Add prefixed declaration
  250. */
  251. Supports.prototype.process = function process(rule) {
  252. var ast = brackets.parse(rule.params);
  253. ast = this.normalize(ast);
  254. ast = this.remove(ast, rule.params);
  255. ast = this.add(ast, rule.params);
  256. ast = this.cleanBrackets(ast);
  257. rule.params = brackets.stringify(ast);
  258. };
  259. /**
  260. * Check global options
  261. */
  262. Supports.prototype.disabled = function disabled(node) {
  263. if (!this.all.options.grid) {
  264. if (node.prop === 'display' && node.value.indexOf('grid') !== -1) {
  265. return true;
  266. }
  267. if (node.prop.indexOf('grid') !== -1 || node.prop === 'justify-items') {
  268. return true;
  269. }
  270. }
  271. if (this.all.options.flexbox === false) {
  272. if (node.prop === 'display' && node.value.indexOf('flex') !== -1) {
  273. return true;
  274. }
  275. var other = ['order', 'justify-content', 'align-items', 'align-content'];
  276. if (node.prop.indexOf('flex') !== -1 || other.indexOf(node.prop) !== -1) {
  277. return true;
  278. }
  279. }
  280. return false;
  281. };
  282. return Supports;
  283. }();
  284. module.exports = Supports;