Conversation
Fix: Optimized code structure Optimized code structure Fix: Add support for Css variables
5e88066 to
d2998f7
Compare
HackbrettXXX
left a comment
There was a problem hiding this comment.
Thanks for the PR. Please add some test cases. I'll have a closer look at the code afterwards.
hi, |
HackbrettXXX
left a comment
There was a problem hiding this comment.
Thanks for adding the test case. I think the approach is a bit flawed: the lookup for a variable value includes all available CSS rule. However, it should only include the rules that apply to the current element.
test/specs/css-variables/spec.svg
Outdated
| <style>.bluePolyline {stroke: var(--color); } .polyLineColors {--color: blue;}</style> | ||
| <style>.redPolyline { --colorRed: red; stroke: var(--colorRed);}</style> | ||
| <style>.greenPolyline { stroke: var(--colorGreen);}</style> |
There was a problem hiding this comment.
Just nitpicking, but "textColors" would probably be more understandable than "polylineColors" here.
There was a problem hiding this comment.
Yep , I just copy code from issues. not rename class.
I will modify them
There was a problem hiding this comment.
You’re right, maybe I should rework my code withelement.style.getPropertyValue(“—my-var”);
and add support for custom property fallback values.
I will try to fix it.
Thank you for your review.
There was a problem hiding this comment.
I haven't checked the code too much,
I don't know if I can use the DOM API to get the current element style or analytic CSS-variables key,
if there is no DOM, maybe I can only get the CSS-variables key through Regular Expression.
| <text class="green greenPolyline" y="20" x="20">Value With Space</text> | ||
| <text y="40" x="20" class="rgb" >RGB Color</text> | ||
| <text y="60" x="20" class="space">Var With Space </text> | ||
|
|
There was a problem hiding this comment.
Please add a test case like this
<style>.variable { stroke: var(--color); }</style>
<style>.red { --color red; }</style>
<style>.green { --color green; }</style>
<text class="red variable">Red</text>
<text class="green variable">Green</text>
And a couple of tests that test the inheritance of the variables.
Also add a test case where the variable is not defined.
Add test cases for CSS specificity.
There was a problem hiding this comment.
Sorry, this is my first PR, not well thought out, I will add and test them
There was a problem hiding this comment.
Hi,I fix all bug.and add many test cases.
Looking forward to your review.
QvQ
HackbrettXXX
left a comment
There was a problem hiding this comment.
Sorry for the delay, I was really busy. Thanks for the changes.
test/specs/css-variables/spec.svg
Outdated
|
|
||
| <style>.TestValue { stroke: var(--colorGreen);}</style> | ||
| <style>.green {--colorGreen: #009900 }</style> | ||
| <text class="green greenPolyline" y="20" x="20">Test Value With Space</text> |
There was a problem hiding this comment.
greenPolyline seems to be a leftover.
src/context/stylesheets.ts
Outdated
| getCssValue(selector: string): string | undefined { | ||
| const value: string = selector.replace(/^\s+|\s+$/g, '') | ||
| this.cssVariableList = [] | ||
| if (this.analysisCssVariables(value, 0)) { | ||
| } | ||
| return undefined | ||
| } |
src/context/stylesheets.ts
Outdated
| valueIndex = 0 | ||
| valueName = '' | ||
| //exclude 'var(' and ')' form cssValueString | ||
| for (let ch = 0; ch < valueString.length; ch++) { | ||
| if (valueString[ch] == 'v' && valueString.slice(ch, ch + 4) == 'var(') { | ||
| valueIndex++ | ||
| ch += 3 | ||
| continue | ||
| } | ||
| if (valueString[ch] == ')' && valueIndex > 0) { | ||
| valueIndex-- | ||
| continue | ||
| } | ||
| valueName += valueString[ch] | ||
| } |
There was a problem hiding this comment.
Why do I need this loop? Couldn't we just adjust the slice call?
There was a problem hiding this comment.
The value maybe like this var(--colorOne,var(--colorTwo,rgb(0,0,0))).
I was thinking of removing var( and ),then split with ,
Do you have a better solution?
There was a problem hiding this comment.
Hi!
I refactored my code,
reduced the loop level,
reduced unnecessary calculations,
Put the code in a more logical place.
Looking forward to your review.
src/context/stylesheets.ts
Outdated
| originalCss: valueString, | ||
| valueName: valueName.split(varReg) | ||
| }) | ||
| return this.analysisCssVariables(selector, untreatedIndex + varEnd) |
There was a problem hiding this comment.
I don't like the recursive approach. A loop would probably be more readable and better performance-wise.
src/context/stylesheets.ts
Outdated
| private readonly loadExternalSheets: boolean | ||
| private readonly styleSheets: CSSStyleSheet[] | ||
|
|
||
| private cssVariableList: CssVariable[] |
There was a problem hiding this comment.
I don't think we need the field. Just return the list from analysisCssVariables (or pass a local list as 'out' variable).
src/context/stylesheets.ts
Outdated
| const cssValue: string = mostSpecificRule.style.getPropertyValue(propertyCss) | ||
| const varReg = /var\(.*?\)/gi | ||
| if (cssValue && varReg.test(cssValue)) { | ||
| const originalValue = cssValue.replace(/^\s+|\s+$/g, '') |
src/context/stylesheets.ts
Outdated
| let res = originalValue | ||
| this.cssVariableList = [] | ||
| if (this.analysisCssVariables(originalValue, 0)) { | ||
| this.cssVariableList.map(CssVariable => { |
src/context/stylesheets.ts
Outdated
| for (let i = 0; i < CssVariable.valueName.length; i++) { | ||
| const name = CssVariable.valueName[i].replace(/^\s+|\s+$/g, '') | ||
| if (name.slice(0, 2) == '--') { | ||
| const css = getComputedStyle(node).getPropertyValue(name) |
There was a problem hiding this comment.
getComputedStyle won't work, because the SVG is not part of document.
There was a problem hiding this comment.
It's been a long time,Looking forward to your review.
There was a problem hiding this comment.
Sadly, @HackbrettXXX has not been available in recent months. He will get back to you, I am very sure, once he is back to work. Please allow him a bit more time.
HackbrettXXX
left a comment
There was a problem hiding this comment.
I'm back and finally have time to review this again. Sorry for the very long delay.
Please add still more test cases (like mentioned in my previous comment). Especially for CSS specificity and my example test case.
Please also try to make the code more readable, extract some functions, make the loop more readable, etc.
Hi,
I add a support for Css variables
I analyzed cssValue then capture with
/var\(.*?\)/giProcessing with function getCssValue().
Best regards
Fixes ##206