addWhitespaceAroundMathOperators() — tailwindcss Function Reference
Architecture documentation for the addWhitespaceAroundMathOperators() function in math-operators.ts from the tailwindcss codebase.
Entity Profile
Dependency Diagram
graph TD 6297163c_7d9c_1e1c_ddff_20f6098d38f6["addWhitespaceAroundMathOperators()"] 27cd040f_e3a9_f8b7_3c7c_0547b201eeb0["math-operators.ts"] 6297163c_7d9c_1e1c_ddff_20f6098d38f6 -->|defined in| 27cd040f_e3a9_f8b7_3c7c_0547b201eeb0 8e4634e2_7c1f_b6c1_18b2_bd8f97be3651["decodeArbitraryValue()"] 8e4634e2_7c1f_b6c1_18b2_bd8f97be3651 -->|calls| 6297163c_7d9c_1e1c_ddff_20f6098d38f6 style 6297163c_7d9c_1e1c_ddff_20f6098d38f6 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/tailwindcss/src/utils/math-operators.ts lines 45–205
export function addWhitespaceAroundMathOperators(input: string) {
// Bail early if there are no math functions in the input
if (!MATH_FUNCTIONS.some((fn) => input.includes(fn))) {
return input
}
let result = ''
let formattable: boolean[] = []
let valuePos = null
let lastValuePos = null
for (let i = 0; i < input.length; i++) {
let char = input.charCodeAt(i)
// Track if we see a number followed by a unit, then we know for sure that
// this is not a function call.
if (char >= ZERO && char <= NINE) {
valuePos = i
}
// If we saw a number before, and we see normal a-z character, then we
// assume this is a value such as `123px`
else if (
valuePos !== null &&
(char === PERCENT ||
(char >= LOWER_A && char <= LOWER_Z) ||
(char >= UPPER_A && char <= UPPER_Z))
) {
valuePos = i
}
// Once we see something else, we reset the value position
else {
lastValuePos = valuePos
valuePos = null
}
// Determine if we're inside a math function
if (char === OPEN_PAREN) {
result += input[i]
// Scan backwards to determine the function name. This assumes math
// functions are named with lowercase alphanumeric characters.
let start = i
for (let j = i - 1; j >= 0; j--) {
let inner = input.charCodeAt(j)
if (inner >= ZERO && inner <= NINE) {
start = j // 0-9
} else if (inner >= LOWER_A && inner <= LOWER_Z) {
start = j // a-z
} else {
break
}
}
let fn = input.slice(start, i)
// This is a known math function so start formatting
if (MATH_FUNCTIONS.includes(fn)) {
formattable.unshift(true)
continue
}
// We've encountered nested parens inside a math function, record that and
// keep formatting until we've closed all parens.
else if (formattable[0] && fn === '') {
formattable.unshift(true)
continue
}
// This is not a known math function so don't format it
formattable.unshift(false)
continue
}
// We've exited the function so format according to the parent function's
// type.
else if (char === CLOSE_PAREN) {
Domain
Subdomains
Called By
Source
Frequently Asked Questions
What does addWhitespaceAroundMathOperators() do?
addWhitespaceAroundMathOperators() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/utils/math-operators.ts.
Where is addWhitespaceAroundMathOperators() defined?
addWhitespaceAroundMathOperators() is defined in packages/tailwindcss/src/utils/math-operators.ts at line 45.
What calls addWhitespaceAroundMathOperators()?
addWhitespaceAroundMathOperators() is called by 1 function(s): decodeArbitraryValue.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free