wishthis/node_modules/autoprefixer/lib/transition.js

330 lines
8 KiB
JavaScript
Raw Normal View History

2023-08-17 09:47:40 +00:00
let { list } = require('postcss')
let parser = require('postcss-value-parser')
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
let Browsers = require('./browsers')
let vendor = require('./vendor')
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
class Transition {
constructor(prefixes) {
this.props = ['transition', 'transition-property']
this.prefixes = prefixes
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
2022-01-21 08:28:41 +00:00
/**
* Process transition and add prefixes for all necessary properties
*/
2023-08-17 09:47:40 +00:00
add(decl, result) {
let prefix, prop
let add = this.prefixes.add[decl.prop]
let vendorPrefixes = this.ruleVendorPrefixes(decl)
let declPrefixes = vendorPrefixes || (add && add.prefixes) || []
let params = this.parse(decl.value)
let names = params.map(i => this.findProp(i))
let added = []
if (names.some(i => i[0] === '-')) {
return
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
for (let param of params) {
prop = this.findProp(param)
if (prop[0] === '-') continue
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
let prefixer = this.prefixes.add[prop]
if (!prefixer || !prefixer.prefixes) continue
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
for (prefix of prefixer.prefixes) {
if (vendorPrefixes && !vendorPrefixes.some(p => prefix.includes(p))) {
continue
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
let prefixed = this.prefixes.prefixed(prop, prefix)
2022-01-21 08:28:41 +00:00
if (prefixed !== '-ms-transform' && !names.includes(prefixed)) {
if (!this.disabled(prop, prefix)) {
2023-08-17 09:47:40 +00:00
added.push(this.clone(prop, prefixed, param))
2022-01-21 08:28:41 +00:00
}
}
}
}
2023-08-17 09:47:40 +00:00
params = params.concat(added)
let value = this.stringify(params)
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
let webkitClean = this.stringify(
this.cleanFromUnprefixed(params, '-webkit-')
)
2022-01-21 08:28:41 +00:00
if (declPrefixes.includes('-webkit-')) {
2023-08-17 09:47:40 +00:00
this.cloneBefore(decl, `-webkit-${decl.prop}`, webkitClean)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
this.cloneBefore(decl, decl.prop, webkitClean)
2022-01-21 08:28:41 +00:00
if (declPrefixes.includes('-o-')) {
2023-08-17 09:47:40 +00:00
let operaClean = this.stringify(this.cleanFromUnprefixed(params, '-o-'))
this.cloneBefore(decl, `-o-${decl.prop}`, operaClean)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
for (prefix of declPrefixes) {
2022-01-21 08:28:41 +00:00
if (prefix !== '-webkit-' && prefix !== '-o-') {
2023-08-17 09:47:40 +00:00
let prefixValue = this.stringify(
this.cleanOtherPrefixes(params, prefix)
)
this.cloneBefore(decl, prefix + decl.prop, prefixValue)
2022-01-21 08:28:41 +00:00
}
}
if (value !== decl.value && !this.already(decl, decl.prop, value)) {
2023-08-17 09:47:40 +00:00
this.checkForWarning(result, decl)
decl.cloneBefore()
decl.value = value
2022-01-21 08:28:41 +00:00
}
}
/**
* Does we already have this declaration
*/
2023-08-17 09:47:40 +00:00
already(decl, prop, value) {
return decl.parent.some(i => i.prop === prop && i.value === value)
2022-01-21 08:28:41 +00:00
}
/**
* Show transition-property warning
*/
2023-08-17 09:47:40 +00:00
checkForWarning(result, decl) {
2022-01-21 08:28:41 +00:00
if (decl.prop !== 'transition-property') {
2023-08-17 09:47:40 +00:00
return
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
let isPrefixed = false
let hasAssociatedProp = false
decl.parent.each(i => {
2022-01-21 08:28:41 +00:00
if (i.type !== 'decl') {
2023-08-17 09:47:40 +00:00
return undefined
2022-01-21 08:28:41 +00:00
}
if (i.prop.indexOf('transition-') !== 0) {
2023-08-17 09:47:40 +00:00
return undefined
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
let values = list.comma(i.value)
// check if current Rule's transition-property comma separated value list needs prefixes
2022-01-21 08:28:41 +00:00
if (i.prop === 'transition-property') {
2023-08-17 09:47:40 +00:00
values.forEach(value => {
let lookup = this.prefixes.add[value]
if (lookup && lookup.prefixes && lookup.prefixes.length > 0) {
isPrefixed = true
}
})
return undefined
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
// check if another transition-* prop in current Rule has comma separated value list
hasAssociatedProp = hasAssociatedProp || values.length > 1
return false
})
if (isPrefixed && hasAssociatedProp) {
decl.warn(
result,
'Replace transition-property to transition, ' +
'because Autoprefixer could not support ' +
'any cases of transition-property ' +
'and other transition-*'
)
2022-01-21 08:28:41 +00:00
}
}
2023-08-17 09:47:40 +00:00
2022-01-21 08:28:41 +00:00
/**
2023-08-17 09:47:40 +00:00
* Remove all non-webkit prefixes and unprefixed params if we have prefixed
2022-01-21 08:28:41 +00:00
*/
2023-08-17 09:47:40 +00:00
cleanFromUnprefixed(params, prefix) {
let remove = params
.map(i => this.findProp(i))
.filter(i => i.slice(0, prefix.length) === prefix)
.map(i => this.prefixes.unprefixed(i))
let result = []
for (let param of params) {
let prop = this.findProp(param)
let p = vendor.prefix(prop)
if (!remove.includes(prop) && (p === prefix || p === '')) {
result.push(param)
2022-01-21 08:28:41 +00:00
}
}
2023-08-17 09:47:40 +00:00
return result
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
cleanOtherPrefixes(params, prefix) {
return params.filter(param => {
let current = vendor.prefix(this.findProp(param))
return current === '' || current === prefix
})
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
2022-01-21 08:28:41 +00:00
/**
2023-08-17 09:47:40 +00:00
* Return new param array with different name
2022-01-21 08:28:41 +00:00
*/
2023-08-17 09:47:40 +00:00
clone(origin, name, param) {
let result = []
let changed = false
for (let i of param) {
if (!changed && i.type === 'word' && i.value === origin) {
result.push({ type: 'word', value: name })
changed = true
} else {
result.push(i)
2022-01-21 08:28:41 +00:00
}
}
2023-08-17 09:47:40 +00:00
return result
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
/**
* Add declaration if it is not exist
*/
cloneBefore(decl, prop, value) {
if (!this.already(decl, prop, value)) {
decl.cloneBefore({ prop, value })
2022-01-21 08:28:41 +00:00
}
}
2023-08-17 09:47:40 +00:00
2022-01-21 08:28:41 +00:00
/**
2023-08-17 09:47:40 +00:00
* Check property for disabled by option
2022-01-21 08:28:41 +00:00
*/
2023-08-17 09:47:40 +00:00
disabled(prop, prefix) {
let other = ['order', 'justify-content', 'align-self', 'align-content']
if (prop.includes('flex') || other.includes(prop)) {
if (this.prefixes.options.flexbox === false) {
return true
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
if (this.prefixes.options.flexbox === 'no-2009') {
return prefix.includes('2009')
2022-01-21 08:28:41 +00:00
}
}
2023-08-17 09:47:40 +00:00
return undefined
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
2022-01-21 08:28:41 +00:00
/**
* Find or create separator
*/
2023-08-17 09:47:40 +00:00
div(params) {
for (let param of params) {
for (let node of param) {
2022-01-21 08:28:41 +00:00
if (node.type === 'div' && node.value === ',') {
2023-08-17 09:47:40 +00:00
return node
2022-01-21 08:28:41 +00:00
}
}
}
2023-08-17 09:47:40 +00:00
return { after: ' ', type: 'div', value: ',' }
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
2022-01-21 08:28:41 +00:00
/**
2023-08-17 09:47:40 +00:00
* Find property name
2022-01-21 08:28:41 +00:00
*/
2023-08-17 09:47:40 +00:00
findProp(param) {
let prop = param[0].value
if (/^\d/.test(prop)) {
for (let [i, token] of param.entries()) {
if (i !== 0 && token.type === 'word') {
return token.value
}
2022-01-21 08:28:41 +00:00
}
}
2023-08-17 09:47:40 +00:00
return prop
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
/**
* Parse properties list to array
*/
parse(value) {
let ast = parser(value)
let result = []
let param = []
for (let node of ast.nodes) {
param.push(node)
if (node.type === 'div' && node.value === ',') {
result.push(param)
param = []
}
}
result.push(param)
return result.filter(i => i.length > 0)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
2022-01-21 08:28:41 +00:00
/**
2023-08-17 09:47:40 +00:00
* Process transition and remove all unnecessary properties
2022-01-21 08:28:41 +00:00
*/
2023-08-17 09:47:40 +00:00
remove(decl) {
let params = this.parse(decl.value)
params = params.filter(i => {
let prop = this.prefixes.remove[this.findProp(i)]
return !prop || !prop.remove
})
let value = this.stringify(params)
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
if (decl.value === value) {
return
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
if (params.length === 0) {
decl.remove()
return
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
let double = decl.parent.some(i => {
return i.prop === decl.prop && i.value === value
})
let smaller = decl.parent.some(i => {
return i !== decl && i.prop === decl.prop && i.value.length > value.length
})
if (double || smaller) {
decl.remove()
return
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
decl.value = value
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
2022-01-21 08:28:41 +00:00
/**
* Check if transition prop is inside vendor specific rule
*/
2023-08-17 09:47:40 +00:00
ruleVendorPrefixes(decl) {
let { parent } = decl
2022-01-21 08:28:41 +00:00
if (parent.type !== 'rule') {
2023-08-17 09:47:40 +00:00
return false
2022-01-21 08:28:41 +00:00
} else if (!parent.selector.includes(':-')) {
2023-08-17 09:47:40 +00:00
return false
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
let selectors = Browsers.prefixes().filter(s =>
parent.selector.includes(':' + s)
)
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
return selectors.length > 0 ? selectors : false
}
/**
* Return properties string from array
*/
stringify(params) {
if (params.length === 0) {
return ''
}
let nodes = []
for (let param of params) {
if (param[param.length - 1].type !== 'div') {
param.push(this.div(params))
}
nodes = nodes.concat(param)
}
if (nodes[0].type === 'div') {
nodes = nodes.slice(1)
}
if (nodes[nodes.length - 1].type === 'div') {
nodes = nodes.slice(0, +-2 + 1 || undefined)
}
return parser.stringify({ nodes })
}
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
module.exports = Transition