processor.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. (function() {
  2. var OLD_DIRECTION, Processor, Value, utils, vendor;
  3. vendor = require('postcss/lib/vendor');
  4. Value = require('./value');
  5. utils = require('./utils');
  6. OLD_DIRECTION = /(^|[^-])(linear|radial)-gradient\(\s*(top|left|right|bottom)/i;
  7. Processor = (function() {
  8. function Processor(prefixes) {
  9. this.prefixes = prefixes;
  10. }
  11. Processor.prototype.add = function(css, result) {
  12. var keyframes, resolution, supports, viewport;
  13. resolution = this.prefixes.add['@resolution'];
  14. keyframes = this.prefixes.add['@keyframes'];
  15. viewport = this.prefixes.add['@viewport'];
  16. supports = this.prefixes.add['@supports'];
  17. css.walkAtRules((function(_this) {
  18. return function(rule) {
  19. if (rule.name === 'keyframes') {
  20. if (!_this.disabled(rule)) {
  21. return keyframes != null ? keyframes.process(rule) : void 0;
  22. }
  23. } else if (rule.name === 'viewport') {
  24. if (!_this.disabled(rule)) {
  25. return viewport != null ? viewport.process(rule) : void 0;
  26. }
  27. } else if (rule.name === 'supports') {
  28. if (_this.prefixes.options.supports !== false && !_this.disabled(rule)) {
  29. return supports.process(rule);
  30. }
  31. } else if (rule.name === 'media' && rule.params.indexOf('-resolution') !== -1) {
  32. if (!_this.disabled(rule)) {
  33. return resolution != null ? resolution.process(rule) : void 0;
  34. }
  35. }
  36. };
  37. })(this));
  38. css.walkRules((function(_this) {
  39. return function(rule) {
  40. var j, len, ref, results, selector;
  41. if (_this.disabled(rule)) {
  42. return;
  43. }
  44. ref = _this.prefixes.add.selectors;
  45. results = [];
  46. for (j = 0, len = ref.length; j < len; j++) {
  47. selector = ref[j];
  48. results.push(selector.process(rule, result));
  49. }
  50. return results;
  51. };
  52. })(this));
  53. css.walkDecls((function(_this) {
  54. return function(decl) {
  55. var display, prefixer;
  56. if (_this.disabled(decl)) {
  57. return;
  58. }
  59. if (decl.prop === 'display' && decl.value === 'box') {
  60. result.warn('You should write display: flex by final spec ' + 'instead of display: box', {
  61. node: decl
  62. });
  63. return;
  64. }
  65. if (decl.value.indexOf('linear-gradient') !== -1) {
  66. if (OLD_DIRECTION.test(decl.value)) {
  67. result.warn('Gradient has outdated direction syntax. ' + 'New syntax is like `to left` instead of `right`.', {
  68. node: decl
  69. });
  70. }
  71. }
  72. if (decl.prop === 'text-emphasis-position') {
  73. if (decl.value === 'under' || decl.value === 'over') {
  74. result.warn('You should use 2 values for text-emphasis-position ' + 'For example, `under left` instead of just `under`.', {
  75. node: decl
  76. });
  77. }
  78. }
  79. if (decl.value.indexOf('fill-available') !== -1) {
  80. result.warn('Replace fill-available to fill, ' + 'because spec had been changed', {
  81. node: decl
  82. });
  83. }
  84. if (_this.prefixes.options.flexbox !== false) {
  85. if (decl.prop === 'grid-row-end' && decl.value.indexOf('span') === -1) {
  86. result.warn('IE supports only grid-row-end with span. ' + 'You should add grid: false option to Autoprefixer ' + 'and use some JS grid polyfill for full spec support', {
  87. node: decl
  88. });
  89. }
  90. if (decl.prop === 'grid-row') {
  91. if (decl.value.indexOf('/') !== -1 && decl.value.indexOf('span') === -1) {
  92. result.warn('IE supports only grid-row with / and span. ' + 'You should add grid: false option to Autoprefixer ' + 'and use some JS grid polyfill for full spec support', {
  93. node: decl
  94. });
  95. }
  96. }
  97. }
  98. if (decl.prop === 'transition' || decl.prop === 'transition-property') {
  99. return _this.prefixes.transition.add(decl, result);
  100. } else if (decl.prop === 'align-self') {
  101. display = _this.displayType(decl);
  102. if (display !== 'grid' && _this.prefixes.options.flexbox !== false) {
  103. prefixer = _this.prefixes.add['align-self'];
  104. if (prefixer && prefixer.prefixes) {
  105. prefixer.process(decl);
  106. }
  107. }
  108. if (display !== 'flex' && _this.prefixes.options.grid !== false) {
  109. prefixer = _this.prefixes.add['grid-row-align'];
  110. if (prefixer && prefixer.prefixes) {
  111. return prefixer.process(decl);
  112. }
  113. }
  114. } else {
  115. prefixer = _this.prefixes.add[decl.prop];
  116. if (prefixer && prefixer.prefixes) {
  117. return prefixer.process(decl);
  118. }
  119. }
  120. };
  121. })(this));
  122. return css.walkDecls((function(_this) {
  123. return function(decl) {
  124. var j, len, ref, unprefixed, value;
  125. if (_this.disabled(decl)) {
  126. return;
  127. }
  128. unprefixed = _this.prefixes.unprefixed(decl.prop);
  129. ref = _this.prefixes.values('add', unprefixed);
  130. for (j = 0, len = ref.length; j < len; j++) {
  131. value = ref[j];
  132. value.process(decl, result);
  133. }
  134. return Value.save(_this.prefixes, decl);
  135. };
  136. })(this));
  137. };
  138. Processor.prototype.remove = function(css) {
  139. var checker, j, len, ref, resolution;
  140. resolution = this.prefixes.remove['@resolution'];
  141. css.walkAtRules((function(_this) {
  142. return function(rule, i) {
  143. if (_this.prefixes.remove['@' + rule.name]) {
  144. if (!_this.disabled(rule)) {
  145. return rule.parent.removeChild(i);
  146. }
  147. } else if (rule.name === 'media' && rule.params.indexOf('-resolution') !== -1) {
  148. return resolution != null ? resolution.clean(rule) : void 0;
  149. }
  150. };
  151. })(this));
  152. ref = this.prefixes.remove.selectors;
  153. for (j = 0, len = ref.length; j < len; j++) {
  154. checker = ref[j];
  155. css.walkRules((function(_this) {
  156. return function(rule, i) {
  157. if (checker.check(rule)) {
  158. if (!_this.disabled(rule)) {
  159. return rule.parent.removeChild(i);
  160. }
  161. }
  162. };
  163. })(this));
  164. }
  165. return css.walkDecls((function(_this) {
  166. return function(decl, i) {
  167. var k, len1, notHack, ref1, ref2, rule, unprefixed;
  168. if (_this.disabled(decl)) {
  169. return;
  170. }
  171. rule = decl.parent;
  172. unprefixed = _this.prefixes.unprefixed(decl.prop);
  173. if (decl.prop === 'transition' || decl.prop === 'transition-property') {
  174. _this.prefixes.transition.remove(decl);
  175. }
  176. if ((ref1 = _this.prefixes.remove[decl.prop]) != null ? ref1.remove : void 0) {
  177. notHack = _this.prefixes.group(decl).down(function(other) {
  178. return _this.prefixes.normalize(other.prop) === unprefixed;
  179. });
  180. if (unprefixed === 'flex-flow') {
  181. notHack = true;
  182. }
  183. if (notHack && !_this.withHackValue(decl)) {
  184. if (decl.raw('before').indexOf("\n") > -1) {
  185. _this.reduceSpaces(decl);
  186. }
  187. rule.removeChild(i);
  188. return;
  189. }
  190. }
  191. ref2 = _this.prefixes.values('remove', unprefixed);
  192. for (k = 0, len1 = ref2.length; k < len1; k++) {
  193. checker = ref2[k];
  194. if (checker.check(decl.value)) {
  195. unprefixed = checker.unprefixed;
  196. notHack = _this.prefixes.group(decl).down(function(other) {
  197. return other.value.indexOf(unprefixed) !== -1;
  198. });
  199. if (notHack) {
  200. rule.removeChild(i);
  201. return;
  202. } else if (checker.clean) {
  203. checker.clean(decl);
  204. return;
  205. }
  206. }
  207. }
  208. };
  209. })(this));
  210. };
  211. Processor.prototype.withHackValue = function(decl) {
  212. return decl.prop === '-webkit-background-clip' && decl.value === 'text';
  213. };
  214. Processor.prototype.disabled = function(node) {
  215. var other, status;
  216. if (this.prefixes.options.grid === false && node.type === 'decl') {
  217. if (node.prop === 'display' && node.value.indexOf('grid') !== -1) {
  218. return true;
  219. }
  220. if (node.prop.indexOf('grid') !== -1 || node.prop === 'justify-items') {
  221. return true;
  222. }
  223. }
  224. if (this.prefixes.options.flexbox === false && node.type === 'decl') {
  225. if (node.prop === 'display' && node.value.indexOf('flex') !== -1) {
  226. return true;
  227. }
  228. other = ['order', 'justify-content', 'align-items', 'align-content'];
  229. if (node.prop.indexOf('flex') !== -1 || other.indexOf(node.prop) !== -1) {
  230. return true;
  231. }
  232. }
  233. if (node._autoprefixerDisabled != null) {
  234. return node._autoprefixerDisabled;
  235. } else if (node.nodes) {
  236. status = void 0;
  237. node.each(function(i) {
  238. if (i.type !== 'comment') {
  239. return;
  240. }
  241. if (/(!\s*)?autoprefixer:\s*off/i.test(i.text)) {
  242. status = false;
  243. return false;
  244. } else if (/(!\s*)?autoprefixer:\s*on/i.test(i.text)) {
  245. status = true;
  246. return false;
  247. }
  248. });
  249. return node._autoprefixerDisabled = status != null ? !status : node.parent ? this.disabled(node.parent) : false;
  250. } else if (node.parent) {
  251. return node._autoprefixerDisabled = this.disabled(node.parent);
  252. } else {
  253. return false;
  254. }
  255. };
  256. Processor.prototype.reduceSpaces = function(decl) {
  257. var diff, parts, prevMin, stop;
  258. stop = false;
  259. this.prefixes.group(decl).up(function(other) {
  260. return stop = true;
  261. });
  262. if (stop) {
  263. return;
  264. }
  265. parts = decl.raw('before').split("\n");
  266. prevMin = parts[parts.length - 1].length;
  267. diff = false;
  268. return this.prefixes.group(decl).down(function(other) {
  269. var last;
  270. parts = other.raw('before').split("\n");
  271. last = parts.length - 1;
  272. if (parts[last].length > prevMin) {
  273. if (diff === false) {
  274. diff = parts[last].length - prevMin;
  275. }
  276. parts[last] = parts[last].slice(0, -diff);
  277. return other.raws.before = parts.join("\n");
  278. }
  279. });
  280. };
  281. Processor.prototype.displayType = function(decl) {
  282. var i, j, len, ref;
  283. ref = decl.parent.nodes;
  284. for (j = 0, len = ref.length; j < len; j++) {
  285. i = ref[j];
  286. if (i.prop === 'display') {
  287. if (i.value.indexOf('flex') !== -1) {
  288. return 'flex';
  289. } else if (i.value.indexOf('grid') !== -1) {
  290. return 'grid';
  291. }
  292. }
  293. }
  294. return false;
  295. };
  296. return Processor;
  297. })();
  298. module.exports = Processor;
  299. }).call(this);