| 1 | let valueParser = require('postcss-value-parser')
|
|---|
| 2 |
|
|---|
| 3 | function parseValue (value) {
|
|---|
| 4 | let parsed = value.match(/([\d.-]+)(.*)/)
|
|---|
| 5 | if (!parsed || !parsed[1] || !parsed[2] || isNaN(parsed[1])) {
|
|---|
| 6 | return undefined
|
|---|
| 7 | }
|
|---|
| 8 | return [parseFloat(parsed[1]), parsed[2]]
|
|---|
| 9 | }
|
|---|
| 10 |
|
|---|
| 11 | function compose (first, second, third) {
|
|---|
| 12 | if (first && second && third) {
|
|---|
| 13 | return `max(${first}, min(${second}, ${third}))`
|
|---|
| 14 | }
|
|---|
| 15 | if (first && second) {
|
|---|
| 16 | return `max(${first}, ${second})`
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | return first
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | function updateValue (declaration, value, preserve) {
|
|---|
| 23 | let newValue = value
|
|---|
| 24 | let newValueAst = valueParser(value)
|
|---|
| 25 | let valueAST = valueParser(declaration.value)
|
|---|
| 26 | // Walk can't be interrupted, so we only care about first
|
|---|
| 27 | let foundClamp = false
|
|---|
| 28 |
|
|---|
| 29 | valueAST.walk((node, index, nodes) => {
|
|---|
| 30 | let isClamp = node.type === 'function' && node.value === 'clamp'
|
|---|
| 31 |
|
|---|
| 32 | if (!isClamp || foundClamp) {
|
|---|
| 33 | return
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | foundClamp = true
|
|---|
| 37 | nodes[index] = newValueAst
|
|---|
| 38 | })
|
|---|
| 39 |
|
|---|
| 40 | if (foundClamp) {
|
|---|
| 41 | newValue = valueAST.toString()
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | if (preserve) {
|
|---|
| 45 | declaration.cloneBefore({ value: newValue })
|
|---|
| 46 | } else {
|
|---|
| 47 | declaration.value = newValue
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | module.exports = opts => {
|
|---|
| 52 | opts = opts || {}
|
|---|
| 53 | let precalculate = opts.precalculate ? Boolean(opts.precalculate) : false
|
|---|
| 54 | let preserve = opts.preserve ? Boolean(opts.preserve) : false
|
|---|
| 55 |
|
|---|
| 56 | return {
|
|---|
| 57 | postcssPlugin: 'postcss-clamp',
|
|---|
| 58 | Declaration (decl) {
|
|---|
| 59 | if (!decl || !decl.value.includes('clamp')) {
|
|---|
| 60 | return
|
|---|
| 61 | }
|
|---|
| 62 | valueParser(decl.value).walk(node => {
|
|---|
| 63 | let nodes = node.nodes
|
|---|
| 64 | if (
|
|---|
| 65 | node.type !== 'function' ||
|
|---|
| 66 | node.value !== 'clamp' ||
|
|---|
| 67 | nodes.length !== 5
|
|---|
| 68 | ) {
|
|---|
| 69 | return
|
|---|
| 70 | }
|
|---|
| 71 | let first = nodes[0]
|
|---|
| 72 | let second = nodes[2]
|
|---|
| 73 | let third = nodes[4]
|
|---|
| 74 | let naive = compose(
|
|---|
| 75 | valueParser.stringify(first),
|
|---|
| 76 | valueParser.stringify(second),
|
|---|
| 77 | valueParser.stringify(third)
|
|---|
| 78 | )
|
|---|
| 79 | if (!precalculate || second.type !== 'word' || third.type !== 'word') {
|
|---|
| 80 | updateValue(decl, naive, preserve)
|
|---|
| 81 | return
|
|---|
| 82 | }
|
|---|
| 83 | let parsedSecond = parseValue(second.value)
|
|---|
| 84 | let parsedThird = parseValue(third.value)
|
|---|
| 85 | if (parsedSecond === undefined || parsedThird === undefined) {
|
|---|
| 86 | updateValue(decl, naive, preserve)
|
|---|
| 87 | return
|
|---|
| 88 | }
|
|---|
| 89 | let [secondValue, secondUnit] = parsedSecond
|
|---|
| 90 | let [thirdValue, thirdUnit] = parsedThird
|
|---|
| 91 | if (secondUnit !== thirdUnit) {
|
|---|
| 92 | updateValue(decl, naive, preserve)
|
|---|
| 93 | return
|
|---|
| 94 | }
|
|---|
| 95 | let parsedFirst = parseValue(first.value)
|
|---|
| 96 | if (parsedFirst === undefined) {
|
|---|
| 97 | let secondThirdValue = `${secondValue + thirdValue}${secondUnit}`
|
|---|
| 98 | updateValue(
|
|---|
| 99 | decl,
|
|---|
| 100 | compose(valueParser.stringify(first), secondThirdValue),
|
|---|
| 101 | preserve
|
|---|
| 102 | )
|
|---|
| 103 | return
|
|---|
| 104 | }
|
|---|
| 105 | let [firstValue, firstUnit] = parsedFirst
|
|---|
| 106 | if (firstUnit !== secondUnit) {
|
|---|
| 107 | let secondThirdValue = `${secondValue + thirdValue}${secondUnit}`
|
|---|
| 108 | updateValue(
|
|---|
| 109 | decl,
|
|---|
| 110 | compose(valueParser.stringify(first), secondThirdValue),
|
|---|
| 111 | preserve
|
|---|
| 112 | )
|
|---|
| 113 | return
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | updateValue(
|
|---|
| 117 | decl,
|
|---|
| 118 | compose(`${firstValue + secondValue + thirdValue}${secondUnit}`),
|
|---|
| 119 | preserve
|
|---|
| 120 | )
|
|---|
| 121 | })
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | module.exports.postcss = true
|
|---|