transition.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. let { list } = require('postcss')
  2. let parser = require('postcss-value-parser')
  3. let Browsers = require('./browsers')
  4. let vendor = require('./vendor')
  5. class Transition {
  6. constructor(prefixes) {
  7. this.props = ['transition', 'transition-property']
  8. this.prefixes = prefixes
  9. }
  10. /**
  11. * Process transition and add prefixes for all necessary properties
  12. */
  13. add(decl, result) {
  14. let prefix, prop
  15. let add = this.prefixes.add[decl.prop]
  16. let vendorPrefixes = this.ruleVendorPrefixes(decl)
  17. let declPrefixes = vendorPrefixes || (add && add.prefixes) || []
  18. let params = this.parse(decl.value)
  19. let names = params.map(i => this.findProp(i))
  20. let added = []
  21. if (names.some(i => i[0] === '-')) {
  22. return
  23. }
  24. for (let param of params) {
  25. prop = this.findProp(param)
  26. if (prop[0] === '-') continue
  27. let prefixer = this.prefixes.add[prop]
  28. if (!prefixer || !prefixer.prefixes) continue
  29. for (prefix of prefixer.prefixes) {
  30. if (vendorPrefixes && !vendorPrefixes.some(p => prefix.includes(p))) {
  31. continue
  32. }
  33. let prefixed = this.prefixes.prefixed(prop, prefix)
  34. if (prefixed !== '-ms-transform' && !names.includes(prefixed)) {
  35. if (!this.disabled(prop, prefix)) {
  36. added.push(this.clone(prop, prefixed, param))
  37. }
  38. }
  39. }
  40. }
  41. params = params.concat(added)
  42. let value = this.stringify(params)
  43. let webkitClean = this.stringify(
  44. this.cleanFromUnprefixed(params, '-webkit-')
  45. )
  46. if (declPrefixes.includes('-webkit-')) {
  47. this.cloneBefore(decl, `-webkit-${decl.prop}`, webkitClean)
  48. }
  49. this.cloneBefore(decl, decl.prop, webkitClean)
  50. if (declPrefixes.includes('-o-')) {
  51. let operaClean = this.stringify(this.cleanFromUnprefixed(params, '-o-'))
  52. this.cloneBefore(decl, `-o-${decl.prop}`, operaClean)
  53. }
  54. for (prefix of declPrefixes) {
  55. if (prefix !== '-webkit-' && prefix !== '-o-') {
  56. let prefixValue = this.stringify(
  57. this.cleanOtherPrefixes(params, prefix)
  58. )
  59. this.cloneBefore(decl, prefix + decl.prop, prefixValue)
  60. }
  61. }
  62. if (value !== decl.value && !this.already(decl, decl.prop, value)) {
  63. this.checkForWarning(result, decl)
  64. decl.cloneBefore()
  65. decl.value = value
  66. }
  67. }
  68. /**
  69. * Find property name
  70. */
  71. findProp(param) {
  72. let prop = param[0].value
  73. if (/^\d/.test(prop)) {
  74. for (let [i, token] of param.entries()) {
  75. if (i !== 0 && token.type === 'word') {
  76. return token.value
  77. }
  78. }
  79. }
  80. return prop
  81. }
  82. /**
  83. * Does we already have this declaration
  84. */
  85. already(decl, prop, value) {
  86. return decl.parent.some(i => i.prop === prop && i.value === value)
  87. }
  88. /**
  89. * Add declaration if it is not exist
  90. */
  91. cloneBefore(decl, prop, value) {
  92. if (!this.already(decl, prop, value)) {
  93. decl.cloneBefore({ prop, value })
  94. }
  95. }
  96. /**
  97. * Show transition-property warning
  98. */
  99. checkForWarning(result, decl) {
  100. if (decl.prop !== 'transition-property') {
  101. return
  102. }
  103. let isPrefixed = false
  104. let hasAssociatedProp = false
  105. decl.parent.each(i => {
  106. if (i.type !== 'decl') {
  107. return undefined
  108. }
  109. if (i.prop.indexOf('transition-') !== 0) {
  110. return undefined
  111. }
  112. let values = list.comma(i.value)
  113. // check if current Rule's transition-property comma separated value list needs prefixes
  114. if (i.prop === 'transition-property') {
  115. values.forEach(value => {
  116. let lookup = this.prefixes.add[value]
  117. if (lookup && lookup.prefixes && lookup.prefixes.length > 0) {
  118. isPrefixed = true
  119. }
  120. })
  121. return undefined
  122. }
  123. // check if another transition-* prop in current Rule has comma separated value list
  124. hasAssociatedProp = hasAssociatedProp || values.length > 1
  125. return false
  126. })
  127. if (isPrefixed && hasAssociatedProp) {
  128. decl.warn(
  129. result,
  130. 'Replace transition-property to transition, ' +
  131. 'because Autoprefixer could not support ' +
  132. 'any cases of transition-property ' +
  133. 'and other transition-*'
  134. )
  135. }
  136. }
  137. /**
  138. * Process transition and remove all unnecessary properties
  139. */
  140. remove(decl) {
  141. let params = this.parse(decl.value)
  142. params = params.filter(i => {
  143. let prop = this.prefixes.remove[this.findProp(i)]
  144. return !prop || !prop.remove
  145. })
  146. let value = this.stringify(params)
  147. if (decl.value === value) {
  148. return
  149. }
  150. if (params.length === 0) {
  151. decl.remove()
  152. return
  153. }
  154. let double = decl.parent.some(i => {
  155. return i.prop === decl.prop && i.value === value
  156. })
  157. let smaller = decl.parent.some(i => {
  158. return i !== decl && i.prop === decl.prop && i.value.length > value.length
  159. })
  160. if (double || smaller) {
  161. decl.remove()
  162. return
  163. }
  164. decl.value = value
  165. }
  166. /**
  167. * Parse properties list to array
  168. */
  169. parse(value) {
  170. let ast = parser(value)
  171. let result = []
  172. let param = []
  173. for (let node of ast.nodes) {
  174. param.push(node)
  175. if (node.type === 'div' && node.value === ',') {
  176. result.push(param)
  177. param = []
  178. }
  179. }
  180. result.push(param)
  181. return result.filter(i => i.length > 0)
  182. }
  183. /**
  184. * Return properties string from array
  185. */
  186. stringify(params) {
  187. if (params.length === 0) {
  188. return ''
  189. }
  190. let nodes = []
  191. for (let param of params) {
  192. if (param[param.length - 1].type !== 'div') {
  193. param.push(this.div(params))
  194. }
  195. nodes = nodes.concat(param)
  196. }
  197. if (nodes[0].type === 'div') {
  198. nodes = nodes.slice(1)
  199. }
  200. if (nodes[nodes.length - 1].type === 'div') {
  201. nodes = nodes.slice(0, +-2 + 1 || undefined)
  202. }
  203. return parser.stringify({ nodes })
  204. }
  205. /**
  206. * Return new param array with different name
  207. */
  208. clone(origin, name, param) {
  209. let result = []
  210. let changed = false
  211. for (let i of param) {
  212. if (!changed && i.type === 'word' && i.value === origin) {
  213. result.push({ type: 'word', value: name })
  214. changed = true
  215. } else {
  216. result.push(i)
  217. }
  218. }
  219. return result
  220. }
  221. /**
  222. * Find or create separator
  223. */
  224. div(params) {
  225. for (let param of params) {
  226. for (let node of param) {
  227. if (node.type === 'div' && node.value === ',') {
  228. return node
  229. }
  230. }
  231. }
  232. return { type: 'div', value: ',', after: ' ' }
  233. }
  234. cleanOtherPrefixes(params, prefix) {
  235. return params.filter(param => {
  236. let current = vendor.prefix(this.findProp(param))
  237. return current === '' || current === prefix
  238. })
  239. }
  240. /**
  241. * Remove all non-webkit prefixes and unprefixed params if we have prefixed
  242. */
  243. cleanFromUnprefixed(params, prefix) {
  244. let remove = params
  245. .map(i => this.findProp(i))
  246. .filter(i => i.slice(0, prefix.length) === prefix)
  247. .map(i => this.prefixes.unprefixed(i))
  248. let result = []
  249. for (let param of params) {
  250. let prop = this.findProp(param)
  251. let p = vendor.prefix(prop)
  252. if (!remove.includes(prop) && (p === prefix || p === '')) {
  253. result.push(param)
  254. }
  255. }
  256. return result
  257. }
  258. /**
  259. * Check property for disabled by option
  260. */
  261. disabled(prop, prefix) {
  262. let other = ['order', 'justify-content', 'align-self', 'align-content']
  263. if (prop.includes('flex') || other.includes(prop)) {
  264. if (this.prefixes.options.flexbox === false) {
  265. return true
  266. }
  267. if (this.prefixes.options.flexbox === 'no-2009') {
  268. return prefix.includes('2009')
  269. }
  270. }
  271. return undefined
  272. }
  273. /**
  274. * Check if transition prop is inside vendor specific rule
  275. */
  276. ruleVendorPrefixes(decl) {
  277. let { parent } = decl
  278. if (parent.type !== 'rule') {
  279. return false
  280. } else if (!parent.selector.includes(':-')) {
  281. return false
  282. }
  283. let selectors = Browsers.prefixes().filter(s =>
  284. parent.selector.includes(':' + s)
  285. )
  286. return selectors.length > 0 ? selectors : false
  287. }
  288. }
  289. module.exports = Transition