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
  d94e70b3_9bf8_d390_b32f_1a5bd7d409d9["isValidArbitrary()"]
  0bec5ca9_74c8_dcc7_ec12_6404fb6493bd["parseCandidate()"]
  0bec5ca9_74c8_dcc7_ec12_6404fb6493bd -->|calls| d94e70b3_9bf8_d390_b32f_1a5bd7d409d9
  62a1e81d_e57d_83b1_bc05_4b2210993980["parseModifier()"]
  62a1e81d_e57d_83b1_bc05_4b2210993980 -->|calls| d94e70b3_9bf8_d390_b32f_1a5bd7d409d9
  ceb1ff41_a641_27dd_6289_7f779710b411["parseVariant()"]
  ceb1ff41_a641_27dd_6289_7f779710b411 -->|calls| d94e70b3_9bf8_d390_b32f_1a5bd7d409d9
  style d94e70b3_9bf8_d390_b32f_1a5bd7d409d9 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.
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