No Description

processor.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. (function() {
  2. var Processor, Value, utils, vendor;
  3. vendor = require('postcss/lib/vendor');
  4. Value = require('./value');
  5. utils = require('./utils');
  6. Processor = (function() {
  7. function Processor(prefixes) {
  8. this.prefixes = prefixes;
  9. }
  10. Processor.prototype.add = function(css, result) {
  11. var keyframes, resolution, supports, viewport;
  12. resolution = this.prefixes.add['@resolution'];
  13. keyframes = this.prefixes.add['@keyframes'];
  14. viewport = this.prefixes.add['@viewport'];
  15. supports = this.prefixes.add['@supports'];
  16. css.eachAtRule((function(_this) {
  17. return function(rule) {
  18. if (rule.name === 'keyframes') {
  19. if (!_this.disabled(rule)) {
  20. return keyframes != null ? keyframes.process(rule) : void 0;
  21. }
  22. } else if (rule.name === 'viewport') {
  23. if (!_this.disabled(rule)) {
  24. return viewport != null ? viewport.process(rule) : void 0;
  25. }
  26. } else if (rule.name === 'supports') {
  27. if (!_this.disabled(rule)) {
  28. return supports.process(rule);
  29. }
  30. } else if (rule.name === 'media' && rule.params.indexOf('-resolution') !== -1) {
  31. if (!_this.disabled(rule)) {
  32. return resolution != null ? resolution.process(rule) : void 0;
  33. }
  34. }
  35. };
  36. })(this));
  37. css.eachRule((function(_this) {
  38. return function(rule) {
  39. var j, len, ref, results, selector;
  40. if (_this.disabled(rule)) {
  41. return;
  42. }
  43. ref = _this.prefixes.add.selectors;
  44. results = [];
  45. for (j = 0, len = ref.length; j < len; j++) {
  46. selector = ref[j];
  47. results.push(selector.process(rule, result));
  48. }
  49. return results;
  50. };
  51. })(this));
  52. css.eachDecl((function(_this) {
  53. return function(decl) {
  54. var prefix;
  55. if (decl.prop === 'display' && decl.value === 'box') {
  56. result.warn('You should write display: flex by final spec ' + 'instead of display: box', {
  57. node: decl
  58. });
  59. return;
  60. }
  61. prefix = _this.prefixes.add[decl.prop];
  62. if (prefix && prefix.prefixes) {
  63. if (!_this.disabled(decl)) {
  64. return prefix.process(decl, result);
  65. }
  66. }
  67. };
  68. })(this));
  69. return css.eachDecl((function(_this) {
  70. return function(decl) {
  71. var j, len, ref, unprefixed, value;
  72. if (_this.disabled(decl)) {
  73. return;
  74. }
  75. unprefixed = _this.prefixes.unprefixed(decl.prop);
  76. ref = _this.prefixes.values('add', unprefixed);
  77. for (j = 0, len = ref.length; j < len; j++) {
  78. value = ref[j];
  79. value.process(decl, result);
  80. }
  81. return Value.save(_this.prefixes, decl);
  82. };
  83. })(this));
  84. };
  85. Processor.prototype.remove = function(css) {
  86. var checker, j, len, ref, resolution;
  87. resolution = this.prefixes.remove['@resolution'];
  88. css.eachAtRule((function(_this) {
  89. return function(rule, i) {
  90. if (_this.prefixes.remove['@' + rule.name]) {
  91. if (!_this.disabled(rule)) {
  92. return rule.parent.remove(i);
  93. }
  94. } else if (rule.name === 'media' && rule.params.indexOf('-resolution') !== -1) {
  95. return resolution != null ? resolution.clean(rule) : void 0;
  96. }
  97. };
  98. })(this));
  99. ref = this.prefixes.remove.selectors;
  100. for (j = 0, len = ref.length; j < len; j++) {
  101. checker = ref[j];
  102. css.eachRule((function(_this) {
  103. return function(rule, i) {
  104. if (checker.check(rule)) {
  105. if (!_this.disabled(rule)) {
  106. return rule.parent.remove(i);
  107. }
  108. }
  109. };
  110. })(this));
  111. }
  112. return css.eachDecl((function(_this) {
  113. return function(decl, i) {
  114. var k, len1, notHack, ref1, ref2, rule, unprefixed;
  115. if (_this.disabled(decl)) {
  116. return;
  117. }
  118. rule = decl.parent;
  119. unprefixed = _this.prefixes.unprefixed(decl.prop);
  120. if ((ref1 = _this.prefixes.remove[decl.prop]) != null ? ref1.remove : void 0) {
  121. notHack = _this.prefixes.group(decl).down(function(other) {
  122. return other.prop === unprefixed;
  123. });
  124. if (notHack && !_this.withHackValue(decl)) {
  125. if (decl.style('before').indexOf("\n") > -1) {
  126. _this.reduceSpaces(decl);
  127. }
  128. rule.remove(i);
  129. return;
  130. }
  131. }
  132. ref2 = _this.prefixes.values('remove', unprefixed);
  133. for (k = 0, len1 = ref2.length; k < len1; k++) {
  134. checker = ref2[k];
  135. if (checker.check(decl.value)) {
  136. unprefixed = checker.unprefixed;
  137. notHack = _this.prefixes.group(decl).down(function(other) {
  138. return other.value.indexOf(unprefixed) !== -1;
  139. });
  140. if (notHack) {
  141. rule.remove(i);
  142. return;
  143. } else if (checker.clean) {
  144. checker.clean(decl);
  145. return;
  146. }
  147. }
  148. }
  149. };
  150. })(this));
  151. };
  152. Processor.prototype.withHackValue = function(decl) {
  153. return decl.prop === '-webkit-background-clip' && decl.value === 'text';
  154. };
  155. Processor.prototype.disabled = function(node) {
  156. var status;
  157. if (node._autoprefixerDisabled != null) {
  158. return node._autoprefixerDisabled;
  159. } else if (node.nodes) {
  160. status = void 0;
  161. node.each(function(i) {
  162. if (i.type !== 'comment') {
  163. return;
  164. }
  165. if (i.text === 'autoprefixer: off') {
  166. status = false;
  167. return false;
  168. } else if (i.text === 'autoprefixer: on') {
  169. status = true;
  170. return false;
  171. }
  172. });
  173. return node._autoprefixerDisabled = status != null ? !status : node.parent ? this.disabled(node.parent) : false;
  174. } else if (node.parent) {
  175. return node._autoprefixerDisabled = this.disabled(node.parent);
  176. } else {
  177. return false;
  178. }
  179. };
  180. Processor.prototype.reduceSpaces = function(decl) {
  181. var diff, parts, prevMin, stop;
  182. stop = false;
  183. this.prefixes.group(decl).up(function(other) {
  184. return stop = true;
  185. });
  186. if (stop) {
  187. return;
  188. }
  189. parts = decl.style('before').split("\n");
  190. prevMin = parts[parts.length - 1].length;
  191. diff = false;
  192. return this.prefixes.group(decl).down(function(other) {
  193. var last;
  194. parts = other.style('before').split("\n");
  195. last = parts.length - 1;
  196. if (parts[last].length > prevMin) {
  197. if (diff === false) {
  198. diff = parts[last].length - prevMin;
  199. }
  200. parts[last] = parts[last].slice(0, -diff);
  201. return other.before = parts.join("\n");
  202. }
  203. });
  204. };
  205. return Processor;
  206. })();
  207. module.exports = Processor;
  208. }).call(this);