Skip to content

Emulated view encapsulation incorrectly transforms CSS that uses :is() or :where() #45686

Closed

Description

@ChrisTM

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

compiler, Don't known / other

Is this a regression?

No

Description

When using emulated view encapsulation, component CSS that uses the :is or :where pseudo class selector functions gets compiled incorrectly.

Example 1

Consider the following 2 selectors, which differ only in the number of items inside of :is:

:is(.foo) {}
:is(.foo, .bar, .baz) {}

I expected each item in the :is selector list to be transformed the same, but the first and last items are treated differently from the middle items. This is the compiled result:

[_ngcontent-uly-c97]:is(.foo) {}
[_ngcontent-uly-c97]:is(.foo, .bar[_ngcontent-uly-c97], .baz)[_ngcontent-uly-c97] {}

Example 2

When used before or after :host, the number of items inside of :is unexpectedly affects the presence of an [_ngcontent] qualifier outside of :is. The qualifier is missing for 1 item, but added for >1 item. This component CSS:

:is(.foo) :host {}
:is(.foo, .bar) :host {}

:host :is(.foo)  {}
:host :is(.foo, .bar)  {}

Compiles to:

:is(.foo)   [_nghost-kfp-c134] {}
[_ngcontent-kfp-c134]:is(.foo, .bar)   [_nghost-kfp-c134] {}

[_nghost-kfp-c134]   [_ngcontent-kfp-c134]:is(.foo)  {}
[_nghost-kfp-c134]   [_ngcontent-kfp-c134]:is(.foo, .bar)[_ngcontent-kfp-c134]  {}

Reproduction

https://stackblitz.com/edit/angular-ivy-yc96o6?file=src/app/app.component.css demonstrates a live reproduction of this issue.

For my specific use case, I had a selector that looked like:is(a, b, c) :host. I was able to work around the problem by rewriting it to either of :host-context(a, b, c) :host or a :host, b :host, c :host (both compile to the same output). But in general, using :is and :where seems either unsupported or broken. If it's unsupported, I think it would be helpful if the compiler threw an error because the current behavior is hard to predict and does not seem useful.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-yc96o6?file=src/app/app.component.css

Please provide the exception or error you saw

No response

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

I'm not using `ng`, so I can't provide `ng version` output. But the reproduction in StackBlitz is using @angular/* packages at version 13.3.3.

Anything else?

No response

Activity

  1. added
    area: core
    on Apr 20, 2022
  2. added this to the needsTriage milestone on Apr 20, 2022
  3. added
    area: compiler
    on Apr 20, 2022
  4. dario-piotrowicz commented on Apr 27, 2022

    @dario-piotrowicz
    Contributor

    good catch @ChrisTM 🙂 , I've found the bug causing this issue, it does look pretty small and easy to fix

    I've practically solved it, I need to check things, add unit tests and open a PR, I should hopefully be able to do it in the next few days 🙂👍

    (@dylhunn , @JoostK if either of you could assign me to this issue that'd be awesome 🙏 🙂 🙇)

  5. dario-piotrowicz commented on Apr 27, 2022

    @dario-piotrowicz
    Contributor

    Thanks @JoostK! 😃

  6. modified the milestones: needsTriage, Backlog on Apr 28, 2022
  7. modified the milestones: Backlog, needsTriage on Apr 28, 2022
  8. 19 remaining items

  9. montella1507 commented on Nov 15, 2023

    @montella1507

    @montella1507 Do you think my PR #49169 solves your issue ?

    image I have manually monkeypatched these 3 files based on your commit image image

    however.. i dont think it solved my prpoblem

  10. montella1507 commented on Nov 15, 2023

    @montella1507
    image image
  11. montella1507 commented on Nov 16, 2023

    @montella1507

    But you are close
    :-)

    Generated from your code (not working - misaligned whitespace):
    :is(.dark )[_nghost-hvk-c87]

    Working:
    :is(.dark) [_nghost-hvk-c87]

  12. JeanMeche commented on Dec 8, 2023

    @JeanMeche
    Member

    @montella1507 Can you check with the change I just made in #49169. It should fix your issue !

  13. added a commit that references this issue on Oct 9, 2024
    7ee070c
  14. added a commit that references this issue on Oct 10, 2024
    7a6fd42
  15. added a commit that references this issue on Oct 10, 2024
    bc5f117
  16. angular-automatic-lock-bot commented on Nov 10, 2024

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  17. locked and limited conversation to collaborators on Nov 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

P3area: compilerarea: corebugcore: CSS encapsulationstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions