File size: 6,393 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { esc } from "./esc"

/**
 * Recursively parse a string to extract Panda token references (curly or with the `token` function syntax)
 * Allows nested token references, e.g. `token(colors.xxx.yyy, token(colors.aaa.bbb, blue))`
 * Properly ignore CSS vars in fallback syntax, e.g. `token(colors.xxx.yyy, var(--some-var, var(--can-be-nested, blue)))`
 */
export const expandTokenReferences = (
  str: string,
  resolve: (path: string) => string | undefined,
) => {
  let expanded = ""
  let index = 0

  type State = "char" | "token" | "fallback"
  let state = "char" as State
  let tokenPath = ""
  let fallback = ""
  const currentStates = [] as State[]

  while (index < str.length) {
    const char = str[index]

    // Starting a curly bracket token reference
    // `{colors.xxx.yyy}`
    if (char === "{") {
      const endIndex = str.indexOf("}", index)
      // Invalid string, a curly bracket was not closed
      if (endIndex === -1) {
        break
      }

      // Jump to the end of the curly bracket
      // Add the resolved token (CSS var) to the expanded string
      const path = str.slice(index + 1, endIndex)
      const resolved = resolve(path)
      expanded += resolved ?? path
      index = endIndex + 1
      continue
    }

    if (state === "token") {
      // Found a token ref fallback
      // `token(xxx, yyy)`
      if (char === ",") {
        if (str[index] === "") {
          index++
        }

        state = "fallback"
        currentStates.push(state)
        // `token(colors.xxx.yyy, colors.aaa.bbb)`
        // `token(colors.xxx.yyy, blue)`
        const resolved = resolve(tokenPath)
        if (resolved?.endsWith(")")) {
          // we need to get rid of this parenthesis for the fallback syntax
          // var(--colors-xxx-yyy), var(--colors-aaa-bbb)
          //                     ^
          expanded += resolved.slice(0, -1)
        }

        tokenPath = ""
        fallback = ""
        continue
      }
    }

    // `token(colors.xxx.yyy, colors.aaa.bbb)`
    //                      ^^^^^^^^^^^^^^^^^
    // `token(colors.xxx.yyy, blue)`
    //                      ^^^^^^^
    if (state === "fallback") {
      const nextFallback = fallback + char

      // `token(colors.xxx.yyy, var(--some-var, var(--can-be-nested, blue)))`
      //                      ^^^^^^
      if (nextFallback === ", var(") {
        // Compute the end index of the (fallback) CSS var
        const innerEndIndex = cssVarParser(str.slice(index + 1))
        const endIndex = innerEndIndex + index + 1

        // That's the last parenthesis of the CSS var
        // `token(colors.xxx.yyy, var(--some-var, var(--can-be-nested, blue)))`
        //                                                                  ^

        // Now we can extract the inner (fallback) CSS var content
        // `token(colors.xxx.yyy, var(--some-var, var(--can-be-nested, blue)))`
        //                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        const cssVar = str.slice(index + 1, endIndex)
        if (endIndex === -1) {
          break
        }

        // Add it to the expanded string and go back to the previous state
        expanded += ", var(" + cssVar + ")"
        index = endIndex + 1
        state = currentStates.pop() ?? state
        fallback = ""
        continue
      }
    }

    // Already inside a token fn reference
    if (state === "token" || state === "fallback") {
      index++

      // Closing a token fn reference
      // `token(colors.xxx.yyy)`
      //                      ^
      // `token(colors.xxx.yyy, blue)`
      //                            ^
      // `token(colors.xxx.yyy, var(--some-var, var(--can-be-nested, blue)))`
      //                                                                   ^
      if (char === ")") {
        state = currentStates.pop() ?? state ?? "char"
        fallback += char

        // Try to resolve the token path, which is the left part of the token fn
        // `token(tokenPath, fallback))`
        //        ^^^^^^^^^
        const resolved = tokenPath
          ? (resolve(tokenPath) ?? esc(tokenPath))
          : tokenPath

        if (fallback) {
          // `, colors.xxx.yyy` -> `colors.xxx.yyy`
          fallback = fallback.slice(1).trim()

          if (!fallback.startsWith("token(") && fallback.endsWith(")")) {
            fallback = fallback.slice(0, -1)
          }

          if (fallback.includes("token(")) {
            const parsed = expandTokenReferences(fallback, resolve)
            if (parsed) {
              fallback = parsed.slice(0, -1)
            }
          } else if (fallback) {
            const resolvedFallback = resolve(fallback)
            if (resolvedFallback) {
              fallback = resolvedFallback
            }
          }
        }

        const lastChar = expanded.at(-1)
        if (fallback) {
          if (lastChar?.trim()) {
            expanded += resolved.slice(0, -1) + (", " + fallback + ")")
          } else {
            expanded += fallback
          }
        } else {
          expanded += resolved || ")"
        }

        tokenPath = ""
        fallback = ""
        state = "char"
        continue
      }

      if (state === "token") {
        tokenPath += char
      }
      if (state === "fallback") {
        fallback += char
      }

      continue
    }

    const tokenIndex = str.indexOf("token(", index)
    // Starting a token fn reference, jump to the first character inside it
    // `token(colors.xxx.yyy)`
    if (tokenIndex !== -1) {
      const innerTokenIndex = tokenIndex + "token(".length
      expanded += str.slice(index, tokenIndex)
      index = innerTokenIndex
      state = "token"
      currentStates.push(state)
      continue
    }

    expanded += char
    index++
  }

  return expanded
}

/**
 * Finds the closing parenthesis index in a string starting from the start of a CSS var
 * @example cssVarParser('--colors-red-100, var(--colors-blue-200, var(--fallback)))')
 */
const cssVarParser = (str: string) => {
  let index = 0
  const openedParenthesises = ["("]

  while (index < str.length) {
    const char = str[index]
    if (char === "(") {
      openedParenthesises.push(char)
    } else if (char === ")") {
      openedParenthesises.pop()
      if (openedParenthesises.length === 0) {
        return index
      }
    }
    index++
  }

  return index
}