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

Improve normalisation of calc()-like functions #11686

Merged
merged 3 commits into from Jul 26, 2023

Conversation

RobinMalfait
Copy link
Contributor

@RobinMalfait RobinMalfait commented Jul 26, 2023

This PR improves the normalisation of calc() functions.

In CSS, functions such as calc() require spaces around the operators. However, when writing Tailwind CSS classes we can't really use spaces. There are 2 solutions to this problem:

  1. You write a _ instead of a space, which we will convert to a space behind the scenes
  2. You don't write any spaces or _ characters, and you let Tailwind inject the spaces for you

Typically, you would let Tailwind inject the spaces, but we didn't catch all cases. This PR improves that by introducing a mini-parser for handling spaces around operators.

Looking at the examples given in #11682, then the updated result would look like this:

Input Before After
calc((7-32)/(1400-782)) calc((7-32) / (1400 - 782)) calc((7 - 32) / (1400 - 782))
calc((7-3)/(1400-782)) calc((7 - 3) / (1400 - 782)) calc((7 - 3) / (1400 - 782))
calc((7-32)/(1400-782)) calc((7-32) / (1400 - 782)) calc((7 - 32) / (1400 - 782))
calc((70-3)/(1400-782)) calc((70 - 3) / (1400 - 782)) calc((70 - 3) / (1400 - 782))
calc((70-32)/(1400-782)) calc((70-32) / (1400 - 782)) calc((70 - 32) / (1400 - 782))
calc((704-3)/(1400-782)) calc((704 - 3) / (1400 - 782)) calc((704 - 3) / (1400 - 782))
calc((704-320)) calc((704 - 320)) calc((704 - 320))
calc((704-320)/1) calc((704-320) / 1) calc((704 - 320) / 1)
calc((704-320)/(1400-782)) calc((704-320) / (1400 - 782)) calc((704 - 320) / (1400 - 782))

Fixes: #11682

@RobinMalfait RobinMalfait changed the title Improve normalisation of calc() functions Improve normalisation of calc()-like functions Jul 26, 2023
wanted to be sure this worked
@RobinMalfait RobinMalfait merged commit ae4d213 into master Jul 26, 2023
10 checks passed
@RobinMalfait RobinMalfait deleted the fix/improve-calc-normalisation branch July 26, 2023 14:26
thecrypticace added a commit that referenced this pull request Oct 23, 2023
* parse the `calc()`-like expressions and format them

* update changelog

* Add test case for double negatives

wanted to be sure this worked

---------

Co-authored-by: Jordan Pittman <jordan@cryptica.me>
@mblode
Copy link

mblode commented Oct 25, 2023

3.3.4 caused the following line to break.

Input:
pb-[max(12px,calc(2*env(safe-area-inset-bottom)))]

New output:
padding-bottom: max(12px,calc(2 * env(safe - area - inset - bottom)));

Old output:
padding-bottom: max(12px,calc(2 * env(safe-area-inset-bottom)));

@Ryan-Maughan
Copy link

Also having issues with 3.3.4. Seems to add spaces for 'min-content' within a minmax function which is invalid.

Input:
grid-cols-[minmax(min-content,_25%),1fr]

New output:
grid-template-columns: minmax(min - content,25%) 1fr

Old output:
grid-template-columns: minmax(min-content,25%) 1fr

@Serator
Copy link

Serator commented Oct 25, 2023

Hey @RobinMalfait. I too have encountered the problems described above. Tailwind magic is great, but when it's clear and predictable. In this case, not only does this change add problems, it also diverges from regular CSS on an implicit level. Although adding _ adds pain, it's better than implicit magic.

@jokull
Copy link

jokull commented Oct 25, 2023

Another data point where this breaks previously working classes:

Input: grid-cols-[max-content,minmax(max-content,0.5fr)]
Output: grid-template-columns: max-content minmax(max - content,0.5fr)

Note the spacing around the - sign.

@RobinMalfait
Copy link
Contributor Author

Hey @mblode, @Ryan-Maughan, @Serator and @jokull

We introduced a regression here, but this should be fixed by #12283, and is available in the latest release (3.3.5). Sorry for the inconvenience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

calc() not normalizing
6 participants