You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

385 lines
8.5 KiB

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