2023-08-17 09:47:40 +00:00
|
|
|
let Browsers = require('./browsers')
|
|
|
|
let vendor = require('./vendor')
|
|
|
|
let utils = require('./utils')
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively clone objects
|
|
|
|
*/
|
2023-08-17 09:47:40 +00:00
|
|
|
function clone(obj, parent) {
|
|
|
|
let cloned = new obj.constructor()
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
for (let i of Object.keys(obj || {})) {
|
|
|
|
let value = obj[i]
|
2022-01-21 08:28:41 +00:00
|
|
|
if (i === 'parent' && typeof value === 'object') {
|
|
|
|
if (parent) {
|
2023-08-17 09:47:40 +00:00
|
|
|
cloned[i] = parent
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
} else if (i === 'source' || i === null) {
|
2023-08-17 09:47:40 +00:00
|
|
|
cloned[i] = value
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (Array.isArray(value)) {
|
2023-08-17 09:47:40 +00:00
|
|
|
cloned[i] = value.map(x => clone(x, cloned))
|
|
|
|
} else if (
|
|
|
|
i !== '_autoprefixerPrefix' &&
|
|
|
|
i !== '_autoprefixerValues' &&
|
|
|
|
i !== 'proxyCache'
|
|
|
|
) {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (typeof value === 'object' && value !== null) {
|
2023-08-17 09:47:40 +00:00
|
|
|
value = clone(value, cloned)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
cloned[i] = value
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
return cloned
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
class Prefixer {
|
|
|
|
constructor(name, prefixes, all) {
|
|
|
|
this.prefixes = prefixes
|
|
|
|
this.name = name
|
|
|
|
this.all = all
|
|
|
|
}
|
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Clone node and clean autprefixer custom caches
|
|
|
|
*/
|
|
|
|
static clone(node, overrides) {
|
|
|
|
let cloned = clone(node)
|
|
|
|
for (let name in overrides) {
|
|
|
|
cloned[name] = overrides[name]
|
|
|
|
}
|
|
|
|
return cloned
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Add hack to selected names
|
|
|
|
*/
|
|
|
|
static hack(klass) {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (!this.hacks) {
|
2023-08-17 09:47:40 +00:00
|
|
|
this.hacks = {}
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
return klass.names.map(name => {
|
|
|
|
this.hacks[name] = klass
|
|
|
|
return this.hacks[name]
|
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Load hacks for some names
|
|
|
|
*/
|
|
|
|
static load(name, prefixes, all) {
|
|
|
|
let Klass = this.hacks && this.hacks[name]
|
2022-01-21 08:28:41 +00:00
|
|
|
if (Klass) {
|
2023-08-17 09:47:40 +00:00
|
|
|
return new Klass(name, prefixes, all)
|
2022-01-21 08:28:41 +00:00
|
|
|
} else {
|
2023-08-17 09:47:40 +00:00
|
|
|
return new this(name, prefixes, all)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Shortcut for Prefixer.clone
|
|
|
|
*/
|
|
|
|
clone(node, overrides) {
|
|
|
|
return Prefixer.clone(node, overrides)
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Find prefix in node parents
|
|
|
|
*/
|
|
|
|
parentPrefix(node) {
|
|
|
|
let prefix
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (typeof node._autoprefixerPrefix !== 'undefined') {
|
2023-08-17 09:47:40 +00:00
|
|
|
prefix = node._autoprefixerPrefix
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (node.type === 'decl' && node.prop[0] === '-') {
|
2023-08-17 09:47:40 +00:00
|
|
|
prefix = vendor.prefix(node.prop)
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (node.type === 'root') {
|
2023-08-17 09:47:40 +00:00
|
|
|
prefix = false
|
|
|
|
} else if (
|
|
|
|
node.type === 'rule' &&
|
|
|
|
node.selector.includes(':-') &&
|
|
|
|
/:(-\w+-)/.test(node.selector)
|
|
|
|
) {
|
|
|
|
prefix = node.selector.match(/:(-\w+-)/)[1]
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (node.type === 'atrule' && node.name[0] === '-') {
|
2023-08-17 09:47:40 +00:00
|
|
|
prefix = vendor.prefix(node.name)
|
2022-01-21 08:28:41 +00:00
|
|
|
} else {
|
2023-08-17 09:47:40 +00:00
|
|
|
prefix = this.parentPrefix(node.parent)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Browsers.prefixes().includes(prefix)) {
|
2023-08-17 09:47:40 +00:00
|
|
|
prefix = false
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
node._autoprefixerPrefix = prefix
|
|
|
|
|
|
|
|
return node._autoprefixerPrefix
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Clone node with prefixes
|
|
|
|
*/
|
|
|
|
process(node, result) {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (!this.check(node)) {
|
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 parent = this.parentPrefix(node)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let prefixes = this.prefixes.filter(
|
|
|
|
prefix => !parent || parent === utils.removeNote(prefix)
|
|
|
|
)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let added = []
|
|
|
|
for (let prefix of prefixes) {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (this.add(node, prefix, added.concat([prefix]), result)) {
|
2023-08-17 09:47:40 +00:00
|
|
|
added.push(prefix)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
return added
|
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 = Prefixer
|