wishthis/node_modules/autoprefixer/lib/hacks/mask-composite.js

89 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-08-17 09:47:40 +00:00
let Declaration = require('../declaration')
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
class MaskComposite extends Declaration {
2022-01-21 08:28:41 +00:00
/**
* Prefix mask-composite for webkit
*/
2023-08-17 09:47:40 +00:00
insert(decl, prefix, prefixes) {
let isCompositeProp = decl.prop === 'mask-composite'
let compositeValues
2022-01-21 08:28:41 +00:00
if (isCompositeProp) {
2023-08-17 09:47:40 +00:00
compositeValues = decl.value.split(',')
2022-01-21 08:28:41 +00:00
} else {
2023-08-17 09:47:40 +00:00
compositeValues = decl.value.match(MaskComposite.regexp) || []
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
compositeValues = compositeValues.map(el => el.trim()).filter(el => el)
let hasCompositeValues = compositeValues.length
let compositeDecl
2022-01-21 08:28:41 +00:00
if (hasCompositeValues) {
2023-08-17 09:47:40 +00:00
compositeDecl = this.clone(decl)
compositeDecl.value = compositeValues
.map(value => MaskComposite.oldValues[value] || value)
.join(', ')
2022-01-21 08:28:41 +00:00
if (compositeValues.includes('intersect')) {
2023-08-17 09:47:40 +00:00
compositeDecl.value += ', xor'
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
compositeDecl.prop = prefix + 'mask-composite'
2022-01-21 08:28:41 +00:00
}
if (isCompositeProp) {
if (!hasCompositeValues) {
2023-08-17 09:47:40 +00:00
return undefined
2022-01-21 08:28:41 +00:00
}
if (this.needCascade(decl)) {
2023-08-17 09:47:40 +00:00
compositeDecl.raws.before = this.calcBefore(prefixes, decl, prefix)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
return decl.parent.insertBefore(decl, compositeDecl)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
let cloned = this.clone(decl)
cloned.prop = prefix + cloned.prop
2022-01-21 08:28:41 +00:00
if (hasCompositeValues) {
2023-08-17 09:47:40 +00:00
cloned.value = cloned.value.replace(MaskComposite.regexp, '')
2022-01-21 08:28:41 +00:00
}
if (this.needCascade(decl)) {
2023-08-17 09:47:40 +00:00
cloned.raws.before = this.calcBefore(prefixes, decl, prefix)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
decl.parent.insertBefore(decl, cloned)
2022-01-21 08:28:41 +00:00
if (!hasCompositeValues) {
2023-08-17 09:47:40 +00:00
return decl
2022-01-21 08:28:41 +00:00
}
if (this.needCascade(decl)) {
2023-08-17 09:47:40 +00:00
compositeDecl.raws.before = this.calcBefore(prefixes, decl, prefix)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
return decl.parent.insertBefore(decl, compositeDecl)
}
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
MaskComposite.names = ['mask', 'mask-composite']
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
MaskComposite.oldValues = {
2022-01-21 08:28:41 +00:00
add: 'source-over',
2023-08-17 09:47:40 +00:00
exclude: 'xor',
2022-01-21 08:28:41 +00:00
intersect: 'source-in',
2023-08-17 09:47:40 +00:00
subtract: 'source-out'
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
MaskComposite.regexp = new RegExp(
`\\s+(${Object.keys(MaskComposite.oldValues).join(
'|'
)})\\b(?!\\))\\s*(?=[,])`,
'ig'
)
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
module.exports = MaskComposite