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
  d744638c_ed47_aa8c_13df_82eac08886a4["isValidArbitrary()"]
  53cf41fe_5903_d247_3bb3_38414ba7d631["parseCandidate()"]
  53cf41fe_5903_d247_3bb3_38414ba7d631 -->|calls| d744638c_ed47_aa8c_13df_82eac08886a4
  db928a82_c6a9_f0a7_efee_5436571565b0["parseModifier()"]
  db928a82_c6a9_f0a7_efee_5436571565b0 -->|calls| d744638c_ed47_aa8c_13df_82eac08886a4
  ca76ae68_c9c0_d977_a6d8_8ba86685bf25["parseVariant()"]
  ca76ae68_c9c0_d977_a6d8_8ba86685bf25 -->|calls| d744638c_ed47_aa8c_13df_82eac08886a4
  style d744638c_ed47_aa8c_13df_82eac08886a4 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
}

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