lessphp icon indicating copy to clipboard operation
lessphp copied to clipboard

calc(100% - 88px) results in calc(12%)

Open varunshoor opened this issue 13 years ago • 22 comments

As the title says, the data is being changed to some incomprehensible value. Any idea on how to fix it?

varunshoor avatar Oct 19 '12 15:10 varunshoor

Fix is to add unit comparison check in op_number_number() and bail out if the units are not the same. Can someone merge this into the repo?


    // operator on two numbers
    protected function op_number_number($op, $left, $right) {
        // Not same unit? return as is
        if (!empty($left[2]) && !empty($right[2]) && $left[2] != $right[2])
        {
            return array('number', $left[1] . $left[2] . ' ' . $op . ' ' . $right[1] . $right[2], '');
        }

varunshoor avatar Oct 19 '12 16:10 varunshoor

In this case shouldn't the arguments to calc be left alone and not evaluated? There's no way to evaluate 100% - 80px ahead of time.

leafo avatar Oct 19 '12 19:10 leafo

Correct. Isn't the fix I provided right?

Sent from my iPad

On 20-Oct-2012, at 12:34 AM, leaf [email protected] wrote:

In this case shouldn't the arguments to calc be left alone and not evaluated? There's no way to evaluate 100% - 80px ahead of time.

— Reply to this email directly or view it on GitHubhttps://github.com/leafo/lessphp/issues/326#issuecomment-9613802.

varunshoor avatar Oct 19 '12 19:10 varunshoor

No, the correct fix is to look for calc function calls, and disable all expressions from being evaluated inside of them.

leafo avatar Oct 19 '12 19:10 leafo

Be carrefull, @vars should be evaluated inside calc() :

width:calc(25% - (3*@gridGutterWidth/4));

Cerdic avatar Oct 23 '12 13:10 Cerdic

i would expect an exception, if the units do not match, or the silent ignore which was suggested :)

kaystrobach avatar Oct 24 '12 21:10 kaystrobach

So whats the final direction?

varunshoor avatar Nov 05 '12 06:11 varunshoor

I just ran into this. Would be great to see this fixed. Any workaround so far?

kirschkern avatar Apr 18 '13 11:04 kirschkern

what i can see as a solution would be to transfer such a query to:

https://developer.mozilla.org/en-US/docs/CSS/calc

So what are 80% - 90px ... :d

width: calc(100% - 80px);

kaystrobach avatar Apr 18 '13 13:04 kaystrobach

Hi, any news about this issue ? I think @varunshoor solution is good, i'm using it with any problem :)

Another ugly solution would be to write something like

width: calc(~"100% - "@width-right);

It works but I think it doesn't belong to the LESS file to handle parsing problem.

Thanks

cutesquirrel avatar Oct 22 '13 14:10 cutesquirrel

I think operations between dimensions of different types should be just silently ignored. This way the parser leaves calc() intact. Ignoring just the contents inside calc() would be kinda messy.

caiosm1005 avatar Oct 22 '13 16:10 caiosm1005

+1

kaystrobach avatar Nov 02 '13 19:11 kaystrobach

+1

NielsJanssen avatar Dec 10 '13 14:12 NielsJanssen

Any hotfix for this? calc(50% - 1em) gives calc(49%)

moroz1999 avatar Apr 23 '15 14:04 moroz1999

@moroz1999 what do you expect? ... how many pixels is your 100% :smile: nobody knows ...

kaystrobach avatar Apr 23 '15 14:04 kaystrobach

Just write calc(50% ~'-' 1em) instead.

seven-phases-max avatar Apr 23 '15 14:04 seven-phases-max

@kaystrobach I expect the CSS3 feature named "calc" work as designed without being recalculated. https://developer.mozilla.org/en-US/docs/Web/CSS/calc

lessphp should ignore such case at all without making any modifications. It's up to browser to make the equation in this case.

Try to compile the following code in lessphp: .myclass { width: calc(100%-10px); } This is absolutely valid CSS3 without any less code, so this should be rendered into exactly the same string (in other words, ignored without any changes). Instead, you will get: .myclass { width: calc(90%); } Which is absolutely invalid.

moroz1999 avatar Apr 23 '15 14:04 moroz1999

@seven-phases-max, thanks, I'll give it a try!

update:@seven-phases-max, thanks a lot, your solution works just perfectly.

moroz1999 avatar Apr 23 '15 14:04 moroz1999

@moroz1999 ok, perfect, the solution from @seven-phases-max should work

kaystrobach avatar Apr 23 '15 14:04 kaystrobach

I've just run into this with a CSS framework I can't change, in the DokuWiki project, which automatically parses CSS files through LESSPHP. Fixing this to ignore calc() expressions apart from variable expansion would be most welcome!

iainhallam avatar Feb 10 '18 21:02 iainhallam

A trick to insert (nearly) every unparsed code in LESSPHP: https://github.com/splitbrain/dokuwiki/issues/2254#issuecomment-386814503

Michaelsy avatar May 05 '18 16:05 Michaelsy

~"screen CSS code from LESS parser"

Rainbow-Spike avatar Aug 12 '19 06:08 Rainbow-Spike