value.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. let Prefixer = require('./prefixer')
  2. let OldValue = require('./old-value')
  3. let vendor = require('./vendor')
  4. let utils = require('./utils')
  5. class Value extends Prefixer {
  6. /**
  7. * Clone decl for each prefixed values
  8. */
  9. static save(prefixes, decl) {
  10. let prop = decl.prop
  11. let result = []
  12. for (let prefix in decl._autoprefixerValues) {
  13. let value = decl._autoprefixerValues[prefix]
  14. if (value === decl.value) {
  15. continue
  16. }
  17. let item
  18. let propPrefix = vendor.prefix(prop)
  19. if (propPrefix === '-pie-') {
  20. continue
  21. }
  22. if (propPrefix === prefix) {
  23. item = decl.value = value
  24. result.push(item)
  25. continue
  26. }
  27. let prefixed = prefixes.prefixed(prop, prefix)
  28. let rule = decl.parent
  29. if (!rule.every(i => i.prop !== prefixed)) {
  30. result.push(item)
  31. continue
  32. }
  33. let trimmed = value.replace(/\s+/, ' ')
  34. let already = rule.some(
  35. i => i.prop === decl.prop && i.value.replace(/\s+/, ' ') === trimmed
  36. )
  37. if (already) {
  38. result.push(item)
  39. continue
  40. }
  41. let cloned = this.clone(decl, { value })
  42. item = decl.parent.insertBefore(decl, cloned)
  43. result.push(item)
  44. }
  45. return result
  46. }
  47. /**
  48. * Save values with next prefixed token
  49. */
  50. add(decl, prefix) {
  51. if (!decl._autoprefixerValues) {
  52. decl._autoprefixerValues = {}
  53. }
  54. let value = decl._autoprefixerValues[prefix] || this.value(decl)
  55. let before
  56. do {
  57. before = value
  58. value = this.replace(value, prefix)
  59. if (value === false) return
  60. } while (value !== before)
  61. decl._autoprefixerValues[prefix] = value
  62. }
  63. /**
  64. * Is declaration need to be prefixed
  65. */
  66. check(decl) {
  67. let value = decl.value
  68. if (!value.includes(this.name)) {
  69. return false
  70. }
  71. return !!value.match(this.regexp())
  72. }
  73. /**
  74. * Return function to fast find prefixed value
  75. */
  76. old(prefix) {
  77. return new OldValue(this.name, prefix + this.name)
  78. }
  79. /**
  80. * Lazy regexp loading
  81. */
  82. regexp() {
  83. return this.regexpCache || (this.regexpCache = utils.regexp(this.name))
  84. }
  85. /**
  86. * Add prefix to values in string
  87. */
  88. 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. value(decl) {
  95. if (decl.raws.value && decl.raws.value.value === decl.value) {
  96. return decl.raws.value.raw
  97. } else {
  98. return decl.value
  99. }
  100. }
  101. }
  102. module.exports = Value