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

fix: units being removed in math functions #894

Merged
merged 2 commits into from
Apr 1, 2020
Merged

fix: units being removed in math functions #894

merged 2 commits into from
Apr 1, 2020

Conversation

wongjn
Copy link
Contributor

@wongjn wongjn commented Mar 31, 2020

When using min(), max() or clamp(), 0 values get their units stripped, making the value invalid. This pull request preserves any units within these CSS functions.

@anikethsaha
Copy link
Member

Please fix the CI, else LGTM

lowerCasedValue === 'url' ||
lowerCasedValue === 'min' ||
lowerCasedValue === 'max' ||
lowerCasedValue === 'clamp'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to look on CSS spec for all possible values

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

min(), max() and clamp() were introduced in CSS Values Module Level 4, although I don't know if there are other CSS functions in other modules that don't accept unitless 0.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, what about calc?

Copy link
Contributor Author

@wongjn wongjn Apr 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had a look and units on 0 values are preserved already in calc — maybe these should be moved to the same place? This would mean the same value conversions that happen in calc would happen in these Level 4 functions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is strange, I think logic for calc should be here too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would introduce a lot of regressions in the current transformations in calc.

How about something like this to bring the functions together:

         if (
           lowerCasedValue === 'calc' ||
+          lowerCasedValue === 'min' ||
+          lowerCasedValue === 'max' ||
+          lowerCasedValue === 'clamp' ||
           lowerCasedValue === 'hsl' ||
           lowerCasedValue === 'hsla'
         ) {
           walk(node.nodes, (n) => {
             if (n.type === 'word') {
               parseWord(n, opts, true);
             }
           });
           return false;
         }
-        if (
-          lowerCasedValue === 'url' ||
-          lowerCasedValue === 'min' ||
-          lowerCasedValue === 'max' ||
-          lowerCasedValue === 'clamp'
-        ) {
+        if (lowerCasedValue === 'url') {
           return false;
         }
       }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's do it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New pull request?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep

@wongjn
Copy link
Contributor Author

wongjn commented Mar 31, 2020

Most of the failing CI seems to be disconnected from the changes I made, so not sure if it is in the scope of this pull request to fix them?

@codecov-io
Copy link

codecov-io commented Mar 31, 2020

Codecov Report

Merging #894 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #894   +/-   ##
=======================================
  Coverage   97.27%   97.28%           
=======================================
  Files         118      118           
  Lines        3455     3459    +4     
  Branches     1037     1041    +4     
=======================================
+ Hits         3361     3365    +4     
  Misses         86       86           
  Partials        8        8           
Impacted Files Coverage Δ
packages/postcss-convert-values/src/index.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 09bdf86...80f50ec. Read the comment docs.

@anikethsaha
Copy link
Member

jest timeout has to be increased .. I guess 30s might be enough.

@wongjn
Copy link
Contributor Author

wongjn commented Mar 31, 2020

Would it be appropriate to do that in this PR?

@alexander-akait
Copy link
Member

Yes

Copy link
Member

@anikethsaha anikethsaha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
I couldn't find any other function, let me know if there is any !

@alexander-akait alexander-akait merged commit 1ccbd5b into cssnano:master Apr 1, 2020
@alexander-akait
Copy link
Member

Thanks!

@helins
Copy link

helins commented Jan 16, 2021

I believe this is an important fix. I've just spend 3 hours trying to figure this one out in a project. It is not a well-known fact fact a unitless 0's mess with function such as max or clamp and you wouldn't expect cssnano to misbehave with something like that.

Meanwhile, a hack would be to use a very low value (eg. 0.01px) or just "0px" via a custom properties. But those are just hacks.

@srsly4
Copy link

srsly4 commented Apr 13, 2021

Would it be possible to backport this fix into any 4.x.x release available via npm?

At the moment there is no possibility to fix this while working on a react-script project without upgrading the whole webpack subsystem and even yarn resolutions can't force commit for this exact subpackage.

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.

None yet

6 participants