prefixes.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. (function() {
  2. var AtRule, Browsers, Declaration, Prefixes, Processor, Resolution, Selector, Supports, Value, declsCache, utils, vendor;
  3. Declaration = require('./declaration');
  4. Resolution = require('./resolution');
  5. Processor = require('./processor');
  6. Supports = require('./supports');
  7. Browsers = require('./browsers');
  8. Selector = require('./selector');
  9. AtRule = require('./at-rule');
  10. Value = require('./value');
  11. utils = require('./utils');
  12. vendor = require('postcss/lib/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/flex-flow'));
  19. Declaration.hack(require('./hacks/flex-grow'));
  20. Declaration.hack(require('./hacks/flex-wrap'));
  21. Declaration.hack(require('./hacks/align-self'));
  22. Declaration.hack(require('./hacks/flex-basis'));
  23. Declaration.hack(require('./hacks/align-items'));
  24. Declaration.hack(require('./hacks/flex-shrink'));
  25. Declaration.hack(require('./hacks/break-inside'));
  26. Declaration.hack(require('./hacks/border-image'));
  27. Declaration.hack(require('./hacks/align-content'));
  28. Declaration.hack(require('./hacks/border-radius'));
  29. Declaration.hack(require('./hacks/block-logical'));
  30. Declaration.hack(require('./hacks/inline-logical'));
  31. Declaration.hack(require('./hacks/transform-decl'));
  32. Declaration.hack(require('./hacks/flex-direction'));
  33. Declaration.hack(require('./hacks/image-rendering'));
  34. Declaration.hack(require('./hacks/justify-content'));
  35. Declaration.hack(require('./hacks/background-size'));
  36. Value.hack(require('./hacks/gradient'));
  37. Value.hack(require('./hacks/crisp-edges'));
  38. Value.hack(require('./hacks/flex-values'));
  39. Value.hack(require('./hacks/display-flex'));
  40. Value.hack(require('./hacks/filter-value'));
  41. Value.hack(require('./hacks/fill-available'));
  42. Value.hack(require('./hacks/transform-value'));
  43. declsCache = {};
  44. Prefixes = (function() {
  45. function Prefixes(_at_data, _at_browsers, _at_options) {
  46. var _ref;
  47. this.data = _at_data;
  48. this.browsers = _at_browsers;
  49. this.options = _at_options != null ? _at_options : {};
  50. _ref = this.preprocess(this.select(this.data)), this.add = _ref[0], this.remove = _ref[1];
  51. this.processor = new Processor(this);
  52. }
  53. Prefixes.prototype.transitionProps = ['transition', 'transition-property'];
  54. Prefixes.prototype.cleaner = function() {
  55. var empty;
  56. if (!this.cleanerCache) {
  57. if (this.browsers.selected.length) {
  58. empty = new Browsers(this.browsers.data, []);
  59. this.cleanerCache = new Prefixes(this.data, empty, this.options);
  60. } else {
  61. return this;
  62. }
  63. }
  64. return this.cleanerCache;
  65. };
  66. Prefixes.prototype.select = function(list) {
  67. var add, all, data, name, notes, selected;
  68. selected = {
  69. add: {},
  70. remove: {}
  71. };
  72. for (name in list) {
  73. data = list[name];
  74. add = data.browsers.map(function(i) {
  75. var params;
  76. params = i.split(' ');
  77. return {
  78. browser: params[0] + ' ' + params[1],
  79. note: params[2]
  80. };
  81. });
  82. notes = add.filter(function(i) {
  83. return i.note;
  84. }).map((function(_this) {
  85. return function(i) {
  86. return _this.browsers.prefix(i.browser) + ' ' + i.note;
  87. };
  88. })(this));
  89. notes = utils.uniq(notes);
  90. add = add.filter((function(_this) {
  91. return function(i) {
  92. return _this.browsers.isSelected(i.browser);
  93. };
  94. })(this)).map((function(_this) {
  95. return function(i) {
  96. var prefix;
  97. prefix = _this.browsers.prefix(i.browser);
  98. if (i.note) {
  99. return prefix + ' ' + i.note;
  100. } else {
  101. return prefix;
  102. }
  103. };
  104. })(this));
  105. add = this.sort(utils.uniq(add));
  106. all = data.browsers.map((function(_this) {
  107. return function(i) {
  108. return _this.browsers.prefix(i);
  109. };
  110. })(this));
  111. if (data.mistakes) {
  112. all = all.concat(data.mistakes);
  113. }
  114. all = all.concat(notes);
  115. all = utils.uniq(all);
  116. if (add.length) {
  117. selected.add[name] = add;
  118. if (add.length < all.length) {
  119. selected.remove[name] = all.filter(function(i) {
  120. return add.indexOf(i) === -1;
  121. });
  122. }
  123. } else {
  124. selected.remove[name] = all;
  125. }
  126. }
  127. return selected;
  128. };
  129. Prefixes.prototype.sort = function(prefixes) {
  130. return prefixes.sort(function(a, b) {
  131. var aLength, bLength;
  132. aLength = utils.removeNote(a).length;
  133. bLength = utils.removeNote(b).length;
  134. if (aLength === bLength) {
  135. return b.length - a.length;
  136. } else {
  137. return bLength - aLength;
  138. }
  139. });
  140. };
  141. Prefixes.prototype.preprocess = function(selected) {
  142. var add, name, old, olds, prefix, prefixed, prefixes, prop, props, remove, selector, value, values, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _len5, _len6, _m, _n, _o, _ref, _ref1, _ref2;
  143. add = {
  144. selectors: [],
  145. '@supports': new Supports(this)
  146. };
  147. _ref = selected.add;
  148. for (name in _ref) {
  149. prefixes = _ref[name];
  150. if (name === '@keyframes' || name === '@viewport') {
  151. add[name] = new AtRule(name, prefixes, this);
  152. } else if (name === '@resolution') {
  153. add[name] = new Resolution(name, prefixes, this);
  154. } else if (this.data[name].selector) {
  155. add.selectors.push(Selector.load(name, prefixes, this));
  156. } else {
  157. props = this.data[name].transition ? this.transitionProps : this.data[name].props;
  158. if (props) {
  159. value = Value.load(name, prefixes, this);
  160. for (_i = 0, _len = props.length; _i < _len; _i++) {
  161. prop = props[_i];
  162. if (!add[prop]) {
  163. add[prop] = {
  164. values: []
  165. };
  166. }
  167. add[prop].values.push(value);
  168. }
  169. }
  170. if (!this.data[name].props) {
  171. values = ((_ref1 = add[name]) != null ? _ref1.values : void 0) || [];
  172. add[name] = Declaration.load(name, prefixes, this);
  173. add[name].values = values;
  174. }
  175. }
  176. }
  177. remove = {
  178. selectors: []
  179. };
  180. _ref2 = selected.remove;
  181. for (name in _ref2) {
  182. prefixes = _ref2[name];
  183. if (this.data[name].selector) {
  184. selector = Selector.load(name, prefixes);
  185. for (_j = 0, _len1 = prefixes.length; _j < _len1; _j++) {
  186. prefix = prefixes[_j];
  187. remove.selectors.push(selector.old(prefix));
  188. }
  189. } else if (name === '@keyframes' || name === '@viewport') {
  190. for (_k = 0, _len2 = prefixes.length; _k < _len2; _k++) {
  191. prefix = prefixes[_k];
  192. prefixed = '@' + prefix + name.slice(1);
  193. remove[prefixed] = {
  194. remove: true
  195. };
  196. }
  197. } else if (name === '@resolution') {
  198. remove[name] = new Resolution(name, prefixes, this);
  199. } else {
  200. props = this.data[name].transition ? this.transitionProps : this.data[name].props;
  201. if (props) {
  202. value = Value.load(name, [], this);
  203. for (_l = 0, _len3 = prefixes.length; _l < _len3; _l++) {
  204. prefix = prefixes[_l];
  205. old = value.old(prefix);
  206. if (old) {
  207. for (_m = 0, _len4 = props.length; _m < _len4; _m++) {
  208. prop = props[_m];
  209. if (!remove[prop]) {
  210. remove[prop] = {};
  211. }
  212. if (!remove[prop].values) {
  213. remove[prop].values = [];
  214. }
  215. remove[prop].values.push(old);
  216. }
  217. }
  218. }
  219. }
  220. if (!this.data[name].props) {
  221. for (_n = 0, _len5 = prefixes.length; _n < _len5; _n++) {
  222. prefix = prefixes[_n];
  223. prop = vendor.unprefixed(name);
  224. olds = this.decl(name).old(name, prefix);
  225. for (_o = 0, _len6 = olds.length; _o < _len6; _o++) {
  226. prefixed = olds[_o];
  227. if (!remove[prefixed]) {
  228. remove[prefixed] = {};
  229. }
  230. remove[prefixed].remove = true;
  231. }
  232. }
  233. }
  234. }
  235. }
  236. return [add, remove];
  237. };
  238. Prefixes.prototype.decl = function(prop) {
  239. var decl;
  240. decl = declsCache[prop];
  241. if (decl) {
  242. return decl;
  243. } else {
  244. return declsCache[prop] = Declaration.load(prop);
  245. }
  246. };
  247. Prefixes.prototype.unprefixed = function(prop) {
  248. prop = vendor.unprefixed(prop);
  249. return this.decl(prop).normalize(prop);
  250. };
  251. Prefixes.prototype.prefixed = function(prop, prefix) {
  252. prop = vendor.unprefixed(prop);
  253. return this.decl(prop).prefixed(prop, prefix);
  254. };
  255. Prefixes.prototype.values = function(type, prop) {
  256. var data, global, values, _ref, _ref1;
  257. data = this[type];
  258. global = (_ref = data['*']) != null ? _ref.values : void 0;
  259. values = (_ref1 = data[prop]) != null ? _ref1.values : void 0;
  260. if (global && values) {
  261. return utils.uniq(global.concat(values));
  262. } else {
  263. return global || values || [];
  264. }
  265. };
  266. Prefixes.prototype.group = function(decl) {
  267. var checker, index, length, rule, unprefixed;
  268. rule = decl.parent;
  269. index = rule.index(decl);
  270. length = rule.nodes.length;
  271. unprefixed = this.unprefixed(decl.prop);
  272. checker = (function(_this) {
  273. return function(step, callback) {
  274. var other;
  275. index += step;
  276. while (index >= 0 && index < length) {
  277. other = rule.nodes[index];
  278. if (other.type === 'decl') {
  279. if (step === -1 && other.prop === unprefixed) {
  280. if (!Browsers.withPrefix(other.value)) {
  281. break;
  282. }
  283. }
  284. if (_this.unprefixed(other.prop) !== unprefixed) {
  285. break;
  286. } else if (callback(other) === true) {
  287. return true;
  288. }
  289. if (step === +1 && other.prop === unprefixed) {
  290. if (!Browsers.withPrefix(other.value)) {
  291. break;
  292. }
  293. }
  294. }
  295. index += step;
  296. }
  297. return false;
  298. };
  299. })(this);
  300. return {
  301. up: function(callback) {
  302. return checker(-1, callback);
  303. },
  304. down: function(callback) {
  305. return checker(+1, callback);
  306. }
  307. };
  308. };
  309. return Prefixes;
  310. })();
  311. module.exports = Prefixes;
  312. }).call(this);