value.js 3.8 KB

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