2021-04-02 11:16:15 -04:00
|
|
|
let comparisonMap = {
|
|
|
|
|
atrule: ['name', 'params'],
|
|
|
|
|
rule: ['selector'],
|
|
|
|
|
}
|
|
|
|
|
let types = new Set(Object.keys(comparisonMap))
|
|
|
|
|
|
2021-04-02 14:12:47 -04:00
|
|
|
function collapseAdjacentRules() {
|
2021-04-02 11:16:15 -04:00
|
|
|
return (root) => {
|
|
|
|
|
let currentRule = null
|
|
|
|
|
root.each((node) => {
|
|
|
|
|
if (!types.has(node.type)) {
|
|
|
|
|
currentRule = null
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentRule === null) {
|
|
|
|
|
currentRule = node
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let properties = comparisonMap[node.type]
|
|
|
|
|
|
|
|
|
|
if (node.type === 'atrule' && node.name === 'font-face') {
|
|
|
|
|
currentRule = node
|
|
|
|
|
} else if (properties.every((property) => node[property] === currentRule[property])) {
|
|
|
|
|
currentRule.append(node.nodes)
|
|
|
|
|
node.remove()
|
|
|
|
|
} else {
|
|
|
|
|
currentRule = node
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = collapseAdjacentRules
|