transition.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. (function() {
  2. var Transition, list, parser, vendor;
  3. parser = require('postcss-value-parser');
  4. vendor = require('postcss/lib/vendor');
  5. list = require('postcss/lib/list');
  6. Transition = (function() {
  7. function Transition(prefixes) {
  8. this.prefixes = prefixes;
  9. }
  10. Transition.prototype.props = ['transition', 'transition-property'];
  11. Transition.prototype.add = function(decl, result) {
  12. var added, declPrefixes, j, k, l, len, len1, len2, names, operaClean, param, params, prefix, prefixValue, prefixed, prefixer, prop, ref, ref1, value, webkitClean;
  13. declPrefixes = ((ref = this.prefixes.add[decl.prop]) != null ? ref.prefixes : void 0) || [];
  14. params = this.parse(decl.value);
  15. names = params.map((function(_this) {
  16. return function(i) {
  17. return _this.findProp(i);
  18. };
  19. })(this));
  20. added = [];
  21. if (names.some(function(i) {
  22. return i[0] === '-';
  23. })) {
  24. return;
  25. }
  26. for (j = 0, len = params.length; j < len; j++) {
  27. param = params[j];
  28. prop = this.findProp(param);
  29. if (prop[0] === '-') {
  30. continue;
  31. }
  32. prefixer = this.prefixes.add[prop];
  33. if (!(prefixer != null ? prefixer.prefixes : void 0)) {
  34. continue;
  35. }
  36. ref1 = prefixer.prefixes;
  37. for (k = 0, len1 = ref1.length; k < len1; k++) {
  38. prefix = ref1[k];
  39. prefixed = this.prefixes.prefixed(prop, prefix);
  40. if (prefixed !== '-ms-transform' && names.indexOf(prefixed) === -1) {
  41. if (!this.disabled(prop, prefix)) {
  42. added.push(this.clone(prop, prefixed, param));
  43. }
  44. }
  45. }
  46. }
  47. params = params.concat(added);
  48. value = this.stringify(params);
  49. webkitClean = this.stringify(this.cleanFromUnprefixed(params, '-webkit-'));
  50. if (declPrefixes.indexOf('-webkit-') !== -1) {
  51. this.cloneBefore(decl, '-webkit-' + decl.prop, webkitClean);
  52. }
  53. this.cloneBefore(decl, decl.prop, webkitClean);
  54. if (declPrefixes.indexOf('-o-') !== -1) {
  55. operaClean = this.stringify(this.cleanFromUnprefixed(params, '-o-'));
  56. this.cloneBefore(decl, '-o-' + decl.prop, operaClean);
  57. }
  58. for (l = 0, len2 = declPrefixes.length; l < len2; l++) {
  59. prefix = declPrefixes[l];
  60. if (prefix !== '-webkit-' && prefix !== '-o-') {
  61. prefixValue = this.stringify(this.cleanOtherPrefixes(params, prefix));
  62. this.cloneBefore(decl, prefix + decl.prop, prefixValue);
  63. }
  64. }
  65. if (value !== decl.value && !this.already(decl, decl.prop, value)) {
  66. this.checkForWarning(result, decl);
  67. decl.cloneBefore();
  68. return decl.value = value;
  69. }
  70. };
  71. Transition.prototype.findProp = function(param) {
  72. var i, j, len, prop, token;
  73. prop = param[0].value;
  74. if (/^\d/.test(prop)) {
  75. for (i = j = 0, len = param.length; j < len; i = ++j) {
  76. token = param[i];
  77. if (i !== 0 && token.type === 'word') {
  78. return token.value;
  79. }
  80. }
  81. }
  82. return prop;
  83. };
  84. Transition.prototype.already = function(decl, prop, value) {
  85. return decl.parent.some(function(i) {
  86. return i.prop === prop && i.value === value;
  87. });
  88. };
  89. Transition.prototype.cloneBefore = function(decl, prop, value) {
  90. if (!this.already(decl, prop, value)) {
  91. return decl.cloneBefore({
  92. prop: prop,
  93. value: value
  94. });
  95. }
  96. };
  97. Transition.prototype.checkForWarning = function(result, decl) {
  98. if (decl.prop === 'transition-property') {
  99. return decl.parent.each(function(i) {
  100. if (i.type !== 'decl') {
  101. return;
  102. }
  103. if (i.prop.indexOf('transition-') !== 0) {
  104. return;
  105. }
  106. if (i.prop === 'transition-property') {
  107. return;
  108. }
  109. if (list.comma(i.value).length > 1) {
  110. decl.warn(result, 'Replace transition-property to transition, ' + 'because Autoprefixer could not support ' + 'any cases of transition-property ' + 'and other transition-*');
  111. }
  112. return false;
  113. });
  114. }
  115. };
  116. Transition.prototype.remove = function(decl) {
  117. var double, params, smaller, value;
  118. params = this.parse(decl.value);
  119. params = params.filter((function(_this) {
  120. return function(i) {
  121. var ref;
  122. return !((ref = _this.prefixes.remove[_this.findProp(i)]) != null ? ref.remove : void 0);
  123. };
  124. })(this));
  125. value = this.stringify(params);
  126. if (decl.value === value) {
  127. return;
  128. }
  129. if (params.length === 0) {
  130. decl.remove();
  131. return;
  132. }
  133. double = decl.parent.some(function(i) {
  134. return i.prop === decl.prop && i.value === value;
  135. });
  136. smaller = decl.parent.some(function(i) {
  137. return i !== decl && i.prop === decl.prop && i.value.length > value.length;
  138. });
  139. if (double || smaller) {
  140. return decl.remove();
  141. } else {
  142. return decl.value = value;
  143. }
  144. };
  145. Transition.prototype.parse = function(value) {
  146. var ast, j, len, node, param, ref, result;
  147. ast = parser(value);
  148. result = [];
  149. param = [];
  150. ref = ast.nodes;
  151. for (j = 0, len = ref.length; j < len; j++) {
  152. node = ref[j];
  153. param.push(node);
  154. if (node.type === 'div' && node.value === ',') {
  155. result.push(param);
  156. param = [];
  157. }
  158. }
  159. result.push(param);
  160. return result.filter(function(i) {
  161. return i.length > 0;
  162. });
  163. };
  164. Transition.prototype.stringify = function(params) {
  165. var j, len, nodes, param;
  166. if (params.length === 0) {
  167. return '';
  168. }
  169. nodes = [];
  170. for (j = 0, len = params.length; j < len; j++) {
  171. param = params[j];
  172. if (param[param.length - 1].type !== 'div') {
  173. param.push(this.div(params));
  174. }
  175. nodes = nodes.concat(param);
  176. }
  177. if (nodes[0].type === 'div') {
  178. nodes = nodes.slice(1);
  179. }
  180. if (nodes[nodes.length - 1].type === 'div') {
  181. nodes = nodes.slice(0, -1);
  182. }
  183. return parser.stringify({
  184. nodes: nodes
  185. });
  186. };
  187. Transition.prototype.clone = function(origin, name, param) {
  188. var changed, i, j, len, result;
  189. result = [];
  190. changed = false;
  191. for (j = 0, len = param.length; j < len; j++) {
  192. i = param[j];
  193. if (!changed && i.type === 'word' && i.value === origin) {
  194. result.push({
  195. type: 'word',
  196. value: name
  197. });
  198. changed = true;
  199. } else {
  200. result.push(i);
  201. }
  202. }
  203. return result;
  204. };
  205. Transition.prototype.div = function(params) {
  206. var j, k, len, len1, node, param;
  207. for (j = 0, len = params.length; j < len; j++) {
  208. param = params[j];
  209. for (k = 0, len1 = param.length; k < len1; k++) {
  210. node = param[k];
  211. if (node.type === 'div' && node.value === ',') {
  212. return node;
  213. }
  214. }
  215. }
  216. return {
  217. type: 'div',
  218. value: ',',
  219. after: ' '
  220. };
  221. };
  222. Transition.prototype.cleanOtherPrefixes = function(params, prefix) {
  223. return params.filter((function(_this) {
  224. return function(param) {
  225. var current;
  226. current = vendor.prefix(_this.findProp(param));
  227. return current === '' || current === prefix;
  228. };
  229. })(this));
  230. };
  231. Transition.prototype.cleanFromUnprefixed = function(params, prefix) {
  232. var j, len, p, param, prop, remove, result;
  233. result = [];
  234. remove = params.map((function(_this) {
  235. return function(i) {
  236. return _this.findProp(i);
  237. };
  238. })(this)).filter(function(i) {
  239. return i.slice(0, prefix.length) === prefix;
  240. }).map((function(_this) {
  241. return function(i) {
  242. return _this.prefixes.unprefixed(i);
  243. };
  244. })(this));
  245. for (j = 0, len = params.length; j < len; j++) {
  246. param = params[j];
  247. prop = this.findProp(param);
  248. p = vendor.prefix(prop);
  249. if (remove.indexOf(prop) === -1 && (p === prefix || p === '')) {
  250. result.push(param);
  251. }
  252. }
  253. return result;
  254. };
  255. Transition.prototype.disabled = function(prop, prefix) {
  256. var other;
  257. other = ['order', 'justify-content', 'align-self', 'align-content'];
  258. if (prop.indexOf('flex') !== -1 || other.indexOf(prop) !== -1) {
  259. if (this.prefixes.options.flexbox === false) {
  260. return true;
  261. } else if (this.prefixes.options.flexbox === 'no-2009') {
  262. return prefix.indexOf('2009') !== -1;
  263. }
  264. }
  265. };
  266. return Transition;
  267. })();
  268. module.exports = Transition;
  269. }).call(this);