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.

508 lines
12 KiB

4 years ago
  1. "use strict";
  2. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  3. var parser = require('postcss-value-parser');
  4. var vendor = require('postcss').vendor;
  5. var list = require('postcss').list;
  6. var Browsers = require('./browsers');
  7. var Transition =
  8. /*#__PURE__*/
  9. function () {
  10. function Transition(prefixes) {
  11. _defineProperty(this, "props", ['transition', 'transition-property']);
  12. this.prefixes = prefixes;
  13. }
  14. /**
  15. * Process transition and add prefixes for all necessary properties
  16. */
  17. var _proto = Transition.prototype;
  18. _proto.add = function add(decl, result) {
  19. var _this = this;
  20. var prefix, prop;
  21. var add = this.prefixes.add[decl.prop];
  22. var vendorPrefixes = this.ruleVendorPrefixes(decl);
  23. var declPrefixes = vendorPrefixes || add && add.prefixes || [];
  24. var params = this.parse(decl.value);
  25. var names = params.map(function (i) {
  26. return _this.findProp(i);
  27. });
  28. var added = [];
  29. if (names.some(function (i) {
  30. return i[0] === '-';
  31. })) {
  32. return;
  33. }
  34. for (var _iterator = params, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  35. var _ref;
  36. if (_isArray) {
  37. if (_i >= _iterator.length) break;
  38. _ref = _iterator[_i++];
  39. } else {
  40. _i = _iterator.next();
  41. if (_i.done) break;
  42. _ref = _i.value;
  43. }
  44. var param = _ref;
  45. prop = this.findProp(param);
  46. if (prop[0] === '-') continue;
  47. var prefixer = this.prefixes.add[prop];
  48. if (!prefixer || !prefixer.prefixes) continue;
  49. for (var _iterator3 = prefixer.prefixes, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
  50. if (_isArray3) {
  51. if (_i3 >= _iterator3.length) break;
  52. prefix = _iterator3[_i3++];
  53. } else {
  54. _i3 = _iterator3.next();
  55. if (_i3.done) break;
  56. prefix = _i3.value;
  57. }
  58. if (vendorPrefixes && !vendorPrefixes.some(function (p) {
  59. return prefix.includes(p);
  60. })) {
  61. continue;
  62. }
  63. var prefixed = this.prefixes.prefixed(prop, prefix);
  64. if (prefixed !== '-ms-transform' && !names.includes(prefixed)) {
  65. if (!this.disabled(prop, prefix)) {
  66. added.push(this.clone(prop, prefixed, param));
  67. }
  68. }
  69. }
  70. }
  71. params = params.concat(added);
  72. var value = this.stringify(params);
  73. var webkitClean = this.stringify(this.cleanFromUnprefixed(params, '-webkit-'));
  74. if (declPrefixes.includes('-webkit-')) {
  75. this.cloneBefore(decl, "-webkit-" + decl.prop, webkitClean);
  76. }
  77. this.cloneBefore(decl, decl.prop, webkitClean);
  78. if (declPrefixes.includes('-o-')) {
  79. var operaClean = this.stringify(this.cleanFromUnprefixed(params, '-o-'));
  80. this.cloneBefore(decl, "-o-" + decl.prop, operaClean);
  81. }
  82. for (var _iterator2 = declPrefixes, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  83. if (_isArray2) {
  84. if (_i2 >= _iterator2.length) break;
  85. prefix = _iterator2[_i2++];
  86. } else {
  87. _i2 = _iterator2.next();
  88. if (_i2.done) break;
  89. prefix = _i2.value;
  90. }
  91. if (prefix !== '-webkit-' && prefix !== '-o-') {
  92. var prefixValue = this.stringify(this.cleanOtherPrefixes(params, prefix));
  93. this.cloneBefore(decl, prefix + decl.prop, prefixValue);
  94. }
  95. }
  96. if (value !== decl.value && !this.already(decl, decl.prop, value)) {
  97. this.checkForWarning(result, decl);
  98. decl.cloneBefore();
  99. decl.value = value;
  100. }
  101. }
  102. /**
  103. * Find property name
  104. */
  105. ;
  106. _proto.findProp = function findProp(param) {
  107. var prop = param[0].value;
  108. if (/^\d/.test(prop)) {
  109. for (var _iterator4 = param.entries(), _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
  110. var _ref2;
  111. if (_isArray4) {
  112. if (_i4 >= _iterator4.length) break;
  113. _ref2 = _iterator4[_i4++];
  114. } else {
  115. _i4 = _iterator4.next();
  116. if (_i4.done) break;
  117. _ref2 = _i4.value;
  118. }
  119. var _ref3 = _ref2,
  120. i = _ref3[0],
  121. token = _ref3[1];
  122. if (i !== 0 && token.type === 'word') {
  123. return token.value;
  124. }
  125. }
  126. }
  127. return prop;
  128. }
  129. /**
  130. * Does we already have this declaration
  131. */
  132. ;
  133. _proto.already = function already(decl, prop, value) {
  134. return decl.parent.some(function (i) {
  135. return i.prop === prop && i.value === value;
  136. });
  137. }
  138. /**
  139. * Add declaration if it is not exist
  140. */
  141. ;
  142. _proto.cloneBefore = function cloneBefore(decl, prop, value) {
  143. if (!this.already(decl, prop, value)) {
  144. decl.cloneBefore({
  145. prop: prop,
  146. value: value
  147. });
  148. }
  149. }
  150. /**
  151. * Show transition-property warning
  152. */
  153. ;
  154. _proto.checkForWarning = function checkForWarning(result, decl) {
  155. if (decl.prop !== 'transition-property') {
  156. return;
  157. }
  158. decl.parent.each(function (i) {
  159. if (i.type !== 'decl') {
  160. return undefined;
  161. }
  162. if (i.prop.indexOf('transition-') !== 0) {
  163. return undefined;
  164. }
  165. if (i.prop === 'transition-property') {
  166. return undefined;
  167. }
  168. if (list.comma(i.value).length > 1) {
  169. decl.warn(result, 'Replace transition-property to transition, ' + 'because Autoprefixer could not support ' + 'any cases of transition-property ' + 'and other transition-*');
  170. }
  171. return false;
  172. });
  173. }
  174. /**
  175. * Process transition and remove all unnecessary properties
  176. */
  177. ;
  178. _proto.remove = function remove(decl) {
  179. var _this2 = this;
  180. var params = this.parse(decl.value);
  181. params = params.filter(function (i) {
  182. var prop = _this2.prefixes.remove[_this2.findProp(i)];
  183. return !prop || !prop.remove;
  184. });
  185. var value = this.stringify(params);
  186. if (decl.value === value) {
  187. return;
  188. }
  189. if (params.length === 0) {
  190. decl.remove();
  191. return;
  192. }
  193. var _double = decl.parent.some(function (i) {
  194. return i.prop === decl.prop && i.value === value;
  195. });
  196. var smaller = decl.parent.some(function (i) {
  197. return i !== decl && i.prop === decl.prop && i.value.length > value.length;
  198. });
  199. if (_double || smaller) {
  200. decl.remove();
  201. return;
  202. }
  203. decl.value = value;
  204. }
  205. /**
  206. * Parse properties list to array
  207. */
  208. ;
  209. _proto.parse = function parse(value) {
  210. var ast = parser(value);
  211. var result = [];
  212. var param = [];
  213. for (var _iterator5 = ast.nodes, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
  214. var _ref4;
  215. if (_isArray5) {
  216. if (_i5 >= _iterator5.length) break;
  217. _ref4 = _iterator5[_i5++];
  218. } else {
  219. _i5 = _iterator5.next();
  220. if (_i5.done) break;
  221. _ref4 = _i5.value;
  222. }
  223. var node = _ref4;
  224. param.push(node);
  225. if (node.type === 'div' && node.value === ',') {
  226. result.push(param);
  227. param = [];
  228. }
  229. }
  230. result.push(param);
  231. return result.filter(function (i) {
  232. return i.length > 0;
  233. });
  234. }
  235. /**
  236. * Return properties string from array
  237. */
  238. ;
  239. _proto.stringify = function stringify(params) {
  240. if (params.length === 0) {
  241. return '';
  242. }
  243. var nodes = [];
  244. for (var _iterator6 = params, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
  245. var _ref5;
  246. if (_isArray6) {
  247. if (_i6 >= _iterator6.length) break;
  248. _ref5 = _iterator6[_i6++];
  249. } else {
  250. _i6 = _iterator6.next();
  251. if (_i6.done) break;
  252. _ref5 = _i6.value;
  253. }
  254. var param = _ref5;
  255. if (param[param.length - 1].type !== 'div') {
  256. param.push(this.div(params));
  257. }
  258. nodes = nodes.concat(param);
  259. }
  260. if (nodes[0].type === 'div') {
  261. nodes = nodes.slice(1);
  262. }
  263. if (nodes[nodes.length - 1].type === 'div') {
  264. nodes = nodes.slice(0, +-2 + 1 || undefined);
  265. }
  266. return parser.stringify({
  267. nodes: nodes
  268. });
  269. }
  270. /**
  271. * Return new param array with different name
  272. */
  273. ;
  274. _proto.clone = function clone(origin, name, param) {
  275. var result = [];
  276. var changed = false;
  277. for (var _iterator7 = param, _isArray7 = Array.isArray(_iterator7), _i7 = 0, _iterator7 = _isArray7 ? _iterator7 : _iterator7[Symbol.iterator]();;) {
  278. var _ref6;
  279. if (_isArray7) {
  280. if (_i7 >= _iterator7.length) break;
  281. _ref6 = _iterator7[_i7++];
  282. } else {
  283. _i7 = _iterator7.next();
  284. if (_i7.done) break;
  285. _ref6 = _i7.value;
  286. }
  287. var i = _ref6;
  288. if (!changed && i.type === 'word' && i.value === origin) {
  289. result.push({
  290. type: 'word',
  291. value: name
  292. });
  293. changed = true;
  294. } else {
  295. result.push(i);
  296. }
  297. }
  298. return result;
  299. }
  300. /**
  301. * Find or create separator
  302. */
  303. ;
  304. _proto.div = function div(params) {
  305. for (var _iterator8 = params, _isArray8 = Array.isArray(_iterator8), _i8 = 0, _iterator8 = _isArray8 ? _iterator8 : _iterator8[Symbol.iterator]();;) {
  306. var _ref7;
  307. if (_isArray8) {
  308. if (_i8 >= _iterator8.length) break;
  309. _ref7 = _iterator8[_i8++];
  310. } else {
  311. _i8 = _iterator8.next();
  312. if (_i8.done) break;
  313. _ref7 = _i8.value;
  314. }
  315. var param = _ref7;
  316. for (var _iterator9 = param, _isArray9 = Array.isArray(_iterator9), _i9 = 0, _iterator9 = _isArray9 ? _iterator9 : _iterator9[Symbol.iterator]();;) {
  317. var _ref8;
  318. if (_isArray9) {
  319. if (_i9 >= _iterator9.length) break;
  320. _ref8 = _iterator9[_i9++];
  321. } else {
  322. _i9 = _iterator9.next();
  323. if (_i9.done) break;
  324. _ref8 = _i9.value;
  325. }
  326. var node = _ref8;
  327. if (node.type === 'div' && node.value === ',') {
  328. return node;
  329. }
  330. }
  331. }
  332. return {
  333. type: 'div',
  334. value: ',',
  335. after: ' '
  336. };
  337. };
  338. _proto.cleanOtherPrefixes = function cleanOtherPrefixes(params, prefix) {
  339. var _this3 = this;
  340. return params.filter(function (param) {
  341. var current = vendor.prefix(_this3.findProp(param));
  342. return current === '' || current === prefix;
  343. });
  344. }
  345. /**
  346. * Remove all non-webkit prefixes and unprefixed params if we have prefixed
  347. */
  348. ;
  349. _proto.cleanFromUnprefixed = function cleanFromUnprefixed(params, prefix) {
  350. var _this4 = this;
  351. var remove = params.map(function (i) {
  352. return _this4.findProp(i);
  353. }).filter(function (i) {
  354. return i.slice(0, prefix.length) === prefix;
  355. }).map(function (i) {
  356. return _this4.prefixes.unprefixed(i);
  357. });
  358. var result = [];
  359. for (var _iterator10 = params, _isArray10 = Array.isArray(_iterator10), _i10 = 0, _iterator10 = _isArray10 ? _iterator10 : _iterator10[Symbol.iterator]();;) {
  360. var _ref9;
  361. if (_isArray10) {
  362. if (_i10 >= _iterator10.length) break;
  363. _ref9 = _iterator10[_i10++];
  364. } else {
  365. _i10 = _iterator10.next();
  366. if (_i10.done) break;
  367. _ref9 = _i10.value;
  368. }
  369. var param = _ref9;
  370. var prop = this.findProp(param);
  371. var p = vendor.prefix(prop);
  372. if (!remove.includes(prop) && (p === prefix || p === '')) {
  373. result.push(param);
  374. }
  375. }
  376. return result;
  377. }
  378. /**
  379. * Check property for disabled by option
  380. */
  381. ;
  382. _proto.disabled = function disabled(prop, prefix) {
  383. var other = ['order', 'justify-content', 'align-self', 'align-content'];
  384. if (prop.includes('flex') || other.includes(prop)) {
  385. if (this.prefixes.options.flexbox === false) {
  386. return true;
  387. }
  388. if (this.prefixes.options.flexbox === 'no-2009') {
  389. return prefix.includes('2009');
  390. }
  391. }
  392. return undefined;
  393. }
  394. /**
  395. * Check if transition prop is inside vendor specific rule
  396. */
  397. ;
  398. _proto.ruleVendorPrefixes = function ruleVendorPrefixes(decl) {
  399. var parent = decl.parent;
  400. if (parent.type !== 'rule') {
  401. return false;
  402. } else if (!parent.selector.includes(':-')) {
  403. return false;
  404. }
  405. var selectors = Browsers.prefixes().filter(function (s) {
  406. return parent.selector.includes(':' + s);
  407. });
  408. return selectors.length > 0 ? selectors : false;
  409. };
  410. return Transition;
  411. }();
  412. module.exports = Transition;