supports.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. (function() {
  2. var Prefixes, Supports, Value, findCondition, findDecl, list, postcss, split, utils;
  3. Prefixes = require('./prefixes');
  4. Value = require('./value');
  5. utils = require('./utils');
  6. postcss = require('postcss');
  7. list = require('postcss/lib/list');
  8. split = /\(\s*([^\(\):]+)\s*:([^\)]+)/;
  9. findDecl = /\(\s*([^\(\):]+)\s*:\s*(.+)\s*\)/g;
  10. findCondition = /(not\s*)?\(\s*([^\(\):]+)\s*:\s*(.+?(?!\s*or\s*).+?)\s*\)*\s*\)\s*or\s*/gi;
  11. Supports = (function() {
  12. function Supports(_at_all) {
  13. this.all = _at_all;
  14. }
  15. Supports.prototype.virtual = function(prop, value) {
  16. var rule;
  17. rule = postcss.parse('a{}').first;
  18. rule.append({
  19. prop: prop,
  20. value: value,
  21. before: ''
  22. });
  23. return rule;
  24. };
  25. Supports.prototype.prefixed = function(prop, value) {
  26. var decl, prefixer, rule, _i, _j, _len, _len1, _ref, _ref1;
  27. rule = this.virtual(prop, value);
  28. prefixer = this.all.add[prop];
  29. if (prefixer != null) {
  30. if (typeof prefixer.process === "function") {
  31. prefixer.process(rule.first);
  32. }
  33. }
  34. _ref = rule.nodes;
  35. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  36. decl = _ref[_i];
  37. _ref1 = this.all.values('add', prop);
  38. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  39. value = _ref1[_j];
  40. value.process(decl);
  41. }
  42. Value.save(this.all, decl);
  43. }
  44. return rule.nodes;
  45. };
  46. Supports.prototype.clean = function(params) {
  47. return params.replace(findCondition, (function(_this) {
  48. return function(all) {
  49. var check, checker, prop, unprefixed, value, _, _i, _len, _ref, _ref1, _ref2;
  50. if (all.slice(0, 3).toLowerCase() === 'not') {
  51. return all;
  52. }
  53. _ref = all.match(split), _ = _ref[0], prop = _ref[1], value = _ref[2];
  54. unprefixed = _this.all.unprefixed(prop);
  55. if ((_ref1 = _this.all.cleaner().remove[prop]) != null ? _ref1.remove : void 0) {
  56. check = new RegExp('(\\(|\\s)' + utils.escapeRegexp(unprefixed) + ':');
  57. if (check.test(params)) {
  58. return '';
  59. }
  60. }
  61. _ref2 = _this.all.cleaner().values('remove', unprefixed);
  62. for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  63. checker = _ref2[_i];
  64. if (checker.check(value)) {
  65. return '';
  66. }
  67. }
  68. return all;
  69. };
  70. })(this)).replace(/\(\s*\((.*)\)\s*\)/g, '($1)');
  71. };
  72. Supports.prototype.process = function(rule) {
  73. rule.params = this.clean(rule.params);
  74. return rule.params = rule.params.replace(findDecl, (function(_this) {
  75. return function(all, prop, value) {
  76. var i, stringed;
  77. stringed = (function() {
  78. var _i, _len, _ref, _results;
  79. _ref = this.prefixed(prop, value);
  80. _results = [];
  81. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  82. i = _ref[_i];
  83. _results.push("(" + i.prop + ": " + i.value + ")");
  84. }
  85. return _results;
  86. }).call(_this);
  87. if (stringed.length === 1) {
  88. return stringed[0];
  89. } else {
  90. return '(' + stringed.join(' or ') + ')';
  91. }
  92. };
  93. })(this));
  94. };
  95. return Supports;
  96. })();
  97. module.exports = Supports;
  98. }).call(this);