Skip to content

Commit 59a5bd8

Browse files
m-absVladimir Amiorkov
authored and
Vladimir Amiorkov
committed
fix(1845): CSS special selector ":host" only work first time (#1852)
* fix(1845): CSS special selector ":host" only work first time * test(HostSelector): Adds test for the :host css-selector
1 parent 68248ab commit 59a5bd8

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

Diff for: nativescript-angular/renderer.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ export class NativeScriptRendererFactory implements RendererFactory2 {
5757
return this.defaultRenderer;
5858
}
5959

60-
let renderer: NativeScriptRenderer = this.componentRenderers.get(type.id);
60+
let renderer = this.componentRenderers.get(type.id);
6161
if (renderer) {
62+
if (renderer instanceof EmulatedRenderer) {
63+
renderer.applyToHost(element);
64+
}
65+
6266
return renderer;
6367
}
6468

Diff for: tests/app/tests/renderer-tests.ts

+45
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { View, fontInternalProperty, backgroundInternalProperty } from "tns-core
1616
import { nsTestBedAfterEach, nsTestBedBeforeEach, nsTestBedRender } from "nativescript-angular/testing";
1717
import { ComponentFixture, TestBed, async } from "@angular/core/testing";
1818
import { Observable, ReplaySubject } from "rxjs";
19+
import { Label } from "tns-core-modules/ui/label/label";
1920

2021
@Component({
2122
template: `<StackLayout><Label text="Layout"></Label></StackLayout>`
@@ -97,6 +98,37 @@ export class StyledLabelCmp2 {
9798
}
9899
}
99100

101+
@Component({
102+
selector: "host-styled",
103+
styles: [`
104+
Label {
105+
color: blue;
106+
}
107+
108+
:host Label {
109+
color: red;
110+
}
111+
`
112+
],
113+
template: `<Label text="Styled!"></Label>`
114+
})
115+
export class HostStyledCmp {
116+
constructor(public elementRef: ElementRef<ProxyViewContainer>) {
117+
}
118+
}
119+
120+
@Component({
121+
selector: "host-styled-parent",
122+
template: `
123+
<host-styled></host-styled>
124+
<host-styled></host-styled>
125+
`
126+
})
127+
export class HostStyledParentCmp {
128+
constructor(public elementRef: ElementRef<ProxyViewContainer>) {
129+
}
130+
}
131+
100132
@Component({
101133
selector: "ng-if-label",
102134
template: `<Label *ngIf="show" text="iffed"></Label>`
@@ -251,6 +283,7 @@ describe("Renderer E2E", () => {
251283
LayoutWithLabel, LabelCmp, LabelContainer,
252284
ProjectableCmp, ProjectionContainer,
253285
StyledLabelCmp, StyledLabelCmp2,
286+
HostStyledCmp, HostStyledParentCmp,
254287
NgIfLabel, NgIfThenElseComponent, NgIfMultiple,
255288
NgIfTwoElements, NgIfMultiple,
256289
NgIfElseComponent, NgIfThenElseComponent,
@@ -293,6 +326,18 @@ describe("Renderer E2E", () => {
293326
});
294327
});
295328

329+
it("applies component :host styles", () => {
330+
return nsTestBedRender(HostStyledParentCmp).then((fixture) => {
331+
const proxyView = fixture.componentRef.instance.elementRef.nativeElement;
332+
333+
for (let i = 0; i < 2; i += 1) {
334+
const child = proxyView.getChildAt(i) as ProxyViewContainer;
335+
const label = child.getChildAt(0) as Label;
336+
assert.equal(Red, label.style.color.hex);
337+
}
338+
});
339+
});
340+
296341
it("applies component styles from multiple sources", () => {
297342
return nsTestBedRender(StyledLabelCmp2).then((fixture) => {
298343
const componentRef: ComponentRef<StyledLabelCmp2> = fixture.componentRef;

0 commit comments

Comments
 (0)