@@ -5,11 +5,12 @@ export class StyleSheets {
55 private rootSvg : Element
66 private readonly loadExternalSheets : boolean
77 private readonly styleSheets : CSSStyleSheet [ ]
8-
8+ private cssValueMap : Map < string , string >
99 constructor ( rootSvg : Element , loadExtSheets : boolean ) {
1010 this . rootSvg = rootSvg
1111 this . loadExternalSheets = loadExtSheets
1212 this . styleSheets = [ ]
13+ this . cssValueMap = new Map ( )
1314 }
1415
1516 public async load ( ) : Promise < void > {
@@ -88,6 +89,27 @@ export class StyleSheets {
8889 }
8990 }
9091
92+ getCssValue ( selector : string ) : string | undefined {
93+ const value : string = selector
94+ . replace ( / v a r \( / g, '' )
95+ . replace ( / \) / g, '' )
96+ . replace ( / ^ \s + | \s + $ / g, '' )
97+ if ( this . cssValueMap . get ( value ) ) {
98+ return this . cssValueMap . get ( value )
99+ }
100+ for ( const sheet of this . styleSheets ) {
101+ for ( let i = 0 ; i < sheet . cssRules . length ; i ++ ) {
102+ const rule = sheet . cssRules [ i ] as CSSStyleRule
103+ const res = rule . style . getPropertyValue ( value )
104+ if ( res ) {
105+ this . cssValueMap . set ( value , res )
106+ return res
107+ }
108+ }
109+ }
110+ return undefined
111+ }
112+
91113 private static splitSelectorAtCommas ( selectorText : string ) : string [ ] {
92114 const initialRegex = / , | [ " ' ] / g
93115 const closingDoubleQuotesRegex = / [ ^ \\ ] [ " ] / g
@@ -184,6 +206,18 @@ export class StyleSheets {
184206 const mostSpecificRule = matchingRules . reduce ( ( previousValue , currentValue ) =>
185207 compare ( previousValue , currentValue ) === 1 ? previousValue : currentValue
186208 )
187- return mostSpecificRule . style . getPropertyValue ( propertyCss ) || undefined
209+ const cssValue : string = mostSpecificRule . style . getPropertyValue ( propertyCss )
210+ const varReg = / v a r \( .* ?\) / gi
211+ if ( cssValue && varReg . test ( cssValue ) ) {
212+ const cssValueList : RegExpMatchArray = cssValue . match ( varReg ) || [ ]
213+ cssValueList . map ( cssValue => {
214+ console . log ( cssValue , this . styleSheets )
215+ const res = this . getCssValue ( cssValue )
216+ if ( res ) {
217+ cssValue = cssValue . replace ( cssValue , res )
218+ }
219+ } )
220+ }
221+ return cssValue || undefined
188222 }
189223}
0 commit comments