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

current-media() does not interpolate variables, selector() does #2128

Closed
cspotcode opened this issue Feb 29, 2016 · 2 comments · May be fixed by ali8889/plunker-run-plugin#3, dmitriz/fontello#4 or walmartlabs/circus-stylus#5

Comments

@cspotcode
Copy link
Contributor

I'd like to fix this bug and am hoping a maintainer can point me in the right direction.

selector() uses utils.compileSelectors() to render a full selector string. It combines nested selectors and performs variable interpolation. However, current-media() doesn't. Based on the way the latter is used in +cache(), I'm guessing it is supposed to behave like the former?

What's the best way to fix this? Should I implement a utils.compileMedia()? ac40442 seems to be a big media-query refactoring, and a good starting point for my implementation. Also, should current-media() be fixed, or should I add a new BIF that uses utils.compileMedia(), leaving current-media() intact?

I greatly appreciate any advice you can offer.

$foo = 'screen'
@media $foo
    mq: current-media() // expecting '@media screen' but get '@media ($foo)`
@Panya
Copy link
Member

Panya commented Feb 29, 2016

Hi. compileSelectors method doesn't interpolate the selectors (they're interpolated in the evaluator, see https://github.com/stylus/stylus/blob/dev/lib/visitor/evaluator.js#L244 and https://github.com/stylus/stylus/blob/dev/lib/visitor/evaluator.js#L1403-L1439).
This method generates nested selectors and resolves relative references (&, /, ^[0] etc.).

To fix the problem with current-media you need to modify its source https://github.com/stylus/stylus/blob/dev/lib/functions/current-media.js. Basically you need to evaluate value of the media query by invoking this.visit method (this inside every BIF is the reference to current Evaluator instance). Something like:

module.exports = function currentMedia(){
  var self = this;
  return new nodes.String(lookForMedia(this.closestBlock.node) || '');

  function lookForMedia(node){
    if ('media' == node.nodeName) {
      node.val = self.visit(node.val); // magic happens here, the `val` property is the unresolved value of the media query (`$foo` ident in an expression in our case).
      return node.toString();
    } else if (node.block.parent.node) {
      return lookForMedia(node.block.parent.node);
    }
  }
};

@Panya
Copy link
Member

Panya commented Mar 4, 2016

Fixed in the rc-0.54 branch.

@Panya Panya closed this as completed Mar 4, 2016
kizu added a commit that referenced this issue Mar 5, 2016
* dev: (21 commits)
  Fixed History for 0.54.0
  History up
  Evaluate variables in current-media function. Fixes #2128
  Undesired spaces with partial reference selector using ranges. Fixes #2133
  Slightly clarified an issue with combinators at ranges in partial references, re: #2134
  Validate regexp flags for match function
  Fix bug with evaluating default arguments
  Undesired spaces with partial reference selector using ranges. Fixes #2133
  Fixed bug with selectors() function. Closes #2130
  Don't parse empty imports
  Wrong errors with --include-css and --resolve-url used concurrently. Fixes #2125
  misc fixes
  Docs and History up
  SelectorParser: Added initial reference selector
  BIFs: expose "url" function by "embedurl"
  Updated history
  Bump version
  BIFs: Added "index" function
  Updated docs that where merged incorrectly
  added a slice function
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants