processor.js 16 KB

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