prefixes.js 15 KB

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