2023-08-17 09:47:40 +00:00
|
|
|
let Prefixer = require('./prefixer')
|
|
|
|
let OldValue = require('./old-value')
|
|
|
|
let vendor = require('./vendor')
|
|
|
|
let utils = require('./utils')
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
class Value extends Prefixer {
|
2022-01-21 08:28:41 +00:00
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Clone decl for each prefixed values
|
|
|
|
*/
|
|
|
|
static save(prefixes, decl) {
|
|
|
|
let prop = decl.prop
|
|
|
|
let result = []
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
for (let prefix in decl._autoprefixerValues) {
|
|
|
|
let value = decl._autoprefixerValues[prefix]
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (value === decl.value) {
|
2023-08-17 09:47:40 +00:00
|
|
|
continue
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let item
|
|
|
|
let propPrefix = vendor.prefix(prop)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (propPrefix === '-pie-') {
|
2023-08-17 09:47:40 +00:00
|
|
|
continue
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (propPrefix === prefix) {
|
2023-08-17 09:47:40 +00:00
|
|
|
item = decl.value = value
|
|
|
|
result.push(item)
|
|
|
|
continue
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let prefixed = prefixes.prefixed(prop, prefix)
|
|
|
|
let rule = decl.parent
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (!rule.every(i => i.prop !== prefixed)) {
|
|
|
|
result.push(item)
|
|
|
|
continue
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let trimmed = value.replace(/\s+/, ' ')
|
|
|
|
let already = rule.some(
|
|
|
|
i => i.prop === decl.prop && i.value.replace(/\s+/, ' ') === trimmed
|
|
|
|
)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (already) {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.push(item)
|
|
|
|
continue
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let cloned = this.clone(decl, { value })
|
|
|
|
item = decl.parent.insertBefore(decl, cloned)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
result.push(item)
|
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
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Save values with next prefixed token
|
|
|
|
*/
|
|
|
|
add(decl, prefix) {
|
|
|
|
if (!decl._autoprefixerValues) {
|
|
|
|
decl._autoprefixerValues = {}
|
|
|
|
}
|
|
|
|
let value = decl._autoprefixerValues[prefix] || this.value(decl)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let before
|
|
|
|
do {
|
|
|
|
before = value
|
|
|
|
value = this.replace(value, prefix)
|
|
|
|
if (value === false) return
|
|
|
|
} while (value !== before)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
decl._autoprefixerValues[prefix] = value
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Is declaration need to be prefixed
|
|
|
|
*/
|
|
|
|
check(decl) {
|
|
|
|
let value = decl.value
|
2022-01-21 08:28:41 +00:00
|
|
|
if (!value.includes(this.name)) {
|
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
|
|
|
return !!value.match(this.regexp())
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Return function to fast find prefixed value
|
|
|
|
*/
|
|
|
|
old(prefix) {
|
|
|
|
return new OldValue(this.name, prefix + this.name)
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Lazy regexp loading
|
|
|
|
*/
|
|
|
|
regexp() {
|
|
|
|
return this.regexpCache || (this.regexpCache = utils.regexp(this.name))
|
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
|
|
|
* Add prefix to values in string
|
|
|
|
*/
|
|
|
|
replace(string, prefix) {
|
|
|
|
return string.replace(this.regexp(), `$1${prefix}$2`)
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Get value with comments if it was not changed
|
|
|
|
*/
|
|
|
|
value(decl) {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (decl.raws.value && decl.raws.value.value === decl.value) {
|
2023-08-17 09:47:40 +00:00
|
|
|
return decl.raws.value.raw
|
2022-01-21 08:28:41 +00:00
|
|
|
} else {
|
2023-08-17 09:47:40 +00:00
|
|
|
return decl.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
|
|
|
module.exports = Value
|