angular: 18.2.8 breaks multiple selectors in `:not` on `:host`

Which @angular/* package(s) are the source of the bug?

compiler

Is this a regression?

Yes

Description

The following code used to work in 18.2.7

:host {
  &:not(&.foo, &.bar) {
    color: red;
  }
}

According to CSS Selectors 4 it is valid to put multiple selectors into one :not. With 18.2.8 we would need to chain two :nots together to get the same effect (&:not(&.foo):not(&.bar)). This somehow only seems to break on the host element.

Here is a reproduction with 18.2.8: https://stackblitz.com/edit/stackblitz-starters-3tx6cc?file=src%2Fmain.ts

Here is the same code with 18.2.7 where the first text has the expected color: https://stackblitz.com/edit/stackblitz-starters-oq4g3j?file=src%2Fmain.ts

Both have "inlineStyleLanguage": "scss" set (although that somehow doesn’t seem to make a difference).

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 18.2.8 Node: 20.18.0 Package Manager: npm 10.9.0 OS: darwin arm64

Angular: 18.2.8 … animations, cdk, cli, common, compiler, compiler-cli, core … forms, platform-browser, platform-browser-dynamic, router

Package Version

@angular-devkit/architect 0.1802.8 @angular-devkit/build-angular 18.2.8 @angular-devkit/core 18.2.8 @angular-devkit/schematics 18.2.8 @schematics/angular 18.2.8 ng-packagr 18.2.1 rxjs 7.8.1 typescript 5.5.4 zone.js 0.14.10

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 16 (6 by maintainers)

Commits related to this issue

Most upvoted comments

@Blackbaud-PaulCrowder @GeorgySerga we just discussed this issue during the team meeting. The current plan is to:

  • revert the change in v18 branch
  • separately decide on this change in v19

@GeorgySerga I will reach out to you to coordinate.

Hi @Blackbaud-PaulCrowder , @bjarketrux , thank you so much for the examples. Yeah, I can see the pattern, that’s the reason why I was curious if there are any other examples of broken selectors, distinctive from the suffix going after the :where or :is.

You can also mitigate the issue in 18.2.8 by prefixing such selectors with an *, for example, in the provided stack blitz:

    table *:where(td, th) {
      &:first-child { color: green; }
    }

    table *:where(td, th):hover { color: lime; }

In this case the first selector, for instance, would transform to:

table[_ngcontent-ng-c1622321134] *[_ngcontent-ng-c1622321134]:where(td, th):first-child {
    color: green;
}

In general it uses the logic of 18.2.7 for parsing prefixed :where and :is. Because * is scoped, *[content-a] and [content-a] target all the same elements. It is a hack, don’t get me wrong, but maybe you’ll find it helpful in the meantime.

As for the :has scoping question, I tried to answer it here: https://github.com/angular/angular/issues/58153#issuecomment-2406223687 , lmk if this wouldn’t clarify things. Thanks again!

Hello, sorry for the delay in my responses, I tried to tackle the issue over the weekend, but I’m still looking for a better solution. Thank you for the provided examples of broken selectors, they’d be used to ensure things are working correctly for all. If you have any other distinct examples I’d greatly appreciate them.

@Blackbaud-PaulCrowder are :is() use cases you had to substitute also have some sort of postfix to them, i.e :where(.foo):first-child, or is there any other issue? I believe staying on 18.2.7 might be the best option for now. Thank you for your help!

I don’t want to be pushy, buuut 😅 Should this issue get a bit more attention? This is a patch version that potentially could lead to - at best - visual bugs. But these bugs are likely to not get caught by automatic tests. At worst some functionality is not working and it goes undetected.

Would it make sense to roll back the changes that cause this issue and then work on the fix after?

My fix for now is to pin the Angular versions to 18.2.7.

My assumption was that both CSS blocks from the stackblitz example would behave the same:

:host {
  &:not(&.foo, &.bar) {
    color: red;
  }
}

div {
  &:not(&.foo, &.bar) {
    color: blue;
  }
}

If I put that into a SASS playground both produce the same output: https://www.sasscss.com/playground#eJwzNLTKyC8uUajmUlBQs8rLL9FQ00vLz9dRUNNLSizSBIsrKCTn5+QXWSkUpaZYA/m1XLVcXCmZZcRpSsopTYXqAgBYEh2L

Now I would assume that Angular replaces all instances of :host with whatever selector is used to simulate encapsulation. I did this by hand for a vanilla HTML example where I replaced all instances of :host with body: https://stackblitz.com/edit/stackblitz-starters-m2p4oj?file=index.html

The more I look at it, the more I think that our CSS definitely wasn’t well written. So far we found at least two instances in our codebase that break with 18.2.8.

Here is a case that doesn’t involve two selectors in the :not but instead has no & in front of the :not:

Broken in 18.2.8: https://stackblitz.com/edit/stackblitz-starters-cx5pxk?file=src%2Fmain.ts

Working in 18.2.7: https://stackblitz.com/edit/stackblitz-starters-p4zniv?file=src%2Fmain.ts

All of this maybe should have never worked. At this point, I tried so many different combinations that I am getting confused which ones are actually supposed to be valid. Our issue is that we are building a library, but these styles are compiled by the projects using it. So for those updating to 18.2.8 our library breaks. Maybe we just got incredibly unlucky with the fixes in 18.2.8 and unknowingly relied on unsupported behavior for years. If that’s the case I am willing to fix it on our end. But I would imagine others have written similar code. Depending on the scale would it maybe make sense to revert the changes and only release them in a major release? Given that you wrote in your first sentence that there is also a bug with the new version this whole change might need some more consideration?

Hi @JeanMeche and @LeonEck . I tried to add a couple of test cases to see how those would be parsed, there’s definitely a bug in parsing in the new version, but there’s some context.

I’d call 18.2.7 as an old version and 18.2.8 as a new version. Let’s see how :host:not(:host.foo) {} evaluates. Both old and new would give:

[host-a]:not([host-a] .foo) {}

Notice a space before .foo, I think this is a bug in both versions. I didn’t try to change the way :not works, so it should work the same across versions, unless there are comma separated arguments. Old version used to split by comma regardless if it was within parentheses or not.

Let’s look at the snippet provided in the issue :host:not(:host.foo, :host.bar) {}. Old

[host-a]:not([host-a] .foo, .bar)[host-a] {}

It increases [host-a] specificity and breaks the first selector inside :not (there’s space in front of .foo again), but the second selector would work, loosing the scope neat it, it works because of the wrong parsing side effect. The new version produces

[host-a][content-a]:not([host-a] .foo, .bar[host-a]) {}

Definitely not correct, fails the whole selector adding [content-a], so no side effects.

If the selector you provided would look like this :host:not(foo, bar) {} it would target the same elements (with a different specificity maybe, unless I’m missing something).

In the old version it was transformed to:

[host-a]:not(.foo, .bar)[content-a] {}

broken selector, but in the new version it would work:

[host-a]:not(.foo, .bar) {}

So maybe it could be a solution for your snippet. I’m not sure how to go about the change though. The new logic allows the last example to work as intended and makes the initial snippet to fail without side effects. As I mentioned above I didn’t try to alter previous logic for functions other than is and where, such alterations happen because comma is not considered a delimiter anymore if it is nested inside parentheses.

wdyt? Thanks again!