declaration.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  4. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  5. var Prefixer = require('./prefixer');
  6. var Browsers = require('./browsers');
  7. var utils = require('./utils');
  8. var Declaration = function (_Prefixer) {
  9. _inherits(Declaration, _Prefixer);
  10. function Declaration() {
  11. _classCallCheck(this, Declaration);
  12. return _possibleConstructorReturn(this, _Prefixer.apply(this, arguments));
  13. }
  14. /**
  15. * Always true, because we already get prefixer by property name
  16. */
  17. Declaration.prototype.check = function check() /* decl */{
  18. return true;
  19. };
  20. /**
  21. * Return prefixed version of property
  22. */
  23. Declaration.prototype.prefixed = function prefixed(prop, prefix) {
  24. return prefix + prop;
  25. };
  26. /**
  27. * Return unprefixed version of property
  28. */
  29. Declaration.prototype.normalize = function normalize(prop) {
  30. return prop;
  31. };
  32. /**
  33. * Check `value`, that it contain other prefixes, rather than `prefix`
  34. */
  35. Declaration.prototype.otherPrefixes = function otherPrefixes(value, prefix) {
  36. for (var _iterator = Browsers.prefixes(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  37. var _ref;
  38. if (_isArray) {
  39. if (_i >= _iterator.length) break;
  40. _ref = _iterator[_i++];
  41. } else {
  42. _i = _iterator.next();
  43. if (_i.done) break;
  44. _ref = _i.value;
  45. }
  46. var other = _ref;
  47. if (other === prefix) {
  48. continue;
  49. }
  50. if (value.indexOf(other) !== -1) {
  51. return true;
  52. }
  53. }
  54. return false;
  55. };
  56. /**
  57. * Set prefix to declaration
  58. */
  59. Declaration.prototype.set = function set(decl, prefix) {
  60. decl.prop = this.prefixed(decl.prop, prefix);
  61. return decl;
  62. };
  63. /**
  64. * Should we use visual cascade for prefixes
  65. */
  66. Declaration.prototype.needCascade = function needCascade(decl) {
  67. if (!decl._autoprefixerCascade) {
  68. decl._autoprefixerCascade = this.all.options.cascade !== false && decl.raw('before').indexOf('\n') !== -1;
  69. }
  70. return decl._autoprefixerCascade;
  71. };
  72. /**
  73. * Return maximum length of possible prefixed property
  74. */
  75. Declaration.prototype.maxPrefixed = function maxPrefixed(prefixes, decl) {
  76. if (decl._autoprefixerMax) {
  77. return decl._autoprefixerMax;
  78. }
  79. var max = 0;
  80. for (var _iterator2 = prefixes, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  81. var _ref2;
  82. if (_isArray2) {
  83. if (_i2 >= _iterator2.length) break;
  84. _ref2 = _iterator2[_i2++];
  85. } else {
  86. _i2 = _iterator2.next();
  87. if (_i2.done) break;
  88. _ref2 = _i2.value;
  89. }
  90. var prefix = _ref2;
  91. prefix = utils.removeNote(prefix);
  92. if (prefix.length > max) {
  93. max = prefix.length;
  94. }
  95. }
  96. decl._autoprefixerMax = max;
  97. return decl._autoprefixerMax;
  98. };
  99. /**
  100. * Calculate indentation to create visual cascade
  101. */
  102. Declaration.prototype.calcBefore = function calcBefore(prefixes, decl) {
  103. var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
  104. var max = this.maxPrefixed(prefixes, decl);
  105. var diff = max - utils.removeNote(prefix).length;
  106. var before = decl.raw('before');
  107. if (diff > 0) {
  108. before += Array(diff).fill(' ').join('');
  109. }
  110. return before;
  111. };
  112. /**
  113. * Remove visual cascade
  114. */
  115. Declaration.prototype.restoreBefore = function restoreBefore(decl) {
  116. var lines = decl.raw('before').split('\n');
  117. var min = lines[lines.length - 1];
  118. this.all.group(decl).up(function (prefixed) {
  119. var array = prefixed.raw('before').split('\n');
  120. var last = array[array.length - 1];
  121. if (last.length < min.length) {
  122. min = last;
  123. }
  124. });
  125. lines[lines.length - 1] = min;
  126. decl.raws.before = lines.join('\n');
  127. };
  128. /**
  129. * Clone and insert new declaration
  130. */
  131. Declaration.prototype.insert = function insert(decl, prefix, prefixes) {
  132. var cloned = this.set(this.clone(decl), prefix);
  133. if (!cloned) return undefined;
  134. var already = decl.parent.some(function (i) {
  135. return i.prop === cloned.prop && i.value === cloned.value;
  136. });
  137. if (already) {
  138. return undefined;
  139. }
  140. if (this.needCascade(decl)) {
  141. cloned.raws.before = this.calcBefore(prefixes, decl, prefix);
  142. }
  143. return decl.parent.insertBefore(decl, cloned);
  144. };
  145. /**
  146. * Did this declaration has this prefix above
  147. */
  148. Declaration.prototype.isAlready = function isAlready(decl, prefixed) {
  149. var already = this.all.group(decl).up(function (i) {
  150. return i.prop === prefixed;
  151. });
  152. if (!already) {
  153. already = this.all.group(decl).down(function (i) {
  154. return i.prop === prefixed;
  155. });
  156. }
  157. return already;
  158. };
  159. /**
  160. * Clone and add prefixes for declaration
  161. */
  162. Declaration.prototype.add = function add(decl, prefix, prefixes, result) {
  163. var prefixed = this.prefixed(decl.prop, prefix);
  164. if (this.isAlready(decl, prefixed) || this.otherPrefixes(decl.value, prefix)) {
  165. return undefined;
  166. }
  167. return this.insert(decl, prefix, prefixes, result);
  168. };
  169. /**
  170. * Add spaces for visual cascade
  171. */
  172. Declaration.prototype.process = function process(decl, result) {
  173. if (!this.needCascade(decl)) {
  174. _Prefixer.prototype.process.call(this, decl, result);
  175. return;
  176. }
  177. var prefixes = _Prefixer.prototype.process.call(this, decl, result);
  178. if (!prefixes || !prefixes.length) {
  179. return;
  180. }
  181. this.restoreBefore(decl);
  182. decl.raws.before = this.calcBefore(prefixes, decl);
  183. };
  184. /**
  185. * Return list of prefixed properties to clean old prefixes
  186. */
  187. Declaration.prototype.old = function old(prop, prefix) {
  188. return [this.prefixed(prop, prefix)];
  189. };
  190. return Declaration;
  191. }(Prefixer);
  192. module.exports = Declaration;