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

Strange behavior of the subtraction operator #853

Closed
piouPiouM opened this issue Jul 16, 2013 · 4 comments
Closed

Strange behavior of the subtraction operator #853

piouPiouM opened this issue Jul 16, 2013 · 4 comments

Comments

@piouPiouM
Copy link

I noticed the - operator needs to be wrapped by spaces in order to work as intented. Operation 1-1 for example, will return a list and not 0.

What is the origin of this unexpected behavior?

Thx.

Some examples:

$ sass -v
Sass 3.3.0.alpha.184 (Bleeding Edge)
$ sass -i
>> 1 - 1 // returns a number
0
>> 1- 1 // returns a number
0
>> 1 -1  // returns a list and not a number
(1 -1)
>> 1-1  // returns a list and not a number
(1 -1)
>> a-b
"a-b"
>> b-a
"b-a"
>> a-2 // returns a string as expected
"a-2"
>> 1-b // returns a list and not a string
(1 "-b")
@KittyGiraudel
Copy link

This is probably due to the fact - is used as both a hyphenation mark and a substraction operator. In order to differenciate the two, I believe the operator needs to be wrapped by spaces.

I'd love to hear a word of Nathan / Chris on this though. :)

@nex3
Copy link
Contributor

nex3 commented Jul 19, 2013

This is expected behavior, although unfortunately kind of unintuitive at the margins. In Sass, - can be part of an identifier, part of a negative number, a binary subtraction operator, or a unary negation operator. Being an identifier/number has the highest precedence; unary negation has the lowest. This gives rise to all the cases you listed. Let's go over them one by one:

In 1 - 1 it's unambiguously subtraction. Easy peasy. 1- 1 is the same; since 1- isn't a valid number, the - must be the subtraction operator.

1 -1 starts to get tricky. -1 is parsed as a number before Sass ever starts to look for operators. This follows the CSS grammar. 1-1 is parsed the same way.

a-b is a valid CSS identifier, so it's parsed as an unquoted string. Same for b-a and a-2.

1-b is similar to the 1 -1 case; -b is a valid identifier, so it gets parsed as that before any operators are parsed.

@nex3 nex3 closed this as completed Jul 19, 2013
@KittyGiraudel
Copy link

Thanks for detailed explanations Nathan.

@piouPiouM
Copy link
Author

Thx for taking the time to explain it so clearly Nathan!

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

No branches or pull requests

3 participants