Home / Function/ isValidArbitrary() — tailwindcss Function Reference

isValidArbitrary() — tailwindcss Function Reference

Architecture documentation for the isValidArbitrary() function in is-valid-arbitrary.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  872c3879_82c7_fd6e_ce14_258e27bc4877["isValidArbitrary()"]
  412cf613_647f_106c_8e3c_028b7fc06b26["is-valid-arbitrary.ts"]
  872c3879_82c7_fd6e_ce14_258e27bc4877 -->|defined in| 412cf613_647f_106c_8e3c_028b7fc06b26
  d4b90da0_01b5_b21d_ff05_b37798744576["parseCandidate()"]
  d4b90da0_01b5_b21d_ff05_b37798744576 -->|calls| 872c3879_82c7_fd6e_ce14_258e27bc4877
  0cad61e7_3577_6e5c_a58f_e653598f37ea["parseModifier()"]
  0cad61e7_3577_6e5c_a58f_e653598f37ea -->|calls| 872c3879_82c7_fd6e_ce14_258e27bc4877
  7ba77268_84c7_7083_8f22_251a4a791d25["parseVariant()"]
  7ba77268_84c7_7083_8f22_251a4a791d25 -->|calls| 872c3879_82c7_fd6e_ce14_258e27bc4877
  style 872c3879_82c7_fd6e_ce14_258e27bc4877 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/utils/is-valid-arbitrary.ts lines 27–93

export function isValidArbitrary(input: string) {
  // SAFETY: We can use an index into a shared buffer because this function is
  // synchronous, non-recursive, and runs in a single-threaded environment.
  let stackPos = 0
  let len = input.length

  for (let idx = 0; idx < len; idx++) {
    let char = input.charCodeAt(idx)

    switch (char) {
      case BACKSLASH:
        // The next character is escaped, so we skip it.
        idx += 1
        break
      // Strings should be handled as-is until the end of the string. No need to
      // worry about balancing parens, brackets, or curlies inside a string.
      case SINGLE_QUOTE:
      case DOUBLE_QUOTE:
        // Ensure we don't go out of bounds.
        while (++idx < len) {
          let nextChar = input.charCodeAt(idx)

          // The next character is escaped, so we skip it.
          if (nextChar === BACKSLASH) {
            idx += 1
            continue
          }

          if (nextChar === char) {
            break
          }
        }
        break
      case OPEN_PAREN:
        closingBracketStack[stackPos] = CLOSE_PAREN
        stackPos++
        break
      case OPEN_BRACKET:
        closingBracketStack[stackPos] = CLOSE_BRACKET
        stackPos++
        break
      case OPEN_CURLY:
        // NOTE: We intentionally do not consider `{` to move the stack pointer
        // because a candidate like `[&{color:red}]:flex` should not be valid.
        break
      case CLOSE_BRACKET:
      case CLOSE_CURLY:
      case CLOSE_PAREN:
        if (stackPos === 0) return false

        if (stackPos > 0 && char === closingBracketStack[stackPos - 1]) {
          // SAFETY: The buffer does not need to be mutated because the stack is
          // only ever read from or written to its current position. Its current
          // position is only ever incremented after writing to it. Meaning that
          // the buffer can be dirty for the next use and still be correct since
          // reading/writing always starts at position `0`.
          stackPos--
        }
        break
      case SEMICOLON:
        if (stackPos === 0) return false
        break
    }
  }

  return true
}

Domain

Subdomains

Frequently Asked Questions

What does isValidArbitrary() do?
isValidArbitrary() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/utils/is-valid-arbitrary.ts.
Where is isValidArbitrary() defined?
isValidArbitrary() is defined in packages/tailwindcss/src/utils/is-valid-arbitrary.ts at line 27.
What calls isValidArbitrary()?
isValidArbitrary() is called by 3 function(s): parseCandidate, parseModifier, parseVariant.

Analyze Your Own Codebase

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

Try Supermodel Free