Skip to content

CSS min and max function calls that hold CSS variables fail with "Operation on an invalid type"  #3777

Description

@rjgotten

To reproduce:

With strict units enabled, perform a calc-like expression inside a min or max function
and use a CSS variable. E.g.

prop : min(100% - var(--some-var), 10px);

Current behavior:
Compiler will throw "Operation on an invalid type" error.

Expected behavior:
Compiler knows the arguments to min and max can be calc-like and doesn't throw, but retains the arguments as-is.

Environment information:

  • less version: 4.1.3
  • nodejs version: 14
  • operating system: any

Having dug around a bit, it appears that the root of the issue is the fact that Operation is visited and flattened before passing to min and max, which means the var() node trips the compiler error as it afaict does not implement an operate method.

This problem can be avoided by setting evalArgs : false for both functions and performing custom lazy evaluation inside the functions themselves, where it can be wrapped in a try-catch and cause the function to go inert (and be treated as the CSS function proper) when incompatible types are present.

Activity

  1. shindodkar commented on Mar 31, 2023

    @shindodkar

    can i work on this bug?

  2. matthew-dean commented on Mar 31, 2023

    @matthew-dean
    Member

    @shindodkar I don't see why not!

  3. debmalya37 commented on Apr 27, 2023

    @debmalya37

    can I contribute to it ??

  4. matthew-dean commented on May 15, 2023

    @matthew-dean
    Member

    @GitsOfVivek @debmalya37 Anyone can do this at any time. @shindodkar asked about it March 31st, but nothing has been produced yet.

  5. matthew-dean commented on May 15, 2023

    @matthew-dean
    Member

    One way to prevent having to do this several times in several places would be to, rather than creating a try / catch block within each function, would be to create an abstraction that outputs certain functions as-is.

    In other words, a number of functions have had try / catch blocks added within the function statement, and outputs the nodes as-is instead of throwing an error if it can't evaluate something, but that gets repetitive to add per function.

    A downside of doing this is that if the stylesheet author intended Less to evaluate the statement, and there is an error in the expression, rather than showing an error, it will just output the function.

  6. matthew-dean commented on May 15, 2023

    @matthew-dean
    Member

    @GitsOfVivek I'm not sure what more can be said about it. min / max are Less functions. They are also CSS functions. So, passing a var() into it is invalid if you intended it to be evaluated as a Less function at compile time, but valid if you intended it to be evaluated as a CSS function at runtime.

  7. VIRAT9358 commented on Jun 24, 2023

    @VIRAT9358

    Can I be assigned this @matthew-dean

  8. VIRAT9358 commented on Jun 24, 2023

    @VIRAT9358

    prop: clamp(10px, calc(100% - var(--some-var)), 100%);
    Can we use clamp() function has relatively good browser support but may not work in older or less commonly used browsers

  9. Cyddharth-Gupta commented on Jul 10, 2023

    @Cyddharth-Gupta

    I have been pretty active on github for the last few days, I'll be quick in fixing this.
    @matthew-dean can I take up this issue? There hasn't been any progress since the last 2 assignments

  10. matthew-dean commented on Jul 10, 2023

    @matthew-dean
    Member
  11. 3 remaining items

  12. matthew-dean commented on Jul 12, 2023

    @matthew-dean
    Member

    the following test cases are failing: min(1, 4ex, 2pt) and max(5m, 3em);

    I'm guessing Less tries to eagerly normalize them to the same unit type?

  13. Cyddharth-Gupta commented on Jul 12, 2023

    @Cyddharth-Gupta

    @Cyddharth-Gupta Can you start a draft PR I can look at?

    sure, will do that.

  14. Cyddharth-Gupta commented on Jul 14, 2023

    @Cyddharth-Gupta

    @matthew-dean i created a Draft PR a couple of days ago, please have a look at it.

  15. iChenLei commented on Jul 15, 2023

    @iChenLei
    Member

    @Cyddharth-Gupta CI failed , Before starting the PR review, make sure CI passes at least. Thanks

  16. Cyddharth-Gupta commented on Jul 15, 2023

    @Cyddharth-Gupta

    @iChenLei the CI test that are failing are in accordance with the conversation with @matthew-dean , that's why Matthew asked me to start a draft PR, please go through the conversation above.
    Thanks

  17. matthew-dean commented on Jul 17, 2023

    @matthew-dean
    Member

    @Cyddharth-Gupta it says you closed the PR?

  18. Cyddharth-Gupta commented on Jul 17, 2023

    @Cyddharth-Gupta

    @matthew-dean my apologies, didn't get a reply for some days since I created the pr, so I thought it might not be an active issue. I will create a new one by tomorrow.

  19. lubber-de commented on Aug 24, 2023

    @lubber-de

    Workaround: use string escaping

    x {
       prop: e("min(100% - var(--some-var), 10px)");
    }

    results in

    x {
       prop: min(100% - var(--some-var), 10px);
    }
  20. added a commit that references this issue on Aug 25, 2023
  21. removed their assignment
    on Aug 28, 2023
  22. kachurun commented on Nov 7, 2024

    @kachurun

    Same problem with other functions like pow:
    pow(var(--font-ratio-min), var(--font-level, 0))

  23. puckowski commented on Dec 7, 2024

    @puckowski
    Contributor

    I believe PR #4297 should resolve this issue.

    In the short term, you can use fork https://github.com/puckowski/less.js/releases/tag/4.2.1.4

    Fork is Less.js 4.2.1 with fixes for:
    #4252
    #3737
    #4258
    #4264
    #4268
    #4267
    #3777
    #4224

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions