value.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 _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); }; }
  4. function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  5. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  6. 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; } }
  7. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  8. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _defaults(subClass, superClass); }
  9. var vendor = require('postcss').vendor;
  10. var Prefixer = require('./prefixer');
  11. var OldValue = require('./old-value');
  12. var utils = require('./utils');
  13. var Value = /*#__PURE__*/function (_Prefixer) {
  14. _inheritsLoose(Value, _Prefixer);
  15. var _super = _createSuper(Value);
  16. function Value() {
  17. return _Prefixer.apply(this, arguments) || this;
  18. }
  19. /**
  20. * Clone decl for each prefixed values
  21. */
  22. Value.save = function save(prefixes, decl) {
  23. var _this = this;
  24. var prop = decl.prop;
  25. var result = [];
  26. var _loop = function _loop(prefix) {
  27. var value = decl._autoprefixerValues[prefix];
  28. if (value === decl.value) {
  29. return "continue";
  30. }
  31. var item = void 0;
  32. var propPrefix = vendor.prefix(prop);
  33. if (propPrefix === '-pie-') {
  34. return "continue";
  35. }
  36. if (propPrefix === prefix) {
  37. item = decl.value = value;
  38. result.push(item);
  39. return "continue";
  40. }
  41. var prefixed = prefixes.prefixed(prop, prefix);
  42. var rule = decl.parent;
  43. if (!rule.every(function (i) {
  44. return i.prop !== prefixed;
  45. })) {
  46. result.push(item);
  47. return "continue";
  48. }
  49. var trimmed = value.replace(/\s+/, ' ');
  50. var already = rule.some(function (i) {
  51. return i.prop === decl.prop && i.value.replace(/\s+/, ' ') === trimmed;
  52. });
  53. if (already) {
  54. result.push(item);
  55. return "continue";
  56. }
  57. var cloned = _this.clone(decl, {
  58. value: value
  59. });
  60. item = decl.parent.insertBefore(decl, cloned);
  61. result.push(item);
  62. };
  63. for (var prefix in decl._autoprefixerValues) {
  64. var _ret = _loop(prefix);
  65. if (_ret === "continue") continue;
  66. }
  67. return result;
  68. }
  69. /**
  70. * Is declaration need to be prefixed
  71. */
  72. ;
  73. var _proto = Value.prototype;
  74. _proto.check = function check(decl) {
  75. var value = decl.value;
  76. if (!value.includes(this.name)) {
  77. return false;
  78. }
  79. return !!value.match(this.regexp());
  80. }
  81. /**
  82. * Lazy regexp loading
  83. */
  84. ;
  85. _proto.regexp = function regexp() {
  86. return this.regexpCache || (this.regexpCache = utils.regexp(this.name));
  87. }
  88. /**
  89. * Add prefix to values in string
  90. */
  91. ;
  92. _proto.replace = function replace(string, prefix) {
  93. return string.replace(this.regexp(), "$1" + prefix + "$2");
  94. }
  95. /**
  96. * Get value with comments if it was not changed
  97. */
  98. ;
  99. _proto.value = function value(decl) {
  100. if (decl.raws.value && decl.raws.value.value === decl.value) {
  101. return decl.raws.value.raw;
  102. } else {
  103. return decl.value;
  104. }
  105. }
  106. /**
  107. * Save values with next prefixed token
  108. */
  109. ;
  110. _proto.add = function add(decl, prefix) {
  111. if (!decl._autoprefixerValues) {
  112. decl._autoprefixerValues = {};
  113. }
  114. var value = decl._autoprefixerValues[prefix] || this.value(decl);
  115. var before;
  116. do {
  117. before = value;
  118. value = this.replace(value, prefix);
  119. if (value === false) return;
  120. } while (value !== before);
  121. decl._autoprefixerValues[prefix] = value;
  122. }
  123. /**
  124. * Return function to fast find prefixed value
  125. */
  126. ;
  127. _proto.old = function old(prefix) {
  128. return new OldValue(this.name, prefix + this.name);
  129. };
  130. return Value;
  131. }(Prefixer);
  132. module.exports = Value;