Skip to content

Commit 8cf6e6c

Browse files
authored
fix(renderer): correct createElement behaviour for EmulatedRenderer (#718)
setAttribute is now called on createElement instead of appendChild when using EmulatedRenderer.
1 parent a2fee0c commit 8cf6e6c

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

Diff for: nativescript-angular/renderer.ts

+15-29
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import {
44
RendererStyleFlags2, ViewEncapsulation,
55
} from "@angular/core";
66

7-
import { isBlank } from "./lang-facade";
87
import { View } from "ui/core/view";
98
import { addCss } from "application";
109
import { topmost } from "ui/frame";
11-
import { escapeRegexSymbols } from "utils/utils";
1210
import { Device } from "platform";
1311

12+
import { isBlank } from "./lang-facade";
1413
import { ViewUtil } from "./view-util";
1514
import { APP_ROOT_VIEW, DEVICE, getRootPage } from "./platform-providers";
1615
import { NgView } from "./element-registry";
@@ -21,7 +20,6 @@ const COMPONENT_REGEX = /%COMP%/g;
2120
export const COMPONENT_VARIABLE = "%COMP%";
2221
export const HOST_ATTR = `_nghost-${COMPONENT_VARIABLE}`;
2322
export const CONTENT_ATTR = `_ngcontent-${COMPONENT_VARIABLE}`;
24-
const ATTR_REPLACER = new RegExp(escapeRegexSymbols(CONTENT_ATTR), "g");
2523
const ATTR_SANITIZER = /-/g;
2624

2725
@Injectable()
@@ -152,6 +150,7 @@ export class NativeScriptRenderer extends Renderer2 {
152150
// Seems to be called on component dispose only (router outlet)
153151
// TODO: handle this when we resolve routing and navigation.
154152
}
153+
155154
setAttribute(view: NgView, name: string, value: string) {
156155
traceLog(`NativeScriptRenderer.setAttribute ${view} : ${name} = ${value}`);
157156
return this.setProperty(view, name, value);
@@ -229,58 +228,45 @@ class EmulatedRenderer extends NativeScriptRenderer {
229228
private hostAttr: string;
230229

231230
constructor(
232-
private component: RendererType2,
231+
component: RendererType2,
233232
rootView: NgView,
234233
zone: NgZone,
235234
viewUtil: ViewUtil,
236235
) {
237236
super(rootView, zone, viewUtil);
238237

239-
this.addStyles();
240-
this.contentAttr = shimContentAttribute(component.id);
241-
this.hostAttr = shimHostAttribute(component.id);
238+
const componentId = component.id.replace(ATTR_SANITIZER, "_");
239+
this.contentAttr = replaceNgAttribute(CONTENT_ATTR, componentId);
240+
this.hostAttr = replaceNgAttribute(HOST_ATTR, componentId);
241+
this.addStyles(component.styles, componentId);
242242
}
243243

244244
applyToHost(view: NgView) {
245245
super.setAttribute(view, this.hostAttr, "");
246246
}
247247

248248
appendChild(parent: any, newChild: NgView): void {
249-
// Set an attribute to the view to scope component-specific css.
250-
// The property name is pre-generated by Angular.
251-
const cssAttribute = this.replaceNgAttribute(CONTENT_ATTR);
252-
newChild[cssAttribute] = true;
253-
254249
super.appendChild(parent, newChild);
255250
}
256251

257252
createElement(parent: any, name: string): NgView {
258253
const view = super.createElement(parent, name);
254+
255+
// Set an attribute to the view to scope component-specific css.
256+
// The property name is pre-generated by Angular.
259257
super.setAttribute(view, this.contentAttr, "");
260258

261259
return view;
262260
}
263261

264-
private addStyles() {
265-
this.component.styles
266-
.map(s => s.toString())
267-
.map(s => this.replaceNgAttribute(s))
262+
private addStyles(styles: (string | any[])[], componentId: string) {
263+
styles.map(s => s.toString())
264+
.map(s => replaceNgAttribute(s, componentId))
268265
.forEach(addCss);
269266
}
270267

271-
private replaceNgAttribute(input: string): string {
272-
return input.replace(ATTR_REPLACER , `_ng_content_${this.componentId}`);
273-
}
274-
275-
private get componentId(): string {
276-
return this.component.id.replace(ATTR_SANITIZER , "_");
277-
}
278-
}
279-
280-
function shimContentAttribute(componentShortId: string): string {
281-
return CONTENT_ATTR.replace(COMPONENT_REGEX, componentShortId);
282268
}
283269

284-
function shimHostAttribute(componentShortId: string): string {
285-
return HOST_ATTR.replace(COMPONENT_REGEX, componentShortId);
270+
function replaceNgAttribute(input: string, componentId: string): string {
271+
return input.replace(COMPONENT_REGEX, componentId);
286272
}

0 commit comments

Comments
 (0)