processor.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 (notHack && !_this.withHackValue(decl)) {
  181. if (decl.raw('before').indexOf("\n") > -1) {
  182. _this.reduceSpaces(decl);
  183. }
  184. rule.removeChild(i);
  185. return;
  186. }
  187. }
  188. ref2 = _this.prefixes.values('remove', unprefixed);
  189. for (k = 0, len1 = ref2.length; k < len1; k++) {
  190. checker = ref2[k];
  191. if (checker.check(decl.value)) {
  192. unprefixed = checker.unprefixed;
  193. notHack = _this.prefixes.group(decl).down(function(other) {
  194. return other.value.indexOf(unprefixed) !== -1;
  195. });
  196. if (notHack) {
  197. rule.removeChild(i);
  198. return;
  199. } else if (checker.clean) {
  200. checker.clean(decl);
  201. return;
  202. }
  203. }
  204. }
  205. };
  206. })(this));
  207. };
  208. Processor.prototype.withHackValue = function(decl) {
  209. return decl.prop === '-webkit-background-clip' && decl.value === 'text';
  210. };
  211. Processor.prototype.disabled = function(node) {
  212. var other, status;
  213. if (this.prefixes.options.grid === false && node.type === 'decl') {
  214. if (node.prop === 'display' && node.value.indexOf('grid') !== -1) {
  215. return true;
  216. }
  217. if (node.prop.indexOf('grid') !== -1 || node.prop === 'justify-items') {
  218. return true;
  219. }
  220. }
  221. if (this.prefixes.options.flexbox === false && node.type === 'decl') {
  222. if (node.prop === 'display' && node.value.indexOf('flex') !== -1) {
  223. return true;
  224. }
  225. other = ['order', 'justify-content', 'align-items', 'align-content'];
  226. if (node.prop.indexOf('flex') !== -1 || other.indexOf(node.prop) !== -1) {
  227. return true;
  228. }
  229. }
  230. if (node._autoprefixerDisabled != null) {
  231. return node._autoprefixerDisabled;
  232. } else if (node.nodes) {
  233. status = void 0;
  234. node.each(function(i) {
  235. if (i.type !== 'comment') {
  236. return;
  237. }
  238. if (/(!\s*)?autoprefixer:\s*off/i.test(i.text)) {
  239. status = false;
  240. return false;
  241. } else if (/(!\s*)?autoprefixer:\s*on/i.test(i.text)) {
  242. status = true;
  243. return false;
  244. }
  245. });
  246. return node._autoprefixerDisabled = status != null ? !status : node.parent ? this.disabled(node.parent) : false;
  247. } else if (node.parent) {
  248. return node._autoprefixerDisabled = this.disabled(node.parent);
  249. } else {
  250. return false;
  251. }
  252. };
  253. Processor.prototype.reduceSpaces = function(decl) {
  254. var diff, parts, prevMin, stop;
  255. stop = false;
  256. this.prefixes.group(decl).up(function(other) {
  257. return stop = true;
  258. });
  259. if (stop) {
  260. return;
  261. }
  262. parts = decl.raw('before').split("\n");
  263. prevMin = parts[parts.length - 1].length;
  264. diff = false;
  265. return this.prefixes.group(decl).down(function(other) {
  266. var last;
  267. parts = other.raw('before').split("\n");
  268. last = parts.length - 1;
  269. if (parts[last].length > prevMin) {
  270. if (diff === false) {
  271. diff = parts[last].length - prevMin;
  272. }
  273. parts[last] = parts[last].slice(0, -diff);
  274. return other.raws.before = parts.join("\n");
  275. }
  276. });
  277. };
  278. Processor.prototype.displayType = function(decl) {
  279. var i, j, len, ref;
  280. ref = decl.parent.nodes;
  281. for (j = 0, len = ref.length; j < len; j++) {
  282. i = ref[j];
  283. if (i.prop === 'display') {
  284. if (i.value.indexOf('flex') !== -1) {
  285. return 'flex';
  286. } else if (i.value.indexOf('grid') !== -1) {
  287. return 'grid';
  288. }
  289. }
  290. }
  291. return false;
  292. };
  293. return Processor;
  294. })();
  295. module.exports = Processor;
  296. }).call(this);