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

Broken order of operations when using less variables in calculation and not using less to evaluate it #2602

Closed
battlesnake opened this issue Jun 1, 2015 · 7 comments

Comments

@battlesnake
Copy link

Demo:

@a: 1em - 1px;
@b: 1em + 1px;

sum {
    actual: calc(@a + 2 * @b);
    expect: calc(1em - 1px + 2 * (1em + 1px));
    fix: calc((@a) + 2 * (@b));
}

diff {
    actual: calc(@a - 2 * @b);
    expect: calc(1em - 1px - 2 * (1em + 1px));
    fix: calc((@a) - 2 * (@b));
}

Test:

$ lessc -su=on -sm=on test.less
sum {
  actual: calc(1em - 1px + 2 * 1em + 1px);
  expect: calc(1em - 1px + 2 * (1em + 1px));
  fix: calc((1em - 1px) + 2 * (1em + 1px));
}
diff {
  actual: calc(1em - 1px - 2 * 1em + 1px);
  expect: calc(1em - 1px - 2 * (1em + 1px));
  fix: calc((1em - 1px) - 2 * (1em + 1px));
}

I understand that this is defined behaviour and not a bug, however it seems a little unintuitive at first so should probably be in the main docs.

@battlesnake battlesnake changed the title Broken order of operations when deferring to CSS calc() Broken order of operations when using less variables in calculation, but not using less to evaluate it Jun 1, 2015
@battlesnake battlesnake changed the title Broken order of operations when using less variables in calculation, but not using less to evaluate it Broken order of operations when using less variables in calculation and not using less to evaluate it Jun 1, 2015
@rjgotten
Copy link
Contributor

rjgotten commented Jun 1, 2015

Numeric expressions that can not be further reduced should probably have their output CSS value changed to add enclosing braces. Solves the problem completely, I think.

@seven-phases-max
Copy link
Member

Yes, it would make sense to document but we don't have such "all that thousands common pitfalls" section there.

Speaking of the code itself: with -sm=on it would be almost must-have recommendation to always put parens around a variable value if it's supposed to represent an arithmetic expression, e.g::

// -sm:on
@a:  1 + 2;  // this has nothing to do with arithmetics at all!
@b: (1 + 2); // now this is an arithmetic expression

Also in regards of sm:on: see #1872 (and its #1880 follow up). So to be honest I'd recommend to you revert this ;) - unless you're forced to use sm:on by an external library (e.g. Bootstrap).
(su:on does not probably have any critical problems, but considering that almost nobody uses this mode your just make you code less-compatible, beside since it not quite widely used it's also much less tested en masse for possible inconsistencies).


Assuming it's not really a compiler issue but more like a documentation suggestion I wonder if this should be moved to less.js/less-docs tracker.

@seven-phases-max
Copy link
Member

@rjgotten

changed to add enclosing braces

The only purpose of sm:on is "to not treat values like font: ... 1em/2px ...; as arithmetic ops". So by automatically adding parens you'll break those.

@seven-phases-max
Copy link
Member

OK, before comments start to pile up I'm closing this in favour of #1880 (please do not hesitate to put any ideas there). And for docs suggestions please use less.js/less-docs.

@battlesnake
Copy link
Author

sm:on also allows one to use CSS calc. I use su:on as my background is physics and engineering, and I like dimensional consistency.

@seven-phases-max
Copy link
Member

Merged to #1880.

@rjgotten
Copy link
Contributor

rjgotten commented Jun 1, 2015

@seven-phases-max
Oh right. I missed the strictMath option there, in its shorthand form. D'oh! Sorry...

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

No branches or pull requests

4 participants