Skip to content

Commit be3f954

Browse files
committed
fix(renderer): support namespaced attributes (#719)
Platform attributes (such as :ios and :android) are passed as namespaces to the renderer in Angular 4. Caused by: angular/angular#14874.
1 parent 4b30fca commit be3f954

File tree

3 files changed

+18
-35
lines changed

3 files changed

+18
-35
lines changed

Diff for: nativescript-angular/renderer.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
RendererStyleFlags2, ViewEncapsulation,
55
} from "@angular/core";
66

7-
import { escapeRegexSymbols } from "tns-core-modules/utils/utils";
87
import { Device } from "tns-core-modules/platform";
98
import { View } from "tns-core-modules/ui/core/view";
109
import { addCss } from "tns-core-modules/application";
@@ -151,9 +150,10 @@ export class NativeScriptRenderer extends Renderer2 {
151150
// Seems to be called on component dispose only (router outlet)
152151
// TODO: handle this when we resolve routing and navigation.
153152
}
154-
setAttribute(view: NgView, name: string, value: string) {
155-
traceLog(`NativeScriptRenderer.setAttribute ${view} : ${name} = ${value}`);
156-
return this.setProperty(view, name, value);
153+
154+
setAttribute(view: NgView, name: string, value: string, namespace?: string) {
155+
traceLog(`NativeScriptRenderer.setAttribute ${view} : ${name} = ${value}, namespace: ${namespace}`);
156+
return this.viewUtil.setProperty(view, name, value, namespace);
157157
}
158158

159159
removeAttribute(_el: NgView, _name: string): void {
@@ -162,7 +162,7 @@ export class NativeScriptRenderer extends Renderer2 {
162162

163163
setProperty(view: any, name: string, value: any) {
164164
traceLog(`NativeScriptRenderer.setProperty ${view} : ${name} = ${value}`);
165-
this.viewUtil.setProperty(view, name, value);
165+
return this.viewUtil.setProperty(view, name, value);
166166
}
167167

168168
addClass(view: NgView, name: string): void {

Diff for: nativescript-angular/view-util.ts

+9-26
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import {
1313
import { platformNames, Device } from "tns-core-modules/platform";
1414
import { rendererLog as traceLog } from "./trace";
1515

16-
const IOS_PREFX: string = ":ios:";
17-
const ANDROID_PREFX: string = ":android:";
1816
const XML_ATTRIBUTES = Object.freeze(["style", "rows", "columns", "fontAttributes"]);
1917
const whiteSpaceSplitter = /\s+/;
2018

@@ -146,30 +144,8 @@ export class ViewUtil {
146144
return view;
147145
}
148146

149-
private platformFilter(attribute: string): string {
150-
let lowered = attribute.toLowerCase();
151-
if (lowered.indexOf(IOS_PREFX) === 0) {
152-
if (this.isIos) {
153-
return attribute.substr(IOS_PREFX.length);
154-
} else {
155-
return null;
156-
}
157-
}
158-
159-
if (lowered.indexOf(ANDROID_PREFX) === 0) {
160-
if (this.isAndroid) {
161-
return attribute.substr(ANDROID_PREFX.length);
162-
} else {
163-
return null;
164-
}
165-
}
166-
167-
return attribute;
168-
}
169-
170-
public setProperty(view: NgView, attributeName: string, value: any): void {
171-
attributeName = this.platformFilter(attributeName);
172-
if (!attributeName) {
147+
public setProperty(view: NgView, attributeName: string, value: any, namespace?: string): void {
148+
if (namespace && !this.runsIn(namespace)) {
173149
return;
174150
}
175151

@@ -219,6 +195,12 @@ export class ViewUtil {
219195
return found ? index : -1;
220196
}
221197

198+
private runsIn(platform: string): boolean {
199+
return (platform === "ios" && this.isIos) ||
200+
(platform === "android" && this.isAndroid);
201+
}
202+
203+
222204
private setPropertyInternal(view: NgView, attributeName: string, value: any): void {
223205
traceLog("Setting attribute: " + attributeName);
224206

@@ -306,3 +288,4 @@ export class ViewUtil {
306288
view.style[styleName] = unsetValue;
307289
}
308290
}
291+

Diff for: tests/app/tests/property-sets.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,28 @@ describe("setting View properties", () => {
8787
it("sets ios property in ios", () => {
8888
let view = new TestView();
8989
let testUtil = new ViewUtil(iosDevice);
90-
testUtil.setProperty(view, ":ios:anyValue", "blah");
90+
testUtil.setProperty(view, "anyValue", "blah", "ios");
9191
assert.strictEqual("blah", view.anyValue);
9292
});
9393

9494
it("doesn\'t set android property in ios", () => {
9595
let view = new TestView();
9696
let testUtil = new ViewUtil(iosDevice);
97-
testUtil.setProperty(view, ":android:anyValue", "blah");
97+
testUtil.setProperty(view, "anyValue", "blah", "android");
9898
assert.isUndefined(view.anyValue);
9999
});
100100

101101
it("sets android property in android", () => {
102102
let view = new TestView();
103103
let testUtil = new ViewUtil(androidDevice);
104-
testUtil.setProperty(view, ":android:anyValue", "blah");
104+
testUtil.setProperty(view, "anyValue", "blah", "android");
105105
assert.strictEqual("blah", view.anyValue);
106106
});
107107

108108
it("doesn\'t set ios property in android", () => {
109109
let view = new TestView();
110110
let testUtil = new ViewUtil(androidDevice);
111-
testUtil.setProperty(view, ":ios:anyValue", "blah");
111+
testUtil.setProperty(view, "anyValue", "blah", "ios");
112112
assert.isUndefined(view.anyValue);
113113
});
114114
});

0 commit comments

Comments
 (0)