Home / File/ decode-arbitrary-value.ts — tailwindcss Source File

decode-arbitrary-value.ts — tailwindcss Source File

Architecture documentation for decode-arbitrary-value.ts, a typescript file in the tailwindcss codebase. 3 imports, 3 dependents.

File typescript OxideEngine PreProcessors 3 imports 3 dependents 4 functions

Entity Profile

Dependency Diagram

graph LR
  e90d9c51_31f0_3175_a861_610a15e277e5["decode-arbitrary-value.ts"]
  1d3f1613_f144_938f_08f7_49039a46ad49["value-parser.ts"]
  e90d9c51_31f0_3175_a861_610a15e277e5 --> 1d3f1613_f144_938f_08f7_49039a46ad49
  49e6f0af_1e9c_a3fb_0227_698f6d66f3cc["math-operators.ts"]
  e90d9c51_31f0_3175_a861_610a15e277e5 --> 49e6f0af_1e9c_a3fb_0227_698f6d66f3cc
  0fb4e44c_8cd3_11bf_b382_1f29f9ed00b0["addWhitespaceAroundMathOperators"]
  e90d9c51_31f0_3175_a861_610a15e277e5 --> 0fb4e44c_8cd3_11bf_b382_1f29f9ed00b0
  669e6a28_c71f_3c5e_9c53_915cede7da78["candidate.ts"]
  669e6a28_c71f_3c5e_9c53_915cede7da78 --> e90d9c51_31f0_3175_a861_610a15e277e5
  d9b3f9a7_24aa_0263_17df_cd322fafcbd1["decode-arbitrary-value.bench.ts"]
  d9b3f9a7_24aa_0263_17df_cd322fafcbd1 --> e90d9c51_31f0_3175_a861_610a15e277e5
  255bd22e_9af3_f897_fdcf_33b0fca12156["decode-arbitrary-value.test.ts"]
  255bd22e_9af3_f897_fdcf_33b0fca12156 --> e90d9c51_31f0_3175_a861_610a15e277e5
  style e90d9c51_31f0_3175_a861_610a15e277e5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import * as ValueParser from '../value-parser'
import { addWhitespaceAroundMathOperators } from './math-operators'

export function decodeArbitraryValue(input: string): string {
  // There are definitely no functions in the input, so bail early
  if (input.indexOf('(') === -1) {
    return convertUnderscoresToWhitespace(input)
  }

  let ast = ValueParser.parse(input)
  recursivelyDecodeArbitraryValues(ast)
  input = ValueParser.toCss(ast)

  input = addWhitespaceAroundMathOperators(input)

  return input
}

/**
 * Convert `_` to ` `, except for escaped underscores `\_` they should be
 * converted to `_` instead.
 */
function convertUnderscoresToWhitespace(input: string, skipUnderscoreToSpace = false) {
  let output = ''
  for (let i = 0; i < input.length; i++) {
    let char = input[i]

    // Escaped underscore
    if (char === '\\' && input[i + 1] === '_') {
      output += '_'
      i += 1
    }

    // Unescaped underscore
    else if (char === '_' && !skipUnderscoreToSpace) {
      output += ' '
    }

    // All other characters
    else {
      output += char
    }
  }

  return output
}

function recursivelyDecodeArbitraryValues(ast: ValueParser.ValueAstNode[]) {
  for (let node of ast) {
    switch (node.kind) {
      case 'function': {
        if (node.value === 'url' || node.value.endsWith('_url')) {
          // Don't decode underscores in url() but do decode the function name
          node.value = convertUnderscoresToWhitespace(node.value)
          break
        }

        if (
          node.value === 'var' ||
          node.value.endsWith('_var') ||
          node.value === 'theme' ||
          node.value.endsWith('_theme')
        ) {
          node.value = convertUnderscoresToWhitespace(node.value)
          for (let i = 0; i < node.nodes.length; i++) {
            // Don't decode underscores to spaces in the first argument of var()
            if (i == 0 && node.nodes[i].kind === 'word') {
              node.nodes[i].value = convertUnderscoresToWhitespace(node.nodes[i].value, true)
              continue
            }
            recursivelyDecodeArbitraryValues([node.nodes[i]])
          }
          break
        }

        node.value = convertUnderscoresToWhitespace(node.value)
        recursivelyDecodeArbitraryValues(node.nodes)
        break
      }
      case 'separator':
      case 'word': {
        node.value = convertUnderscoresToWhitespace(node.value)
        break
      }
      default:
        never(node)
    }
  }
}

function never(value: never): never {
  throw new Error(`Unexpected value: ${value}`)
}

Domain

Subdomains

Frequently Asked Questions

What does decode-arbitrary-value.ts do?
decode-arbitrary-value.ts is a source file in the tailwindcss codebase, written in typescript. It belongs to the OxideEngine domain, PreProcessors subdomain.
What functions are defined in decode-arbitrary-value.ts?
decode-arbitrary-value.ts defines 4 function(s): convertUnderscoresToWhitespace, decodeArbitraryValue, never, recursivelyDecodeArbitraryValues.
What does decode-arbitrary-value.ts depend on?
decode-arbitrary-value.ts imports 3 module(s): addWhitespaceAroundMathOperators, math-operators.ts, value-parser.ts.
What files import decode-arbitrary-value.ts?
decode-arbitrary-value.ts is imported by 3 file(s): candidate.ts, decode-arbitrary-value.bench.ts, decode-arbitrary-value.test.ts.
Where is decode-arbitrary-value.ts in the architecture?
decode-arbitrary-value.ts is located at packages/tailwindcss/src/utils/decode-arbitrary-value.ts (domain: OxideEngine, subdomain: PreProcessors, directory: packages/tailwindcss/src/utils).

Analyze Your Own Codebase

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

Try Supermodel Free