declaration.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. let Prefixer = require('./prefixer')
  2. let Browsers = require('./browsers')
  3. let utils = require('./utils')
  4. class Declaration extends Prefixer {
  5. /**
  6. * Clone and add prefixes for declaration
  7. */
  8. add(decl, prefix, prefixes, result) {
  9. let prefixed = this.prefixed(decl.prop, prefix)
  10. if (
  11. this.isAlready(decl, prefixed) ||
  12. this.otherPrefixes(decl.value, prefix)
  13. ) {
  14. return undefined
  15. }
  16. return this.insert(decl, prefix, prefixes, result)
  17. }
  18. /**
  19. * Calculate indentation to create visual cascade
  20. */
  21. calcBefore(prefixes, decl, prefix = '') {
  22. let max = this.maxPrefixed(prefixes, decl)
  23. let diff = max - utils.removeNote(prefix).length
  24. let before = decl.raw('before')
  25. if (diff > 0) {
  26. before += Array(diff).fill(' ').join('')
  27. }
  28. return before
  29. }
  30. /**
  31. * Always true, because we already get prefixer by property name
  32. */
  33. check(/* decl */) {
  34. return true
  35. }
  36. /**
  37. * Clone and insert new declaration
  38. */
  39. insert(decl, prefix, prefixes) {
  40. let cloned = this.set(this.clone(decl), prefix)
  41. if (!cloned) return undefined
  42. let already = decl.parent.some(
  43. i => i.prop === cloned.prop && i.value === cloned.value
  44. )
  45. if (already) {
  46. return undefined
  47. }
  48. if (this.needCascade(decl)) {
  49. cloned.raws.before = this.calcBefore(prefixes, decl, prefix)
  50. }
  51. return decl.parent.insertBefore(decl, cloned)
  52. }
  53. /**
  54. * Did this declaration has this prefix above
  55. */
  56. isAlready(decl, prefixed) {
  57. let already = this.all.group(decl).up(i => i.prop === prefixed)
  58. if (!already) {
  59. already = this.all.group(decl).down(i => i.prop === prefixed)
  60. }
  61. return already
  62. }
  63. /**
  64. * Return maximum length of possible prefixed property
  65. */
  66. maxPrefixed(prefixes, decl) {
  67. if (decl._autoprefixerMax) {
  68. return decl._autoprefixerMax
  69. }
  70. let max = 0
  71. for (let prefix of prefixes) {
  72. prefix = utils.removeNote(prefix)
  73. if (prefix.length > max) {
  74. max = prefix.length
  75. }
  76. }
  77. decl._autoprefixerMax = max
  78. return decl._autoprefixerMax
  79. }
  80. /**
  81. * Should we use visual cascade for prefixes
  82. */
  83. needCascade(decl) {
  84. if (!decl._autoprefixerCascade) {
  85. decl._autoprefixerCascade =
  86. this.all.options.cascade !== false && decl.raw('before').includes('\n')
  87. }
  88. return decl._autoprefixerCascade
  89. }
  90. /**
  91. * Return unprefixed version of property
  92. */
  93. normalize(prop) {
  94. return prop
  95. }
  96. /**
  97. * Return list of prefixed properties to clean old prefixes
  98. */
  99. old(prop, prefix) {
  100. return [this.prefixed(prop, prefix)]
  101. }
  102. /**
  103. * Check `value`, that it contain other prefixes, rather than `prefix`
  104. */
  105. otherPrefixes(value, prefix) {
  106. for (let other of Browsers.prefixes()) {
  107. if (other === prefix) {
  108. continue
  109. }
  110. if (value.includes(other)) {
  111. return value.replace(/var\([^)]+\)/, '').includes(other)
  112. }
  113. }
  114. return false
  115. }
  116. /**
  117. * Return prefixed version of property
  118. */
  119. prefixed(prop, prefix) {
  120. return prefix + prop
  121. }
  122. /**
  123. * Add spaces for visual cascade
  124. */
  125. process(decl, result) {
  126. if (!this.needCascade(decl)) {
  127. super.process(decl, result)
  128. return
  129. }
  130. let prefixes = super.process(decl, result)
  131. if (!prefixes || !prefixes.length) {
  132. return
  133. }
  134. this.restoreBefore(decl)
  135. decl.raws.before = this.calcBefore(prefixes, decl)
  136. }
  137. /**
  138. * Remove visual cascade
  139. */
  140. restoreBefore(decl) {
  141. let lines = decl.raw('before').split('\n')
  142. let min = lines[lines.length - 1]
  143. this.all.group(decl).up(prefixed => {
  144. let array = prefixed.raw('before').split('\n')
  145. let last = array[array.length - 1]
  146. if (last.length < min.length) {
  147. min = last
  148. }
  149. })
  150. lines[lines.length - 1] = min
  151. decl.raws.before = lines.join('\n')
  152. }
  153. /**
  154. * Set prefix to declaration
  155. */
  156. set(decl, prefix) {
  157. decl.prop = this.prefixed(decl.prop, prefix)
  158. return decl
  159. }
  160. }
  161. module.exports = Declaration