2023-08-17 09:47:40 +00:00
|
|
|
let Prefixer = require('./prefixer')
|
|
|
|
let Browsers = require('./browsers')
|
|
|
|
let utils = require('./utils')
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
class Declaration extends Prefixer {
|
|
|
|
/**
|
|
|
|
* Clone and add prefixes for declaration
|
|
|
|
*/
|
|
|
|
add(decl, prefix, prefixes, result) {
|
|
|
|
let prefixed = this.prefixed(decl.prop, prefix)
|
|
|
|
if (
|
|
|
|
this.isAlready(decl, prefixed) ||
|
|
|
|
this.otherPrefixes(decl.value, prefix)
|
|
|
|
) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
return this.insert(decl, prefix, prefixes, result)
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Calculate indentation to create visual cascade
|
|
|
|
*/
|
|
|
|
calcBefore(prefixes, decl, prefix = '') {
|
|
|
|
let max = this.maxPrefixed(prefixes, decl)
|
|
|
|
let diff = max - utils.removeNote(prefix).length
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let before = decl.raw('before')
|
|
|
|
if (diff > 0) {
|
|
|
|
before += Array(diff).fill(' ').join('')
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
return before
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Always true, because we already get prefixer by property name
|
|
|
|
*/
|
|
|
|
check(/* decl */) {
|
|
|
|
return true
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Clone and insert new declaration
|
|
|
|
*/
|
|
|
|
insert(decl, prefix, prefixes) {
|
|
|
|
let cloned = this.set(this.clone(decl), prefix)
|
|
|
|
if (!cloned) return undefined
|
|
|
|
|
|
|
|
let already = decl.parent.some(
|
|
|
|
i => i.prop === cloned.prop && i.value === cloned.value
|
|
|
|
)
|
|
|
|
if (already) {
|
|
|
|
return undefined
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (this.needCascade(decl)) {
|
|
|
|
cloned.raws.before = this.calcBefore(prefixes, decl, prefix)
|
|
|
|
}
|
|
|
|
return decl.parent.insertBefore(decl, cloned)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Did this declaration has this prefix above
|
|
|
|
*/
|
|
|
|
isAlready(decl, prefixed) {
|
|
|
|
let already = this.all.group(decl).up(i => i.prop === prefixed)
|
|
|
|
if (!already) {
|
|
|
|
already = this.all.group(decl).down(i => i.prop === prefixed)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
return already
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Return maximum length of possible prefixed property
|
|
|
|
*/
|
|
|
|
maxPrefixed(prefixes, decl) {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (decl._autoprefixerMax) {
|
2023-08-17 09:47:40 +00:00
|
|
|
return decl._autoprefixerMax
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let max = 0
|
|
|
|
for (let prefix of prefixes) {
|
|
|
|
prefix = utils.removeNote(prefix)
|
2022-01-21 08:28:41 +00:00
|
|
|
if (prefix.length > max) {
|
2023-08-17 09:47:40 +00:00
|
|
|
max = prefix.length
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
decl._autoprefixerMax = max
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
return decl._autoprefixerMax
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Should we use visual cascade for prefixes
|
|
|
|
*/
|
|
|
|
needCascade(decl) {
|
|
|
|
if (!decl._autoprefixerCascade) {
|
|
|
|
decl._autoprefixerCascade =
|
|
|
|
this.all.options.cascade !== false && decl.raw('before').includes('\n')
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
return decl._autoprefixerCascade
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Return unprefixed version of property
|
|
|
|
*/
|
|
|
|
normalize(prop) {
|
|
|
|
return prop
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Return list of prefixed properties to clean old prefixes
|
|
|
|
*/
|
|
|
|
old(prop, prefix) {
|
|
|
|
return [this.prefixed(prop, prefix)]
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Check `value`, that it contain other prefixes, rather than `prefix`
|
|
|
|
*/
|
|
|
|
otherPrefixes(value, prefix) {
|
|
|
|
for (let other of Browsers.prefixes()) {
|
|
|
|
if (other === prefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (value.includes(other)) {
|
|
|
|
return value.replace(/var\([^)]+\)/, '').includes(other)
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
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
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Return prefixed version of property
|
|
|
|
*/
|
|
|
|
prefixed(prop, prefix) {
|
|
|
|
return prefix + prop
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Add spaces for visual cascade
|
|
|
|
*/
|
|
|
|
process(decl, result) {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (!this.needCascade(decl)) {
|
2023-08-17 09:47:40 +00:00
|
|
|
super.process(decl, result)
|
|
|
|
return
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let prefixes = super.process(decl, result)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (!prefixes || !prefixes.length) {
|
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
|
|
|
this.restoreBefore(decl)
|
|
|
|
decl.raws.before = this.calcBefore(prefixes, decl)
|
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 visual cascade
|
|
|
|
*/
|
|
|
|
restoreBefore(decl) {
|
|
|
|
let lines = decl.raw('before').split('\n')
|
|
|
|
let min = lines[lines.length - 1]
|
|
|
|
|
|
|
|
this.all.group(decl).up(prefixed => {
|
|
|
|
let array = prefixed.raw('before').split('\n')
|
|
|
|
let last = array[array.length - 1]
|
|
|
|
if (last.length < min.length) {
|
|
|
|
min = last
|
|
|
|
}
|
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
lines[lines.length - 1] = min
|
|
|
|
decl.raws.before = lines.join('\n')
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Set prefix to declaration
|
|
|
|
*/
|
|
|
|
set(decl, prefix) {
|
|
|
|
decl.prop = this.prefixed(decl.prop, prefix)
|
|
|
|
return decl
|
|
|
|
}
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
module.exports = Declaration
|