transition.js 11 KB

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