postcss / postcss-custom-properties

Use Custom Properties in CSS
https://postcss.github.io/postcss-custom-properties
MIT License
596 stars 77 forks source link

fix: regexp for match custom property #190

Closed Scrum closed 4 years ago

Scrum commented 5 years ago

/(^|[^\w-])var\([\W\w]+\)/.test("0var(--test)") => false /var\(--.*?\)/.test("0var(--test)") => true

jonathantneal commented 5 years ago

@Scrum, is 0var(--test) supposed to pass? Can you show an example? For example, with your change, I do not believe the following should pass:

/var\(--.*?\)/.test("avar(--test)") => true
Scrum commented 5 years ago

is 0var(--test) supposed to pass?

yes, because if old browsers are not supported custome property it is falsely transformed into 10px eg

Can you show an example?

@for $step from 0 to 1 {
  .offset-m-$(step) {
    margin: $(step)var(--offset-ext);
  }
}

after iteration we get

.offset-m-0 {
    margin: 0var(--offset-ext);
 }
.offset-m-1 {
    margin: 1var(--offset-ext);
 }

both values 0var(--offset-ext) 1var(--offset-ext) in your pattern will be false

For example, with your change, I do not believe the following should pass:

proof from the console

Screenshot 2019-07-15 at 16 30 18

jonathantneal commented 5 years ago

I’m not sure that’s supposed to work at all.

See https://codepen.io/embed/prefill?default-tab=css,result&data=%7B%22css%22%3A%22body+%7B+margin%3A+0+%7D%5Cnbody+%7B+margin%3A+0var(--awesome%2C+20px)+%7D%22%2C%22html%22%3A%22test%22%7D

Scrum commented 5 years ago

I’m not sure that’s supposed to work at all.

in the case of a native example, this does not work (although I think it is wrong) but in your example the fallback will work.

In my example, the fallback will not be prepared. your regular expression does not fit.

Moreover, if I use the option preserve then I don’t pull the fullback value

postcssCustomProperties({
  preserve: false
});
jonathantneal commented 4 years ago

I’m willing to reopen this, but I am closing it because the examples provided should not pass in valid CSS.