less.js icon indicating copy to clipboard operation
less.js copied to clipboard

fix: Output correct css when using css function in max().

Open lumburr opened this issue 3 years ago • 10 comments

What: fix #3604

Why:

How: For the following ast node

[Dimension, Expression]

less will skip non-Dimension nodes https://github.com/less/less.js/blob/master/packages/less/src/less/functions/number.js#L26-L31 So, max(1, calc(1 + 1)) is treated as max(1).

We should not skip these nodes. Checklist:

  • [ ] Documentation
  • [x] Added/updated unit tests
  • [x] Code complete

lumburr avatar Mar 31 '22 01:03 lumburr

Thanks for contribution.

iChenLei avatar Mar 31 '22 02:03 iChenLei

So, max(1, calc(1 + 1)) is treated as max(1).

That doesn't seem correct. max(1, calc(1 + 1)) should output max(1, calc(1 + 1)) in the CSS. Similar for min(). See other function escaping code for functions like rgb() which output as-is if something like a var() statement is used.

matthew-dean avatar Apr 21 '22 20:04 matthew-dean

In rgb((0 128 var(--num)), the program tries to convert var(--number) into a number,Throws an exception if the conversion cannot be performed. https://github.com/less/less.js/blob/1eafc06db4ed062761ce6ca65044204cf32308a1/packages/less/src/less/functions/color.js#L40-L51 So, we also throw an exception on non-Dimension value. Since the node was not successfully converted, Less will be output as is

lumburr avatar Apr 24 '22 08:04 lumburr

@lumburr That's true but this will output as-is if compiled in Less.

.rule {
  color: rgb(0 128 var(--num));
}

So I'm saying, this in Less:

.rule {
  value: max(1, calc(1 + 1));
  }

...should output:

.rule {
   value: max(1, calc(1 + 1));
 }

matthew-dean avatar Apr 24 '22 22:04 matthew-dean

Ohh,@matthew-dean I may not have expressed it clearly. You are completely correct that in Less:

.rule {
  value: max(1, calc(1 + 1));
}

should output

.rule {
  value: max(1, calc(1 + 1));
}

And now the issue is caused because: max(1, calc(1 + 1)) is treated as max(1)

So what this PR does is to make Less output

.rule {
  value: max(1, calc(1 + 1));
}

correctly again.

lumburr avatar Apr 25 '22 02:04 lumburr

Oops, can you resolve conflicts?

matthew-dean avatar Aug 20 '22 17:08 matthew-dean

Hi @lumburr, thanks for your contribution. Is there a chance you'll be able to fix merge conflicts here so that this PR can be merged?

bugron avatar Jan 12 '23 10:01 bugron

@iChenLei Would you have time to resolve conflicts and add test cases for this issue to see if they are resolved?

matthew-dean avatar Mar 25 '24 15:03 matthew-dean

Or @lumburr do you have time to do this?

matthew-dean avatar Mar 25 '24 15:03 matthew-dean

@matthew-dean I've finished this in another pr #4266, could you CR and merge it? 😁

SoonIter avatar Mar 26 '24 08:03 SoonIter