You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

542 lines
15 KiB

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