declaration.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 _createForOfIteratorHelperLoose(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } it = o[Symbol.iterator](); return it.next.bind(it); }
  4. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  5. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  6. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _defaults(subClass, superClass); }
  7. var Prefixer = require('./prefixer');
  8. var Browsers = require('./browsers');
  9. var utils = require('./utils');
  10. var Declaration = /*#__PURE__*/function (_Prefixer) {
  11. _inheritsLoose(Declaration, _Prefixer);
  12. function Declaration() {
  13. return _Prefixer.apply(this, arguments) || this;
  14. }
  15. var _proto = Declaration.prototype;
  16. /**
  17. * Always true, because we already get prefixer by property name
  18. */
  19. _proto.check = function check()
  20. /* decl */
  21. {
  22. return true;
  23. }
  24. /**
  25. * Return prefixed version of property
  26. */
  27. ;
  28. _proto.prefixed = function prefixed(prop, prefix) {
  29. return prefix + prop;
  30. }
  31. /**
  32. * Return unprefixed version of property
  33. */
  34. ;
  35. _proto.normalize = function normalize(prop) {
  36. return prop;
  37. }
  38. /**
  39. * Check `value`, that it contain other prefixes, rather than `prefix`
  40. */
  41. ;
  42. _proto.otherPrefixes = function otherPrefixes(value, prefix) {
  43. for (var _iterator = _createForOfIteratorHelperLoose(Browsers.prefixes()), _step; !(_step = _iterator()).done;) {
  44. var other = _step.value;
  45. if (other === prefix) {
  46. continue;
  47. }
  48. if (value.includes(other)) {
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. /**
  55. * Set prefix to declaration
  56. */
  57. ;
  58. _proto.set = function set(decl, prefix) {
  59. decl.prop = this.prefixed(decl.prop, prefix);
  60. return decl;
  61. }
  62. /**
  63. * Should we use visual cascade for prefixes
  64. */
  65. ;
  66. _proto.needCascade = function needCascade(decl) {
  67. if (!decl._autoprefixerCascade) {
  68. decl._autoprefixerCascade = this.all.options.cascade !== false && decl.raw('before').includes('\n');
  69. }
  70. return decl._autoprefixerCascade;
  71. }
  72. /**
  73. * Return maximum length of possible prefixed property
  74. */
  75. ;
  76. _proto.maxPrefixed = function maxPrefixed(prefixes, decl) {
  77. if (decl._autoprefixerMax) {
  78. return decl._autoprefixerMax;
  79. }
  80. var max = 0;
  81. for (var _iterator2 = _createForOfIteratorHelperLoose(prefixes), _step2; !(_step2 = _iterator2()).done;) {
  82. var prefix = _step2.value;
  83. prefix = utils.removeNote(prefix);
  84. if (prefix.length > max) {
  85. max = prefix.length;
  86. }
  87. }
  88. decl._autoprefixerMax = max;
  89. return decl._autoprefixerMax;
  90. }
  91. /**
  92. * Calculate indentation to create visual cascade
  93. */
  94. ;
  95. _proto.calcBefore = function calcBefore(prefixes, decl, prefix) {
  96. if (prefix === void 0) {
  97. prefix = '';
  98. }
  99. var max = this.maxPrefixed(prefixes, decl);
  100. var diff = max - utils.removeNote(prefix).length;
  101. var before = decl.raw('before');
  102. if (diff > 0) {
  103. before += Array(diff).fill(' ').join('');
  104. }
  105. return before;
  106. }
  107. /**
  108. * Remove visual cascade
  109. */
  110. ;
  111. _proto.restoreBefore = function restoreBefore(decl) {
  112. var lines = decl.raw('before').split('\n');
  113. var min = lines[lines.length - 1];
  114. this.all.group(decl).up(function (prefixed) {
  115. var array = prefixed.raw('before').split('\n');
  116. var last = array[array.length - 1];
  117. if (last.length < min.length) {
  118. min = last;
  119. }
  120. });
  121. lines[lines.length - 1] = min;
  122. decl.raws.before = lines.join('\n');
  123. }
  124. /**
  125. * Clone and insert new declaration
  126. */
  127. ;
  128. _proto.insert = function insert(decl, prefix, prefixes) {
  129. var cloned = this.set(this.clone(decl), prefix);
  130. if (!cloned) return undefined;
  131. var already = decl.parent.some(function (i) {
  132. return i.prop === cloned.prop && i.value === cloned.value;
  133. });
  134. if (already) {
  135. return undefined;
  136. }
  137. if (this.needCascade(decl)) {
  138. cloned.raws.before = this.calcBefore(prefixes, decl, prefix);
  139. }
  140. return decl.parent.insertBefore(decl, cloned);
  141. }
  142. /**
  143. * Did this declaration has this prefix above
  144. */
  145. ;
  146. _proto.isAlready = function isAlready(decl, prefixed) {
  147. var already = this.all.group(decl).up(function (i) {
  148. return i.prop === prefixed;
  149. });
  150. if (!already) {
  151. already = this.all.group(decl).down(function (i) {
  152. return i.prop === prefixed;
  153. });
  154. }
  155. return already;
  156. }
  157. /**
  158. * Clone and add prefixes for declaration
  159. */
  160. ;
  161. _proto.add = function add(decl, prefix, prefixes, result) {
  162. var prefixed = this.prefixed(decl.prop, prefix);
  163. if (this.isAlready(decl, prefixed) || this.otherPrefixes(decl.value, prefix)) {
  164. return undefined;
  165. }
  166. return this.insert(decl, prefix, prefixes, result);
  167. }
  168. /**
  169. * Add spaces for visual cascade
  170. */
  171. ;
  172. _proto.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. ;
  188. _proto.old = function old(prop, prefix) {
  189. return [this.prefixed(prop, prefix)];
  190. };
  191. return Declaration;
  192. }(Prefixer);
  193. module.exports = Declaration;