MadLittleMods / postcss-css-variables

PostCSS plugin to transform CSS Custom Properties(CSS variables) syntax into a static representation
https://madlittlemods.github.io/postcss-css-variables/playground/
Other
536 stars 62 forks source link

Fix algorithm to find balanced var() pairs #112

Closed Poetro closed 4 years ago

Poetro commented 4 years ago

Follow-up to PR https://github.com/MadLittleMods/postcss-css-variables/pull/112

Fix https://github.com/MadLittleMods/postcss-css-variables/issues/114

The parenthesis matching was not checking the right balance before. For the declaration value var(--box-shadow, 0px 2px 8px 0px rgba(0, 0, 0, 0.5)) it would extract the following parts

{
  "pre": "",
  "body": "--box-shadow, 0px 2px 8px 0px rgba(0, 0, 0, 0.5",
  "post": ")"
}

as it finds the var( look for the closing ) token, which it then finds just after 0.5. Instead it should look for ( ) pairs, and check if the opening ( is after var. This way in the previous case, it would find:

{
  "pre": "",
  "body": "--box-shadow, 0px 2px 8px 0px rgba(0, 0, 0, 0.5)",
  "post": ""
}
MadLittleMods commented 4 years ago

Thanks for the contribution @Poetro! Sorry for the delay 🙇

Appreciate the clear code and tests ❤️