Skip to content

fix(1845): CSS special selector ":host" only work first time #1852

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

Merged
merged 2 commits into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion nativescript-angular/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ export class NativeScriptRendererFactory implements RendererFactory2 {
return this.defaultRenderer;
}

let renderer: NativeScriptRenderer = this.componentRenderers.get(type.id);
let renderer = this.componentRenderers.get(type.id);
if (renderer) {
if (renderer instanceof EmulatedRenderer) {
renderer.applyToHost(element);
}

return renderer;
}

Expand Down
45 changes: 45 additions & 0 deletions tests/app/tests/renderer-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { View, fontInternalProperty, backgroundInternalProperty } from "tns-core
import { nsTestBedAfterEach, nsTestBedBeforeEach, nsTestBedRender } from "nativescript-angular/testing";
import { ComponentFixture, TestBed, async } from "@angular/core/testing";
import { Observable, ReplaySubject } from "rxjs";
import { Label } from "tns-core-modules/ui/label/label";

@Component({
template: `<StackLayout><Label text="Layout"></Label></StackLayout>`
Expand Down Expand Up @@ -97,6 +98,37 @@ export class StyledLabelCmp2 {
}
}

@Component({
selector: "host-styled",
styles: [`
Label {
color: blue;
}

:host Label {
color: red;
}
`
],
template: `<Label text="Styled!"></Label>`
})
export class HostStyledCmp {
constructor(public elementRef: ElementRef<ProxyViewContainer>) {
}
}

@Component({
selector: "host-styled-parent",
template: `
<host-styled></host-styled>
<host-styled></host-styled>
`
})
export class HostStyledParentCmp {
constructor(public elementRef: ElementRef<ProxyViewContainer>) {
}
}

@Component({
selector: "ng-if-label",
template: `<Label *ngIf="show" text="iffed"></Label>`
Expand Down Expand Up @@ -251,6 +283,7 @@ describe("Renderer E2E", () => {
LayoutWithLabel, LabelCmp, LabelContainer,
ProjectableCmp, ProjectionContainer,
StyledLabelCmp, StyledLabelCmp2,
HostStyledCmp, HostStyledParentCmp,
NgIfLabel, NgIfThenElseComponent, NgIfMultiple,
NgIfTwoElements, NgIfMultiple,
NgIfElseComponent, NgIfThenElseComponent,
Expand Down Expand Up @@ -293,6 +326,18 @@ describe("Renderer E2E", () => {
});
});

it("applies component :host styles", () => {
return nsTestBedRender(HostStyledParentCmp).then((fixture) => {
const proxyView = fixture.componentRef.instance.elementRef.nativeElement;

for (let i = 0; i < 2; i += 1) {
const child = proxyView.getChildAt(i) as ProxyViewContainer;
const label = child.getChildAt(0) as Label;
assert.equal(Red, label.style.color.hex);
}
});
});

it("applies component styles from multiple sources", () => {
return nsTestBedRender(StyledLabelCmp2).then((fixture) => {
const componentRef: ComponentRef<StyledLabelCmp2> = fixture.componentRef;
Expand Down