Skip to content

Commit 973dc8d

Browse files
sis0k0hdeshev
authored andcommitted
fix(renderer): correct createElement behaviour for EmulatedRenderer (#718)
setAttribute is now called on createElement instead of appendChild when using EmulatedRenderer.
1 parent be3f954 commit 973dc8d

File tree

1 file changed

+13
-27
lines changed

1 file changed

+13
-27
lines changed

Diff for: nativescript-angular/renderer.ts

+13-27
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const COMPONENT_REGEX = /%COMP%/g;
2020
export const COMPONENT_VARIABLE = "%COMP%";
2121
export const HOST_ATTR = `_nghost-${COMPONENT_VARIABLE}`;
2222
export const CONTENT_ATTR = `_ngcontent-${COMPONENT_VARIABLE}`;
23-
const ATTR_REPLACER = new RegExp(escapeRegexSymbols(CONTENT_ATTR), "g");
2423
const ATTR_SANITIZER = /-/g;
2524

2625
@Injectable()
@@ -228,58 +227,45 @@ class EmulatedRenderer extends NativeScriptRenderer {
228227
private hostAttr: string;
229228

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

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

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

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

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

260258
return view;
261259
}
262260

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

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

283-
function shimHostAttribute(componentShortId: string): string {
284-
return HOST_ATTR.replace(COMPONENT_REGEX, componentShortId);
269+
function replaceNgAttribute(input: string, componentId: string): string {
270+
return input.replace(COMPONENT_REGEX, componentId);
285271
}

0 commit comments

Comments
 (0)