Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Add support for Css variables #209

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
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.replace(/^\s+|\s+$/g, ''))
}
})
}
return resValue || undefined
}
}
1 change: 1 addition & 0 deletions test/common/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ window.tests = [
'complete-organization-chart',
'complete-organization-chart-new',
'complete-social-network',
'css-variables',
'custom-fonts',
'display-none-and-visibility-inheritance',
'duplicate-ids',
Expand Down
Binary file added test/specs/css-variables/reference.pdf
Binary file not shown.
14 changes: 14 additions & 0 deletions test/specs/css-variables/spec.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.