declaration.js 7.6 KB

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