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

CSS styles not applied if using attribute HostBinding and styling a DIV with the direct child selector in a production build #49100

Closed
Marcarrian opened this issue Feb 16, 2023 · 9 comments
Labels
area: compiler Issues related to `ngc`, Angular's template compiler regression Indicates than the issue relates to something that worked in a previous version
Milestone

Comments

@Marcarrian
Copy link

Marcarrian commented Feb 16, 2023

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

Don't known / other

Is this a regression?

Yes

Description

If using attribute HostBinding and styling a DIV element with the direct child selector, the styles are not applied.
This happens only in a producton build.

Reproduction:
app.component.html

<div class="example-1">
  Example 1 (css: > div)
</div>
<div class="example-2">
  Example 2 (css: div)
</div>
<section class="example-3">
  Example 3 (css: > section)
</section>
<section class="example-4">
  Example 4 (css: section)
</section>

app.component.ts

import {Component, HostBinding} from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent {

  @HostBinding('attr.data-foo')
  public foo = 'bar';
}

app.component.scss

:host {
  &[data-foo="bar"] {
    > div.example-1 {
      background-color: cyan; // not working
    }

    div.example-2 {
      background-color: yellow; // working
    }

    > section.example-3 {
      background-color: red; // working
    }

    section.example-4 {
      background-color: green; // working
    }
  }
}

image

In the transpiled CSS we noticed various issues:

Example 1:

  • "nghost" attribute is missing after the attribute selector [data-foo=bar]
  • "nghost" attribute after div.example-1 should instead be "ngcontent"

Example 2:

  • "ngcontent" attribute is missing after div.example-2. IMPORTANT! This is not the cause of this problem, but causes the styles to leak into child components. Please let me know if I should file a separate issue for this case.

transpiled css

Please provide a link to a minimal reproduction of the bug

https://github.com/Marcarrian/angular-15-attribute-host-binding-issue

Please provide the exception or error you saw

No response

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

Angular CLI: 15.1.5        
Node: 16.16.0              
Package Manager: npm 8.11.0
OS: win32 x64              
                           
Angular: 15.1.4            
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1501.5
@angular-devkit/build-angular   15.1.5
@angular-devkit/core            15.1.5
@angular-devkit/schematics      15.1.5
@angular/cli                    15.1.5
@schematics/angular             15.1.5
rxjs                            7.8.0
typescript                      4.9.5

Anything else?

No response

Marcarrian added a commit to SchweizerischeBundesbahnen/scion-workbench that referenced this issue Feb 16, 2023
Since Angular 15, Angular incorrectly transpiles SCSS to CSS in the following constellation:
- Attribute HostBinding (@HostBinding('attr.data-foo'))
- Styling DIV using direct child selector
- Production build

Example:
```scss
:host[data-foo="bar"] > div {
  background-color: cyan; // not working
}
```

We have reported this issue to Angular angular/angular#49100
Marcarrian added a commit to SchweizerischeBundesbahnen/scion-workbench that referenced this issue Feb 16, 2023
Since Angular 15, Angular incorrectly transpiles SCSS to CSS in the following constellation:
- Attribute HostBinding (@HostBinding('attr.data-foo'))
- Styling DIV using direct child selector
- Production build

Example:
```scss
:host[data-foo="bar"] > div {
  background-color: cyan; // not working
}
```

We have reported this issue to Angular angular/angular#49100
@Marcarrian Marcarrian changed the title CSS styles not applied when using attribute HostBinding and styling a DIV with the direct child selector in a production build CSS styles not applied if using attribute HostBinding and styling a DIV with the direct child selector in a production build Feb 16, 2023
Marcarrian added a commit to SchweizerischeBundesbahnen/scion-workbench that referenced this issue Feb 16, 2023
Since Angular 15, Angular incorrectly transpiles SCSS to CSS in the following constellation:
- Attribute HostBinding (@HostBinding('attr.data-foo'))
- Styling DIV using direct child selector
- Production build

Example:
```scss
:host[data-foo="bar"] > div {
  background-color: cyan; // not working
}
```

We have reported this issue to Angular angular/angular#49100
@JeanMeche
Copy link
Member

I looks like that it was a regression introduced in 15.1.2. I suspect my fix #48558 to be the cause. I will look into it.

Thanks for the report 👍

danielwiehl pushed a commit to SchweizerischeBundesbahnen/scion-workbench that referenced this issue Feb 16, 2023
Since Angular 15, Angular incorrectly transpiles SCSS to CSS in the following constellation:
- Attribute HostBinding (@HostBinding('attr.data-foo'))
- Styling DIV using direct child selector
- Production build

Example:
```scss
:host[data-foo="bar"] > div {
  background-color: cyan; // not working
}
```

We have reported this issue to Angular angular/angular#49100
@JeanMeche
Copy link
Member

@dario-piotrowicz. It definitely looks like my change is at the root of this issue (any hex character after the > will break the shimming) but I was not able to reproduce it in the unit tests.

In host_and_host_context_spec.ts:
expect(shim(':host[foo] > div {}', 'contenta', 'a-host')).toEqualCss(('[foo][a-host] > div[contenta] {}'))
Is valid.

Any idea why ?

@dario-piotrowicz
Copy link
Contributor

dario-piotrowicz commented Feb 16, 2023

@JeanMeche I'm not too sure, but if the test is valid I suspect that that's not the text that the shim function is receiving in production (since shimming happens are the processing styles and whatnot), as a first step I would make absolutely sure to know what the shim function is actually receiving when it is breaking (by dropping at its start a console log or a breakpoint)

have you tried that?

JeanMeche added a commit to JeanMeche/angular that referenced this issue Feb 16, 2023
In prod builds, selectors are optimized and spaces a removed. angular#48558 introduced a regression on selectors without spaces. This commit fixes tihs.

Fixes angular#49100
@JeanMeche
Copy link
Member

JeanMeche commented Feb 16, 2023

Thanks for the heads up. Just found the difference: Selectors had no space on prod builds !

I've opened #49118 for review

@Marcarrian
Copy link
Author

Marcarrian commented Feb 22, 2023

Hey @JeanMeche, thank you for the analysis and the potential fix. We worked around this issue by using the CSS "is" selector:

:host {
  &:is([data-foo="bar"]) {
    > div.example-5 {
      background-color: lightskyblue; // working in dev and prod build
    }
  }
}

In the constellation of using two attribute selectors, we noticed a similar issue. This time, the styles are applied only in a production build.

:host {
  &:is([data-foo="bar"], [data-foo-2="bar-2"]) {
    > div.example-6 {
      background-color: violet; // ONLY working in prod build
    }
  }
}

Please let me know if I should file a separate issue or if this is sufficient.

I have updated my reproduction repository with examples 5 and 6.

@alan-agius4 alan-agius4 added the area: compiler Issues related to `ngc`, Angular's template compiler label Feb 22, 2023
@ngbot ngbot bot added this to the needsTriage milestone Feb 22, 2023
@JeanMeche
Copy link
Member

Nice finding, Could you open a separate issue ? It wasn't working even before the regression was introduced !

@Marcarrian
Copy link
Author

I have created a separate issue #49157

@hisham
Copy link

hisham commented Jun 5, 2023

any update on the fix for this issue? We're affected by it as well.

JeanMeche added a commit to JeanMeche/angular that referenced this issue Jun 5, 2023
In prod builds, selectors are optimized and spaces a removed. angular#48558 introduced a regression on selectors without spaces. This commit fixes tihs.

Fixes angular#49100
JeanMeche added a commit to JeanMeche/angular that referenced this issue Jun 5, 2023
In prod builds, selectors are optimized and spaces a removed. angular#48558 introduced a regression on selectors without spaces. This commit fixes tihs.

Fixes angular#49100
@JeanMeche JeanMeche added the regression Indicates than the issue relates to something that worked in a previous version label Jun 5, 2023
JeanMeche added a commit to JeanMeche/angular that referenced this issue Jun 10, 2023
In prod builds, selectors are optimized and spaces a removed. angular#48558 introduced a regression on selectors without spaces. This commit fixes tihs.

Fixes angular#49100
JeanMeche added a commit to JeanMeche/angular that referenced this issue Jun 10, 2023
In prod builds, selectors are optimized and spaces a removed. angular#48558 introduced a regression on selectors without spaces. This commit fixes tihs.

Fixes angular#49100
JeanMeche added a commit to JeanMeche/angular that referenced this issue Jul 5, 2023
In prod builds, selectors are optimized and spaces a removed. angular#48558 introduced a regression on selectors without spaces. This commit fixes tihs.

Fixes angular#49100
JeanMeche added a commit to JeanMeche/angular that referenced this issue Aug 1, 2023
In prod builds, selectors are optimized and spaces a removed. angular#48558 introduced a regression on selectors without spaces. This commit fixes tihs.

Fixes angular#49100
JeanMeche added a commit to JeanMeche/angular that referenced this issue Aug 4, 2023
In prod builds, selectors are optimized and spaces a removed. angular#48558 introduced a regression on selectors without spaces. This commit fixes tihs.

Fixes angular#49100
atscott pushed a commit that referenced this issue Oct 11, 2023
In prod builds, selectors are optimized and spaces a removed. #48558 introduced a regression on selectors without spaces. This commit fixes tihs.

Fixes #49100

PR Close #49118
@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Nov 11, 2023
ChellappanRajan pushed a commit to ChellappanRajan/angular that referenced this issue Jan 23, 2024
…ar#49118)

In prod builds, selectors are optimized and spaces a removed. angular#48558 introduced a regression on selectors without spaces. This commit fixes tihs.

Fixes angular#49100

PR Close angular#49118
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: compiler Issues related to `ngc`, Angular's template compiler regression Indicates than the issue relates to something that worked in a previous version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants