prefixes.js 14 KB

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