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. * Is declaration need to be prefixed
  49. */
  50. check(decl) {
  51. let value = decl.value
  52. if (!value.includes(this.name)) {
  53. return false
  54. }
  55. return !!value.match(this.regexp())
  56. }
  57. /**
  58. * Lazy regexp loading
  59. */
  60. regexp() {
  61. return this.regexpCache || (this.regexpCache = utils.regexp(this.name))
  62. }
  63. /**
  64. * Add prefix to values in string
  65. */
  66. replace(string, prefix) {
  67. return string.replace(this.regexp(), `$1${prefix}$2`)
  68. }
  69. /**
  70. * Get value with comments if it was not changed
  71. */
  72. value(decl) {
  73. if (decl.raws.value && decl.raws.value.value === decl.value) {
  74. return decl.raws.value.raw
  75. } else {
  76. return decl.value
  77. }
  78. }
  79. /**
  80. * Save values with next prefixed token
  81. */
  82. add(decl, prefix) {
  83. if (!decl._autoprefixerValues) {
  84. decl._autoprefixerValues = {}
  85. }
  86. let value = decl._autoprefixerValues[prefix] || this.value(decl)
  87. let before
  88. do {
  89. before = value
  90. value = this.replace(value, prefix)
  91. if (value === false) return
  92. } while (value !== before)
  93. decl._autoprefixerValues[prefix] = value
  94. }
  95. /**
  96. * Return function to fast find prefixed value
  97. */
  98. old(prefix) {
  99. return new OldValue(this.name, prefix + this.name)
  100. }
  101. }
  102. module.exports = Value