Skip to content

Commit c1508eb

Browse files
committed
Refactor(CSS): Replace core.math-fn with core.math transformer and update clamp handling
1 parent 7c6423a commit c1508eb

5 files changed

Lines changed: 78 additions & 75 deletions

File tree

‎packages/core/src/config/functions.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const functions = {
3333
oklab: { unit: '' },
3434
oklch: { unit: '' },
3535
'light-dark': { unit: '' },
36-
clamp: { transformer: ['core.math-fn', { name: 'clamp' }] },
36+
clamp: { transformer: ['core.math', { name: 'clamp', wrapArguments: true }] },
3737
repeat: { unit: '' },
3838
'linear-gradient': {},
3939
'radial-gradient': {},
@@ -49,7 +49,7 @@ const functions = {
4949
'scaleY': { unit: '' },
5050
'scaleZ': { unit: '' },
5151
$: { transformer: 'core.variable' },
52-
calc: { transformer: 'core.calc' },
52+
calc: { transformer: 'core.math' },
5353
} satisfies Record<string, FunctionDefinition>
5454

55-
export default functions
55+
export default functions

‎packages/core/src/function-transformers/core.math-fn.ts‎

Lines changed: 0 additions & 61 deletions
This file was deleted.

packages/core/src/function-transformers/core.calc.ts renamed to packages/core/src/function-transformers/core.math.ts

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,68 @@ import { StringValueComponent } from '../types/syntax'
33
import functions from '../config/functions'
44
import { BASE_UNIT_REGEX } from '../common'
55

6-
export default function coreCalc(this: SyntaxRule, value: string, bypassVariableNames: string[]) {
6+
interface CoreMathData {
7+
name?: string
8+
wrapArguments?: boolean
9+
}
10+
11+
function splitTopLevelArguments(value: string) {
12+
const parts: string[] = []
13+
let depth = 0
14+
let start = 0
15+
for (let i = 0; i < value.length; i++) {
16+
const char = value[i]
17+
if (char === '(') {
18+
depth++
19+
} else if (char === ')') {
20+
depth--
21+
} else if (char === ',' && depth === 0) {
22+
parts.push(value.slice(start, i), char)
23+
start = i + 1
24+
}
25+
}
26+
parts.push(value.slice(start))
27+
return parts
28+
}
29+
30+
function hasTopLevelOperator(value: string) {
31+
let depth = 0
32+
for (let i = 0; i < value.length; i++) {
33+
const char = value[i]
34+
if (char === '(') {
35+
depth++
36+
} else if (char === ')') {
37+
depth--
38+
} else if (depth === 0 && (char === '+' || char === '-' || char === '*' || char === '/')) {
39+
let previousIndex = i - 1
40+
while (value[previousIndex] === ' ') {
41+
previousIndex--
42+
}
43+
const previousChar = value[previousIndex]
44+
if ((char === '+' || char === '-') && (!previousChar || previousChar === '(' || previousChar === ',' || previousChar === ' ')) {
45+
continue
46+
}
47+
return true
48+
}
49+
}
50+
return false
51+
}
52+
53+
function wrapCalcArguments(value: string) {
54+
return splitTopLevelArguments(value).map((part) => {
55+
if (part === ',') return part
56+
const trimmed = part.trim()
57+
if (!trimmed || /^(?:calc|clamp|min|max|var)\(/.test(trimmed) || !hasTopLevelOperator(trimmed)) {
58+
return part
59+
}
60+
const leadingWhitespace = part.match(/^\s*/)![0]
61+
const trailingWhitespace = part.match(/\s*$/)![0]
62+
return `${leadingWhitespace}calc(${trimmed})${trailingWhitespace}`
63+
}).join('')
64+
}
65+
66+
export default function coreMath(this: SyntaxRule, value: string, bypassVariableNames: string[], data?: CoreMathData) {
67+
const functionName = data?.name ?? 'calc'
768
const valueComponents: SyntaxRule['valueComponents'] = []
869
let i = 0
970

@@ -201,5 +262,10 @@ export default function coreCalc(this: SyntaxRule, value: string, bypassVariable
201262
handleUnitChecking()
202263
}
203264
anaylzeDeeply(valueComponents, false, false, false, false)
204-
return 'calc(' + this.resolveValue(valueComponents, this.definition.unit, bypassVariableNames, true) + ')'
205-
}
265+
266+
let resolvedValue = this.resolveValue(valueComponents, this.definition.unit, bypassVariableNames, true)
267+
if (data?.wrapArguments) {
268+
resolvedValue = wrapCalcArguments(resolvedValue)
269+
}
270+
return functionName + '(' + resolvedValue + ')'
271+
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import coreCalc from './core.calc'
2-
import coreMathFn from './core.math-fn'
1+
import coreMath from './core.math'
32
import coreVariable from './core.variable'
43

54
const functionTransformers = {
6-
'core.calc': coreCalc,
7-
'core.math-fn': coreMathFn,
5+
'core.math': coreMath,
86
'core.variable': coreVariable,
97
}
108

119
export default functionTransformers
1210

13-
export type FunctionTransformerNames = keyof typeof functionTransformers
11+
export type FunctionTransformerNames = keyof typeof functionTransformers

‎packages/core/tests/issue-358-clamp.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ describe('issue #358: clamp() with bare arithmetic in middle arg', () => {
1919
test('clamp() args that are already calc() are not double-wrapped', () => {
2020
const css = new MasterCSS(undefined, defaultConfig)
2121
const rule = css.create('font-size:clamp(1rem,calc(2vw+1rem),3rem)')
22-
expect(rule?.text).toMatch(/clamp\(1rem,calc\(2vw\s*\+\s*1rem\),3rem\)/)
22+
expect(rule?.text).toMatch(/clamp\(1rem,\s*calc\(2vw\s*\+\s*1rem\),\s*3rem\)/)
2323
expect(rule?.text).not.toContain('calc(calc(')
2424
})
2525

2626
test('clamp() with negative leading values still parses correctly', () => {
2727
const css = new MasterCSS(undefined, defaultConfig)
2828
const rule = css.create('font-size:clamp(-1rem,2vw,3rem)')
2929
// Single token with leading minus is a sign, not arithmetic — no calc wrapping.
30-
expect(rule?.text).toContain('clamp(-1rem,2vw,3rem)')
30+
expect(rule?.text).toContain('clamp(-1rem, 2vw, 3rem)')
3131
})
3232

3333
test('clamp() with calc() wrapper still works (regression)', () => {
@@ -42,6 +42,6 @@ describe('issue #358: clamp() with bare arithmetic in middle arg', () => {
4242
const css = new MasterCSS(undefined, defaultConfig)
4343
const rule = css.create('font-size:clamp(1rem,2vw,3rem)')
4444
expect(rule).toBeDefined()
45-
expect(rule?.text).toContain('clamp(1rem,2vw,3rem)')
45+
expect(rule?.text).toContain('clamp(1rem, 2vw, 3rem)')
4646
})
4747
})

0 commit comments

Comments
 (0)