Home / Function/ substituteFunctionsInValue() — tailwindcss Function Reference

substituteFunctionsInValue() — tailwindcss Function Reference

Architecture documentation for the substituteFunctionsInValue() function in canonicalize-candidates.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  80648867_f6ce_78f3_6e5a_59822ca19561["substituteFunctionsInValue()"]
  7d350d81_5de1_f9f3_5b2c_19ec8fd3c37e["canonicalize-candidates.ts"]
  80648867_f6ce_78f3_6e5a_59822ca19561 -->|defined in| 7d350d81_5de1_f9f3_5b2c_19ec8fd3c37e
  77acef4a_feff_6e9f_7c6b_2b6942c9ad63["createConverterCache()"]
  77acef4a_feff_6e9f_7c6b_2b6942c9ad63 -->|calls| 80648867_f6ce_78f3_6e5a_59822ca19561
  1af71b0a_110a_194a_35c3_5161cc11f054["eventuallyUnquote()"]
  80648867_f6ce_78f3_6e5a_59822ca19561 -->|calls| 1af71b0a_110a_194a_35c3_5161cc11f054
  ed78da58_8727_ad98_120c_61f35cea357a["walk()"]
  80648867_f6ce_78f3_6e5a_59822ca19561 -->|calls| ed78da58_8727_ad98_120c_61f35cea357a
  2da63033_d079_7b37_5cfb_3877674a70b9["toCss()"]
  80648867_f6ce_78f3_6e5a_59822ca19561 -->|calls| 2da63033_d079_7b37_5cfb_3877674a70b9
  b9517e77_a36f_4751_899c_27d813f3dbb3["parse()"]
  80648867_f6ce_78f3_6e5a_59822ca19561 -->|calls| b9517e77_a36f_4751_899c_27d813f3dbb3
  style 80648867_f6ce_78f3_6e5a_59822ca19561 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/canonicalize-candidates.ts lines 801–879

function substituteFunctionsInValue(
  ast: ValueParser.ValueAstNode[],
  handle: (value: string, fallback?: string) => string | null,
) {
  walk(ast, (node, ctx) => {
    if (node.kind === 'function' && node.value === 'theme') {
      if (node.nodes.length < 1) return

      // Ignore whitespace before the first argument
      if (node.nodes[0].kind === 'separator' && node.nodes[0].value.trim() === '') {
        node.nodes.shift()
      }

      let pathNode = node.nodes[0]
      if (pathNode.kind !== 'word') return

      let path = pathNode.value

      // For the theme function arguments, we require all separators to contain
      // comma (`,`), spaces alone should be merged into the previous word to
      // avoid splitting in this case:
      //
      // theme(--color-red-500 / 75%) theme(--color-red-500 / 75%, foo, bar)
      //
      // We only need to do this for the first node, as the fallback values are
      // passed through as-is.
      let skipUntilIndex = 1
      for (let i = skipUntilIndex; i < node.nodes.length; i++) {
        if (node.nodes[i].value.includes(',')) {
          break
        }
        path += ValueParser.toCss([node.nodes[i]])
        skipUntilIndex = i + 1
      }

      path = eventuallyUnquote(path)
      let fallbackValues = node.nodes.slice(skipUntilIndex + 1)

      let replacement =
        fallbackValues.length > 0 ? handle(path, ValueParser.toCss(fallbackValues)) : handle(path)
      if (replacement === null) return

      if (ctx.parent) {
        let idx = ctx.parent.nodes.indexOf(node) - 1
        while (idx !== -1) {
          let previous = ctx.parent.nodes[idx]
          // Skip the space separator
          if (previous.kind === 'separator' && previous.value.trim() === '') {
            idx -= 1
            continue
          }

          // If the previous node is a word and contains an operator, we need to
          // wrap the replacement in parentheses to make the output less
          // ambiguous.
          //
          // Input:
          // - `calc(100dvh-theme(spacing.2))`
          //
          // Output:
          // - `calc(100dvh-(--spacing(2)))`
          //
          // Not:
          // -`calc(100dvh---spacing(2))`
          //
          if (/^[-+*/]$/.test(previous.value.trim())) {
            replacement = `(${replacement})`
          }

          break
        }
      }

      return WalkAction.Replace(ValueParser.parse(replacement))
    }
  })

  return ValueParser.toCss(ast)
}

Subdomains

Frequently Asked Questions

What does substituteFunctionsInValue() do?
substituteFunctionsInValue() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/canonicalize-candidates.ts.
Where is substituteFunctionsInValue() defined?
substituteFunctionsInValue() is defined in packages/tailwindcss/src/canonicalize-candidates.ts at line 801.
What does substituteFunctionsInValue() call?
substituteFunctionsInValue() calls 4 function(s): eventuallyUnquote, parse, toCss, walk.
What calls substituteFunctionsInValue()?
substituteFunctionsInValue() is called by 1 function(s): createConverterCache.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free