prefixes.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. var Declaration = require('./declaration');
  4. var Resolution = require('./resolution');
  5. var Transition = require('./transition');
  6. var Processor = require('./processor');
  7. var Supports = require('./supports');
  8. var Browsers = require('./browsers');
  9. var Selector = require('./selector');
  10. var AtRule = require('./at-rule');
  11. var Value = require('./value');
  12. var utils = require('./utils');
  13. var vendor = require('postcss').vendor;
  14. Selector.hack(require('./hacks/fullscreen'));
  15. Selector.hack(require('./hacks/placeholder'));
  16. Declaration.hack(require('./hacks/flex'));
  17. Declaration.hack(require('./hacks/order'));
  18. Declaration.hack(require('./hacks/filter'));
  19. Declaration.hack(require('./hacks/grid-end'));
  20. Declaration.hack(require('./hacks/animation'));
  21. Declaration.hack(require('./hacks/flex-flow'));
  22. Declaration.hack(require('./hacks/flex-grow'));
  23. Declaration.hack(require('./hacks/flex-wrap'));
  24. Declaration.hack(require('./hacks/grid-area'));
  25. Declaration.hack(require('./hacks/grid-start'));
  26. Declaration.hack(require('./hacks/align-self'));
  27. Declaration.hack(require('./hacks/appearance'));
  28. Declaration.hack(require('./hacks/flex-basis'));
  29. Declaration.hack(require('./hacks/mask-border'));
  30. Declaration.hack(require('./hacks/align-items'));
  31. Declaration.hack(require('./hacks/flex-shrink'));
  32. Declaration.hack(require('./hacks/break-props'));
  33. Declaration.hack(require('./hacks/writing-mode'));
  34. Declaration.hack(require('./hacks/border-image'));
  35. Declaration.hack(require('./hacks/align-content'));
  36. Declaration.hack(require('./hacks/border-radius'));
  37. Declaration.hack(require('./hacks/block-logical'));
  38. Declaration.hack(require('./hacks/grid-template'));
  39. Declaration.hack(require('./hacks/inline-logical'));
  40. Declaration.hack(require('./hacks/grid-row-align'));
  41. Declaration.hack(require('./hacks/transform-decl'));
  42. Declaration.hack(require('./hacks/flex-direction'));
  43. Declaration.hack(require('./hacks/image-rendering'));
  44. Declaration.hack(require('./hacks/text-decoration'));
  45. Declaration.hack(require('./hacks/justify-content'));
  46. Declaration.hack(require('./hacks/background-size'));
  47. Declaration.hack(require('./hacks/grid-row-column'));
  48. Declaration.hack(require('./hacks/grid-rows-columns'));
  49. Declaration.hack(require('./hacks/grid-column-align'));
  50. Declaration.hack(require('./hacks/overscroll-behavior'));
  51. Declaration.hack(require('./hacks/grid-template-areas'));
  52. Declaration.hack(require('./hacks/text-emphasis-position'));
  53. Declaration.hack(require('./hacks/color-adjust'));
  54. Value.hack(require('./hacks/gradient'));
  55. Value.hack(require('./hacks/intrinsic'));
  56. Value.hack(require('./hacks/pixelated'));
  57. Value.hack(require('./hacks/image-set'));
  58. Value.hack(require('./hacks/cross-fade'));
  59. Value.hack(require('./hacks/display-flex'));
  60. Value.hack(require('./hacks/display-grid'));
  61. Value.hack(require('./hacks/filter-value'));
  62. var declsCache = {};
  63. var Prefixes = function () {
  64. function Prefixes(data, browsers) {
  65. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  66. _classCallCheck(this, Prefixes);
  67. this.data = data;
  68. this.browsers = browsers;
  69. this.options = options;
  70. var _preprocess = this.preprocess(this.select(this.data));
  71. this.add = _preprocess[0];
  72. this.remove = _preprocess[1];
  73. this.transition = new Transition(this);
  74. this.processor = new Processor(this);
  75. }
  76. /**
  77. * Return clone instance to remove all prefixes
  78. */
  79. Prefixes.prototype.cleaner = function cleaner() {
  80. if (this.cleanerCache) {
  81. return this.cleanerCache;
  82. }
  83. if (this.browsers.selected.length) {
  84. var empty = new Browsers(this.browsers.data, []);
  85. this.cleanerCache = new Prefixes(this.data, empty, this.options);
  86. } else {
  87. return this;
  88. }
  89. return this.cleanerCache;
  90. };
  91. /**
  92. * Select prefixes from data, which is necessary for selected browsers
  93. */
  94. Prefixes.prototype.select = function select(list) {
  95. var _this = this;
  96. var selected = { add: {}, remove: {} };
  97. var _loop = function _loop(name) {
  98. var data = list[name];
  99. var add = data.browsers.map(function (i) {
  100. var params = i.split(' ');
  101. return {
  102. browser: params[0] + ' ' + params[1],
  103. note: params[2]
  104. };
  105. });
  106. var notes = add.filter(function (i) {
  107. return i.note;
  108. }).map(function (i) {
  109. return _this.browsers.prefix(i.browser) + ' ' + i.note;
  110. });
  111. notes = utils.uniq(notes);
  112. add = add.filter(function (i) {
  113. return _this.browsers.isSelected(i.browser);
  114. }).map(function (i) {
  115. var prefix = _this.browsers.prefix(i.browser);
  116. if (i.note) {
  117. return prefix + ' ' + i.note;
  118. } else {
  119. return prefix;
  120. }
  121. });
  122. add = _this.sort(utils.uniq(add));
  123. if (_this.options.flexbox === 'no-2009') {
  124. add = add.filter(function (i) {
  125. return i.indexOf('2009') === -1;
  126. });
  127. }
  128. var all = data.browsers.map(function (i) {
  129. return _this.browsers.prefix(i);
  130. });
  131. if (data.mistakes) {
  132. all = all.concat(data.mistakes);
  133. }
  134. all = all.concat(notes);
  135. all = utils.uniq(all);
  136. if (add.length) {
  137. selected.add[name] = add;
  138. if (add.length < all.length) {
  139. selected.remove[name] = all.filter(function (i) {
  140. return add.indexOf(i) === -1;
  141. });
  142. }
  143. } else {
  144. selected.remove[name] = all;
  145. }
  146. };
  147. for (var name in list) {
  148. _loop(name);
  149. }
  150. return selected;
  151. };
  152. /**
  153. * Sort vendor prefixes
  154. */
  155. Prefixes.prototype.sort = function sort(prefixes) {
  156. return prefixes.sort(function (a, b) {
  157. var aLength = utils.removeNote(a).length;
  158. var bLength = utils.removeNote(b).length;
  159. if (aLength === bLength) {
  160. return b.length - a.length;
  161. } else {
  162. return bLength - aLength;
  163. }
  164. });
  165. };
  166. /**
  167. * Cache prefixes data to fast CSS processing
  168. */
  169. Prefixes.prototype.preprocess = function preprocess(selected) {
  170. var add = {
  171. 'selectors': [],
  172. '@supports': new Supports(Prefixes, this)
  173. };
  174. for (var name in selected.add) {
  175. var prefixes = selected.add[name];
  176. if (name === '@keyframes' || name === '@viewport') {
  177. add[name] = new AtRule(name, prefixes, this);
  178. } else if (name === '@resolution') {
  179. add[name] = new Resolution(name, prefixes, this);
  180. } else if (this.data[name].selector) {
  181. add.selectors.push(Selector.load(name, prefixes, this));
  182. } else {
  183. var props = this.data[name].props;
  184. if (props) {
  185. var value = Value.load(name, prefixes, this);
  186. for (var _iterator = props, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  187. var _ref;
  188. if (_isArray) {
  189. if (_i >= _iterator.length) break;
  190. _ref = _iterator[_i++];
  191. } else {
  192. _i = _iterator.next();
  193. if (_i.done) break;
  194. _ref = _i.value;
  195. }
  196. var prop = _ref;
  197. if (!add[prop]) {
  198. add[prop] = { values: [] };
  199. }
  200. add[prop].values.push(value);
  201. }
  202. } else {
  203. var values = add[name] && add[name].values || [];
  204. add[name] = Declaration.load(name, prefixes, this);
  205. add[name].values = values;
  206. }
  207. }
  208. }
  209. var remove = { selectors: [] };
  210. for (var _name in selected.remove) {
  211. var _prefixes = selected.remove[_name];
  212. if (this.data[_name].selector) {
  213. var selector = Selector.load(_name, _prefixes);
  214. for (var _iterator2 = _prefixes, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  215. var _ref2;
  216. if (_isArray2) {
  217. if (_i2 >= _iterator2.length) break;
  218. _ref2 = _iterator2[_i2++];
  219. } else {
  220. _i2 = _iterator2.next();
  221. if (_i2.done) break;
  222. _ref2 = _i2.value;
  223. }
  224. var prefix = _ref2;
  225. remove.selectors.push(selector.old(prefix));
  226. }
  227. } else if (_name === '@keyframes' || _name === '@viewport') {
  228. for (var _iterator3 = _prefixes, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
  229. var _ref3;
  230. if (_isArray3) {
  231. if (_i3 >= _iterator3.length) break;
  232. _ref3 = _iterator3[_i3++];
  233. } else {
  234. _i3 = _iterator3.next();
  235. if (_i3.done) break;
  236. _ref3 = _i3.value;
  237. }
  238. var _prefix = _ref3;
  239. var prefixed = '@' + _prefix + _name.slice(1);
  240. remove[prefixed] = { remove: true };
  241. }
  242. } else if (_name === '@resolution') {
  243. remove[_name] = new Resolution(_name, _prefixes, this);
  244. } else {
  245. var _props = this.data[_name].props;
  246. if (_props) {
  247. var _value = Value.load(_name, [], this);
  248. for (var _iterator4 = _prefixes, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
  249. var _ref4;
  250. if (_isArray4) {
  251. if (_i4 >= _iterator4.length) break;
  252. _ref4 = _iterator4[_i4++];
  253. } else {
  254. _i4 = _iterator4.next();
  255. if (_i4.done) break;
  256. _ref4 = _i4.value;
  257. }
  258. var _prefix2 = _ref4;
  259. var old = _value.old(_prefix2);
  260. if (old) {
  261. for (var _iterator5 = _props, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
  262. var _ref5;
  263. if (_isArray5) {
  264. if (_i5 >= _iterator5.length) break;
  265. _ref5 = _iterator5[_i5++];
  266. } else {
  267. _i5 = _iterator5.next();
  268. if (_i5.done) break;
  269. _ref5 = _i5.value;
  270. }
  271. var _prop = _ref5;
  272. if (!remove[_prop]) {
  273. remove[_prop] = {};
  274. }
  275. if (!remove[_prop].values) {
  276. remove[_prop].values = [];
  277. }
  278. remove[_prop].values.push(old);
  279. }
  280. }
  281. }
  282. } else {
  283. for (var _iterator6 = _prefixes, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
  284. var _ref6;
  285. if (_isArray6) {
  286. if (_i6 >= _iterator6.length) break;
  287. _ref6 = _iterator6[_i6++];
  288. } else {
  289. _i6 = _iterator6.next();
  290. if (_i6.done) break;
  291. _ref6 = _i6.value;
  292. }
  293. var _prefix3 = _ref6;
  294. var olds = this.decl(_name).old(_name, _prefix3);
  295. if (_name === 'align-self') {
  296. var a = add[_name] && add[_name].prefixes;
  297. if (a) {
  298. if (_prefix3 === '-webkit- 2009' && a.indexOf('-webkit-') !== -1) {
  299. continue;
  300. } else if (_prefix3 === '-webkit-' && a.indexOf('-webkit- 2009') !== -1) {
  301. continue;
  302. }
  303. }
  304. }
  305. for (var _iterator7 = olds, _isArray7 = Array.isArray(_iterator7), _i7 = 0, _iterator7 = _isArray7 ? _iterator7 : _iterator7[Symbol.iterator]();;) {
  306. var _ref7;
  307. if (_isArray7) {
  308. if (_i7 >= _iterator7.length) break;
  309. _ref7 = _iterator7[_i7++];
  310. } else {
  311. _i7 = _iterator7.next();
  312. if (_i7.done) break;
  313. _ref7 = _i7.value;
  314. }
  315. var _prefixed = _ref7;
  316. if (!remove[_prefixed]) {
  317. remove[_prefixed] = {};
  318. }
  319. remove[_prefixed].remove = true;
  320. }
  321. }
  322. }
  323. }
  324. }
  325. return [add, remove];
  326. };
  327. /**
  328. * Declaration loader with caching
  329. */
  330. Prefixes.prototype.decl = function decl(prop) {
  331. var decl = declsCache[prop];
  332. if (decl) {
  333. return decl;
  334. } else {
  335. declsCache[prop] = Declaration.load(prop);
  336. return declsCache[prop];
  337. }
  338. };
  339. /**
  340. * Return unprefixed version of property
  341. */
  342. Prefixes.prototype.unprefixed = function unprefixed(prop) {
  343. var value = this.normalize(vendor.unprefixed(prop));
  344. if (value === 'flex-direction') {
  345. value = 'flex-flow';
  346. }
  347. return value;
  348. };
  349. /**
  350. * Normalize prefix for remover
  351. */
  352. Prefixes.prototype.normalize = function normalize(prop) {
  353. return this.decl(prop).normalize(prop);
  354. };
  355. /**
  356. * Return prefixed version of property
  357. */
  358. Prefixes.prototype.prefixed = function prefixed(prop, prefix) {
  359. prop = vendor.unprefixed(prop);
  360. return this.decl(prop).prefixed(prop, prefix);
  361. };
  362. /**
  363. * Return values, which must be prefixed in selected property
  364. */
  365. Prefixes.prototype.values = function values(type, prop) {
  366. var data = this[type];
  367. var global = data['*'] && data['*'].values;
  368. var values = data[prop] && data[prop].values;
  369. if (global && values) {
  370. return utils.uniq(global.concat(values));
  371. } else {
  372. return global || values || [];
  373. }
  374. };
  375. /**
  376. * Group declaration by unprefixed property to check them
  377. */
  378. Prefixes.prototype.group = function group(decl) {
  379. var _this2 = this;
  380. var rule = decl.parent;
  381. var index = rule.index(decl);
  382. var length = rule.nodes.length;
  383. var unprefixed = this.unprefixed(decl.prop);
  384. var checker = function checker(step, callback) {
  385. index += step;
  386. while (index >= 0 && index < length) {
  387. var other = rule.nodes[index];
  388. if (other.type === 'decl') {
  389. if (step === -1 && other.prop === unprefixed) {
  390. if (!Browsers.withPrefix(other.value)) {
  391. break;
  392. }
  393. }
  394. if (_this2.unprefixed(other.prop) !== unprefixed) {
  395. break;
  396. } else if (callback(other) === true) {
  397. return true;
  398. }
  399. if (step === +1 && other.prop === unprefixed) {
  400. if (!Browsers.withPrefix(other.value)) {
  401. break;
  402. }
  403. }
  404. }
  405. index += step;
  406. }
  407. return false;
  408. };
  409. return {
  410. up: function up(callback) {
  411. return checker(-1, callback);
  412. },
  413. down: function down(callback) {
  414. return checker(+1, callback);
  415. }
  416. };
  417. };
  418. return Prefixes;
  419. }();
  420. module.exports = Prefixes;