@@ -14,6 +14,7 @@ import {
1414 SupportedLanguageIds ,
1515 VAR_KEYWORD_REVERSE ,
1616 CACHE ,
17+ PATTERN_ALL_VARIABLE_USAGES ,
1718} from "./constants" ;
1819import { CSSVarDeclarations } from "./main" ;
1920import { serializeColor } from "./color-parser" ;
@@ -100,6 +101,7 @@ export const populateValue = async (
100101 // Get Color for each, and modify the cssVar Record.
101102 const vars = getCSSDeclarationArray ( cssVars ) ;
102103
104+ // TODO(phoenisx) Remove this state.
103105 const cssVarsMapToSelf = vars . reduce ( ( defs , cssVar ) => {
104106 defs [ cssVar . property ] = cssVar ;
105107 return defs ;
@@ -110,7 +112,7 @@ export const populateValue = async (
110112 for await ( const cssVar of vars ) {
111113 const isVariable = getVariableType ( cssVar . value ) ;
112114 if ( typeof isVariable === "string" ) {
113- const value = getValue ( getVariableName ( cssVar . value ) , cssVarsMapToSelf ) ;
115+ const value = getValue ( getVariableName ( cssVar . value ) , vars ) ;
114116 cssVar . value = value || cssVar . value ;
115117 }
116118
@@ -124,16 +126,28 @@ export const populateValue = async (
124126} ;
125127
126128function getValue (
127- name : string ,
128- cssVarsMapToSelf : Record < string , CSSVarDeclarations | null >
129+ prop : string , // Points to CSS variable name (a.k.a Propname in a CSS declarations)
130+ cssVars : CSSVarDeclarations [ ]
129131) : string | null {
130- const currentCssVar = cssVarsMapToSelf [ name ] ;
132+ const currentCssVar = cssVars . reduce < CSSVarDeclarations | null > (
133+ ( currentCssVar , cssVar ) => {
134+ if (
135+ ! currentCssVar &&
136+ cssVar . property === prop &&
137+ ! cssVar . value . match ( PATTERN_ALL_VARIABLE_USAGES )
138+ ) {
139+ currentCssVar = cssVar ;
140+ }
141+ return currentCssVar ;
142+ } ,
143+ null
144+ ) ;
131145 if ( currentCssVar ) {
132146 const variableType = getVariableType ( currentCssVar . value ) ;
133147 if ( variableType === null ) {
134148 return currentCssVar . value ;
135149 } else {
136- return getValue ( getVariableName ( currentCssVar . value ) , cssVarsMapToSelf ) ;
150+ return getValue ( getVariableName ( currentCssVar . value ) , cssVars ) ;
137151 }
138152 }
139153
0 commit comments