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

Dynamic division with interpolation not compatible with libsass #948

Closed
atomicpages opened this issue Mar 15, 2015 · 6 comments · Fixed by #1184
Closed

Dynamic division with interpolation not compatible with libsass #948

atomicpages opened this issue Mar 15, 2015 · 6 comments · Fixed by #1184

Comments

@atomicpages
Copy link

Found this problem with playing with node-sass the other day with this code snippet:

@function _calcRU($px, $base, $unit) {
    $unit: quote($unit);

    @if not $unit == "em" or not $unit == "rem" or not $unit == "%" {
        @debug "Invalid unit, choose 'em' or 'rem'";
        @return null;
    }

    @if not unitless($px) {
        $px: strip-units($px);
    }

    @if not unitless($base) {
        $base: strip-units($base);
    }

    @if($unit == "%") {
        @return percentage($px / $base);
    }

    @return ($px / $base) * 1#{$unit};
}

This test with Sass 3.2.19 and above

body {
    font-size: _calcRU(12, 10, "%");
    font-size: _calcRU(16, 10, "rem");
    font-size: _calcRU(20, 10, "em");
    font-size: _calcRU(24, 10, "rem");
}

yields

body {
    font-size: 120%;
    font-size: 1.6rem;
    font-size: 2em;
    font-size: 2.4rem;
}

Testing with the current libsass master I get "invalid operands for multiplication".

@mgreter
Copy link
Contributor

mgreter commented Mar 15, 2015

I can confirm that this bug exists on latest master.
Test case can probably be shortened to:

foo { bar: 10 * 5#{px}; }

@mgreter
Copy link
Contributor

mgreter commented Mar 16, 2015

@xzyfer I gave it a try, but it is a bit more complicated than expected. Actually with my first approach it started to accept stuff like this (which I found quite neat, but is not what ruby sass does):

10f#{o}o + 20foo -> 30foo

It also correctly complained when units where off for calculations. Not sure what the reasons are for ruby sass to not evaluate interpolated stuff into numbers again (there was actuall only one failing spec test with function type returning number instead of string). IMO ruby aborts the Binary_Expression parsing when it sees some interpolated stuff, which seems not that easy to implement currently (I suspect this has also something to do with our "everything is a space list" issue).

@xzyfer
Copy link
Contributor

xzyfer commented Mar 17, 2015

I'm not surprised this is difficult. My initial impression was that this is related to #442.

@KittyGiraudel
Copy link

Not sure what the reasons are for ruby sass to not evaluate interpolated stuff into numbers again (there was actuall only one failing spec test with function type returning number instead of string).

Because I believe Sass designers do not want units to be thought as a random string living at the end of a number. While there seem to be an issue with LibSass on this case, the initial problem could be solved by correctly using numbers with units, rather than dealing with strings all along.

@mgreter mgreter self-assigned this May 9, 2015
mgreter added a commit to mgreter/libsass that referenced this issue May 9, 2015
@mgreter mgreter modified the milestones: 3.2.3, 3.3 May 9, 2015
mgreter added a commit to mgreter/libsass that referenced this issue May 9, 2015
mgreter added a commit to mgreter/libsass that referenced this issue May 9, 2015
@xzyfer
Copy link
Contributor

xzyfer commented May 13, 2015

This fix was a false positive due to sass/sass-spec#380. The spec still fails.

@xzyfer xzyfer reopened this May 13, 2015
@xzyfer xzyfer modified the milestones: 3.3, 3.2.3 May 13, 2015
@mgreter mgreter removed their assignment May 13, 2015
@mgreter
Copy link
Contributor

mgreter commented Jul 13, 2015

We now have a passing spec test since #1249 was merged 🎈

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

Successfully merging a pull request may close this issue.

4 participants