prefixes.js 12 KB

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