processor.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. var Value = require('./value');
  4. var OLD_LINEAR = /(^|[^-])linear-gradient\(\s*(top|left|right|bottom)/i;
  5. var OLD_RADIAL = /(^|[^-])radial-gradient\(\s*\d+(\w*|%)\s+\d+(\w*|%)\s*,/i;
  6. var RADIAL_BLOCK = /\(((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*)\)/i;
  7. var IGNORE_NEXT = /(!\s*)?autoprefixer:\s*ignore\s+next/i;
  8. var SIZES = ['width', 'height', 'min-width', 'max-width', 'min-height', 'max-height', 'inline-size', 'min-inline-size', 'max-inline-size', 'block-size', 'min-block-size', 'max-block-size'];
  9. var Processor = function () {
  10. function Processor(prefixes) {
  11. _classCallCheck(this, Processor);
  12. this.prefixes = prefixes;
  13. }
  14. /**
  15. * Add necessary prefixes
  16. */
  17. Processor.prototype.add = function add(css, result) {
  18. var _this = this;
  19. // At-rules
  20. var resolution = this.prefixes.add['@resolution'];
  21. var keyframes = this.prefixes.add['@keyframes'];
  22. var viewport = this.prefixes.add['@viewport'];
  23. var supports = this.prefixes.add['@supports'];
  24. css.walkAtRules(function (rule) {
  25. if (rule.name === 'keyframes') {
  26. if (!_this.disabled(rule, result)) {
  27. return keyframes && keyframes.process(rule);
  28. }
  29. } else if (rule.name === 'viewport') {
  30. if (!_this.disabled(rule, result)) {
  31. return viewport && viewport.process(rule);
  32. }
  33. } else if (rule.name === 'supports') {
  34. if (_this.prefixes.options.supports !== false && !_this.disabled(rule, result)) {
  35. return supports.process(rule);
  36. }
  37. } else if (rule.name === 'media' && rule.params.indexOf('-resolution') !== -1) {
  38. if (!_this.disabled(rule, result)) {
  39. return resolution && resolution.process(rule);
  40. }
  41. }
  42. return undefined;
  43. });
  44. // Selectors
  45. css.walkRules(function (rule) {
  46. if (_this.disabled(rule, result)) return undefined;
  47. return _this.prefixes.add.selectors.map(function (selector) {
  48. return selector.process(rule, result);
  49. });
  50. });
  51. css.walkDecls(function (decl) {
  52. if (_this.disabledDecl(decl, result)) return undefined;
  53. var prop = decl.prop;
  54. var value = decl.value;
  55. if (prop === 'grid-row-span') {
  56. result.warn('grid-row-span is not part of final Grid Layout. ' + 'Use grid-row.', { node: decl });
  57. return undefined;
  58. } else if (prop === 'grid-column-span') {
  59. result.warn('grid-column-span is not part of final Grid Layout. ' + 'Use grid-column.', { node: decl });
  60. return undefined;
  61. } else if (prop === 'display' && value === 'box') {
  62. result.warn('You should write display: flex by final spec ' + 'instead of display: box', { node: decl });
  63. return undefined;
  64. } else if (prop === 'text-emphasis-position') {
  65. if (value === 'under' || value === 'over') {
  66. result.warn('You should use 2 values for text-emphasis-position ' + 'For example, `under left` instead of just `under`.', { node: decl });
  67. }
  68. } else {
  69. var grid = _this.prefixes.add['grid-area'];
  70. if (_this.prefixes.options.grid && grid && grid.prefixes) {
  71. if (prop === 'grid-auto-columns') {
  72. result.warn('grid-auto-columns is not supported by IE', { node: decl });
  73. return undefined;
  74. } else if (prop === 'grid-auto-rows') {
  75. result.warn('grid-auto-rows is not supported by IE', { node: decl });
  76. return undefined;
  77. } else if (prop === 'grid-auto-flow') {
  78. result.warn('grid-auto-flow is not supported by IE', { node: decl });
  79. return undefined;
  80. } else if (value.indexOf('auto-fit') !== -1) {
  81. result.warn('auto-fit value is not supported by IE', { node: decl, word: 'auto-fit' });
  82. return undefined;
  83. } else if (value.indexOf('auto-fill') !== -1) {
  84. result.warn('auto-fill value is not supported by IE', { node: decl, word: 'auto-fill' });
  85. return undefined;
  86. }
  87. }
  88. if (value.indexOf('radial-gradient') !== -1) {
  89. if (OLD_RADIAL.test(decl.value)) {
  90. result.warn('Gradient has outdated direction syntax. ' + 'New syntax is like `closest-side at 0 0` ' + 'instead of `0 0, closest-side`.', { node: decl });
  91. } else {
  92. var match = value.match(RADIAL_BLOCK);
  93. if (match) {
  94. if (/cover/.test(match[1])) {
  95. result.warn('Gradient has outdated direction syntax. ' + 'Replace `cover` to `farthest-corner`.', { node: decl });
  96. } else if (/contain/.test(match[1])) {
  97. result.warn('Gradient has outdated direction syntax. ' + 'Replace `contain` to `closest-side`.', { node: decl });
  98. }
  99. }
  100. }
  101. }
  102. if (value.indexOf('linear-gradient') !== -1) {
  103. if (OLD_LINEAR.test(value)) {
  104. result.warn('Gradient has outdated direction syntax. ' + 'New syntax is like `to left` instead of `right`.', { node: decl });
  105. }
  106. }
  107. }
  108. if (SIZES.indexOf(decl.prop) !== -1) {
  109. if (decl.value.indexOf('-fill-available') === -1) {
  110. if (decl.value.indexOf('fill-available') !== -1) {
  111. result.warn('Replace fill-available to stretch, ' + 'because spec had been changed', { node: decl });
  112. } else if (decl.value.indexOf('fill') !== -1) {
  113. result.warn('Replace fill to stretch, ' + 'because spec had been changed', { node: decl });
  114. }
  115. }
  116. }
  117. var prefixer = void 0;
  118. if (decl.prop === 'transition' || decl.prop === 'transition-property') {
  119. // Transition
  120. return _this.prefixes.transition.add(decl, result);
  121. } else if (decl.prop === 'align-self') {
  122. // align-self flexbox or grid
  123. var display = _this.displayType(decl);
  124. if (display !== 'grid' && _this.prefixes.options.flexbox !== false) {
  125. prefixer = _this.prefixes.add['align-self'];
  126. if (prefixer && prefixer.prefixes) {
  127. prefixer.process(decl);
  128. }
  129. }
  130. if (display !== 'flex' && _this.prefixes.options.grid !== false) {
  131. prefixer = _this.prefixes.add['grid-row-align'];
  132. if (prefixer && prefixer.prefixes) {
  133. return prefixer.process(decl, result);
  134. }
  135. }
  136. } else if (decl.prop === 'justify-self') {
  137. // justify-self flexbox or grid
  138. var _display = _this.displayType(decl);
  139. if (_display !== 'flex' && _this.prefixes.options.grid !== false) {
  140. prefixer = _this.prefixes.add['grid-column-align'];
  141. if (prefixer && prefixer.prefixes) {
  142. return prefixer.process(decl, result);
  143. }
  144. }
  145. } else {
  146. // Properties
  147. prefixer = _this.prefixes.add[decl.prop];
  148. if (prefixer && prefixer.prefixes) {
  149. return prefixer.process(decl, result);
  150. }
  151. }
  152. return undefined;
  153. });
  154. // Values
  155. return css.walkDecls(function (decl) {
  156. if (_this.disabledValue(decl, result)) return;
  157. var unprefixed = _this.prefixes.unprefixed(decl.prop);
  158. for (var _iterator = _this.prefixes.values('add', unprefixed), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  159. var _ref;
  160. if (_isArray) {
  161. if (_i >= _iterator.length) break;
  162. _ref = _iterator[_i++];
  163. } else {
  164. _i = _iterator.next();
  165. if (_i.done) break;
  166. _ref = _i.value;
  167. }
  168. var value = _ref;
  169. value.process(decl, result);
  170. }
  171. Value.save(_this.prefixes, decl);
  172. });
  173. };
  174. /**
  175. * Remove unnecessary pefixes
  176. */
  177. Processor.prototype.remove = function remove(css, result) {
  178. var _this2 = this;
  179. // At-rules
  180. var resolution = this.prefixes.remove['@resolution'];
  181. css.walkAtRules(function (rule, i) {
  182. if (_this2.prefixes.remove['@' + rule.name]) {
  183. if (!_this2.disabled(rule, result)) {
  184. rule.parent.removeChild(i);
  185. }
  186. } else if (rule.name === 'media' && rule.params.indexOf('-resolution') !== -1 && resolution) {
  187. resolution.clean(rule);
  188. }
  189. });
  190. // Selectors
  191. var _loop = function _loop(checker) {
  192. css.walkRules(function (rule, i) {
  193. if (checker.check(rule)) {
  194. if (!_this2.disabled(rule, result)) {
  195. rule.parent.removeChild(i);
  196. }
  197. }
  198. });
  199. };
  200. for (var _iterator2 = this.prefixes.remove.selectors, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  201. var _ref2;
  202. if (_isArray2) {
  203. if (_i2 >= _iterator2.length) break;
  204. _ref2 = _iterator2[_i2++];
  205. } else {
  206. _i2 = _iterator2.next();
  207. if (_i2.done) break;
  208. _ref2 = _i2.value;
  209. }
  210. var checker = _ref2;
  211. _loop(checker);
  212. }
  213. return css.walkDecls(function (decl, i) {
  214. if (_this2.disabled(decl, result)) return;
  215. var rule = decl.parent;
  216. var unprefixed = _this2.prefixes.unprefixed(decl.prop);
  217. // Transition
  218. if (decl.prop === 'transition' || decl.prop === 'transition-property') {
  219. _this2.prefixes.transition.remove(decl);
  220. }
  221. // Properties
  222. if (_this2.prefixes.remove[decl.prop] && _this2.prefixes.remove[decl.prop].remove) {
  223. var notHack = _this2.prefixes.group(decl).down(function (other) {
  224. return _this2.prefixes.normalize(other.prop) === unprefixed;
  225. });
  226. if (unprefixed === 'flex-flow') {
  227. notHack = true;
  228. }
  229. if (notHack && !_this2.withHackValue(decl)) {
  230. if (decl.raw('before').indexOf('\n') > -1) {
  231. _this2.reduceSpaces(decl);
  232. }
  233. rule.removeChild(i);
  234. return;
  235. }
  236. }
  237. // Values
  238. for (var _iterator3 = _this2.prefixes.values('remove', unprefixed), _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
  239. var _ref3;
  240. if (_isArray3) {
  241. if (_i3 >= _iterator3.length) break;
  242. _ref3 = _iterator3[_i3++];
  243. } else {
  244. _i3 = _iterator3.next();
  245. if (_i3.done) break;
  246. _ref3 = _i3.value;
  247. }
  248. var checker = _ref3;
  249. if (!checker.check(decl.value)) {
  250. continue;
  251. }
  252. unprefixed = checker.unprefixed;
  253. var _notHack = _this2.prefixes.group(decl).down(function (other) {
  254. return other.value.indexOf(unprefixed) !== -1;
  255. });
  256. if (_notHack) {
  257. rule.removeChild(i);
  258. return;
  259. }
  260. }
  261. });
  262. };
  263. /**
  264. * Some rare old values, which is not in standard
  265. */
  266. Processor.prototype.withHackValue = function withHackValue(decl) {
  267. return decl.prop === '-webkit-background-clip' && decl.value === 'text';
  268. };
  269. /**
  270. * Check for grid/flexbox options.
  271. */
  272. Processor.prototype.disabledValue = function disabledValue(node, result) {
  273. if (this.prefixes.options.grid === false && node.type === 'decl') {
  274. if (node.prop === 'display' && node.value.indexOf('grid') !== -1) {
  275. return true;
  276. }
  277. }
  278. if (this.prefixes.options.flexbox === false && node.type === 'decl') {
  279. if (node.prop === 'display' && node.value.indexOf('flex') !== -1) {
  280. return true;
  281. }
  282. }
  283. return this.disabled(node, result);
  284. };
  285. /**
  286. * Check for grid/flexbox options.
  287. */
  288. Processor.prototype.disabledDecl = function disabledDecl(node, result) {
  289. if (this.prefixes.options.grid === false && node.type === 'decl') {
  290. if (node.prop.indexOf('grid') !== -1 || node.prop === 'justify-items') {
  291. return true;
  292. }
  293. }
  294. if (this.prefixes.options.flexbox === false && node.type === 'decl') {
  295. var other = ['order', 'justify-content', 'align-items', 'align-content'];
  296. if (node.prop.indexOf('flex') !== -1 || other.indexOf(node.prop) !== -1) {
  297. return true;
  298. }
  299. }
  300. return this.disabled(node, result);
  301. };
  302. /**
  303. * Check for control comment and global options
  304. */
  305. Processor.prototype.disabled = function disabled(node, result) {
  306. if (!node) return false;
  307. if (node._autoprefixerDisabled !== undefined) {
  308. return node._autoprefixerDisabled;
  309. }
  310. if (node.parent) {
  311. var p = node.prev();
  312. if (p && p.type === 'comment' && IGNORE_NEXT.test(p.text)) {
  313. node._autoprefixerDisabled = true;
  314. node._autoprefixerSelfDisabled = true;
  315. return true;
  316. }
  317. }
  318. var value = null;
  319. if (node.nodes) {
  320. var status = undefined;
  321. node.each(function (i) {
  322. if (i.type !== 'comment') return;
  323. if (/(!\s*)?autoprefixer:\s*(off|on)/i.test(i.text)) {
  324. if (typeof status !== 'undefined') {
  325. result.warn('Second Autoprefixer control comment ' + 'was ignored. Autoprefixer applies control ' + 'comment to whole block, not to next rules.', { node: i });
  326. } else {
  327. status = /on/i.test(i.text);
  328. }
  329. }
  330. });
  331. if (status !== undefined) {
  332. value = !status;
  333. }
  334. }
  335. if (!node.nodes || value === null) {
  336. if (node.parent) {
  337. var isParentDisabled = this.disabled(node.parent, result);
  338. if (node.parent._autoprefixerSelfDisabled === true) {
  339. value = false;
  340. } else {
  341. value = isParentDisabled;
  342. }
  343. } else {
  344. value = false;
  345. }
  346. }
  347. node._autoprefixerDisabled = value;
  348. return value;
  349. };
  350. /**
  351. * Normalize spaces in cascade declaration group
  352. */
  353. Processor.prototype.reduceSpaces = function reduceSpaces(decl) {
  354. var stop = false;
  355. this.prefixes.group(decl).up(function () {
  356. stop = true;
  357. return true;
  358. });
  359. if (stop) {
  360. return;
  361. }
  362. var parts = decl.raw('before').split('\n');
  363. var prevMin = parts[parts.length - 1].length;
  364. var diff = false;
  365. this.prefixes.group(decl).down(function (other) {
  366. parts = other.raw('before').split('\n');
  367. var last = parts.length - 1;
  368. if (parts[last].length > prevMin) {
  369. if (diff === false) {
  370. diff = parts[last].length - prevMin;
  371. }
  372. parts[last] = parts[last].slice(0, -diff);
  373. other.raws.before = parts.join('\n');
  374. }
  375. });
  376. };
  377. /**
  378. * Is it flebox or grid rule
  379. */
  380. Processor.prototype.displayType = function displayType(decl) {
  381. for (var _iterator4 = decl.parent.nodes, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
  382. var _ref4;
  383. if (_isArray4) {
  384. if (_i4 >= _iterator4.length) break;
  385. _ref4 = _iterator4[_i4++];
  386. } else {
  387. _i4 = _iterator4.next();
  388. if (_i4.done) break;
  389. _ref4 = _i4.value;
  390. }
  391. var i = _ref4;
  392. if (i.prop !== 'display') {
  393. continue;
  394. }
  395. if (i.value.indexOf('flex') !== -1) {
  396. return 'flex';
  397. }
  398. if (i.value.indexOf('grid') !== -1) {
  399. return 'grid';
  400. }
  401. }
  402. return false;
  403. };
  404. return Processor;
  405. }();
  406. module.exports = Processor;