Skip to content

Commit

Permalink
Fix: Add support for Css variables
Browse files Browse the repository at this point in the history
Fix: Optimized code structure

Optimized code structure

Fix: Add support for Css variables
  • Loading branch information
TOKdawn committed Apr 22, 2022
1 parent 0b3a452 commit d2998f7
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/context/stylesheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ export class StyleSheets {
private rootSvg: Element
private readonly loadExternalSheets: boolean
private readonly styleSheets: CSSStyleSheet[]

private cssValueMap: Map<string, string>
constructor(rootSvg: Element, loadExtSheets: boolean) {
this.rootSvg = rootSvg
this.loadExternalSheets = loadExtSheets
this.styleSheets = []
this.cssValueMap = new Map()
}

public async load(): Promise<void> {
Expand Down Expand Up @@ -88,6 +89,27 @@ export class StyleSheets {
}
}

getCssValue(selector: string): string | undefined {
const value: string = selector
.replace(/var\(/g, '')
.replace(/\)/g, '')
.replace(/^\s+|\s+$/g, '')
if (this.cssValueMap.get(value)) {
return this.cssValueMap.get(value)
}
for (const sheet of this.styleSheets) {
for (let i = 0; i < sheet.cssRules.length; i++) {
const rule = sheet.cssRules[i] as CSSStyleRule
const res = rule.style.getPropertyValue(value)
if (res) {
this.cssValueMap.set(value, res)
return res
}
}
}
return undefined
}

private static splitSelectorAtCommas(selectorText: string): string[] {
const initialRegex = /,|["']/g
const closingDoubleQuotesRegex = /[^\\]["]/g
Expand Down Expand Up @@ -184,6 +206,17 @@ export class StyleSheets {
const mostSpecificRule = matchingRules.reduce((previousValue, currentValue) =>
compare(previousValue, currentValue) === 1 ? previousValue : currentValue
)
return mostSpecificRule.style.getPropertyValue(propertyCss) || undefined
let resValue: string = mostSpecificRule.style.getPropertyValue(propertyCss)
const varReg = /var\(.*?\)/gi
if (resValue && varReg.test(resValue)) {
const cssValueList: RegExpMatchArray = resValue.match(varReg) || []
cssValueList.map(cssValue => {
const res = this.getCssValue(cssValue)
if (res) {
resValue = resValue.replace(cssValue, res)
}
})
}
return resValue || undefined
}
}

0 comments on commit d2998f7

Please sign in to comment.