You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
6.1 KiB

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