value.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. 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; }
  4. 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) : subClass.__proto__ = superClass; }
  5. var Prefixer = require('./prefixer');
  6. var OldValue = require('./old-value');
  7. var utils = require('./utils');
  8. var vendor = require('postcss/lib/vendor');
  9. var Value = function (_Prefixer) {
  10. _inherits(Value, _Prefixer);
  11. function Value() {
  12. _classCallCheck(this, Value);
  13. return _possibleConstructorReturn(this, _Prefixer.apply(this, arguments));
  14. }
  15. /**
  16. * Clone decl for each prefixed values
  17. */
  18. Value.save = function save(prefixes, decl) {
  19. var _this2 = 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 = _this2.clone(decl, { value: value });
  54. item = decl.parent.insertBefore(decl, cloned);
  55. result.push(item);
  56. };
  57. for (var prefix in decl._autoprefixerValues) {
  58. var _ret = _loop(prefix);
  59. if (_ret === 'continue') continue;
  60. }
  61. return result;
  62. };
  63. /**
  64. * Is declaration need to be prefixed
  65. */
  66. Value.prototype.check = function check(decl) {
  67. var value = decl.value;
  68. if (value.indexOf(this.name) === -1) {
  69. return false;
  70. }
  71. return !!value.match(this.regexp());
  72. };
  73. /**
  74. * Lazy regexp loading
  75. */
  76. Value.prototype.regexp = function regexp() {
  77. return this.regexpCache || (this.regexpCache = utils.regexp(this.name));
  78. };
  79. /**
  80. * Add prefix to values in string
  81. */
  82. Value.prototype.replace = function replace(string, prefix) {
  83. return string.replace(this.regexp(), '$1' + prefix + '$2');
  84. };
  85. /**
  86. * Get value with comments if it was not changed
  87. */
  88. Value.prototype.value = function value(decl) {
  89. if (decl.raws.value && decl.raws.value.value === decl.value) {
  90. return decl.raws.value.raw;
  91. } else {
  92. return decl.value;
  93. }
  94. };
  95. /**
  96. * Save values with next prefixed token
  97. */
  98. Value.prototype.add = function add(decl, prefix) {
  99. if (!decl._autoprefixerValues) {
  100. decl._autoprefixerValues = {};
  101. }
  102. var value = decl._autoprefixerValues[prefix] || this.value(decl);
  103. value = this.replace(value, prefix);
  104. if (value) {
  105. decl._autoprefixerValues[prefix] = value;
  106. }
  107. };
  108. /**
  109. * Return function to fast find prefixed value
  110. */
  111. Value.prototype.old = function old(prefix) {
  112. return new OldValue(this.name, prefix + this.name);
  113. };
  114. return Value;
  115. }(Prefixer);
  116. module.exports = Value;