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

[Media queries] Negation is ignored when nested media queries exist #2425

Closed
yivo opened this issue Jun 21, 2017 · 8 comments
Closed

[Media queries] Negation is ignored when nested media queries exist #2425

yivo opened this issue Jun 21, 2017 · 8 comments

Comments

@yivo
Copy link

yivo commented Jun 21, 2017

libsass 3.5.0.beta.3-54-gdebb1e32

http://libsass.ocbnet.ch/srcmap/#LmJsb2NrIHsKICBAbWVkaWEgbm90IHByaW50IHsKICAgIEBtZWRpYSAobWluLXdpZHRoOiA2MDBweCkgewogICAgICBjb2xvcjogZ3JlZW47CiAgICB9CiAgfQp9

input.scss

.block {
  @media not print {
    @media (min-width: 600px) {
      color: green;
    }
  }
}

Actual results

@media print and (min-width: 600px) {
  .block {
    color: green; } }

Expected result

@media not print and (min-width: 600px) {
  .block {
    color: green; } }
@yivo
Copy link
Author

yivo commented Jun 21, 2017

It works well if @media not print is nested.

.block {
  @media (min-width: 600px) {
    @media not print {
      color: green;
    }
  }
}

@mgreter
Copy link
Contributor

mgreter commented Nov 12, 2017

Media queries are not very flexible with the "not" operation, see https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries. Sass should probably error in this situation, although we may be able to convert the logic in certain cases (e.g. inverting the min-width to a max-width statement, to invert the intention, albeit I did not really think hard if that makes sense).

@xzyfer
Copy link
Contributor

xzyfer commented Mar 6, 2018

@nex3 I've noticed the output between Ruby and Dart Sass is different in this case. Can you please confirm the correct output.

Input

.block {
  @media not print {
    @media (min-width: 600px) {
      color: green;
    }
  }
}

Ruby Sass

Dart Sass

@media not print and (min-width: 600px) {
  .block {
    color: green;
  }
}

@sass sass deleted a comment from yivo Mar 6, 2018
@sass sass deleted a comment from nschonni Mar 6, 2018
@nex3
Copy link
Contributor

nex3 commented Mar 8, 2018

This is complicated. I can say for sure that Dart Sass is wrong: @media A {@media B { ... }} should match media for which both A and B are true, and if A = not print, not print and B matches media where A is true and B is false. That's why Ruby Sass produces no output: because there wasn't any way to create a query that means the right thing.

However, the most recent media query spec is more flexible in the syntax it supports for queries. It allows @media (not print) and (min-width: 600px), which does express the correct semantics. But I'm not sure how broad the browser support for this syntax is, or how stable it's going to be going forward, both of which determine whether it's safe to target. Perhaps @tabatkins can provide insight?

@nex3
Copy link
Contributor

nex3 commented Mar 16, 2018

I talked to @tabatkins, who says

syntax is stable, but currently unsupported.
(so as usual, take "stable" with a grain of salt until webcompat actually freezes it in, but I don't expect any changes and it's been the same for a while)

I think we should follow Ruby Sass's behavior here and just emit nothing until some browser actually supports more advanced queries.

@tabatkins
Copy link

👍

@nex3
Copy link
Contributor

nex3 commented Mar 16, 2018

Looking closer at the spec, it looks like it's irrelevant for this specific case anyway—(not print) and (min-width: 600px) isn't actually valid, since the <media-type> production can only appear at the top level of the parse tree.

@nex3
Copy link
Contributor

nex3 commented Mar 16, 2018

...although I suppose not print and not (min-width: 600px) would work.

xzyfer added a commit to xzyfer/libsass that referenced this issue Mar 28, 2018
There was a typo when this logic was ported over from Ruby Sass.

Fixes sass#2425
Spec sass/sass-spec#1243
xzyfer added a commit that referenced this issue Mar 28, 2018
There was a typo when this logic was ported over from Ruby Sass.

Fixes #2425
Spec sass/sass-spec#1243
xzyfer added a commit that referenced this issue Mar 28, 2018
There was a typo when this logic was ported over from Ruby Sass.

Fixes #2425
Spec sass/sass-spec#1243
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

5 participants