prefixes.js 14 KB

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