value.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. (function() {
  2. var OldValue, Prefixer, Value, utils, vendor,
  3. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  4. hasProp = {}.hasOwnProperty;
  5. Prefixer = require('./prefixer');
  6. OldValue = require('./old-value');
  7. utils = require('./utils');
  8. vendor = require('postcss/lib/vendor');
  9. Value = (function(superClass) {
  10. extend(Value, superClass);
  11. function Value() {
  12. return Value.__super__.constructor.apply(this, arguments);
  13. }
  14. Value.save = function(prefixes, decl) {
  15. var already, cloned, prefix, prefixed, prop, propPrefix, ref, results, rule, trimmed, value;
  16. prop = decl.prop;
  17. ref = decl._autoprefixerValues;
  18. results = [];
  19. for (prefix in ref) {
  20. value = ref[prefix];
  21. if (value === decl.value) {
  22. continue;
  23. }
  24. propPrefix = vendor.prefix(prop);
  25. if (propPrefix === prefix) {
  26. results.push(decl.value = value);
  27. } else if (propPrefix === '-pie-') {
  28. continue;
  29. } else {
  30. prefixed = prefixes.prefixed(prop, prefix);
  31. rule = decl.parent;
  32. if (rule.every(function(i) {
  33. return i.prop !== prefixed;
  34. })) {
  35. trimmed = value.replace(/\s+/, ' ');
  36. already = rule.some(function(i) {
  37. return i.prop === decl.prop && i.value.replace(/\s+/, ' ') === trimmed;
  38. });
  39. if (!already) {
  40. cloned = this.clone(decl, {
  41. value: value
  42. });
  43. results.push(decl.parent.insertBefore(decl, cloned));
  44. } else {
  45. results.push(void 0);
  46. }
  47. } else {
  48. results.push(void 0);
  49. }
  50. }
  51. }
  52. return results;
  53. };
  54. Value.prototype.check = function(decl) {
  55. var value;
  56. value = decl.value;
  57. if (value.indexOf(this.name) !== -1) {
  58. return !!value.match(this.regexp());
  59. } else {
  60. return false;
  61. }
  62. };
  63. Value.prototype.regexp = function() {
  64. return this.regexpCache || (this.regexpCache = utils.regexp(this.name));
  65. };
  66. Value.prototype.replace = function(string, prefix) {
  67. return string.replace(this.regexp(), '$1' + prefix + '$2');
  68. };
  69. Value.prototype.value = function(decl) {
  70. if (decl.raws.value && decl.raws.value.value === decl.value) {
  71. return decl.raws.value.raw;
  72. } else {
  73. return decl.value;
  74. }
  75. };
  76. Value.prototype.add = function(decl, prefix) {
  77. var value;
  78. decl._autoprefixerValues || (decl._autoprefixerValues = {});
  79. value = decl._autoprefixerValues[prefix] || this.value(decl);
  80. value = this.replace(value, prefix);
  81. if (value) {
  82. return decl._autoprefixerValues[prefix] = value;
  83. }
  84. };
  85. Value.prototype.old = function(prefix) {
  86. return new OldValue(this.name, prefix + this.name);
  87. };
  88. return Value;
  89. })(Prefixer);
  90. module.exports = Value;
  91. }).call(this);