value.js 4.9 KB

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