question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Interpolation with parent reference (#{&}) inside multiple selector generates incorrect selector

See original GitHub issue

When you have a multiple selector and then you have #{&} interpolation inside, you get very strange output:

.a,
.b,
.c,
.d {
    #{&} .foo {
        width: 25px;
    }
}

Renders now (using gem install sass --pre) to:

.a .a, .a .b, .a .c, .a .d .foo,
.b .a,
.b .b,
.b .c,
.b .d .foo,
.c .a,
.c .b,
.c .c,
.c .d .foo,
.d .a,
.d .b,
.d .c,
.d .d .foo {
  width: 25px; }

I won’t call it expected 😃

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
kizucommented, Nov 7, 2013

This is just a minimal example, in my case it is used for bem-like classes, like this:

#{&}__element {
}

Which is impossible without interpolation. And in some complex cases things like this:

.block1,
.block2,
.block3,
.block4 {
    #{&}__element1,
    #{&}__element2,
    #{&}__element3,
    #{&}__element4 {
        width: 25px;

        #{&}_modifier1,
        #{&}_modifier2,
        #{&}_modifier3,
        #{&}_modifier4 {
            width: 50px;
        }
    }
}

Again, artifical example, but not impossible, there could be such cases, but with current Sass it expands… look for yourself, I won’t put here those 653kb and 131150 lines of generated trash 😃

1reaction
lolmauscommented, Nov 16, 2013

This also fails miserably:

.fieldset {
  background: red;
  #{&}-field {
    background: blue;
    #{&}-label {
      background: green;
    }
  }
}

Produces:

.fieldset {
  background: red;
}
.fieldset .fieldset-field {
  background: blue;
}
.fieldset .fieldset-field .fieldset .fieldset-field-label {
  background: green;
}

Should produce:

.fieldset {
  background: red;
}
.fieldset-field {
  background: blue;
}
.fieldset-field-label {
  background: green;
}

In Less and Stylus this works fine since forever.

@kizu, could you please mention “parent selector” and “&” in this thread’s title?

UPD: works as a charm like this (kudos to @Snugug):

.fieldset {
  background: red;
  @at-root #{&}-field {
    background: blue;
    @at-root #{&}-label {
      background: green;
    }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

In Sass, how to reference two parent classes with ampersand ...
Using interpolation on the parent selector causes Sass to evaluate it as a string ... This is now possible in pure CSS with...
Read more >
Parent Selector - Sass
The parent selector, & , is a special selector invented by Sass that's used in nested selectors to refer to the outer selector....
Read more >
The Sass Ampersand - CSS-Tricks
The & comes in handy when you're nesting and you want to create a more specific ... The & always refers to the...
Read more >
Syntax improvements related to the & selector #286 - GitHub
Having selectors be a first-class type (that is, storing them in variables) is very, very different than allowing variables without interpolation in selectors....
Read more >
Features In-Depth | Less.js
Multiple &. & may appear more than once within a selector. This makes it possible to repeatedly refer to a parent selector without...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found