processor.js 16 KB

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