Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}