declaration.js 7.9 KB

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